Module: SafePgMigrations::StatementInsurer::RemoveColumnIndex

Included in:
SafePgMigrations::StatementInsurer
Defined in:
lib/safe-pg-migrations/plugins/statement_insurer/remove_column_index.rb

Instance Method Summary collapse

Instance Method Details

#remove_column_with_composite_index(table, column) ⇒ Object

Raises:

  • (StandardError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/safe-pg-migrations/plugins/statement_insurer/remove_column_index.rb', line 6

def remove_column_with_composite_index(table, column)
  existing_indexes = indexes(table).select { |index|
    index.columns.size > 1 && index.columns.include?(column.to_s)
  }

  return unless existing_indexes.any?

  error_message = <<~ERROR
    Cannot drop column #{column} from table #{table} because composite index(es): #{existing_indexes.map(&:name).join(', ')} is/are present.
    If they are still required, create the index(es) without #{column} before dropping the existing index(es).
    Then you will be able to drop the column.
  ERROR

  raise StandardError, error_message
end