Module: ActiveRecord::ConnectionAdapters::SchemaStatements
- Defined in:
- lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb
Overview
:nodoc:
Constant Summary collapse
- FUNCTIONAL_INDEX_REGEXP =
Regexp used to find the function name and function argument of a function call
/(\w+)\(((?:'.+'(?:::\w+)?, *)*)(\w+)\)/
Instance Method Summary collapse
-
#add_index_with_concurrently(table_name, column_name, options = {}) ⇒ Object
Redefine original add_index method to handle :concurrently option.
-
#index_exists?(table_name, column_name, options = {}) ⇒ Boolean
Check to see if an index exists on a table for a given index definition.
-
#index_name(table_name, options) ⇒ Object
Derive the name of the index from the given table name and options hash.
Instance Method Details
#add_index_with_concurrently(table_name, column_name, options = {}) ⇒ Object
Redefine original add_index method to handle :concurrently option.
Adds a new index to the table. column_name
can be a single Symbol, or an Array of Symbols.
Creating a partial index
add_index(:accounts, [:branch_id, :party_id], :using => 'BTree'
:unique => true, :concurrently => true, :where => 'active')
generates
CREATE UNIQUE INDEX CONCURRENTLY
index_accounts_on_branch_id_and_party_id
ON
accounts(branch_id, party_id)
WHERE
active
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb', line 25 def add_index_with_concurrently(table_name, column_name, = {}) creation_method = .delete(:concurrently) ? 'CONCURRENTLY' : nil index_name, index_type, index_columns, , index_algorithm, index_using = (table_name, column_name, ) # GOTCHA: # It ensures that there is no existing index only for the case when the index # is created concurrently to avoid changing the error behavior for default # index creation. # -- zekefast 2012-09-25 # GOTCHA: # This check prevents invalid index creation, so after migration failed # here there is no need to go to database and clean it from invalid # indexes. But note that this handles only one of the cases when index # creation can fail!!! All other case should be procesed manually. # -- zekefast 2012-09-25 if creation_method.present? && index_exists?(table_name, column_name, ) raise ::PgPower::IndexExistsError, "Index #{index_name} for `#{table_name}.#{column_name}` " \ "column can not be created concurrently, because such index already exists." end statements = [] statements << "CREATE #{index_type} INDEX" statements << creation_method if creation_method.present? statements << index_algorithm if index_algorithm.present? statements << quote_column_name(index_name) statements << "ON" statements << quote_table_name(table_name) statements << index_using if index_using.present? statements << "(#{index_columns})" if index_columns.present? statements << if .present? sql = statements.join(' ') execute(sql) end |
#index_exists?(table_name, column_name, options = {}) ⇒ Boolean
Check to see if an index exists on a table for a given index definition.
Examples
# Check that a partial index exists
index_exists?(:suppliers, :company_id, :where => 'active')
# GIVEN: 'index_suppliers_on_company_id' UNIQUE, btree (company_id) WHERE active
index_exists?(:suppliers, :company_id, :unique => true, :where => 'active') => true
index_exists?(:suppliers, :company_id, :unique => true) => false
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb', line 77 def index_exists?(table_name, column_name, = {}) column_names = Array.wrap(column_name) index_name = .key?(:name) ? [:name].to_s : index_name(table_name, :column => column_names) # Always compare the index name default_comparator = lambda { |index| index.name == index_name } comparators = [default_comparator] # Add a comparator for each index option that is part of the query = [:unique, :where] .each do |index_option| comparators << if .key?(index_option) lambda do |index| pg_where_clause = index.send(index_option) # pg does nothing to boolean clauses, e.g. 'where active' => 'where active' if pg_where_clause.is_a?(TrueClass) or pg_where_clause.is_a?(FalseClass) pg_where_clause == [index_option] else # pg adds parentheses around non-boolean clauses, e.g. 'where color IS NULL' => 'where (color is NULL)' pg_where_clause.gsub!(/[()]/,'') # pg casts string comparison ::text. e.g. "where color = 'black'" => "where ((color)::text = 'black'::text)" pg_where_clause.gsub!(/::text/,'') # prevent case from impacting the comparison pg_where_clause.downcase == [index_option].downcase end end else # If the given index_option is not an argument to the index_exists? query, # select only those pg indexes that do not have the component lambda { |index| index.send(index_option).blank? } end end # Search all indexes for any that match all comparators indexes(table_name).any? do |index| comparators.inject(true) { |ret, comparator| ret && comparator.call(index) } end end |
#index_name(table_name, options) ⇒ Object
Derive the name of the index from the given table name and options hash.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb', line 117 def index_name(table_name, ) #:nodoc: if Hash === # legacy support if [:column] column_names = Array.wrap([:column]).map {|c| expression_index_name(c)} "index_#{table_name}_on_#{column_names * '_and_'}" elsif [:name] [:name] else raise ArgumentError, "You must specify the index name" end else index_name(table_name, :column => ) end end |