Ruby-on-rails 如何回滚特定的迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3647685/
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 a specific migration?
提问by AnApprentice
I have the following migration file db\migrate\20100905201547_create_blocks.rb
我有以下迁移文件 db\migrate\20100905201547_create_blocks.rb
How can I specifically rollback that migration file?
我如何专门回滚该迁移文件?
回答by Zachary Wright
rake db:rollback STEP=1
Is a way to do this, if the migration you want to rollback is the last one applied. You can substitute 1 for however many migrations you want to go back.
如果要回滚的迁移是最后应用的迁移,则是一种方法。您可以将 1 替换为要返回的迁移次数。
For example:
例如:
rake db:rollback STEP=5
Will also rollback all the migration that happened later (4, 3, 2 and also 1).
还将回滚稍后发生的所有迁移(4、3、2 和 1)。
To roll back all migrations back to (and including) a target migration, use: (This corrected command was added AFTER all the comments pointing out the error in the original post)
要将所有迁移回滚到(并包括)目标迁移,请使用:(在指出原始帖子中的错误的所有评论之后添加了此更正的命令)
rake db:migrate VERSION=20100905201547
In order to rollback ONLY ONE specific migration (OUT OF ORDER) use:
为了仅回滚一个特定的迁移(乱序),请使用:
rake db:migrate:down VERSION=20100905201547
Note that this will NOT rollback any interceding migrations -- only the one listed. If that is not what you intended, you can safely run rake db:migrateand it will re-run only that one, skipping any others that were not previously rolled back.
请注意,这不会回滚任何中间迁移——仅回滚列出的迁移。如果这不是你想要的,你可以安全地运行rake db:migrate,它只会重新运行那个,跳过以前没有回滚的任何其他人。
And if you ever want to migrate a single migration out of order, there is also its inverse db:migrate:up:
如果您想无序地迁移单个迁移,还有它的逆db:migrate:up:
rake db:migrate:up VERSION=20100905201547
回答by John Creamer
rake db:migrate:down VERSION=20100905201547
will roll back the specific file.
将回滚特定文件。
To find the version of all migrations, you can use this command:
要查找所有迁移的版本,可以使用以下命令:
rake db:migrate:status
Or, simply the prefix of the migration's file name is the version you need to rollback.
或者,只需迁移文件名的前缀就是您需要回滚的版本。
See the Ruby on Rails guide entryon migrations.
请参阅有关迁移的 Ruby on Rails 指南条目。
回答by Waleed
To rollback the last migration you can do:
要回滚上次迁移,您可以执行以下操作:
rake db:rollback
If you want to rollback a specific migration with a version you should do:
如果要使用版本回滚特定迁移,您应该执行以下操作:
rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION
For e.g. if the version is 20141201122027, you will do:
例如,如果版本是 20141201122027,您将执行以下操作:
rake db:migrate:down VERSION=20141201122027
to rollback that specific migration.
回滚该特定迁移。
回答by uma
You can rollback your migration by using rake db:rollbackwith different options. The syntax will be different according to your requirements.
您可以使用rake db:rollback不同的选项来回滚迁移。根据您的要求,语法会有所不同。
If you want to rollback just the last migration, then you can use either
如果您只想回滚上次迁移,那么您可以使用
rake db:rollback
or
或者
rake db:rollback STEP=1
If you want rollback number of migrations at once, then you simply pass an argument:
如果您想一次回滚迁移次数,那么您只需传递一个参数:
rake db:rollback STEP=n
where nis number of migrations to rollback, counting from latest migration.
哪里n是要回滚的迁移数,从最新迁移开始计算。
If you want to rollback to a specific migration, then you should pass the version of the migration in the following:
如果要回滚到特定迁移,则应在以下内容中传递迁移版本:
rake db:migrate:down VERSION=xxxxx
where xxxxx is the version number of the migration.
其中 xxxxx 是迁移的版本号。
回答by Hardik
rake db:migrate:down VERSION=your_migrations's_version_number_here
rake db:migrate:down VERSION=your_migrations's_version_number_here
The version is the numerical prefix on the migration's file name
版本是迁移文件名的数字前缀
How to find version:
如何找到版本:
Your migration files are stored in your rails_root/db/migratedirectory. Find appropriate file up to which you want to rollback and copy the prefix number.
您的迁移文件存储在您的rails_root/db/migrate目录中。找到要回滚的适当文件并复制前缀号。
for example
例如
file name: 20140208031131_create_roles.rbthen the version is 20140208031131
文件名:20140208031131_create_roles.rb那么版本是20140208031131
回答by Deepak Mahakale
Rolling back last migration:
回滚上次迁移:
# rails < 5.0
rake db:rollback
# rails >= 5.0
rake db:rollback
# or
rails db:rollback
Rolling back last nnumber of migrations
回滚最后一次n迁移
# rails < 5.0
rake db:rollback STEP=2
# rails >= 5.0
rake db:rollback STEP=2
# or
rails db:rollback STEP=2
Rolling back a specific migration
回滚特定的迁移
# rails < 5.0
rake db:migrate:down VERSION=20100905201547
# rails >= 5.0
rake db:migrate:down VERSION=20100905201547
# or
rails db:migrate:down VERSION=20100905201547
回答by Sandip Vavhal
To rollback the last migration you can do:
要回滚上次迁移,您可以执行以下操作:
rake db:rollback
If you want to rollback a specific migration with a version you should do:
如果要使用版本回滚特定迁移,您应该执行以下操作:
rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION
If the migration file you want to rollback was called db/migrate/20141201122027_create_some_table.rb, then the VERSION for that migration is 20141201122027, which is the timestamp of when that migration was created, and the command to roll back that migration would be:
如果您要回滚的迁移文件被称为db/migrate/20141201122027_create_some_table.rb,则该迁移的 VERSION 是20141201122027,这是创建该迁移时的时间戳,并且回滚该迁移的命令将是:
rake db:migrate:down VERSION=20141201122027
回答by Santanu
If it is a reversible migration and the last one which has been executed, then run rake db:rollback. And you can always use version.
e.g
如果是可逆迁移并且是最后一个已执行的迁移,则运行rake db:rollback. 而且您始终可以使用版本。例如
the migration file is 20140716084539_create_customer_stats.rb,so the rollback command will be,
rake db:migrate:down VERSION=20140716084539
迁移文件是 20140716084539_create_customer_stats.rb,所以回滚命令是,
rake db:migrate:down VERSION=20140716084539
回答by Jon Schneider
To roll back all migrations up to a particular version(e.g. 20181002222222), use:
要将所有迁移回滚到特定版本(例如20181002222222),请使用:
rake db:migrate VERSION=20181002222222
(Note that this uses db:migrate-- not db:migrate:downas in other answers to this question.)
(请注意,这使用db:migrate-db:migrate:down与此问题的其他答案不同。)
Assuming the specified migration version is older than the current version, this will roll back all migrations up to, but not including, the specified version.
假设指定的迁移版本比当前版本旧,这会将所有迁移回滚到指定版本,但不包括指定版本。
For example, if rake db:migrate:statusinitially displays:
例如,如果rake db:migrate:status最初显示:
(... some older migrations ...)
up 20181001002039 Some migration description
up 20181002222222 Some migration description
up 20181003171932 Some migration description
up 20181004211151 Some migration description
up 20181005151403 Some migration description
Running:
跑步:
rake db:migrate VERSION=20181002222222
Will result in:
会导致:
(... some older migrations ...)
up 20181001002039 Some migration description
up 20181002222222 Some migration description
down 20181003171932 Some migration description
down 20181004211151 Some migration description
down 20181005151403 Some migration description
Reference: https://makandracards.com/makandra/845-migrate-or-revert-only-some-migrations
参考:https: //makandracards.com/makandra/845-migrate-or-revert-only-some-migrations
回答by Manish Shrivastava
From Rails Guide
来自Rails 指南
Reverting Previous Migrations
恢复以前的迁移
You can use Active Record's ability to rollback migrations using the revertmethod:
你可以使用 Active Record 的revert方法来回滚迁移:
require_relative '20100905201547_create_blocks'
class FixupCreateBlock < ActiveRecord::Migration
def change
revert CreateBlock
create_table(:apples) do |t|
t.string :variety
end
end
end
The revertmethod also accepts a block of instructions to reverse. This could be useful to revert selected parts of previous migrations. For example, let's imagine that CreateBlock is committed and it is later decided it would be best to use Active Record validations, in place of the CHECK constraint, to verify the zipcode.
该revert方法还接受要反转的指令块。这对于恢复以前迁移的选定部分可能很有用。例如,假设 CreateBlock 已提交,后来决定最好使用 Active Record 验证代替 CHECK 约束来验证邮政编码。
class DontUseConstraintForZipcodeValidationMigration < ActiveRecord::Migration
def change
revert do
# copy-pasted code from CreateBlock
reversible do |dir|
dir.up do
# add a CHECK constraint
execute <<-SQL
ALTER TABLE distributors
ADD CONSTRAINT zipchk
CHECK (char_length(zipcode) = 5);
SQL
end
dir.down do
execute <<-SQL
ALTER TABLE distributors
DROP CONSTRAINT zipchk
SQL
end
end
# The rest of the migration was ok
end
end
end
The same migration could also have been written without using revert but this would have involved a few more steps: reversing the order of create_table and reversible, replacing create_table by drop_table, and finally replacing up by down and vice-versa. This is all taken care of by revert.
也可以在不使用 revert 的情况下编写相同的迁移,但这将涉及更多步骤:反转 create_table 和 reversible 的顺序,用 drop_table 替换 create_table,最后用 down 替换 up,反之亦然。这一切都由 revert 处理。

