Ruby on Rails:如何使用 rake db:migrate 恢复迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7694487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails: How can I revert a migration with rake db:migrate?
提问by shibly
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.
现在,如果我执行 rake db:migrate ,则会创建 users 表。
How can i revert this migration, i.e. how can I delete the users table using rake again ?
如何恢复此迁移,即如何再次使用 rake 删除用户表?
回答by Mahesh
Run the following command
运行以下命令
rake db:migrate:down VERSION=<version>
where <version>is the version number of your migration file you want to revert.
<version>您要还原的迁移文件的版本号在哪里。
eg. if you want to revert a migration with file name 3846656238_create_users.rb
例如。如果要恢复文件名为 3846656238_create_users.rb 的迁移
rake db:migrate:down VERSION=3846656238
耙分贝:迁移:向下版本=3846656238
回答by damienbrz
Just run this command:
只需运行此命令:
rake db:rollback
回答by Michael Durrant
I believe there are three options available for reverting migrations (they also overlap):
我相信有三个选项可用于恢复迁移(它们也重叠):
Roll down the most recentmigration:
rake db:migrate:down# Rails 2 only.Roll down a number(n) ofrecent migrations:
rake db:rollback STEP=nRoll down toa previous, specificversion:
$ rake db:migrate:down VERSION=nnn# Rails 3 (provide version number also).
向下滚动最近的迁移:
rake db:migrate:down# 仅限 Rails 2。向下滚动最近迁移的数量(n):
rake db:rollback STEP=n向下滚动到以前的特定版本:
$ rake db:migrate:down VERSION=nnn# Rails 3(也提供版本号)。
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
版本号表示提交的 SHA(安全哈希算法),它是一个长十六进制数,看起来像 886af3194768917c78e...你可以通过这样做来看到它 git log
You can see these commands (and others) with their descriptions by using rake -T db:which for rails 3.2 includes:
您可以使用rake -T db:which for rails 3.2 包含以下命令(和其他命令)及其说明:
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)
回答by bender
You can do rollback and specify how many last migrations will be rollbacked, e.g.
您可以执行回滚并指定将回滚的最后一次迁移的数量,例如
rake db:rollback STEP=3
for 3 last migrations.
用于最后 3 次迁移。
回答by LukeBickleTWA
As an new programmer (or to other new programmers)
作为一名新程序员(或其他新程序员)
rake db:rollbackworks about half the time. I start there.
rake db:rollback大约有一半的时间工作。我从那里开始。
If not, rake db:migrate:down VERSION=3846656238
如果不, rake db:migrate:down VERSION=3846656238
plug in VERSION for the version number of your migration file you want to revert.
为要还原的迁移文件的版本号插入 VERSION。
回答by keneth
rake db:migrate:redo
It will undo and reapply the last migration.
它将撤消并重新应用上次迁移。
回答by Mihir Kumar Thakur
For rails 5 we can use rails command instead of rake
对于 rails 5,我们可以使用 rails command instead of rake
rails db:migrate:down VERSION=<version>
example
例子
rails db:migrate:down VERSION=20170330090327
rails db:migrate:down 版本=20170330090327
回答by Arun JP
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
它显示了我们之前运行的所有迁移的状态、迁移 ID 和迁移名称。选择您的迁移 ID(即您的版本号)并将该 ID 放在以下命令中 version= ,, 之后,然后按 Enter
bundle exec rake db:migrate:down VERSION=
回答by BKSpurgeon
How to Roll back a migration
如何回滚迁移
(1) First Identify The Migration ID
(1)首先确定迁移ID
rake db:migrate:status
rake db:migrate:status
- Copy the ID number.
- 复制身号码。
(2) Then Roll back the migration
(2) 然后回滚迁移
rake db:migrate:down VERSION=20190802023239
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.
- 粘贴上面的相关 ID 号。当然,在您的情况下,迁移 ID 会有所不同!使用正确的迁移 ID。
.......and now you're off to the races!
.......现在你开始比赛了!


