迁移只是一个 Ruby 文件,因此您可以require
:
require "./db/migrate/20120117195557_create_model.rb"
class ReverseCreateModel < ActiveRecord::Migration
def up
CreateModel.new.down
end
def down
CreateModel.new.up
end
end
如果您的原始迁移使用change
,则必须使用CreateModel.new.migrate(:down)
and CreateModel.new.migrate(:up)
。
就我而言,当使用migrate(direction)
迁移时会生成更多输出:
== ReverseCreateModel: migrating ======================================
== CreateModel: reverting =============================================
(...)
== CreateModel: reverted (0.0018s) ====================================
== ReverseCreateModel: migrated (0.0019s) =============================
代替:
== ReverseCreateModel: migrating ======================================
(...)
== ReverseCreateModel: migrated (0.0019s) =============================
这个答案基于https://stackoverflow.com/a/754316/183791