在 Rails 中还原特定的旧迁移而不还原以后的迁移

Revert specific, old migration in Rails without reverting later migrations

我开始在 Rails 应用程序中使用 Rolify,并在大约 15 次迁移前创建了一个迁移来设置其表。我现在决定用我自己的代码替换它,并希望在不触及所有后续迁移的情况下恢复该迁移。数据库正在使用中,所以恢复 15,删除我不想添加的那个,然后应用后续的 14 会破坏数据。

Section 3.11 of the Rails Guide on migrations 建议这可以通过创建一个新的迁移来完成,该迁移按名称恢复特定的旧迁移:

class FixupExampleMigration < ActiveRecord::Migration
  def change
    revert ExampleMigration

    create_table(:apples) do |t|
      t.string :variety
    end
  end
end

我尝试根据我的上下文对其进行自定义,如下所示:

class RolifyDestroyRoles < ActiveRecord::Migration
  def change
    revert RolifyCreateRoles
  end
end

(我最初的 Rolify 迁移的第一行是 class RolifyCreateRoles < ActiveRecord::Migration)。但是,我收到命名空间错误:

StandardError: An error has occurred, this and all later migrations canceled:

uninitialized constant RolifyDestroyRoles::RolifyCreateRoles/home/slack/rails/tracker/db/migrate/20150127093921_rolify_destroy_roles.rb:3:in `change'

也许 Rails 中发生了一些变化 4. 有谁知道我应该如何引用 RolifyCreateRoles 以便 Rails 可以找到它?

还原 rails 中的特定迁移:

假设我们有一个迁移:

db/migrate/20150127071749_create_users.rb

revert: 
rake db:migrate:down VERSION=20150127071749

setup again:
rake db:migrate:up VERSION=20150127071749

希望对您有所帮助:)