Is there good way to retrieve migration version number?
I need to implement a method in a model which behave differently on and beyond a specific migration version.
I found assume_migrated_upto_version in connection adapter is retrieving version from database but can't find others.
Background: I'm trying to remove two columns from table A, want to move them into table B, and add association to the table B from the table A.
During this change, I need to access these two columns. but after that, I want to add proxy method for these columns for compatibility.
If you need a bash one-liner to determine whether to run a migration or not (e.g., only migrate in a Heroku release phase command when there is a pending migration), this works: (rails db:migrate:status | grep "^\s*down") && rails db:migrate || echo "No pending migrations found."
i found these steps most useful. To check for status, run rails db:migrate:status . Then you'll have a good view of the migrations you want to remove. Then, run rails db:rollback to revert the changes one by one.
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.
After you have run a migration plan in your assistant (like selecting a cloud instance and entities to migrate), re-run the app migration using the following URL: rest/migration/latest/app-migration/rerun/<planId> . This operation can be repeated multiple times as required. Replace planId with your own unique planID.
There's a much nicer way: rake db:migrate:status
up 20120530222941 Create shenanigans up 20120613030015 *** NO FILE ***
Indicating that I've deleted my latest migration file.
Or more simply:
> rake db:version Current version: 20120613030015
Rails 5.2
and higher:
> ApplicationRecord.connection.migration_context.current_version (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC => 20200510093804 > ApplicationRecord.connection.migration_context.get_all_versions (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC => [20191005164928, 20191006111502, ...
Rails up to 5.1.7
:
> ActiveRecord::Migrator.current_version (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" => 20120110085802 > ActiveRecord::Migrator.get_all_versions (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" => [20111114121610, 20111115091108, ...
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