Module: PgPower::CreateIndexConcurrently::Migrator

Extended by:
ActiveSupport::Concern
Defined in:
lib/pg_power/create_index_concurrently.rb

Overview

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 Also:

  • ActiveRecord::Migrator.migrate
  • ActiveRecord::Migrator.ddl_transaction

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



197
198
199
# File 'lib/pg_power/create_index_concurrently.rb', line 197

def self.included(klass)
  klass.alias_method_chain :ddl_transaction, :postponed_queries
end

Instance Method Details

#ddl_transaction_with_postponed_queries(*args, &block) ⇒ Object

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

See Also:

  • ActiveRecord::Migrator.ddl_transaction


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/pg_power/create_index_concurrently.rb', line 206

def ddl_transaction_with_postponed_queries(*args, &block)
  ddl_transaction_without_postponed_queries(*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