[英]Ruby on Rails + devise: How can i create customize users table with devis rake db:migrate?
[英]Ruby on Rails: How can I revert a migration with rake db:migrate?
安装设备后,我得到了这个。
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表?
运行以下命令
rake db:migrate:down VERSION=<version>
其中<version>
是您要还原的迁移文件的版本号。
例如。 如果要还原文件名为3846656238_create_users.rb的迁移
rake db:migrate:down VERSION = 3846656238
只需运行此命令:
rake db:rollback
我相信有三种方法可用于还原迁移(它们也重叠):
下载最近的迁移:
rake db:migrate:down
#Rails 2 only。
下载最近一次(n)次迁移:
rake db:rollback STEP=n
滚下来到以前特定版本:
$ rake db:migrate:down VERSION=nnn
#Rails 3(也提供版本号)。
版本号表示提交的SHA(安全哈希算法),它是一个长十六进制数字,看起来像886af3194768917c78e ...你可以通过git log
看到它
您可以使用rake -T db:
来查看这些命令(和其他命令)及其描述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)
您可以执行回滚并指定将回滚的最后一次迁移的数量,例如
rake db:rollback STEP=3
最后3次迁移。
rake db:migrate:redo
它将撤消并重新应用上次迁移。
作为一名新程序员(或其他新程序员)
rake db:rollback
工作时间大约是一半。 我从那里开始。
如果没有,则rake db:migrate:down VERSION=3846656238
插入VERSION以获取要还原的迁移文件的版本号。
对于rails 5,我们可以使用rails command instead of rake
rails db:migrate:down VERSION=<version>
例
rails db:migrate:down VERSION = 20170330090327
在终端中运行此命令:
rake db:migrate:status
要么
bundle exec rake db:migrate:status
它显示了我们之前运行的所有迁移的状态,迁移ID和迁移名称。 选择您的迁移ID(即您的版本号)并在version = ,,,之后将该id放入以下命令中,然后按Enter键
bundle exec rake db:migrate:down VERSION=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.