103

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 ?

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
cola
  • 12,198
  • 36
  • 105
  • 165

9 Answers9

157

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

Mahesh
  • 6,378
  • 2
  • 26
  • 35
  • I'm getting an `UnknownMigrationVersionError` but I figured out it's because my migrations are inside `db/migrate/main` , does anybody know a workaround for this to have `db:migrate:down` look inside that specific directory or the migrate subdirectories? – tf.rz Mar 28 '16 at 17:02
  • This is just for rails 3 onwards. My life is in rails 2. So sad – morhook Nov 02 '18 at 18:25
  • @morhook This works for rails 3 also. Check the docs here https://guides.rubyonrails.org/v3.2/migrations.html – Mahesh Feb 11 '19 at 17:05
  • You are right! It works for both rails 2 and rails 3. Thanks @Mahesh for your input! – morhook Mar 06 '19 at 16:42
117

Just run this command:

rake db:rollback
damienbrz
  • 1,806
  • 2
  • 11
  • 15
68

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.

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

    rake db:rollback STEP=n

  3. 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)
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • 5
    Roll down _to_ a specific version: `rake db:migrate VERSION=` – Ajedi32 Jun 03 '13 at 19:06
  • 3
    At least for rails 3.0.20, the first command is wrong. A single `rake db:migrate:down` aborts with the error message "VERSION is required". The [recommended](http://guides.rubyonrails.org/migrations.html#rolling-back) `rake db:rollback` however works. – martin Jun 19 '13 at 11:27
  • As detailed in the answer, Rails 2 ONLY for the first commmand. – Michael Durrant Jul 07 '14 at 12:01
  • 1
    Environment variables are case sensitive so it should be `STEP` and `VERSION` – Kostas Rousis Jul 07 '14 at 12:11
  • To clarify, `$ rake db:migrate:down VERSION=nnn` doesn't roll down _to_ a version, it migrates down _the_ version specified. – johnml Jul 15 '16 at 18:28
16

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

rake db:rollback STEP=3

for 3 last migrations.

bender
  • 1,430
  • 10
  • 14
  • this is a more quicker and easier way, instead of looking up version numbers if you want to undo the last few migrations – Pre-alpha Mar 31 '17 at 08:00
11

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.

Stedy
  • 7,359
  • 14
  • 57
  • 77
LukeBickleTWA
  • 111
  • 1
  • 3
10
rake db:migrate:redo

It will undo and reapply the last migration.

keneth
  • 197
  • 3
  • 4
5

For rails 5 we can use rails command instead of rake

rails db:migrate:down VERSION=<version>

example

rails db:migrate:down VERSION=20170330090327

3

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=
kamwo
  • 1,980
  • 1
  • 23
  • 32
Arun JP
  • 31
  • 3
1

How to Roll back a migration

Another way. I prefer this because you need to be explicit, rather than rolling back - just in case you might make a mistake.

(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!

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

BenKoshy
  • 33,477
  • 14
  • 111
  • 80