在Active Record迁移中可逆并恢复

时间:2013-11-04 02:51:50

标签: ruby-on-rails activerecord

我查看了Rails指南和Rails API,我无法理解可逆和还原的用法。

例如,请参阅此处链接的示例http://guides.rubyonrails.org/migrations.html#using-reversible,其中包含以下内容:\

它说 Complex migrations may require processing that Active Record doesn't know how to reverse. You can use reversible to specify what to do when running a migration what else to do when reverting it. For example,

class ExampleMigration < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.references :category
    end

reversible do |dir|
  dir.up do
    #add a foreign key
    execute <<-SQL
      ALTER TABLE products
        ADD CONSTRAINT fk_products_categories
        FOREIGN KEY (category_id)
        REFERENCES categories(id)
    SQL
  end
  dir.down do
    execute <<-SQL
      ALTER TABLE products
        DROP FOREIGN KEY fk_products_categories
    SQL
  end
end

add_column :users, :home_page_url, :string
rename_column :users, :email, :email_address
end

我知道向下部分中的代码将在回滚时运行,但为什么要在up块中包含代码?我还看到了另一个例子,它有一个只有一个向上块的可逆部分。这些代码的目的是什么?

最后,我不明白revert。下面是Rails指南中包含的示例,但对我来说没什么意义。

`The revert method 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 ExampleMigration is committed and it is later decided it would be best to serialize the product list instead. One could write:
class SerializeProductListMigration < ActiveRecord::Migration
  def change
    add_column :categories, :product_list


reversible do |dir|
      dir.up do
        # transfer data from Products to Category#product_list
      end
      dir.down do
        # create Products from Category#product_list
      end
    end

    revert do
      # copy-pasted code from ExampleMigration
      create_table :products do |t|
        t.references :category
      end

      reversible do |dir|
        dir.up do
          #add a foreign key
          execute <<-SQL
            ALTER TABLE products
              ADD CONSTRAINT fk_products_categories
              FOREIGN KEY (category_id)
              REFERENCES categories(id)
          SQL
        end
        dir.down do
          execute <<-SQL
            ALTER TABLE products
              DROP FOREIGN KEY fk_products_categories
          SQL
        end
      end

      # The rest of the migration was ok
    end
  end
end`

1 个答案:

答案 0 :(得分:1)

我不是这方面的专家,但是通过阅读指南我的理解如下:

第一个示例中的reversible调用表示change迁移的四个组成部分中的第二个。 (注意:你所拥有的缩进在这方面有误导性,应该更新以匹配指南。)它与其他组件有关但不同,所以它有updownreversible部分。我无法解释为什么你会revert只有一个方向而不是另一方,正如你所说的那样。

{{1}}调用告诉系统通过名称或提供描述(转发)迁移的块来恢复先前的迁移。你展示的例子是后一种情况,我认为最好通过仔细阅读指南中的段落来理解:

  

也可以在不使用恢复的情况下编写相同的迁移   但这将涉及更多的步骤:扭转顺序   create_table和reversible,用drop_table替换create_table,和   最后取而代之,反之亦然。这一切都得到了照顾   通过还原。