lib/declare_schema/model/index_definition.rb
# extract IndexSpecs from an existing table
# always includes the PRIMARY KEY index
extract IndexSpecs from an existing table
always includes the PRIMARY KEY index
extract IndexSpecs from an existing table always includes the PRIMARY KEY index
def for_model(model, old_table_name = nil)
          t = old_table_name || model.table_name
          primary_key_columns = Array(model.connection.primary_key(t)).presence || fallback_find_primary_key(model, t) or
            raise "could not find primary key for table #{t} in #{model.connection.columns(t).inspect}"
          primary_key_found = false
          index_definitions = model.connection.indexes(t).map do |i|
            model.ignore_indexes.include?(i.name) and next
            if i.name == PRIMARY_KEY_NAME
              i.columns == primary_key_columns && i.unique or
                raise "primary key on #{t} was not unique on #{primary_key_columns} (was unique=#{i.unique} on #{i.columns})"
              primary_key_found = true
            elsif i.columns == primary_key_columns && i.unique
              next
            end
            new(model, i.columns, name: i.name, unique: i.unique, where: i.where, table_name: old_table_name)
          end.compact
          if !primary_key_found
            index_definitions << new(model, primary_key_columns, name: PRIMARY_KEY_NAME, unique: true, where: nil, table_name: old_table_name)
          end
          index_definitions
        end
def for_model ( model , old_table_name = nil ) t = old_table_name || model . table_name primary_key_columns = Array ( model . connection . primary_key ( t ) ) . presence || fallback_find_primary_key ( model , t ) or raise "could not find primary key for table #{t} in #{model.connection.columns(t).inspect}" primary_key_found = false index_definitions = model . connection . indexes ( t ) . map do | i | model . ignore_indexes . include? ( i . name ) and next if i . name == PRIMARY_KEY_NAME i . columns == primary_key_columns && i . unique or raise "primary key on #{t} was not unique on #{primary_key_columns} (was unique=#{i.unique} on #{i.columns})" primary_key_found = true elsif i . columns == primary_key_columns && i . unique next end new ( model , i . columns , name : i . name , unique : i . unique , where : i . where , table_name : old_table_name ) end . compact if ! primary_key_found index_definitions << new ( model , primary_key_columns , name : PRIMARY_KEY_NAME , unique : true , where : nil , table_name : old_table_name ) end index_definitions end
extract IndexSpecs from an existing table
always includes the PRIMARY KEY index
extract IndexSpecs from an existing table always includes the PRIMARY KEY index
# skip this primary key index since we'll create it below, with PRIMARY_KEY_NAME
model
<not_specific>
old_table_name
<not_specific>