Ruby on Rails: How can I revert a migration with r

2019-01-30 00:29发布

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 ?

8条回答
来,给爷笑一个
2楼-- · 2019-01-30 00:40

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楼-- · 2019-01-30 00:41
rake db:migrate:redo

It will undo and reapply the last migration.

查看更多
虎瘦雄心在
4楼-- · 2019-01-30 00:43

For rails 4+ run:

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

for rails 3 you need to run several times (it will go back one migration each time):

rake db:rollback
查看更多
该账号已被封号
5楼-- · 2019-01-30 00:43

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=
查看更多
Luminary・发光体
6楼-- · 2019-01-30 00:49

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.

查看更多
男人必须洒脱
7楼-- · 2019-01-30 00:51

Just run this command:

rake db:rollback
查看更多
登录 后发表回答