shi*_*bly 90 migration rake ruby-on-rails database-migration ruby-on-rails-3.1
安装设备后,我得到了这个.
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
Run Code Online (Sandbox Code Playgroud)
现在,如果我执行rake db:migrate,将创建users表.
如何恢复此迁移,即如何再次使用rake删除users表?
Mah*_*esh 141
运行以下命令
rake db:migrate:down VERSION=<version>
Run Code Online (Sandbox Code Playgroud)
<version>您要还原的迁移文件的版本号在哪里.
例如.如果要还原文件名为3846656238_create_users.rb的迁移
rake db:migrate:down VERSION = 3846656238
Mic*_*ant 63
我相信有三种方法可用于还原迁移(它们也重叠):
下载最近的迁移:
rake db:migrate:down #Rails 2只.
滚下来的数(n)最近的迁移:
rake db:rollback STEP=n
滚下来到以前的,具体的版本:
$ rake db:migrate:down VERSION=nnn #Rails 3(也提供版本号).
版本号表示提交的SHA(安全哈希算法),它是一个长十六进制数字,看起来像886af3194768917c78e ......你可以通过这样做看到它 git log
您可以通过使用rake -T db: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)
Run Code Online (Sandbox Code Playgroud)
ben*_*der 15
您可以执行回滚并指定将回滚的最后一次迁移的数量,例如
rake db:rollback STEP=3
Run Code Online (Sandbox Code Playgroud)
最后3次迁移.
小智 10
作为一名新程序员(或其他新程序员)
rake db:rollback工作大约一半的时间.我从那里开始.
如果不, rake db:migrate:down VERSION=3846656238
插入VERSION以获取要还原的迁移文件的版本号.
对于rails 5,我们可以使用 rails command instead of rake
rails db:migrate:down VERSION=<version>
Run Code Online (Sandbox Code Playgroud)
例
rails db:migrate:down VERSION = 20170330090327