ruby-on-rails 活动记录迁移中的可逆和恢复

raogr8fs  于 11个月前  发布在  Ruby
关注(0)|答案(2)|浏览(133)

我看过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

字符串
我知道down部分的代码将在回滚时运行,但为什么要在up块中包含代码?我还看到另一个例子,它有一个可逆的部分,只有一个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`

u5rb5r59

u5rb5r591#

我不是这方面的Maven,但我从阅读指南中的理解如下:
第一个示例中的reversible调用表示change迁移的四个组件中的第二个。(注:在这方面,您的缩进是误导性的,可能应该更新以匹配指南。)它与其他组件相关,但与其他组件不同,所以它有一个updown两个部分是有意义的,我不能解释为什么你只有一个方向的reversible,而不是另一个方向,正如你所看到的。
revert调用告诉系统通过名称或通过提供描述(向前)迁移的块来恢复先前的迁移。您所展示的示例是后一种情况,我认为通过仔细阅读指南中的段落可以更好地理解,即:
同样的迁移也可以在不使用revert的情况下编写,但这将涉及更多的步骤:颠倒create_table和reversible的顺序,用drop_table替换create_table,最后用down替换up,反之亦然。

xam8gpfp

xam8gpfp2#

我看了rails指南版本是v7.1.2
如果你使用revert

def change
    revert do
      # copy-pasted code from ExampleMigration
      reversible do |direction|
        direction.up do
          add_column :apples, :address, :string
        end
        direction.down do
          remove_column :apples, :address
        end
      end

      # The rest of the migration was ok
    end
  end

字符串
如果你像这样使用可逆转的

def change
    reversible do |direction|
      change_table :apples do |_t|
        direction.up   { remove_column :apples, :address }
        direction.down { add_column :apples, :address, :string }
      end
    end
  end


对于以下内容,

  • 同样的迁移也可以在不使用revert的情况下编写,但这将涉及更多的步骤:颠倒create_table和reversible的顺序,用drop_table替换create_table,最后用down替换up,反之亦然。

相关问题