1032

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

How can I specifically rollback that migration file?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • 2
    Does this address the issue? You'll just need to do `Class.down` http://stackoverflow.com/questions/753919/run-a-single-migration-file – danivovich Sep 05 '10 at 20:39
  • 4
    Every information on migrations [Here](http://guides.rubyonrails.org/migrations.html) – Nishutosh Sharma Jan 02 '13 at 06:08
  • 1
    Do you want to roll back only that single specific migration (even if there are newer migrations that come after it)? Or do you want to roll back the database to the state it was in before that migration, and any subsequent migrations, were applied? – Jon Schneider Oct 03 '18 at 20:40
  • many times this command 'rails db:rollback' – rnewed_user Oct 06 '22 at 16:30

19 Answers19

1746
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zachary Wright
  • 23,480
  • 10
  • 42
  • 56
  • 15
    "In order to rollback to specific version" - doesn't the command that follows only rollback a specific migration, rather than rolling all the way back to that version? – Andrew Grimm Jul 11 '16 at 07:57
  • 13
    "In order to rollback to specific version use..." **This answer is incorrect!** This will rollback the migration in isolation as explained by other answers. – Rhys van der Waerden Nov 22 '16 at 03:44
  • 7
    WARNING: I made this mistake: only use rake db:migrate:down VERSION=20100905201547 to rollback IN ISOLATION!!! one migration file. This is mentioned in the comment above, but I missed it. – pixelearth Jan 26 '17 at 21:25
  • 4
    Another word of warning - don't ever do `STEP=-1`. I did that once and it went mad, rolling back everything. Not nice! This was Rails 4.2 - I guess it may be fixed by now. – Dave Hartnoll Mar 13 '18 at 16:37
  • 2
    Wrote an article in my blog about migrations, that explains how and when to use these commands: https://railsguides.net/polish-rails-migrations/ – ka8725 Apr 14 '18 at 01:22
  • Nice blog but why do you guess the migration's nationality?! :D – Mark Nov 28 '18 at 15:42
  • On rails 5.2 this did not also rollback the target migration for me. As mentioned in 'rake db:migrate VERSION=20100905201547' – Cody Elhard Apr 19 '19 at 13:06
  • 6
    Don't forget you can use `rake db:migrate:status` to see all the specific migrations and their status! – Danodemo Feb 20 '20 at 18:04
  • 2
    In rails 6.1.3.2 `rails db:migrate VERSION=` does *not* run the down for the migration given by VERSION – chrisortman Sep 15 '21 at 19:55
945
rake db:migrate:down VERSION=20100905201547

will roll back the specific file.


To find the version of all migrations, you can use this command:

rake db:migrate:status

Or, simply the prefix of the migration's file name is the version you need to rollback.


See the Ruby on Rails guide entry on migrations.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Creamer
  • 9,704
  • 1
  • 14
  • 8
  • 51
    Definitely the preferred answer in my opinion. – streetlogics Feb 27 '13 at 17:07
  • Would this rollback migration be pushed across all remote servers, eg. Heroku? Would it be better to create a new migration file just to undo the specific changes? – cevaris Aug 11 '13 at 00:00
  • 32
    It's worth mentioning that if you roll back a specific migration and do not want it to re-migrate on upcoming rake tasks, then delete the migrate file as well. – BradGreens Aug 20 '13 at 18:17
  • Yup. This is way better than stepping. Stepping has its place, but if you just want to fix a single migration (and don't care about losing the data in the table, obviously) this is the stuff of legends. – Dudo Sep 04 '13 at 17:50
  • 4
    Note: it seems that if the up migration never succeeded but have only been partially executed, the down does nothing. – cyrilchampier Oct 09 '13 at 12:13
  • Even rake db:migrate VERSION=old_version works! – sadfuzzy Feb 14 '14 at 09:54
  • Can I simply remove the migration file once performing this migration down method? – Samuel Mar 09 '14 at 22:39
  • 1
    @nerith, it's probably true only for databases which doesn't support Transactional DDL. MySQL doesn't support Transactional DDL: http://dev.mysql.com/doc/refman/5.0/en/cannot-roll-back.html PostreSQL does: https://wiki.postgresql.org/wiki/Transactional_DDL_in_PostgreSQL:_A_Competitive_Analysis So if your migration on MySQL database is broken then you have manually to delete part of migration that succeeded. – Иван Бишевац Jul 25 '14 at 09:33
  • 1
    Another observation regarding @BradGreens comment. If you do want to remove the migration file, and it has already been deployed, you'll want to roll back production/staging before you commit the code with the removed file. Otherwise you won't be able to rollback/migrate:down. – AdamT Aug 06 '17 at 07:21
  • Note that this does not roll back all migrations up to and including the specified migration; rather, it rolls back just that single specific migration (leaving any subsequent migrations still applied). – Jon Schneider Sep 17 '18 at 18:16
  • @AdamT you shouldn't be rolling back migrations in production in general. If the migration slipped that far then it is preferrable to create a new migration wich reverses the previous migration. It is a hellsih mess to try and rollback migrations in prod. Something might end up accidentally rerunning it before you have time to get rid of the file. Also surely some of your colleagues may have by now also run the migration. You need to then instruct them to remember to roll it back before pulling lates code changes.. and ofc also manaually fix the staging env and what not.. not a good idea. – Timo Sep 16 '21 at 17:36
70

To rollback the last migration you can do:

rake db:rollback

If you want to rollback a specific migration with a version, you should do:

rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION

For example, if the version is 20141201122027, you will do

rake db:migrate:down VERSION=20141201122027

to rollback that specific migration.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Waleed
  • 766
  • 5
  • 4
41

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements.

If you want to rollback just the last migration, then you can use either

rake db:rollback

or

rake db:rollback STEP=1

If you want rollback number of migrations at once, then you simply pass an argument:

rake db:rollback STEP=n

where n is number of migrations to rollback, counting from latest migration.

If you want to rollback to a specific migration, then you should pass the version of the migration in the following:

rake db:migrate:down VERSION=xxxxx

where xxxxx is the version number of the migration.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
uma
  • 2,932
  • 26
  • 20
29

Use:

rake db:migrate:down VERSION=your_migrations's_version_number_here

The version is the numerical prefix on the migration's file name.

How to find the version:

Your migration files are stored in your rails_root/db/migrate directory. Find the appropriate file up to which you want to rollback and copy the prefix number.

For example:

File name: 20140208031131_create_roles.rb

Then the version is 20140208031131.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hardik
  • 3,815
  • 3
  • 35
  • 45
20

Rolling back the last migration:

# rails < 5.0
rake db:rollback

# rails >= 5.0
rake db:rollback
# or
rails db:rollback

Rolling back the last n number of migrations

# rails < 5.0
rake db:rollback STEP=2

# rails >= 5.0
rake db:rollback STEP=2
# or
rails db:rollback STEP=2

Rolling back a specific migration

# rails < 5.0
rake db:migrate:down VERSION=20100905201547

# rails >= 5.0
rake db:migrate:down VERSION=20100905201547
# or
rails db:migrate:down VERSION=20100905201547
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
14

To rollback the last migration you can do:

rake db:rollback

If you want to rollback a specific migration with a version you should do:

rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION

If the migration file you want to rollback was called db/migrate/20141201122027_create_some_table.rb, then the VERSION for that migration is 20141201122027, which is the timestamp of when that migration was created, and the command to roll back that migration would be:

rake db:migrate:down VERSION=20141201122027
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sandip Vavhal
  • 140
  • 1
  • 8
13

To roll back all migrations up to a particular version (e.g. 20181002222222), use:

rake db:migrate VERSION=20181002222222

(Note that this uses db:migrate -- not db:migrate:down as in other answers to this question.)

Assuming the specified migration version is older than the current version, this will roll back all migrations up to, but not including, the specified version.

For example, if rake db:migrate:status initially displays:

  (... some older migrations ...)
  up      20181001002039  Some migration description
  up      20181002222222  Some migration description
  up      20181003171932  Some migration description
  up      20181004211151  Some migration description
  up      20181005151403  Some migration description

Running:

rake db:migrate VERSION=20181002222222

Will result in:

  (... some older migrations ...)
  up      20181001002039  Some migration description
  up      20181002222222  Some migration description
  down    20181003171932  Some migration description
  down    20181004211151  Some migration description
  down    20181005151403  Some migration description

Reference: Migrate or revert only some migrations

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
7

If you are using Ruby on Rails 3

Step: 1 (check the last migration)

bundle exec rake db:migrate:status

Step: 2 (roll back the last migration)

bundle exec rake db:rollback

Now, you can revert the migration with safety one by one.

For a specific migration

rails d migration <migration_name>

For reverting multiple migrations

bundle exec rake db:rollback STEP=n

where n is how many migrations you want to rollback.

Example: bundle exec rake db:rollback STEP=5

blackgreen
  • 34,072
  • 23
  • 111
  • 129
CHAVDA MEET
  • 777
  • 8
  • 14
  • For multiple migrations, it only works if your migrations are in a following order and the last you applied, not if you want to rollback specific ones – gogaz Oct 05 '22 at 14:00
6

If it is a reversible migration and the last one which has been executed, then run rake db:rollback. And you can always use the version.

For example, if the migration file is 20140716084539_create_customer_stats.rb, the rollback command will be:

rake db:migrate:down VERSION=20140716084539
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Santanu
  • 960
  • 10
  • 14
6

A migration file looks like this,

 20221213051020_my_migrations

In this case, the model name should be MyMigration. The migration ends with a plural word, so it ends with migrations.

To roll back this particular migration, you have to understand that the first part of the migration name (number in front of the migration name) is the migration number.

To roll back this migration, just open the terminal and write,

rake db:migrate:down VERSION=migration_number

So finally, you have to actually type in the terminal to roll back this particular migration,

Write the below command on terminal to rollback a particular migration, upper command is just to explain you

rake db:migrate:down VERSION=20221213051020

Just remember that each migration has a different migration number, so watch carefully and copy paste or type manually.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Akash
  • 83
  • 1
  • 6
5

Migrations change the state of the database using the command

bundle exec rake db:migrate

We can undo a single migration step using

bundle exec rake db:rollback

To go all the way back to the beginning, we can use

bundle exec rake db:migrate VERSION=0

As you might guess, substituting any other number for 0 migrates to that version number, where the version numbers come from listing the migrations sequentially.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nirupa
  • 787
  • 1
  • 8
  • 20
5

You can run down migration command to rollback the migration like below:

rake db:migrate:down VERSION=20100905201547
Sachin Singh
  • 993
  • 8
  • 16
3

Well, in rails 5 it's quite easy

rake db:migrate:status

or

rails db:migrate:status

It was modified to handle both the same way. Then just pick which version you want to roll back and then run

rake db:migrate VERSION=2013424230423

Make sure VERSION is all capital letters.

If you have a problem with any step of the migration or stuck in the middle simply, go to the migration file and comment out the lines that were already migrated.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shahin
  • 2,507
  • 1
  • 13
  • 17
  • 1
    I highlight the hint for command **rake db:migrate:status** . . . It's great for overview to see the current execution state of the migration files. – Beauty Aug 01 '17 at 19:00
2

If you want to rollback and migrate you can run:

rake db:migrate:redo

That's the same as:

rake db:rollback
rake db:migrate
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Iwan B.
  • 3,982
  • 2
  • 27
  • 18
2

In addition:

When a migration you deployed long ago does not let you migrate a new one.

I work in a larger Ruby on Rails application with more than a thousand of migration files. And, it takes a month for us to ship a medium-sized feature. I was working on a feature and I had deployed a migration a month ago, and then in the review process the structure of migration and filename changed, now I try to deploy my new code, the build failed saying:

ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "my_new_field" of relation "accounts" already exists

None of the above-mentioned solutions worked for me, because the old migration file was missing and the field I intended to create in my new migration file already existed in the database. The only solution that worked for me is:

  1. I scped the file to the server
  2. I opened the rails console
  3. I required the file in the IRB session
  4. then AddNewMyNewFieldToAccounts.new.down

And then I could run the deploy build again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shiva
  • 11,485
  • 2
  • 67
  • 84
2

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. After doing so, you can check the status again to be fully confident.

Next, if you want to remove or delete. Run rails d migration <migration_name>. This would clean up the versions you created.

After that's done, you can proceed to making new changes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aiman Farhan
  • 111
  • 1
  • 2
1

For a multiple databases configurations (RoR >= v6), you must append the database name in the command, like:

  • rails db:rollback:primary, where primary is the name of the database in your config/databases.yml file, to roll back the last migration. You can make usage of the STEPS attribute here, as usual.
  • rails db:migrate:down:primary VERSION=your_migration_timestamp, to revert only the provided migration version. Here primary is the name of the database too.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Batalla
  • 1,184
  • 1
  • 11
  • 22
0

If you want to revert from the last migration, use the rake db:rollback command. It's working fine for me!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Perumal T
  • 37
  • 1
  • 2
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 23 '22 at 10:52