跳至内容 跳至搜索

Exception 可以被抛出以阻止迁移回滚。例如,以下迁移不可逆。回滚此迁移将引发 ActiveRecord::IrreversibleMigration 错误。

class IrreversibleMigrationExample < ActiveRecord::Migration[8.0]
  def change
    create_table :distributors do |t|
      t.string :zipcode
    end

    execute <<~SQL
      ALTER TABLE distributors
        ADD CONSTRAINT zipchk
          CHECK (char_length(zipcode) = 5) NO INHERIT;
    SQL
  end
end

有两种方法可以缓解此问题。

  1. 定义 #up#down 方法而不是 #change

class ReversibleMigrationExample < ActiveRecord::Migration[8.0]
  def up
    create_table :distributors do |t|
      t.string :zipcode
    end

    execute <<~SQL
      ALTER TABLE distributors
        ADD CONSTRAINT zipchk
          CHECK (char_length(zipcode) = 5) NO INHERIT;
    SQL
  end

  def down
    execute <<~SQL
      ALTER TABLE distributors
        DROP CONSTRAINT zipchk
    SQL

    drop_table :distributors
  end
end
  1. #change 方法中使用可逆方法

class ReversibleMigrationExample < ActiveRecord::Migration[8.0]
  def change
    create_table :distributors do |t|
      t.string :zipcode
    end

    reversible do |dir|
      dir.up do
        execute <<~SQL
          ALTER TABLE distributors
            ADD CONSTRAINT zipchk
              CHECK (char_length(zipcode) = 5) NO INHERIT;
        SQL
      end

      dir.down do
        execute <<~SQL
          ALTER TABLE distributors
            DROP CONSTRAINT zipchk
        SQL
      end
    end
  end
end