declare_schema 1.4.0.colin.2 → 1.4.0.colin.4
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/habtm_model_shim.rb +5 -5
- data/lib/declare_schema/model.rb +9 -14
- data/lib/declare_schema/version.rb +1 -1
- data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +7 -6
- data/spec/lib/declare_schema/model/index_definition_spec.rb +5 -4
- 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: b48f50b03282da270199e8bb46e4e7a2d69068a8753203364835db206692a94a
|
4
|
+
data.tar.gz: b72de21ed3123f0b4e9527d5e5ee805742b2c21628adeabd4213925858062914
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24edb7af1a43c955d5bc3664d755ea30d225bf1bce15e98765693ee7de2da87b4d66c42b0c6f6818b5a81b9485536da4ccd824319b91744eee6837c7317d80ca
|
7
|
+
data.tar.gz: 996169b7db3b3a8426968e8924181a0c588b7035d00a563fb78708eb8e47e2384f20b5838a2bc491ccbef3f45c6e3f462c3e0095261477841cfe666b320acfd2
|
data/Gemfile.lock
CHANGED
@@ -52,23 +52,23 @@ module DeclareSchema
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def index_definitions_with_primary_key
|
55
|
-
[
|
55
|
+
@index_definitions_with_primary_key ||= Set.new([
|
56
56
|
IndexDefinition.new(self, foreign_keys, unique: true, name: Model::IndexDefinition::PRIMARY_KEY_NAME), # creates a primary composite key on both foreign keys
|
57
57
|
IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique
|
58
|
-
]
|
58
|
+
])
|
59
59
|
end
|
60
60
|
|
61
61
|
alias_method :index_definitions, :index_definitions_with_primary_key
|
62
62
|
|
63
63
|
def ignore_indexes
|
64
|
-
|
64
|
+
@ignore_indexes ||= Set.new
|
65
65
|
end
|
66
66
|
|
67
67
|
def constraint_specs
|
68
|
-
[
|
68
|
+
@constraint_specs ||= Set.new([
|
69
69
|
ForeignKeyDefinition.new(self, foreign_keys.first, parent_table: foreign_key_classes.first.table_name, constraint_name: "#{join_table}_FK1", dependent: :delete),
|
70
70
|
ForeignKeyDefinition.new(self, foreign_keys.last, parent_table: foreign_key_classes.last.table_name, constraint_name: "#{join_table}_FK2", dependent: :delete)
|
71
|
-
]
|
71
|
+
])
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
data/lib/declare_schema/model.rb
CHANGED
@@ -16,7 +16,6 @@ module DeclareSchema
|
|
16
16
|
# attr_types holds the type class for any attribute reader (i.e. getter
|
17
17
|
# method) that returns rich-types
|
18
18
|
inheriting_cattr_reader attr_types: HashWithIndifferentAccess.new
|
19
|
-
inheriting_cattr_reader attr_order: []
|
20
19
|
|
21
20
|
# field_specs holds FieldSpec objects for every declared
|
22
21
|
# field. Note that attribute readers are created (by ActiveRecord)
|
@@ -26,9 +25,9 @@ module DeclareSchema
|
|
26
25
|
inheriting_cattr_reader field_specs: HashWithIndifferentAccess.new
|
27
26
|
|
28
27
|
# index_definitions holds IndexDefinition objects for all the declared indexes.
|
29
|
-
inheriting_cattr_reader index_definitions:
|
30
|
-
inheriting_cattr_reader ignore_indexes:
|
31
|
-
inheriting_cattr_reader constraint_specs:
|
28
|
+
inheriting_cattr_reader index_definitions: Set.new
|
29
|
+
inheriting_cattr_reader ignore_indexes: Set.new
|
30
|
+
inheriting_cattr_reader constraint_specs: Set.new
|
32
31
|
|
33
32
|
# table_options holds optional configuration for the create_table statement
|
34
33
|
# supported options include :charset and :collation
|
@@ -50,11 +49,7 @@ module DeclareSchema
|
|
50
49
|
|
51
50
|
module ClassMethods
|
52
51
|
def index(fields, **options)
|
53
|
-
|
54
|
-
index = ::DeclareSchema::Model::IndexDefinition.new(self, fields, **options)
|
55
|
-
unless index_definitions.any? { |index_spec| index_spec.equivalent?(index) }
|
56
|
-
index_definitions << index
|
57
|
-
end
|
52
|
+
index_definitions << ::DeclareSchema::Model::IndexDefinition.new(self, fields, **options)
|
58
53
|
end
|
59
54
|
|
60
55
|
def primary_key_index(*fields)
|
@@ -85,7 +80,6 @@ module DeclareSchema
|
|
85
80
|
_add_validations_for_field(name, type, args, options)
|
86
81
|
_add_index_for_field(name, args, options)
|
87
82
|
field_specs[name] = ::DeclareSchema::Model::FieldSpec.new(self, name, type, position: field_specs.size, **options)
|
88
|
-
attr_order << name unless attr_order.include?(name)
|
89
83
|
end
|
90
84
|
|
91
85
|
def index_definitions_with_primary_key
|
@@ -122,19 +116,20 @@ module DeclareSchema
|
|
122
116
|
index_options = {}
|
123
117
|
case index_value
|
124
118
|
when String
|
125
|
-
Kernel.warn("belongs_to index: 'name' is deprecated; use index: { name: 'name' } instead (in #{
|
119
|
+
Kernel.warn("belongs_to index: 'name' is deprecated; use index: { name: 'name' } instead (in #{name})")
|
126
120
|
index_options[:name] = index_value
|
127
|
-
# when false -- impossible since we checked that above
|
128
121
|
when true
|
122
|
+
when false
|
123
|
+
raise ArgumentError, "belongs_to index: false contradicts others options #{options.inspect} (in #{name})"
|
129
124
|
when nil
|
130
125
|
when Hash
|
131
126
|
index_options = index_value
|
132
127
|
else
|
133
|
-
raise ArgumentError, "belongs_to index: must be true or false or a Hash; got #{index_value.inspect}"
|
128
|
+
raise ArgumentError, "belongs_to index: must be true or false or a Hash; got #{index_value.inspect} (in #{name})"
|
134
129
|
end
|
135
130
|
|
136
131
|
if options.has_key?(:unique)
|
137
|
-
Kernel.warn("belongs_to unique: true|false is deprecated; use index: { unique: true|false } instead (in #{
|
132
|
+
Kernel.warn("belongs_to unique: true|false is deprecated; use index: { unique: true|false } instead (in #{name})")
|
138
133
|
index_options[:unique] = options.delete(:unique)
|
139
134
|
end
|
140
135
|
|
@@ -141,8 +141,8 @@ RSpec.describe DeclareSchema::Model::HabtmModelShim do
|
|
141
141
|
end
|
142
142
|
|
143
143
|
describe 'ignore_indexes' do
|
144
|
-
it 'returns empty
|
145
|
-
expect(subject.ignore_indexes).to eq(
|
144
|
+
it 'returns empty Set' do
|
145
|
+
expect(subject.ignore_indexes).to eq(Set.new)
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -156,10 +156,11 @@ RSpec.describe DeclareSchema::Model::HabtmModelShim do
|
|
156
156
|
expect(result.first.parent_table_name).to be(Parent1.table_name)
|
157
157
|
expect(result.first.on_delete_cascade).to be_truthy
|
158
158
|
|
159
|
-
|
160
|
-
expect(
|
161
|
-
expect(
|
162
|
-
expect(
|
159
|
+
sample = result.to_a.last
|
160
|
+
expect(sample).to be_a(::DeclareSchema::Model::ForeignKeyDefinition)
|
161
|
+
expect(sample.foreign_key).to eq(foreign_keys.last)
|
162
|
+
expect(sample.parent_table_name).to be(Parent2.table_name)
|
163
|
+
expect(sample.on_delete_cascade).to be_truthy
|
163
164
|
end
|
164
165
|
end
|
165
166
|
end
|
@@ -112,15 +112,16 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
|
|
112
112
|
it do
|
113
113
|
expect(model_class.index_definitions.size).to eq(1)
|
114
114
|
|
115
|
-
|
116
|
-
expect(
|
117
|
-
expect(
|
115
|
+
sample = model_class.index_definitions.to_a.first
|
116
|
+
expect(sample.name).to eq('index_index_definition_test_models_on_name')
|
117
|
+
expect(sample.fields).to eq(['name'])
|
118
|
+
expect(sample.unique).to eq(false)
|
118
119
|
end
|
119
120
|
end
|
120
121
|
|
121
122
|
describe 'has index_definitions_with_primary_key' do
|
122
123
|
it do
|
123
|
-
expect(model_class.index_definitions_with_primary_key).to be_kind_of(
|
124
|
+
expect(model_class.index_definitions_with_primary_key).to be_kind_of(Set)
|
124
125
|
result = model_class.index_definitions_with_primary_key.sort_by(&:name)
|
125
126
|
expect(result.size).to eq(2)
|
126
127
|
|