Ruby-on-rails 如何使用 rake db:migrate 仅回滚一步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4352848/
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
How to rollback just one step using rake db:migrate
提问by mko
After adding migration files in the db/migratefolder and running rake db:migrate, I want get back to the previous step, I think using VERSION=nis the right way to do that, but I don't know the correct value of n to use. Is there any command to check the current n value?
在文件db/migrate夹中添加迁移文件并运行后rake db:migrate,我想回到上一步,我认为使用VERSION=n是正确的方法,但我不知道要使用的正确值 n。是否有任何命令可以检查当前的 n 值?
It would be great if anyone could provide full instructions on how to use rake db:migrate.
如果有人能提供有关如何使用rake db:migrate.
回答by rwilliams
回答by Ajedi32
Roll back the most recent migration:
回滚最近的迁移:
rake db:rollback
Roll back the nmost recent migrations:
回滚n最近的迁移:
rake db:rollback STEP=n
You can find full instructions on the use of Rails migration tasks for rake on the Rails Guide for running migrations.
您可以在Rails Guide for running migrations上找到有关使用 Rails 迁移任务进行 rake 的完整说明。
Here's some more:
这里还有一些:
rake db:migrate- Run all migrations that haven't been run alreadyrake db:migrate VERSION=20080906120000- Run all necessary migrations (up or down) to get to the given versionrake db:migrate RAILS_ENV=test- Run migrations in the given environmentrake db:migrate:redo- Roll back one migration and run it againrake db:migrate:redo STEP=n- Roll back the lastnmigrations and run them againrake db:migrate:up VERSION=20080906120000- Run theupmethod for the given migrationrake db:migrate:down VERSION=20080906120000- Run thedownmethod for the given migration
rake db:migrate- 运行所有尚未运行的迁移rake db:migrate VERSION=20080906120000- 运行所有必要的迁移(向上或向下)以获取给定版本rake db:migrate RAILS_ENV=test- 在给定环境中运行迁移rake db:migrate:redo- 回滚一次迁移并再次运行rake db:migrate:redo STEP=n- 回滚上次n迁移并再次运行它们rake db:migrate:up VERSION=20080906120000- 运行up给定迁移的方法rake db:migrate:down VERSION=20080906120000- 运行down给定迁移的方法
And to answer your question about where you get a migration's version number from:
并回答有关从何处获取迁移版本号的问题:
The version is the numerical prefix on the migration's filename. For example, to migrate to version 20080906120000 run
$ rake db:migrate VERSION=20080906120000
版本是迁移文件名的数字前缀。例如,要迁移到版本 20080906120000 运行
$ rake db:migrate VERSION=20080906120000
(From Running Migrationsin the Rails Guides)
(来自Rails 指南中的运行迁移)
回答by Hemali
Best way is running Particular migration again by using down or up(in rails 4. It's change)
最好的方法是通过使用 down 或 up 再次运行特定迁移(在 rails 4. 它的变化)
rails db:migrate:up VERSION=timestamp
rails db:migrate:up 版本=时间戳
Now how you get the timestamp. Go to this path
现在你如何获得时间戳。走这条路
/db/migrate
/db/迁移
Identify migration file you want to revert.pick the timestamp from that file name.
确定要还原的迁移文件。从该文件名中选择时间戳。
回答by ?oàn Ngh?a
If the version is 20150616132425, then use:
如果版本是20150616132425,则使用:
rails db:migrate:down VERSION=20150616132425
回答by BjarneD
Other people have already answered you how to rollback, but you also asked how you could identify the version number of a migration.
其他人已经回答了您如何回滚,但您还询问了如何识别迁移的版本号。
rake db:migrate:statusgives a list of your migrations version, name and status (up or down)- Your can also find the migration file, which contain a timestamp in the filename, that is the version number. Migrations are located in folder:
/db/migrate
rake db:migrate:status给出您的迁移版本、名称和状态(向上或向下)的列表- 您还可以找到迁移文件,其中包含文件名中的时间戳,即版本号。迁移位于文件夹中:
/db/migrate

