安装设计模型用户后,我得到了这个。
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
现在,如果我执行rake db:migrate,将创建users表。
如何恢复此迁移,即如何再次使用rake删除users表?
答案 0 :(得分:140)
运行以下命令
rake db:migrate:down VERSION=<version>
其中<version>
是您要还原的迁移文件的版本号。
例如。如果要还原文件名为3846656238_create_users.rb的迁移
rake db:migrate:down VERSION = 3846656238
答案 1 :(得分:110)
只需运行此命令:
rake db:rollback
答案 2 :(得分:62)
我相信有三种方法可用于还原迁移(它们也重叠):
下拉最近的迁移:
rake db:migrate:down
仅限#Rails 2。
向下滚动最近一次次次迁移:
<强> rake db:rollback STEP=n
强>
将下拉到之前的特定版本:
$ rake db:migrate:down VERSION=nnn
#Rails 3(也提供版本号)。
版本号表示提交的SHA(安全哈希算法),它是一个长十六进制数字,看起来像886af3194768917c78e ......你可以通过git log
您可以使用适用于rails 3.2的rake -T db:
来查看这些命令(和其他命令)及其说明:
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)
答案 3 :(得分:13)
您可以执行回滚并指定将回滚的最后一次迁移的数量,例如
rake db:rollback STEP=3
最后3次迁移。
答案 4 :(得分:9)
rake db:migrate:redo
它将撤消并重新应用上次迁移。
答案 5 :(得分:9)
作为新程序员(或其他新程序员)
rake db:rollback
的工作时间大约是一半。我从那里开始。
如果没有,rake db:migrate:down VERSION=3846656238
插入VERSION以获取要还原的迁移文件的版本号。
答案 6 :(得分:3)
对于rails 5,我们可以使用rails command instead of rake
rails db:migrate:down VERSION=<version>
例如
rails db:migrate:down VERSION = 20170330090327
答案 7 :(得分:2)
在终端中运行此命令:
rake db:migrate:status
或
bundle exec rake db:migrate:status
它显示我们之前运行的所有迁移的状态,迁移ID,迁移名称。选择您的迁移ID(即您的版本号),并在version = ,,,之后将该ID放入以下命令中,然后按Enter键
bundle exec rake db:migrate:down VERSION=
答案 8 :(得分:0)