Module: PgPower::CreateIndexConcurrently::Migration
- Defined in:
- lib/pg_power/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
-
#add_foreign_key(from_table, to_table, options = {}, &block) ⇒ Object
Add a foreign key.
-
#add_index(table_name, column_name, options = {}, &block) ⇒ nil
Add a new index to the table.
-
#process_postponed_queries ⇒ ::PgPower::CreateIndexConcurrently::Migration
Execute all postponed index creation.
Instance Method Details
#add_foreign_key(from_table, to_table, options = {}, &block) ⇒ Object
Add a foreign key.
Options:
-
:column
-
:primary_key
-
:dependent
-
:exclude_index [Boolean]
-
:concurrent_index [Boolean]
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/pg_power/create_index_concurrently.rb', line 110 def add_foreign_key(from_table, to_table, = {}, &block) from_table = ::ActiveRecord::Migrator.proper_table_name(from_table) if [:concurrent_index] if [:exclude_index] raise ArgumentError, 'Conflicted options(exclude_index, concurrent_index) was found, both are set to true.' end [:column] ||= connection.id_column_name_from_table_name(to_table) = .merge(:concurrently => [:concurrent_index]) = { :concurrently => true } enque(from_table, [:column], ) end # GOTCHA: # proceed foreign key creation, but giving :concurrent_index => true # prevent normal index creation in PgPower's `add_foreign_key`. # So, postponed creation could be done after transaction. # -- zekefast 2012-09-12 connection.add_foreign_key(from_table, to_table, , &block) end |
#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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pg_power/create_index_concurrently.rb', line 72 def add_index(table_name, column_name, = {}, &block) table_name = ::ActiveRecord::Migrator.proper_table_name(table_name) # GOTCHA: # checks if index should be created concurretnly 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 PgPower's `add_index`. # Block is given for future compatibility. # -- zekefast 2012-09-12 unless [:concurrently] return connection.add_index(table_name, column_name, , &block) end enque(table_name, column_name, , &block) nil end |
#process_postponed_queries ⇒ ::PgPower::CreateIndexConcurrently::Migration
Execute all postponed index creation.
135 136 137 138 139 140 141 142 143 |
# File 'lib/pg_power/create_index_concurrently.rb', line 135 def process_postponed_queries Array(@postponed_queries).each do |arguments, block| connection.add_index(*arguments, &block) end clear_queue self end |