Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback a specific migration?

I have the following migration file db\migrate\20100905201547_create_blocks.rb

How can I specifically rollback that migration file?

like image 956
AnApprentice Avatar asked Sep 05 '10 20:09

AnApprentice


People also ask

How do I undo a specific migration Sequelize?

Undoing Migrations​ You can use db:migrate:undo , this command will revert most the recent migration. You can revert back to the initial state by undoing all migrations with the db:migrate:undo:all command. You can also revert back to a specific migration by passing its name with the --to option.

How do I roll back migration 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.


1 Answers

rake db:rollback STEP=1 

Is a way to do this, if the migration you want to rollback is the last one applied. You can substitute 1 for however many migrations you want to go back.

For example:

rake db:rollback STEP=5 

Will also rollback all the migration that happened later (4, 3, 2 and also 1).

To roll back all migrations back to (and including) a target migration, use: (This corrected command was added AFTER all the comments pointing out the error in the original post)

rake db:migrate VERSION=20100905201547 

In order to rollback ONLY ONE specific migration (OUT OF ORDER) use:

rake db:migrate:down VERSION=20100905201547 

Note that this will NOT rollback any interceding migrations -- only the one listed. If that is not what you intended, you can safely run rake db:migrate and it will re-run only that one, skipping any others that were not previously rolled back.

And if you ever want to migrate a single migration out of order, there is also its inverse db:migrate:up:

rake db:migrate:up VERSION=20100905201547 
like image 181
Zachary Wright Avatar answered Sep 19 '22 23:09

Zachary Wright