Skip to content

Commit

Permalink
Create associations directly via the through table
Browse files Browse the repository at this point in the history
Rails 7 changed the way migrations are run.  The migration class
constant is now removed and the file is reloaded between migrations.

This means tests should not use expect/allow on migration stub classes
as the constant stubbed is not the same constant the migration is run against.

Additionally, comparisons of objects created before the migration with ones
after will also not work.  In that case, you can assert by id or other
attributes instead of comparing objects.

Rails also checks association classes are correct and will also not match
expectations.  For example, instead of using host to create or query
the host association, use host_id instead.  This also works with has_manys
such as miq_product_feature_ids.

See https://github.com/rails/rails/pull 42198
  • Loading branch information
jrafanie committed May 29, 2024
1 parent 23b18a0 commit d641800
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Switch < ActiveRecord::Base
let(:switch_stub) { migration_stub(:Switch) }
let(:host_stub) { migration_stub(:Host) }
let(:ems_stub) { migration_stub(:ExtManagementSystem) }
let(:host_switch_stub) { migration_stub(:HostSwitch) }

migration_context :up do
it "migrates a series of representative rows" do
Expand Down Expand Up @@ -73,11 +74,11 @@ class Switch < ActiveRecord::Base
:type => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch",
:ems_id => nil)
# Host -> switches mapping
host_esx.host_switches.create!(:host_id => host_esx.id, :switch_id => dvswitch.id)
host_esx.host_switches.create!(:host_id => host_esx.id, :switch_id => host_switch.id)
host_esx_archived.host_switches.create!(:host_id => host_esx_archived.id, :switch_id => host_switch_archived.id)
host_esx_archived.host_switches.create!(:host_id => host_esx_archived.id, :switch_id => dvswitch_archived.id)
host_redhat.host_switches.create!(:host_id => host_redhat.id, :switch_id => redhat_switch.id)
host_switch_stub.create!(:host_id => host_esx.id, :switch_id => dvswitch.id)
host_switch_stub.create!(:host_id => host_esx.id, :switch_id => host_switch.id)
host_switch_stub.create!(:host_id => host_esx_archived.id, :switch_id => host_switch_archived.id)
host_switch_stub.create!(:host_id => host_esx_archived.id, :switch_id => dvswitch_archived.id)
host_switch_stub.create!(:host_id => host_redhat.id, :switch_id => redhat_switch.id)

migrate

Expand Down

0 comments on commit d641800

Please sign in to comment.