Module: PgSaurus::CreateIndexConcurrently::Migration

Defined in:
lib/pg_saurus/create_index_concurrently.rb

Overview

Provides ability to postpone index creation queries in migrations.

Overrides ‘add_index` and `add_foreign_key` methods for migration to be able to prevent indexes creation inside scope of transaction if they have to be created concurrently. Allows to run creation of postponed indexes.

This module included into ActiveRecord::Migration class to extend it with new features.

All postponed index creation queries are stored inside migration instance.

Instance Method Summary collapse

Instance Method Details

#add_index(table_name, column_name, options = {}, &block) ⇒ nil

Add a new index to the table. column_name can be a single Symbol, or an Array of Symbols.

Parameters:

  • table_name (Symbol, String)
  • column_name (Symbol, String, Array<Symbol, String>)
  • options (optional, Hash) (defaults to: {})

Options Hash (options):

  • :unique (Boolean)
  • :concurrently (Boolean)
  • :where (String)

Returns:

  • (nil)

See Also:

  • in pg_saurus gem


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pg_saurus/create_index_concurrently.rb', line 72

def add_index(table_name, column_name, options = {}, &block)
  table_name = proper_table_name(table_name)
  # GOTCHA:
  #   checks if index should be created concurrently then put it into
  #   the queue to wait till queue processing will be called (should be
  #   happended after closing transaction).
  #   Otherwise just delegate call to PgSaurus's `add_index`.
  #   Block is given for future compatibility.
  #   -- zekefast 2012-09-12
  unless options[:concurrently]
    return connection.add_index(table_name, column_name, options, &block)
  end

  enque(table_name, column_name, options, &block)
  nil
end

#process_postponed_queries::PgSaurus::CreateIndexConcurrently::Migration

Execute all postponed index creation.



92
93
94
95
96
97
98
99
100
# File 'lib/pg_saurus/create_index_concurrently.rb', line 92

def process_postponed_queries
  Array(@postponed_queries).each do |arguments, block|
    connection.add_index(*arguments, &block)
  end

  clear_queue

  self
end