Since the newest Rails version, ActiveRecord::MigrationContext#new
seems to take a new argument called schema_migration
. But I have no idea what to pass there and where to get it.
I cannot find any information on it. I googled for an hour, all examples for MigrationContext
I found referred to older rails versions. The class MigrationContext doesn't seem to be documented at all. From the source code I couldn't figure out what to pass either.
Some context: I am trying to test some of my more dangerous migrations. I found quite a few tutorials and it seemed easy and I went along an did it. But the code that prepares the state of the test db so I can apply the migration is currently not working. Sadly all the tutorials used older Rails versions and this fails due to the wrong number of arguments:
ActiveRecord::MigrationContext.new(migrations_paths)
To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.
You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.
Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema. rb file to match the structure of your database.
I found out what I need to pass:
ActiveRecord::Base.connection.schema_migration
So the whole code would be:
migrations_paths = ActiveRecord::Migrator.migrations_paths
schema_migration = ActiveRecord::Base.connection.schema_migration
migration_context = ActiveRecord::MigrationContext.new(migrations_paths, schema_migration)
I googled and tried for an hour and didn't figure it out, but just after posting the question, I finally stumbled upon this github issue https://github.com/pat/combustion/issues/98 which had the same problem and a solution.
You might also just want to use ActiveRecord::Base.connection.migration_context
instead of ActiveRecord::MigrationContext.new
. No arguments needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With