Class: DeclareSchema::Model::IndexDefinition
- Inherits:
-
Object
- Object
- DeclareSchema::Model::IndexDefinition
- Includes:
- Comparable
- Defined in:
- lib/declare_schema/model/index_definition.rb
Defined Under Namespace
Classes: IndexNameTooLongError
Constant Summary collapse
- PRIMARY_KEY_NAME =
"PRIMARY"
Instance Attribute Summary collapse
-
#explicit_name ⇒ Object
readonly
TODO: replace ‘fields` with `columns` and remove alias.
-
#fields ⇒ Object
(also: #columns)
readonly
TODO: replace ‘fields` with `columns` and remove alias.
-
#name ⇒ Object
readonly
TODO: replace ‘fields` with `columns` and remove alias.
-
#table ⇒ Object
readonly
TODO: replace ‘fields` with `columns` and remove alias.
-
#unique ⇒ Object
readonly
TODO: replace ‘fields` with `columns` and remove alias.
-
#where ⇒ Object
readonly
TODO: replace ‘fields` with `columns` and remove alias.
Class Method Summary collapse
- .default_index_name(table, fields) ⇒ Object
-
.for_model(model, old_table_name = nil) ⇒ Object
extract IndexSpecs from an existing table includes the PRIMARY KEY index.
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #equivalent?(rhs) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(model, fields, **options) ⇒ IndexDefinition
constructor
A new instance of IndexDefinition.
- #primary_key? ⇒ Boolean
- #settings ⇒ Object
- #to_key ⇒ Object
- #with_name(new_name) ⇒ Object
Constructor Details
#initialize(model, fields, **options) ⇒ IndexDefinition
Returns a new instance of IndexDefinition.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/declare_schema/model/index_definition.rb', line 18 def initialize(model, fields, **) @model = model @table = .delete(:table_name) || model.table_name @fields = Array.wrap(fields).map(&:to_s) @explicit_name = [:name] unless .delete(:allow_equivalent) @name = .delete(:name) || self.class.default_index_name(@table, @fields) @unique = .delete(:unique) || name == PRIMARY_KEY_NAME || false if DeclareSchema.max_index_and_constraint_name_length && @name.length > DeclareSchema.max_index_and_constraint_name_length raise IndexNameTooLongError, "Index '#{@name}' exceeds configured limit of #{DeclareSchema.max_index_and_constraint_name_length} characters. Give it a shorter name, or adjust DeclareSchema.max_index_and_constraint_name_length if you know your database can accept longer names." end if (where = [:where]) @where = where.start_with?('(') ? where : "(#{where})" end end |
Instance Attribute Details
#explicit_name ⇒ Object (readonly)
TODO: replace ‘fields` with `columns` and remove alias. -Colin
11 12 13 |
# File 'lib/declare_schema/model/index_definition.rb', line 11 def explicit_name @explicit_name end |
#fields ⇒ Object (readonly) Also known as: columns
TODO: replace ‘fields` with `columns` and remove alias. -Colin
11 12 13 |
# File 'lib/declare_schema/model/index_definition.rb', line 11 def fields @fields end |
#name ⇒ Object (readonly)
TODO: replace ‘fields` with `columns` and remove alias. -Colin
11 12 13 |
# File 'lib/declare_schema/model/index_definition.rb', line 11 def name @name end |
#table ⇒ Object (readonly)
TODO: replace ‘fields` with `columns` and remove alias. -Colin
11 12 13 |
# File 'lib/declare_schema/model/index_definition.rb', line 11 def table @table end |
#unique ⇒ Object (readonly)
TODO: replace ‘fields` with `columns` and remove alias. -Colin
11 12 13 |
# File 'lib/declare_schema/model/index_definition.rb', line 11 def unique @unique end |
#where ⇒ Object (readonly)
TODO: replace ‘fields` with `columns` and remove alias. -Colin
11 12 13 |
# File 'lib/declare_schema/model/index_definition.rb', line 11 def where @where end |
Class Method Details
.default_index_name(table, fields) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/declare_schema/model/index_definition.rb', line 61 def default_index_name(table, fields) index_name = nil [:long_index_name, :short_index_name].find do |method_name| index_name = send(method_name, table, fields) if DeclareSchema.max_index_and_constraint_name_length.nil? || index_name.length <= DeclareSchema.max_index_and_constraint_name_length break index_name end end or raise IndexNameTooLongError, "Default index name '#{index_name}' exceeds configured limit of #{DeclareSchema.max_index_and_constraint_name_length} characters. Use the `name:` option to give it a shorter name, or adjust DeclareSchema.max_index_and_constraint_name_length if you know your database can accept longer names." end |
.for_model(model, old_table_name = nil) ⇒ Object
extract IndexSpecs from an existing table includes the PRIMARY KEY index
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/declare_schema/model/index_definition.rb', line 38 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 primary_key_columns 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 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 |
Instance Method Details
#<=>(rhs) ⇒ Object
117 118 119 |
# File 'lib/declare_schema/model/index_definition.rb', line 117 def <=>(rhs) to_key <=> rhs.to_key end |
#equivalent?(rhs) ⇒ Boolean
121 122 123 |
# File 'lib/declare_schema/model/index_definition.rb', line 121 def equivalent?(rhs) settings == rhs.settings end |
#hash ⇒ Object
113 114 115 |
# File 'lib/declare_schema/model/index_definition.rb', line 113 def hash to_key.hash end |
#primary_key? ⇒ Boolean
101 102 103 |
# File 'lib/declare_schema/model/index_definition.rb', line 101 def primary_key? name == PRIMARY_KEY_NAME end |
#settings ⇒ Object
109 110 111 |
# File 'lib/declare_schema/model/index_definition.rb', line 109 def settings @settings ||= [table, fields, unique].map(&:to_s) end |
#to_key ⇒ Object
105 106 107 |
# File 'lib/declare_schema/model/index_definition.rb', line 105 def to_key @key ||= [table, fields, name, unique, where].map(&:to_s) end |
#with_name(new_name) ⇒ Object
125 126 127 |
# File 'lib/declare_schema/model/index_definition.rb', line 125 def with_name(new_name) self.class.new(@model, @fields, table_name: @table_name, index_name: @index_name, unique: @unique, name: new_name) end |