Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails migration: best way to retrieve current migration version

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.

like image 672
shigeya Avatar asked Jan 13 '12 03:01

shigeya


People also ask

How can I check my rails migration status?

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."

How do I rollback the last migration in Rails?

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.

Which command is used to rollback migration in Rails?

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.

How do you rerun a migration?

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.


2 Answers

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 
like image 59
Peter Ehrlich Avatar answered Oct 13 '22 05:10

Peter Ehrlich


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,    ... 
like image 24
jibiel Avatar answered Oct 13 '22 05:10

jibiel