3

Rails Guides と Rails API を見てきましたが、reversible と revert の使い方がわかりません。

たとえば、ここにリンクされている例を参照してください 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 ブロックにコードを含めるのはなぜですか? アップブロックのみの可逆セクションを持つ別の例も見ました。そのようなコードの目的は何でしょうか?

最後に、私は理解していません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`
4

1 に答える 1

1

私は決してこれの専門家ではありませんが、ガイドを読んだことによる私の理解は次のとおりです。

最初の例の呼び出しは、移行reversibleの 4 つのコンポーネントの 2 番目を表しています。(注: インデントはこの点で誤解を招く可能性があり、おそらくガイドに合わせて更新する必要があります。) これは他のコンポーネントに関連していますが、それとは異なるため、 andセクションchangeの両方を持つことは理にかなっています。あなたが見たことを示したように、なぜあなたが一方の方向だけを持ち、もう一方の方向を持たないのか説明できません。updownreversible

このrevert呼び出しは、名前または (前方) 移行を説明するブロックを提供することによって、以前の移行を元に戻すようにシステムに指示しています。あなたが示した例は後者のケースであり、ガイドの次の段落を注意深く読むことで最もよく理解できると思います。

revert を使用せずに同じ移行を記述することもできますが、これにはさらにいくつかの手順が必要になります。つまり、create_table と reversible の順序を逆にし、create_table を drop_table に置き換え、最後に up を down に置き換え、その逆です。これはすべて revert によって処理されます。

于 2013-11-04T04:20:08.380 に答える