Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out database migration logs on Rails?

I want to rollback my database to a certain version on Rails using the STEP parameter, but I don't know how many steps should i rollback so I want to check the migration log. Can I do that on Rails (v3.2.13)?

like image 338
ardiyu07 Avatar asked Apr 27 '13 04:04

ardiyu07


People also ask

How can I check my rails migration status?

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.

What is migration in ROR?

Migrations are a convenient way to alter your database schema over time in a consistent way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. You can think of each migration as being a new 'version' of the database.

How do I go down last migration in rails?

rails db:rollback:primary , where primary is the name of the database in your config/databases. yml file, to rollback the last migration. You can make usage of the STEPS attribute here, as usual. rails db:rollback:primary VERSION=your_migration_timestamp , to rollback only the provided migration version.


2 Answers

Try the following:

rake db:migrate:status

It will give you the following output, up meaning migration has been run, down hasn't been run yet:

Status   Migration ID    Migration Name
--------------------------------------------------
   up     20120328154345  Devise create users
   up     20120331182021  Create websites
like image 127
mind.blank Avatar answered Oct 26 '22 23:10

mind.blank


You can try to get migration versions by

 >  ActiveRecord::Migrator.current_version
   (38.7ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" 
 => 20130403113845 
1.9.3-p392 :002 > ActiveRecord::Migrator.get_all_versions
   (0.8ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" 
 => [20130327085819, 20130327085820, 20130327085821, 20130327085822, 20130327085823, 20130327085824, 20130327085825, 20130327085826, 20130327085827, 20130327085828, 20130327085829, 20130327085830,........

or you can use the time stamp of the specific migration upto which you want to rollback and use

rake db:migrate:down VERSION= timestamp

like image 42
Magnum Avatar answered Oct 27 '22 00:10

Magnum