Ruby on Rails: How can I revert a migration with rake db:migrate?

Ruby on-RailsRuby on-Rails-3.1MigrationRakeDatabase Migration

Ruby on-Rails Problem Overview


After installing devise MODEL User i got this.

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

Now if i do rake db:migrate the users table will be created.

How can i revert this migration, i.e. how can I delete the users table using rake again ?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Run the following command

rake db:migrate:down VERSION=<version>

where <version> is the version number of your migration file you want to revert.

eg. if you want to revert a migration with file name 3846656238_create_users.rb > rake db:migrate:down VERSION=3846656238

Solution 2 - Ruby on-Rails

Just run this command:

rake db:rollback

Solution 3 - Ruby on-Rails

I believe there are three options available for reverting migrations (they also overlap):

  1. Roll down the most recent migration:

rake db:migrate:down # Rails 2 only.

  1. Roll down a number(n) of recent migrations:

rake db:rollback STEP=n

  1. Roll down to a previous, specific version:

$ rake db:migrate:down VERSION=nnn # Rails 3 (provide version number also).

Version Number means the SHA(Secure Hash Algorithm) for the commit which is a long hexadecimal number which looks something like 886af3194768917c78e... You can see it by doing git log

You can see these commands (and others) with their descriptions by using rake -T db: which for rails 3.2 includes:

rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status  # Display status of migrations
rake db:rollback        # Rolls the schema back to the previous version (specify steps w/ STEP=n)

Solution 4 - Ruby on-Rails

You can do rollback and specify how many last migrations will be rollbacked, e.g.

rake db:rollback STEP=3

for 3 last migrations.

Solution 5 - Ruby on-Rails

As an new programmer (or to other new programmers)

rake db:rollback works about half the time. I start there.

If not, rake db:migrate:down VERSION=3846656238

plug in VERSION for the version number of your migration file you want to revert.

Solution 6 - Ruby on-Rails

rake db:migrate:redo

It will undo and reapply the last migration.

Solution 7 - Ruby on-Rails

For rails 5 we can use rails command instead of rake

rails db:migrate:down VERSION=<version>

example

> rails db:migrate:down VERSION=20170330090327

Solution 8 - Ruby on-Rails

Run this command in your terminal:

rake db:migrate:status

or

bundle exec rake db:migrate:status

It shows the status, migration ID's, migration name for all migration we ran previously. select your migration id (i.e your version number) and put that id in the following command after version= ,,, and press enter

bundle exec rake db:migrate:down VERSION=

Solution 9 - Ruby on-Rails

How to Roll back a migration

(1) First Identify The Migration ID

rake db:migrate:status

  • Copy the ID number.

Identify the migration to roll back.

(2) Then Roll back the migration

rake db:migrate:down VERSION=20190802023239

  • Paste the relevant ID number above. Of course, in your case, the migration ID will be different! Use the correct migration ID.

.......and now you're off to the races!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshiblyView Question on Stackoverflow
Solution 1 - Ruby on-RailsMaheshView Answer on Stackoverflow
Solution 2 - Ruby on-RailsdamienbrzView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMichael DurrantView Answer on Stackoverflow
Solution 4 - Ruby on-RailsbenderView Answer on Stackoverflow
Solution 5 - Ruby on-RailsLukeBickleTWAView Answer on Stackoverflow
Solution 6 - Ruby on-RailskenethView Answer on Stackoverflow
Solution 7 - Ruby on-RailsMihir Kumar ThakurView Answer on Stackoverflow
Solution 8 - Ruby on-RailsArun JPView Answer on Stackoverflow
Solution 9 - Ruby on-RailsBenKoshyView Answer on Stackoverflow