declare_schema 3.1.0.colin.1 → 3.1.0.colin.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/pipeline.yml +1 -1
- data/CHANGELOG.md +3 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +151 -105
- data/config/brakeman.ignore +5 -5
- data/declare_schema.gemspec +1 -1
- data/lib/declare_schema/model/deferred_field_spec.rb +38 -0
- data/lib/declare_schema/model/field_spec.rb +23 -2
- data/lib/declare_schema/model/habtm_model_shim.rb +30 -3
- data/lib/declare_schema/model.rb +142 -13
- data/lib/declare_schema/version.rb +1 -1
- data/lib/declare_schema.rb +13 -7
- data/lib/generators/declare_schema/migration/migrator.rb +11 -30
- data/spec/lib/declare_schema/deferred_field_spec_spec.rb +28 -0
- data/spec/lib/declare_schema/migration_generator_spec.rb +258 -4
- data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +13 -7
- data/spec/lib/generators/declare_schema/migration/migrator_default_pk_type_spec.rb +54 -0
- metadata +7 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2ddb4bb46a974562b49103da5ead57c6ece3d2e7ef76acee4e5b8415df3da761
|
|
4
|
+
data.tar.gz: 652addeb7c6a6a3c4e1e81dbcdf91250d7c5e8884a1f9084e5dcad61b13abab6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ac38c743e5e469e23a0aee102e8dd6222f6f40004d74dfd8f5503b14e93ee9ce1a2c314f4468f4c110d0821a4b6717f97de32492636c9925252db4a49d278931
|
|
7
|
+
data.tar.gz: c6758a1c6a3f389cb59a0a0e4b5aace6a02f83d91e943eb1fb9d9d8639ab6bd499b383ced560bf9e58f1934a90460d25607c2b25b62cf2cd024cb0067e3681c6
|
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,9 @@ Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0
|
|
|
8
8
|
### Added
|
|
9
9
|
- Add HABTM support for arbitrary primary key in the referenced table (rather than just :bigint).
|
|
10
10
|
|
|
11
|
+
### Removed
|
|
12
|
+
- Drop support for Rails 6.x. Minimum supported Rails is now 7.0.
|
|
13
|
+
|
|
11
14
|
## [3.0.0] - 2025-04-08
|
|
12
15
|
### Changed
|
|
13
16
|
- The `timestamps` DSL method to create `created_at` and `updated_at` columns now defaults to `null: false` for `datetime` columns
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,126 +1,153 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
declare_schema (3.1.0.colin.
|
|
5
|
-
rails (>=
|
|
4
|
+
declare_schema (3.1.0.colin.3)
|
|
5
|
+
rails (>= 7.0)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
actioncable (
|
|
11
|
-
actionpack (=
|
|
12
|
-
activesupport (=
|
|
10
|
+
actioncable (7.2.3.1)
|
|
11
|
+
actionpack (= 7.2.3.1)
|
|
12
|
+
activesupport (= 7.2.3.1)
|
|
13
13
|
nio4r (~> 2.0)
|
|
14
14
|
websocket-driver (>= 0.6.1)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
zeitwerk (~> 2.6)
|
|
16
|
+
actionmailbox (7.2.3.1)
|
|
17
|
+
actionpack (= 7.2.3.1)
|
|
18
|
+
activejob (= 7.2.3.1)
|
|
19
|
+
activerecord (= 7.2.3.1)
|
|
20
|
+
activestorage (= 7.2.3.1)
|
|
21
|
+
activesupport (= 7.2.3.1)
|
|
22
|
+
mail (>= 2.8.0)
|
|
23
|
+
actionmailer (7.2.3.1)
|
|
24
|
+
actionpack (= 7.2.3.1)
|
|
25
|
+
actionview (= 7.2.3.1)
|
|
26
|
+
activejob (= 7.2.3.1)
|
|
27
|
+
activesupport (= 7.2.3.1)
|
|
28
|
+
mail (>= 2.8.0)
|
|
29
|
+
rails-dom-testing (~> 2.2)
|
|
30
|
+
actionpack (7.2.3.1)
|
|
31
|
+
actionview (= 7.2.3.1)
|
|
32
|
+
activesupport (= 7.2.3.1)
|
|
33
|
+
cgi
|
|
34
|
+
nokogiri (>= 1.8.5)
|
|
35
|
+
racc
|
|
36
|
+
rack (>= 2.2.4, < 3.3)
|
|
37
|
+
rack-session (>= 1.0.1)
|
|
33
38
|
rack-test (>= 0.6.3)
|
|
34
|
-
rails-dom-testing (~> 2.
|
|
35
|
-
rails-html-sanitizer (~> 1.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
rails-dom-testing (~> 2.2)
|
|
40
|
+
rails-html-sanitizer (~> 1.6)
|
|
41
|
+
useragent (~> 0.16)
|
|
42
|
+
actiontext (7.2.3.1)
|
|
43
|
+
actionpack (= 7.2.3.1)
|
|
44
|
+
activerecord (= 7.2.3.1)
|
|
45
|
+
activestorage (= 7.2.3.1)
|
|
46
|
+
activesupport (= 7.2.3.1)
|
|
47
|
+
globalid (>= 0.6.0)
|
|
41
48
|
nokogiri (>= 1.8.5)
|
|
42
|
-
actionview (
|
|
43
|
-
activesupport (=
|
|
49
|
+
actionview (7.2.3.1)
|
|
50
|
+
activesupport (= 7.2.3.1)
|
|
44
51
|
builder (~> 3.1)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
rails-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
cgi
|
|
53
|
+
erubi (~> 1.11)
|
|
54
|
+
rails-dom-testing (~> 2.2)
|
|
55
|
+
rails-html-sanitizer (~> 1.6)
|
|
56
|
+
activejob (7.2.3.1)
|
|
57
|
+
activesupport (= 7.2.3.1)
|
|
50
58
|
globalid (>= 0.3.6)
|
|
51
|
-
activemodel (
|
|
52
|
-
activesupport (=
|
|
53
|
-
activerecord (
|
|
54
|
-
activemodel (=
|
|
55
|
-
activesupport (=
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
activemodel (7.2.3.1)
|
|
60
|
+
activesupport (= 7.2.3.1)
|
|
61
|
+
activerecord (7.2.3.1)
|
|
62
|
+
activemodel (= 7.2.3.1)
|
|
63
|
+
activesupport (= 7.2.3.1)
|
|
64
|
+
timeout (>= 0.4.0)
|
|
65
|
+
activestorage (7.2.3.1)
|
|
66
|
+
actionpack (= 7.2.3.1)
|
|
67
|
+
activejob (= 7.2.3.1)
|
|
68
|
+
activerecord (= 7.2.3.1)
|
|
69
|
+
activesupport (= 7.2.3.1)
|
|
61
70
|
marcel (~> 1.0)
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
71
|
+
activesupport (7.2.3.1)
|
|
72
|
+
base64
|
|
73
|
+
benchmark (>= 0.3)
|
|
74
|
+
bigdecimal
|
|
75
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
|
76
|
+
connection_pool (>= 2.2.5)
|
|
77
|
+
drb
|
|
65
78
|
i18n (>= 1.6, < 2)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
79
|
+
logger (>= 1.4.2)
|
|
80
|
+
minitest (>= 5.1, < 6)
|
|
81
|
+
securerandom (>= 0.3)
|
|
82
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
|
69
83
|
appraisal (2.5.0)
|
|
70
84
|
bundler
|
|
71
85
|
rake
|
|
72
86
|
thor (>= 0.14.0)
|
|
73
87
|
ast (2.4.2)
|
|
74
88
|
base64 (0.2.0)
|
|
89
|
+
benchmark (0.5.0)
|
|
75
90
|
bigdecimal (3.1.9)
|
|
76
91
|
bootsnap (1.18.4)
|
|
77
92
|
msgpack (~> 1.2)
|
|
78
93
|
builder (3.3.0)
|
|
79
94
|
byebug (11.1.3)
|
|
95
|
+
cgi (0.5.1)
|
|
80
96
|
climate_control (0.2.0)
|
|
81
97
|
coderay (1.1.3)
|
|
82
98
|
concurrent-ruby (1.3.4)
|
|
99
|
+
connection_pool (3.0.2)
|
|
83
100
|
crass (1.0.6)
|
|
84
|
-
date (3.
|
|
101
|
+
date (3.5.1)
|
|
85
102
|
diff-lcs (1.5.1)
|
|
86
|
-
|
|
103
|
+
drb (2.2.3)
|
|
104
|
+
erb (6.0.4)
|
|
105
|
+
erubi (1.13.1)
|
|
87
106
|
ffi (1.17.0)
|
|
88
|
-
globalid (1.
|
|
107
|
+
globalid (1.3.0)
|
|
89
108
|
activesupport (>= 6.1)
|
|
90
|
-
i18n (1.14.
|
|
109
|
+
i18n (1.14.8)
|
|
91
110
|
concurrent-ruby (~> 1.0)
|
|
111
|
+
io-console (0.8.2)
|
|
112
|
+
irb (1.18.0)
|
|
113
|
+
pp (>= 0.6.0)
|
|
114
|
+
prism (>= 1.3.0)
|
|
115
|
+
rdoc (>= 4.0.0)
|
|
116
|
+
reline (>= 0.4.2)
|
|
92
117
|
json (2.7.2)
|
|
93
118
|
language_server-protocol (3.17.0.3)
|
|
94
119
|
listen (3.9.0)
|
|
95
120
|
rb-fsevent (~> 0.10, >= 0.10.3)
|
|
96
121
|
rb-inotify (~> 0.9, >= 0.9.10)
|
|
97
|
-
|
|
122
|
+
logger (1.7.0)
|
|
123
|
+
loofah (2.25.1)
|
|
98
124
|
crass (~> 1.0.2)
|
|
99
125
|
nokogiri (>= 1.12.0)
|
|
100
|
-
mail (2.
|
|
126
|
+
mail (2.9.0)
|
|
127
|
+
logger
|
|
101
128
|
mini_mime (>= 0.1.1)
|
|
102
129
|
net-imap
|
|
103
130
|
net-pop
|
|
104
131
|
net-smtp
|
|
105
|
-
marcel (1.0
|
|
132
|
+
marcel (1.1.0)
|
|
106
133
|
method_source (1.1.0)
|
|
107
134
|
mini_mime (1.1.5)
|
|
108
|
-
mini_portile2 (2.8.
|
|
109
|
-
minitest (5.
|
|
135
|
+
mini_portile2 (2.8.9)
|
|
136
|
+
minitest (5.27.0)
|
|
110
137
|
msgpack (1.7.2)
|
|
111
138
|
mutex_m (0.3.0)
|
|
112
139
|
mysql2 (0.5.6)
|
|
113
|
-
net-imap (0.4
|
|
140
|
+
net-imap (0.6.4)
|
|
114
141
|
date
|
|
115
142
|
net-protocol
|
|
116
143
|
net-pop (0.1.2)
|
|
117
144
|
net-protocol
|
|
118
145
|
net-protocol (0.2.2)
|
|
119
146
|
timeout
|
|
120
|
-
net-smtp (0.5.
|
|
147
|
+
net-smtp (0.5.1)
|
|
121
148
|
net-protocol
|
|
122
|
-
nio4r (2.7.
|
|
123
|
-
nokogiri (1.
|
|
149
|
+
nio4r (2.7.5)
|
|
150
|
+
nokogiri (1.19.3)
|
|
124
151
|
mini_portile2 (~> 2.8.2)
|
|
125
152
|
racc (~> 1.4)
|
|
126
153
|
parallel (1.26.2)
|
|
@@ -128,54 +155,75 @@ GEM
|
|
|
128
155
|
ast (~> 2.4.1)
|
|
129
156
|
racc
|
|
130
157
|
pg (1.5.7)
|
|
158
|
+
pp (0.6.3)
|
|
159
|
+
prettyprint
|
|
160
|
+
prettyprint (0.2.0)
|
|
161
|
+
prism (1.9.0)
|
|
131
162
|
pry (0.14.2)
|
|
132
163
|
coderay (~> 1.1)
|
|
133
164
|
method_source (~> 1.0)
|
|
134
165
|
pry-byebug (3.10.1)
|
|
135
166
|
byebug (~> 11.0)
|
|
136
167
|
pry (>= 0.13, < 0.15)
|
|
168
|
+
psych (5.3.1)
|
|
169
|
+
date
|
|
170
|
+
stringio
|
|
137
171
|
racc (1.8.1)
|
|
138
|
-
rack (
|
|
139
|
-
rack-
|
|
172
|
+
rack (3.2.6)
|
|
173
|
+
rack-session (2.1.2)
|
|
174
|
+
base64 (>= 0.1.0)
|
|
175
|
+
rack (>= 3.0.0)
|
|
176
|
+
rack-test (2.2.0)
|
|
140
177
|
rack (>= 1.3)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
178
|
+
rackup (2.3.1)
|
|
179
|
+
rack (>= 3)
|
|
180
|
+
rails (7.2.3.1)
|
|
181
|
+
actioncable (= 7.2.3.1)
|
|
182
|
+
actionmailbox (= 7.2.3.1)
|
|
183
|
+
actionmailer (= 7.2.3.1)
|
|
184
|
+
actionpack (= 7.2.3.1)
|
|
185
|
+
actiontext (= 7.2.3.1)
|
|
186
|
+
actionview (= 7.2.3.1)
|
|
187
|
+
activejob (= 7.2.3.1)
|
|
188
|
+
activemodel (= 7.2.3.1)
|
|
189
|
+
activerecord (= 7.2.3.1)
|
|
190
|
+
activestorage (= 7.2.3.1)
|
|
191
|
+
activesupport (= 7.2.3.1)
|
|
153
192
|
bundler (>= 1.15.0)
|
|
154
|
-
railties (=
|
|
155
|
-
|
|
156
|
-
rails-dom-testing (2.2.0)
|
|
193
|
+
railties (= 7.2.3.1)
|
|
194
|
+
rails-dom-testing (2.3.0)
|
|
157
195
|
activesupport (>= 5.0.0)
|
|
158
196
|
minitest
|
|
159
197
|
nokogiri (>= 1.6)
|
|
160
|
-
rails-html-sanitizer (1.
|
|
161
|
-
loofah (~> 2.
|
|
162
|
-
nokogiri (
|
|
163
|
-
railties (
|
|
164
|
-
actionpack (=
|
|
165
|
-
activesupport (=
|
|
166
|
-
|
|
198
|
+
rails-html-sanitizer (1.7.0)
|
|
199
|
+
loofah (~> 2.25)
|
|
200
|
+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
|
|
201
|
+
railties (7.2.3.1)
|
|
202
|
+
actionpack (= 7.2.3.1)
|
|
203
|
+
activesupport (= 7.2.3.1)
|
|
204
|
+
cgi
|
|
205
|
+
irb (~> 1.13)
|
|
206
|
+
rackup (>= 1.0.0)
|
|
167
207
|
rake (>= 12.2)
|
|
168
|
-
thor (~> 1.0)
|
|
208
|
+
thor (~> 1.0, >= 1.2.2)
|
|
209
|
+
tsort (>= 0.2)
|
|
210
|
+
zeitwerk (~> 2.6)
|
|
169
211
|
rainbow (3.1.1)
|
|
170
|
-
rake (13.2
|
|
212
|
+
rake (13.4.2)
|
|
171
213
|
rb-fsevent (0.11.2)
|
|
172
214
|
rb-inotify (0.11.1)
|
|
173
215
|
ffi (~> 1.0)
|
|
216
|
+
rdoc (7.2.0)
|
|
217
|
+
erb
|
|
218
|
+
psych (>= 4.0.0)
|
|
219
|
+
tsort
|
|
174
220
|
regexp_parser (2.9.2)
|
|
221
|
+
reline (0.6.3)
|
|
222
|
+
io-console (~> 0.5)
|
|
175
223
|
responders (3.1.1)
|
|
176
224
|
actionpack (>= 5.2)
|
|
177
225
|
railties (>= 5.2)
|
|
178
|
-
rexml (3.
|
|
226
|
+
rexml (3.4.4)
|
|
179
227
|
rspec (3.13.0)
|
|
180
228
|
rspec-core (~> 3.13.0)
|
|
181
229
|
rspec-expectations (~> 3.13.0)
|
|
@@ -206,25 +254,23 @@ GEM
|
|
|
206
254
|
rubocop-ast (1.32.0)
|
|
207
255
|
parser (>= 3.3.1.0)
|
|
208
256
|
ruby-progressbar (1.13.0)
|
|
209
|
-
|
|
210
|
-
concurrent-ruby (~> 1.0)
|
|
211
|
-
rack (>= 2.2.4, < 4)
|
|
212
|
-
sprockets-rails (3.5.2)
|
|
213
|
-
actionpack (>= 6.1)
|
|
214
|
-
activesupport (>= 6.1)
|
|
215
|
-
sprockets (>= 3.0.0)
|
|
257
|
+
securerandom (0.4.1)
|
|
216
258
|
sqlite3 (1.7.3)
|
|
217
259
|
mini_portile2 (~> 2.8.0)
|
|
218
|
-
|
|
219
|
-
|
|
260
|
+
stringio (3.2.0)
|
|
261
|
+
thor (1.5.0)
|
|
262
|
+
timeout (0.6.1)
|
|
263
|
+
tsort (0.2.0)
|
|
220
264
|
tzinfo (2.0.6)
|
|
221
265
|
concurrent-ruby (~> 1.0)
|
|
222
266
|
unicode-display_width (2.5.0)
|
|
223
|
-
|
|
267
|
+
useragent (0.16.11)
|
|
268
|
+
websocket-driver (0.8.0)
|
|
269
|
+
base64
|
|
224
270
|
websocket-extensions (>= 0.1.0)
|
|
225
271
|
websocket-extensions (0.1.5)
|
|
226
272
|
yard (0.9.36)
|
|
227
|
-
zeitwerk (2.
|
|
273
|
+
zeitwerk (2.7.5)
|
|
228
274
|
|
|
229
275
|
PLATFORMS
|
|
230
276
|
ruby
|
|
@@ -245,7 +291,7 @@ DEPENDENCIES
|
|
|
245
291
|
pg (~> 1.1)
|
|
246
292
|
pry
|
|
247
293
|
pry-byebug
|
|
248
|
-
rails (~>
|
|
294
|
+
rails (~> 7.0)
|
|
249
295
|
responders
|
|
250
296
|
rspec
|
|
251
297
|
rspec-its
|
data/config/brakeman.ignore
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
{
|
|
4
4
|
"warning_type": "Command Injection",
|
|
5
5
|
"warning_code": 14,
|
|
6
|
-
"fingerprint": "
|
|
6
|
+
"fingerprint": "069879c09668f26db81818bd906f169f4f3be6dc661c9b223fcd6f53b2155369",
|
|
7
7
|
"check_name": "Execute",
|
|
8
8
|
"message": "Possible command injection",
|
|
9
9
|
"file": "lib/declare_schema/command.rb",
|
|
10
|
-
"line":
|
|
10
|
+
"line": 55,
|
|
11
11
|
"link": "https://brakemanscanner.org/docs/warning_types/command_injection/",
|
|
12
12
|
"code": "system(\"bundle exec rails #{Regexp.last_match(1)} declare_schema:#{(args * \" \")}\")",
|
|
13
13
|
"render_path": null,
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
{
|
|
27
27
|
"warning_type": "Command Injection",
|
|
28
28
|
"warning_code": 14,
|
|
29
|
-
"fingerprint": "
|
|
29
|
+
"fingerprint": "9914293cc386897d62f7f645fd2e0bfa51c582f3058ac34357f61b04866a33fe",
|
|
30
30
|
"check_name": "Execute",
|
|
31
31
|
"message": "Possible command injection",
|
|
32
32
|
"file": "lib/declare_schema/command.rb",
|
|
33
33
|
"line": 45,
|
|
34
34
|
"link": "https://brakemanscanner.org/docs/warning_types/command_injection/",
|
|
35
|
-
"code": "system(\"rails new #{\"new\"} #{(args * \" \")} -m #{File.join(Dir.tmpdir, \"declare_schema_app_template\")}
|
|
35
|
+
"code": "system(\"rails new #{\"new\"} #{(args * \" \")} -m #{File.join(Dir.tmpdir, \"declare_schema_app_template\")}\")",
|
|
36
36
|
"render_path": null,
|
|
37
37
|
"location": {
|
|
38
38
|
"type": "method",
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"note": ""
|
|
67
67
|
}
|
|
68
68
|
],
|
|
69
|
-
"updated": "
|
|
69
|
+
"updated": "2026-05-02 06:56:54 -0700",
|
|
70
70
|
"brakeman_version": "5.4.1"
|
|
71
71
|
}
|
data/declare_schema.gemspec
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DeclareSchema
|
|
4
|
+
module Model
|
|
5
|
+
# A placeholder for a {FieldSpec} whose final shape can't be computed at field-
|
|
6
|
+
# declaration time. The only producer today is `belongs_to` between two
|
|
7
|
+
# declare_schema models: we cannot touch `reflection.klass` while declaring the
|
|
8
|
+
# FK without risking a model-load cycle, so we stash an eager default-typed
|
|
9
|
+
# FieldSpec along with a block that knows how to mirror the parent's PK once
|
|
10
|
+
# all models have been eager-loaded.
|
|
11
|
+
#
|
|
12
|
+
# The migrator calls {#resolve} on every value in `field_specs` at the start
|
|
13
|
+
# of migration generation (see `Migrator#generate`); for plain {FieldSpec}s
|
|
14
|
+
# `#resolve` is a no-op returning self, while for instances of this class it
|
|
15
|
+
# invokes the block (memoized) with the default spec and returns the produced
|
|
16
|
+
# {FieldSpec}.
|
|
17
|
+
class DeferredFieldSpec
|
|
18
|
+
# @param default_spec [FieldSpec] the eager placeholder spec
|
|
19
|
+
# @yieldparam default_spec [FieldSpec]
|
|
20
|
+
# @yieldreturn [FieldSpec] the resolved spec
|
|
21
|
+
def initialize(default_spec, &resolver)
|
|
22
|
+
resolver or raise ArgumentError, "DeferredFieldSpec requires a resolver block"
|
|
23
|
+
@default_spec = default_spec
|
|
24
|
+
@resolver = resolver
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Invoke the resolver block with the default spec and return its result.
|
|
28
|
+
# The migrator's `transform_values!(&:resolve)` swaps this DeferredFieldSpec
|
|
29
|
+
# out of `field_specs` for the produced FieldSpec, so resolve is called
|
|
30
|
+
# exactly once per instance in production -- no memoization needed.
|
|
31
|
+
#
|
|
32
|
+
# @return [FieldSpec]
|
|
33
|
+
def resolve
|
|
34
|
+
@resolver.call(@default_spec)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -120,8 +120,29 @@ module DeclareSchema
|
|
|
120
120
|
@sql_options = @options.slice(*SQL_OPTIONS)
|
|
121
121
|
end
|
|
122
122
|
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
# Duck-type counterpart to {DeferredFieldSpec#resolve}. A concrete FieldSpec is
|
|
124
|
+
# already its own resolved form, so this just returns self. Lets the migrator's
|
|
125
|
+
# `transform_values!(&:resolve)` loop dispatch uniformly across both classes.
|
|
126
|
+
#
|
|
127
|
+
# @return [FieldSpec] self
|
|
128
|
+
def resolve
|
|
129
|
+
self
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Build a FieldSpec for a foreign key that mirrors this PK's type/limit/charset/etc.
|
|
133
|
+
# Child column_options (e.g. `default:` from `belongs_to`) win over the parent's
|
|
134
|
+
# @options when they specify the same key.
|
|
135
|
+
#
|
|
136
|
+
# @param model [Class] the child ActiveRecord class
|
|
137
|
+
# @param name [Symbol] the FK column name (e.g. :user_id)
|
|
138
|
+
# @param position [Integer] FieldSpec position in the field_specs hash
|
|
139
|
+
# @param null [Boolean, nil] :null setting from `belongs_to`; nil leaves @options[:null] alone
|
|
140
|
+
# @param column_options [Hash] additional FK-side overrides (e.g. :default)
|
|
141
|
+
# @return [FieldSpec]
|
|
142
|
+
def foreign_key_field_spec(model, name, position:, null:, **column_options)
|
|
143
|
+
options = @options.merge(column_options)
|
|
144
|
+
options[:null] = null unless null.nil?
|
|
145
|
+
self.class.new(model, name, @type, position:, **options)
|
|
125
146
|
end
|
|
126
147
|
|
|
127
148
|
# returns the attributes for schema migrations as a Hash
|
|
@@ -39,15 +39,42 @@ module DeclareSchema
|
|
|
39
39
|
join_table
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
# The migrator (in change_column_back / add_column_back / etc.) wraps column
|
|
43
|
+
# introspection in `with_previous_model_table_name`, which expects a model that
|
|
44
|
+
# exposes `table_name=`. Real AR classes do; this shim doesn't keep AR-style
|
|
45
|
+
# cached metadata keyed on table_name, so we just rewrite @join_table -- the
|
|
46
|
+
# only state our `table_name` reads.
|
|
47
|
+
def table_name=(new_table_name)
|
|
48
|
+
@join_table = new_table_name
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Mirror the AR class API for `model.columns_hash` so the migrator's
|
|
52
|
+
# change_column_back / add_column_back paths work uniformly across real AR
|
|
53
|
+
# models and HABTM shims. Backed by `connection.columns(table_name)` rather
|
|
54
|
+
# than AR's class-level cache because the shim has no class-level cache.
|
|
55
|
+
def columns_hash
|
|
56
|
+
connection.columns(table_name).index_by(&:name)
|
|
57
|
+
end
|
|
58
|
+
|
|
42
59
|
def field_specs
|
|
43
60
|
foreign_keys.each_with_index.each_with_object({}) do |(foreign_key, i), result|
|
|
44
|
-
|
|
61
|
+
parent_model = parent_models[i]
|
|
62
|
+
result[foreign_key] =
|
|
63
|
+
if parent_model.respond_to?(:_foreign_key_field_spec)
|
|
64
|
+
# declare_schema model: mirror the parent's primary key (type, limit, etc.)
|
|
65
|
+
parent_model._foreign_key_field_spec(self, foreign_key, position: i, null: false)
|
|
66
|
+
else
|
|
67
|
+
# Non-declare_schema parent: fall back to the configured default PK type.
|
|
68
|
+
::DeclareSchema::Model::FieldSpec.new(self, foreign_key, ::DeclareSchema.default_generated_primary_key_type, position: i, null: false)
|
|
69
|
+
end
|
|
45
70
|
end
|
|
46
71
|
end
|
|
47
72
|
|
|
73
|
+
# The HABTM join table's primary key is the composite of its two foreign keys.
|
|
74
|
+
# (Rails 7.1+ supports composite PKs natively; on 7.0 nothing in AR introspects this
|
|
75
|
+
# shim, so returning an Array is safe for our callers.)
|
|
48
76
|
def primary_key
|
|
49
|
-
|
|
50
|
-
false # no single-column primary key in database
|
|
77
|
+
foreign_keys
|
|
51
78
|
end
|
|
52
79
|
|
|
53
80
|
def _declared_primary_key
|