declare_schema 1.4.0.colin.6 → 1.4.0.colin.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/declare_schema/model.rb +21 -24
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migrator.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f8226540260a887af839da5afcb4641217c03ac5a6eb2d7a00da2806188f1ce
|
4
|
+
data.tar.gz: 7eeb464b5bfc9b1fbf88446696ede88b8ea582060658421cf2772e62a6205797
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2294e94f2165ec156807fbf9ba98dea10f191bfda1b4c630f5444ab08ee7f80323e86a9af80e968e20e579e21ba24ddef520312a7f0a146d850b6f8927aa50aa
|
7
|
+
data.tar.gz: 2742ce98af1fb6ef8eb4ba1fa40563a635abb584cc1860efd373a474c6dce9fc1a27b630141f94ea6a23f36602d8381b08d6a33b13b6fd3d352b40df85f37daf
|
data/Gemfile.lock
CHANGED
data/lib/declare_schema/model.rb
CHANGED
@@ -57,11 +57,8 @@ module DeclareSchema
|
|
57
57
|
index(fields.flatten, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME)
|
58
58
|
end
|
59
59
|
|
60
|
-
def constraint(
|
61
|
-
|
62
|
-
unless constraint_specs.any? { |constraint_spec| constraint_spec.foreign_key == fkey_s }
|
63
|
-
constraint_specs << DeclareSchema::Model::ForeignKeyDefinition.new(self, fkey, **options)
|
64
|
-
end
|
60
|
+
def constraint(foreign_key, **options)
|
61
|
+
constraint_specs << DeclareSchema::Model::ForeignKeyDefinition.new(self, foreign_key.to_s, **options)
|
65
62
|
end
|
66
63
|
|
67
64
|
# tell the migration generator to ignore the named index. Useful for existing indexes, or for indexes
|
@@ -152,9 +149,9 @@ module DeclareSchema
|
|
152
149
|
|
153
150
|
super
|
154
151
|
|
155
|
-
|
156
|
-
|
157
|
-
|
152
|
+
reflection = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
|
153
|
+
foreign_key = reflection.foreign_key or raise "Couldn't find foreign_key for #{name} in #{reflection.inspect}"
|
154
|
+
foreign_key_id_column_options = column_options.dup
|
158
155
|
|
159
156
|
# Note: the foreign key limit: should match the primary key limit:. (If there is a foreign key constraint,
|
160
157
|
# those limits _must_ match.) We'd like to call _infer_fk_limit and get the limit right from the PK.
|
@@ -165,31 +162,31 @@ module DeclareSchema
|
|
165
162
|
# The one downside of this approach is that application code that asks the field_spec for the declared
|
166
163
|
# foreign key limit: will always get 8 back even if this is a grandfathered foreign key that points to
|
167
164
|
# a limit: 4 primary key. It seems unlikely that any application code would do this.
|
168
|
-
|
169
|
-
if (inferred_limit = _infer_fk_limit(
|
165
|
+
foreign_key_id_column_options[:pre_migration] = ->(field_spec) do
|
166
|
+
if (inferred_limit = _infer_fk_limit(foreign_key, reflection))
|
170
167
|
field_spec.sql_options[:limit] = inferred_limit
|
171
168
|
end
|
172
169
|
end
|
173
170
|
|
174
|
-
declare_field(
|
171
|
+
declare_field(foreign_key.to_sym, :bigint, **foreign_key_id_column_options)
|
175
172
|
|
176
|
-
if
|
173
|
+
if reflection.options[:polymorphic]
|
177
174
|
foreign_type = options[:foreign_type] || "#{name}_type"
|
178
175
|
_declare_polymorphic_type_field(foreign_type, column_options)
|
179
|
-
index([foreign_type,
|
176
|
+
index([foreign_type, foreign_key], **index_options) if index_options
|
180
177
|
else
|
181
|
-
index(
|
182
|
-
constraint(
|
178
|
+
index(foreign_key, **index_options) if index_options
|
179
|
+
constraint(foreign_key, **fk_options) if fk_options[:constraint_name] != false
|
183
180
|
end
|
184
181
|
end
|
185
182
|
|
186
|
-
def _infer_fk_limit(
|
187
|
-
if
|
188
|
-
if (
|
189
|
-
|
183
|
+
def _infer_fk_limit(foreign_key, reflection)
|
184
|
+
if reflection.options[:polymorphic]
|
185
|
+
if (foreign_key_column = columns_hash[foreign_key.to_s]) && foreign_key_column.type == :integer
|
186
|
+
foreign_key_column.limit
|
190
187
|
end
|
191
188
|
else
|
192
|
-
klass =
|
189
|
+
klass = reflection.klass or raise "Couldn't find belongs_to klass for #{name} in #{reflection.inspect}"
|
193
190
|
if (pk_id_type = klass._table_options&.[](:id))
|
194
191
|
if pk_id_type == :integer
|
195
192
|
4
|
@@ -320,11 +317,11 @@ module DeclareSchema
|
|
320
317
|
end
|
321
318
|
|
322
319
|
attr_types[name] ||
|
323
|
-
if (
|
324
|
-
if
|
325
|
-
|
320
|
+
if (reflection = reflections[name.to_s])
|
321
|
+
if reflection.macro.in?([:has_one, :belongs_to]) && !reflection.options[:polymorphic]
|
322
|
+
reflection.klass
|
326
323
|
else
|
327
|
-
|
324
|
+
reflection
|
328
325
|
end
|
329
326
|
end ||
|
330
327
|
if (col = _column(name.to_s))
|
@@ -426,7 +426,7 @@ module Generators
|
|
426
426
|
|
427
427
|
new_table_name = model.table_name
|
428
428
|
existing_indexes = ::DeclareSchema::Model::IndexDefinition.for_model(model, old_table_name)
|
429
|
-
model_indexes_with_equivalents = model.index_definitions_with_primary_key
|
429
|
+
model_indexes_with_equivalents = model.index_definitions_with_primary_key.to_a
|
430
430
|
model_indexes = model_indexes_with_equivalents.map do |i|
|
431
431
|
if i.explicit_name.nil?
|
432
432
|
if (existing = existing_indexes.find { |e| i != e && e.equivalent?(i) })
|
@@ -486,7 +486,7 @@ module Generators
|
|
486
486
|
::DeclareSchema.default_generate_foreign_keys or return []
|
487
487
|
|
488
488
|
existing_fks = ::DeclareSchema::Model::ForeignKeyDefinition.for_model(model, old_table_name)
|
489
|
-
model_fks = model.constraint_specs
|
489
|
+
model_fks = model.constraint_specs.to_a
|
490
490
|
|
491
491
|
fks_to_drop = existing_fks - model_fks
|
492
492
|
fks_to_add = model_fks - existing_fks
|