Ruby on Rails:如何使用 rake db:migrate 恢复迁移?
安装 devise MODEL User 后我得到了这个。
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 将创建用户表。
我如何恢复此迁移,即如何再次使用 rake 删除用户表?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
运行以下命令
,其中
是要恢复的迁移文件的版本号。例如。如果您想恢复文件名为 3846656238_create_users.rb 的迁移
Run the following command
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
只需运行这个命令:
Just run this command:
我相信可以使用三个选项来恢复迁移(它们也重叠):
滚动最近迁移:
rake db:migrate:down
# 仅适用于 Rails 2。滚动一定数量最近的迁移:
rake db:rollback STEP=n
向下滚动到之前的特定版本:
$ rake db:migrate:down VERSION=nnn
# Rails 3(还提供版本号)。版本号表示提交的 SHA(安全哈希算法),它是一个很长的十六进制数字,看起来像 886af3194768917c78e...您可以通过执行
git log
来查看它,您可以看到这些命令(以及其他命令) )及其描述,使用
rake -T db:
,其中 Rails 3.2 包括:I believe there are three options available for reverting migrations (they also overlap):
Roll down the most recent migration:
rake db:migrate:down
# Rails 2 only.Roll down a number(n) of recent migrations:
rake db:rollback STEP=n
Roll down to a previous, specific version:
$ rake db:migrate:down VERSION=nnn
# Rails 3 (provide version number also).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
You can see these commands (and others) with their descriptions by using
rake -T db:
which for rails 3.2 includes:您可以进行回滚并指定将回滚多少个最后的迁移,例如
3 个最后的迁移。
You can do rollback and specify how many last migrations will be rollbacked, e.g.
for 3 last migrations.
作为新程序员(或其他新程序员),
rake db:rollback
的工作时间大约是一半。我从这里开始。如果没有,请
rake db:migrate:down VERSION=3846656238
插入 VERSION 作为要恢复的迁移文件的版本号。
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.
它将撤消并重新应用上次迁移。
It will undo and reapply the last migration.
对于rails 5,我们可以使用rails命令代替rake
示例
For rails 5 we can use
rails command instead of rake
example
在终端中运行此命令:
或者
它显示我们之前运行的所有迁移的状态、迁移 ID、迁移名称。选择您的迁移 ID(即您的版本号)并将该 id 放入以下命令中的 version= ,, 后,然后按 Enter
Run this command in your terminal:
or
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
如何以另一种方式回滚迁移
。我更喜欢这个,因为你需要明确,而不是回滚 - 以防万一你可能会犯错误。
(1) 首先识别迁移ID
rake db:migrate:status
(2) 然后回滚迁移
rake db:migrate:down VERSION=20190802023239
......现在你就可以参加比赛了!
How to Roll back a migration
Another way. I prefer this because you need to be explicit, rather than rolling back - just in case you might make a mistake.
(1) First Identify The Migration ID
rake db:migrate:status
(2) Then Roll back the migration
rake db:migrate:down VERSION=20190802023239
.......and now you're off to the races!