I've been trying to write a spec to test a rails migration, but any guides i find online, (e.g. https://nebulab.com/blog/testing-ruby-on-rails-migrations) use the deprecated:
ActiveRecord::Migrator.new(:up, migrations, current_version).migrate
what is the equivalent in rails 7?
and has anyone a super simple example of testing a migration in rails 7?
Update: I have a migration which renames a table and the related rails model, removes a column from that table, adds another column, and finally migrates existing entries in that table (new column is populated by looking up info using the column that gets dropped)
I mainly want to test that the existing data gets migrated properly
Here was the new setup for my specs after upgrading to Rails 7.1 which pushed ActiveRecord to 7.1:
let(:migration_context) { ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) }
let(:previous_version) { XX_XXX_XXX_XXX_XXX }
let(:current_version) { XX_XXX_XXX_XXX_XXX }
Then when I went to run or revert:
def run_migration
migration_context.up(current_version)
end
def revert_migration
migration_context.down(previous_version)
end
According to the docs it seems like I should have been able to pass :up
to .run
on the context, but I just couldn't get that to trigger the up
method in my migration. But the above did the trick.
Hope this helps!