module PgSaurus::CreateIndexConcurrently::Migrator

Run postponed index creation for each migration.

This module included into (see ::ActiveRecord::Migrator) class to make possible to execute queries for postponed index creation after closing migration's transaction.

@see ::ActiveRecord::Migrator.migrate @see ::ActiveRecord::Migrator.ddl_transaction

Public Instance Methods

ddl_transaction(*args, &block) click to toggle source

Override (see ::ActiveRecord::Migrator.ddl_transaction) to call (see ::PgSaurus::CreateIndexConcurrently::Migration.process_postponed_queries) immediately after transaction.

@see ::ActiveRecord::Migrator.ddl_transaction

Calls superclass method
# File lib/pg_saurus/create_index_concurrently.rb, line 154
def ddl_transaction(*args, &block)
  super(*args, &block)

  # GOTCHA:
  #   This might be a bit tricky, but I've decided that this is the best
  #   way to retrieve migration instance after closing transaction.
  #   The problem that (see ::ActiveRecord::Migrator) doesn't provide any
  #   access to recently launched migration. All logic to iterate through
  #   set of migrations incapsulated in (see ::ActiveRecord::Migrator.migrate)
  #   method.
  #   So, to get access to migration you need to override `migrate` method
  #   and duplicated all logic inside it, plus add call to
  #   `process_postponed_queries`.
  #   I've decided this is less forward compatible then retrieving
  #   value of `migration` variable in context where block
  #   given to `ddl_transaction` method was created.
  #   -- zekefast 2012-09-12
  migration = block.binding.eval('migration')
  migration.process_postponed_queries
end