declare_schema 1.5.0.pre.1 → 2.1.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.devcontainer/Dockerfile +19 -0
- data/.devcontainer/boot.sh +1 -0
- data/.devcontainer/devcontainer.json +55 -0
- data/.devcontainer/docker-compose.yml +40 -0
- data/.github/workflows/declare_schema_build.yml +35 -11
- data/Appraisals +2 -10
- data/CHANGELOG.md +8 -1
- data/CODE-OF-CONDUCT.md +16 -0
- data/CONTRIBUTING.md +46 -0
- data/Gemfile +6 -1
- data/Gemfile.lock +88 -78
- data/README.md +38 -14
- data/Rakefile +5 -15
- data/catalog-info.yaml +35 -0
- data/config/brakeman.ignore +2 -2
- data/declare_schema.gemspec +1 -1
- data/gemfiles/{rails_6_1_sqlite3.gemfile → rails_6_1.gemfile} +3 -0
- data/gemfiles/{rails_7_0_sqlite3.gemfile → rails_7_0.gemfile} +3 -0
- data/gemfiles/{rails_7_1_sqlite3.gemfile → rails_7_1.gemfile} +3 -0
- data/gemfiles/{rails_7_0_mysql2.gemfile → rails_7_2.gemfile} +4 -1
- data/lib/declare_schema/command.rb +2 -8
- data/lib/declare_schema/model/column.rb +1 -1
- data/lib/declare_schema/model/foreign_key_definition.rb +9 -12
- data/lib/declare_schema/model/index_definition.rb +16 -8
- data/lib/declare_schema/model.rb +10 -14
- data/lib/declare_schema/schema_change/base.rb +4 -0
- data/lib/declare_schema/schema_change/primary_key_change.rb +19 -6
- data/lib/declare_schema/version.rb +1 -1
- data/lib/declare_schema.rb +20 -8
- data/lib/generators/declare_schema/migration/migration_generator.rb +17 -8
- data/lib/generators/declare_schema/migration/migrator.rb +16 -9
- data/spec/fixtures/migrations/mysql2/will_generate_unique_constraint_names_rails_6.txt +15 -0
- data/spec/fixtures/migrations/mysql2/will_generate_unique_constraint_names_rails_7.txt +15 -0
- data/spec/fixtures/migrations/postgresql/will_generate_unique_constraint_names_rails_6.txt +15 -0
- data/spec/fixtures/migrations/postgresql/will_generate_unique_constraint_names_rails_7.txt +15 -0
- data/spec/fixtures/migrations/sqlite3/will_generate_unique_constraint_names_rails_6.txt +15 -0
- data/spec/fixtures/migrations/sqlite3/will_generate_unique_constraint_names_rails_7.txt +15 -0
- data/spec/lib/declare_schema/api_spec.rb +1 -7
- data/spec/lib/declare_schema/field_declaration_dsl_spec.rb +3 -3
- data/spec/lib/declare_schema/field_spec_spec.rb +68 -45
- data/spec/lib/declare_schema/generator_spec.rb +29 -71
- data/spec/lib/declare_schema/interactive_primary_key_spec.rb +3 -10
- data/spec/lib/declare_schema/migration_generator_spec.rb +249 -255
- data/spec/lib/declare_schema/model/column_spec.rb +89 -41
- data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +14 -8
- data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +4 -9
- data/spec/lib/declare_schema/model/index_definition_spec.rb +18 -10
- data/spec/lib/declare_schema/model/table_options_definition_spec.rb +11 -11
- data/spec/lib/declare_schema/schema_change/base_spec.rb +5 -7
- data/spec/lib/declare_schema/schema_change/column_add_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/column_change_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/column_remove_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/column_rename_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/foreign_key_add_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/foreign_key_remove_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/index_add_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/index_remove_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/primary_key_change_spec.rb +39 -15
- data/spec/lib/declare_schema/schema_change/table_add_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/table_change_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/table_remove_spec.rb +1 -3
- data/spec/lib/declare_schema/schema_change/table_rename_spec.rb +1 -3
- data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +0 -4
- data/spec/spec_helper.rb +3 -0
- data/spec/support/adapter_specific_test_helpers.rb +25 -0
- data/spec/{lib/declare_schema → support}/prepare_testapp.rb +3 -1
- data/spec/support/test_app_spec_helpers.rb +7 -0
- metadata +24 -11
- data/gemfiles/rails_6_1_mysql2.gemfile +0 -23
- data/gemfiles/rails_7_1_mysql2.gemfile +0 -23
@@ -1,58 +1,52 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
begin
|
4
|
-
require 'mysql2'
|
5
|
-
rescue LoadError
|
6
|
-
end
|
7
|
-
|
8
|
-
begin
|
9
|
-
require 'sqlite3'
|
10
|
-
rescue LoadError
|
11
|
-
end
|
12
|
-
|
13
3
|
RSpec.describe 'DeclareSchema Migration Generator' do
|
14
|
-
|
15
|
-
|
16
|
-
end
|
4
|
+
include_context 'prepare test app'
|
5
|
+
|
17
6
|
let(:text_limit) do
|
18
|
-
if
|
7
|
+
if current_adapter == 'mysql2'
|
19
8
|
", limit: 4294967295"
|
20
9
|
end
|
21
10
|
end
|
22
11
|
let(:charset_and_collation) do
|
23
|
-
if
|
12
|
+
if current_adapter == 'mysql2'
|
24
13
|
', charset: "utf8mb4", collation: "utf8mb4_bin"'
|
25
14
|
end
|
26
15
|
end
|
16
|
+
let(:primary_key_type) do
|
17
|
+
if current_adapter == 'postgresql'
|
18
|
+
':bigserial'
|
19
|
+
else
|
20
|
+
':bigint'
|
21
|
+
end
|
22
|
+
end
|
27
23
|
let(:create_table_charset_and_collation) do
|
28
|
-
if
|
24
|
+
if current_adapter == 'mysql2'
|
29
25
|
", options: \"CHARACTER SET utf8mb4 COLLATE utf8mb4_bin\""
|
30
26
|
end
|
31
27
|
end
|
32
28
|
let(:datetime_precision) do
|
33
29
|
if ActiveSupport::VERSION::MAJOR >= 7
|
34
30
|
', precision: 6'
|
35
|
-
elsif
|
31
|
+
elsif current_adapter == 'mysql2'
|
36
32
|
', precision: 0'
|
37
33
|
end
|
38
34
|
end
|
39
35
|
let(:table_options) do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
''
|
46
|
-
end
|
36
|
+
case current_adapter
|
37
|
+
when 'mysql2'
|
38
|
+
', options: "DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", charset: "utf8mb4", collation: "utf8mb4_bin"'
|
39
|
+
when 'postgresql'
|
40
|
+
""
|
47
41
|
else
|
48
42
|
", id: :integer"
|
49
43
|
end
|
50
44
|
end
|
51
45
|
let(:lock_version_limit) do
|
52
|
-
if
|
53
|
-
", limit: 4"
|
54
|
-
else
|
46
|
+
if current_adapter == 'sqlite3'
|
55
47
|
''
|
48
|
+
else
|
49
|
+
', limit: 4'
|
56
50
|
end
|
57
51
|
end
|
58
52
|
|
@@ -63,8 +57,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
63
57
|
|
64
58
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("").and migrate_down("")
|
65
59
|
|
66
|
-
class Advert < ActiveRecord::Base
|
67
|
-
end
|
60
|
+
class Advert < ActiveRecord::Base; end # rubocop:disable Lint/ConstantDefinitionInBlock
|
68
61
|
|
69
62
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("").and migrate_down("")
|
70
63
|
|
@@ -73,7 +66,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
73
66
|
Advert.connection.schema_cache.clear!
|
74
67
|
Advert.reset_column_information
|
75
68
|
|
76
|
-
class Advert < ActiveRecord::Base
|
69
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
77
70
|
declare_schema do
|
78
71
|
string :name, limit: 250, null: true
|
79
72
|
end
|
@@ -82,18 +75,18 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
82
75
|
up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
|
83
76
|
expect(migrations).to(
|
84
77
|
migrate_up(<<~EOS.strip)
|
85
|
-
create_table :adverts, id:
|
78
|
+
create_table :adverts, id: #{primary_key_type}#{create_table_charset_and_collation} do |t|
|
86
79
|
t.string :name, limit: 250, null: true#{charset_and_collation}
|
87
80
|
end
|
88
81
|
EOS
|
89
|
-
.and
|
82
|
+
.and(migrate_down("drop_table :adverts"))
|
90
83
|
)
|
91
84
|
end
|
92
85
|
|
93
86
|
ActiveRecord::Migration.class_eval(up)
|
94
87
|
expect(Advert.columns.map(&:name)).to eq(["id", "name"])
|
95
88
|
|
96
|
-
class Advert < ActiveRecord::Base
|
89
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
97
90
|
declare_schema do
|
98
91
|
string :name, limit: 250, null: true
|
99
92
|
text :body, null: true
|
@@ -109,14 +102,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
109
102
|
add_column :adverts, :body, :text#{text_limit}, null: true#{charset_and_collation}
|
110
103
|
add_column :adverts, :published_at, :datetime, null: true
|
111
104
|
EOS
|
112
|
-
.and
|
113
|
-
|
114
|
-
|
105
|
+
.and(migrate_down(<<~EOS.strip))
|
106
|
+
remove_column :adverts, :published_at
|
107
|
+
remove_column :adverts, :body
|
115
108
|
EOS
|
116
109
|
)
|
117
110
|
|
118
111
|
Advert.field_specs.clear # not normally needed
|
119
|
-
class Advert < ActiveRecord::Base
|
112
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
120
113
|
declare_schema do
|
121
114
|
string :name, limit: 250, null: true
|
122
115
|
text :body, null: true
|
@@ -124,13 +117,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
124
117
|
end
|
125
118
|
|
126
119
|
expect(migrate).to(
|
127
|
-
|
128
|
-
|
129
|
-
|
120
|
+
migrate_up("remove_column :adverts, :published_at").and(
|
121
|
+
migrate_down("add_column :adverts, :published_at, :datetime#{datetime_precision}, null: true")
|
122
|
+
)
|
130
123
|
)
|
131
124
|
|
132
125
|
nuke_model_class(Advert)
|
133
|
-
class Advert < ActiveRecord::Base
|
126
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
134
127
|
declare_schema do
|
135
128
|
string :title, limit: 250, null: true
|
136
129
|
text :body, null: true
|
@@ -142,21 +135,21 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
142
135
|
add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
|
143
136
|
remove_column :adverts, :name
|
144
137
|
EOS
|
145
|
-
.and
|
146
|
-
|
147
|
-
|
138
|
+
.and(migrate_down(<<~EOS.strip))
|
139
|
+
add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
|
140
|
+
remove_column :adverts, :title
|
148
141
|
EOS
|
149
142
|
)
|
150
143
|
|
151
144
|
expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { name: :title })).to(
|
152
|
-
|
153
|
-
|
154
|
-
|
145
|
+
migrate_up("rename_column :adverts, :name, :title").and(
|
146
|
+
migrate_down("rename_column :adverts, :title, :name")
|
147
|
+
)
|
155
148
|
)
|
156
149
|
|
157
150
|
migrate
|
158
151
|
|
159
|
-
class Advert < ActiveRecord::Base
|
152
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
160
153
|
declare_schema do
|
161
154
|
text :title, null: true
|
162
155
|
text :body, null: true
|
@@ -164,12 +157,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
164
157
|
end
|
165
158
|
|
166
159
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
167
|
-
|
168
|
-
|
169
|
-
|
160
|
+
migrate_up("change_column :adverts, :title, :text#{text_limit}, null: true#{charset_and_collation}").and(
|
161
|
+
migrate_down("change_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}")
|
162
|
+
)
|
170
163
|
)
|
171
164
|
|
172
|
-
class Advert < ActiveRecord::Base
|
165
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
173
166
|
declare_schema do
|
174
167
|
string :title, default: "Untitled", limit: 250, null: true
|
175
168
|
text :body, null: true
|
@@ -177,17 +170,17 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
177
170
|
end
|
178
171
|
|
179
172
|
expect(migrate).to(
|
180
|
-
|
173
|
+
migrate_up(<<~EOS.strip)
|
181
174
|
change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
175
|
+
EOS
|
176
|
+
.and(migrate_down(<<~EOS.strip))
|
177
|
+
change_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
|
178
|
+
EOS
|
186
179
|
)
|
187
180
|
|
188
181
|
### Limits
|
189
182
|
|
190
|
-
class Advert < ActiveRecord::Base
|
183
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
191
184
|
declare_schema do
|
192
185
|
integer :price, null: true, limit: 2
|
193
186
|
end
|
@@ -200,23 +193,23 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
200
193
|
# Now run the migration, then change the limit:
|
201
194
|
|
202
195
|
ActiveRecord::Migration.class_eval(up)
|
203
|
-
class Advert < ActiveRecord::Base
|
196
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
204
197
|
declare_schema do
|
205
198
|
integer :price, null: true, limit: 3
|
206
199
|
end
|
207
200
|
end
|
208
201
|
|
209
202
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
210
|
-
|
203
|
+
migrate_up(<<~EOS.strip)
|
211
204
|
change_column :adverts, :price, :integer, limit: 3, null: true
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
205
|
+
EOS
|
206
|
+
.and(migrate_down(<<~EOS.strip))
|
207
|
+
change_column :adverts, :price, :integer, limit: 2, null: true
|
208
|
+
EOS
|
216
209
|
)
|
217
210
|
|
218
|
-
ActiveRecord::Migration.class_eval("remove_column :adverts, :price")
|
219
|
-
class Advert < ActiveRecord::Base
|
211
|
+
ActiveRecord::Migration.class_eval("remove_column :adverts, :price", __FILE__, __LINE__)
|
212
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
220
213
|
declare_schema do
|
221
214
|
decimal :price, precision: 4, scale: 1, null: true
|
222
215
|
end
|
@@ -227,11 +220,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
227
220
|
# If a `limit` is given, it will only be used in MySQL, to choose the smallest TEXT field that will accommodate
|
228
221
|
# that limit (0xff for TINYTEXT, 0xffff for TEXT, 0xffffff for MEDIUMTEXT, 0xffffffff for LONGTEXT).
|
229
222
|
|
230
|
-
if
|
223
|
+
if current_adapter == 'sqlite3'
|
231
224
|
expect(::DeclareSchema::Model::FieldSpec.mysql_text_limits?).to be_falsey
|
232
225
|
end
|
233
226
|
|
234
|
-
class Advert < ActiveRecord::Base
|
227
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
235
228
|
declare_schema do
|
236
229
|
text :notes
|
237
230
|
text :description, limit: 30000
|
@@ -239,11 +232,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
239
232
|
end
|
240
233
|
|
241
234
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
242
|
-
|
235
|
+
migrate_up(<<~EOS.strip)
|
243
236
|
add_column :adverts, :price, :decimal, precision: 4, scale: 1, null: true
|
244
237
|
add_column :adverts, :notes, :text#{text_limit}, null: false#{charset_and_collation}
|
245
|
-
add_column :adverts, :description, :text#{', limit: 65535' if
|
246
|
-
|
238
|
+
add_column :adverts, :description, :text#{', limit: 65535' if current_adapter == 'mysql2'}, null: false#{charset_and_collation}
|
239
|
+
EOS
|
247
240
|
)
|
248
241
|
|
249
242
|
Advert.field_specs.delete :price
|
@@ -252,7 +245,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
252
245
|
|
253
246
|
# In MySQL, limits are applied, rounded up:
|
254
247
|
|
255
|
-
if
|
248
|
+
if current_adapter == 'mysql2'
|
256
249
|
expect(::DeclareSchema::Model::FieldSpec.mysql_text_limits?).to be_truthy
|
257
250
|
|
258
251
|
class Advert < ActiveRecord::Base
|
@@ -263,10 +256,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
263
256
|
end
|
264
257
|
|
265
258
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
266
|
-
|
259
|
+
migrate_up(<<~EOS.strip)
|
267
260
|
add_column :adverts, :notes, :text, limit: 4294967295, null: false#{charset_and_collation}
|
268
261
|
add_column :adverts, :description, :text, limit: 255, null: false#{charset_and_collation}
|
269
|
-
|
262
|
+
EOS
|
270
263
|
)
|
271
264
|
|
272
265
|
Advert.field_specs.delete :notes
|
@@ -274,7 +267,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
274
267
|
# Limits that are too high for MySQL will raise an exception.
|
275
268
|
|
276
269
|
expect do
|
277
|
-
class Advert < ActiveRecord::Base
|
270
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
278
271
|
declare_schema do
|
279
272
|
text :notes
|
280
273
|
text :description, limit: 0x1_0000_0000
|
@@ -291,8 +284,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
291
284
|
Advert.connection.execute "ALTER TABLE adverts ADD COLUMN description TINYTEXT"
|
292
285
|
Advert.connection.schema_cache.clear!
|
293
286
|
Advert.reset_column_information
|
294
|
-
expect(Advert.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables).
|
295
|
-
to eq(["adverts"])
|
287
|
+
expect(Advert.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables).to eq(["adverts"])
|
296
288
|
expect(Advert.columns.map(&:name)).to eq(["id", "body", "title", "description"])
|
297
289
|
|
298
290
|
# Now migrate to an unstated text limit:
|
@@ -304,12 +296,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
304
296
|
end
|
305
297
|
|
306
298
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
307
|
-
|
299
|
+
migrate_up(<<~EOS.strip)
|
308
300
|
change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
301
|
+
EOS
|
302
|
+
.and(migrate_down(<<~EOS.strip))
|
303
|
+
change_column :adverts, :description, :text#{', limit: 255' if current_adapter == 'mysql2'}, null: true#{charset_and_collation}
|
304
|
+
EOS
|
313
305
|
)
|
314
306
|
|
315
307
|
# And migrate to a stated text limit that is the same as the unstated one:
|
@@ -321,19 +313,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
321
313
|
end
|
322
314
|
|
323
315
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
324
|
-
|
316
|
+
migrate_up(<<~EOS.strip)
|
325
317
|
change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
318
|
+
EOS
|
319
|
+
.and(migrate_down(<<~EOS.strip))
|
320
|
+
change_column :adverts, :description, :text#{', limit: 255' if current_adapter == 'mysql2'}, null: true#{charset_and_collation}
|
321
|
+
EOS
|
330
322
|
)
|
331
323
|
end
|
332
324
|
|
333
325
|
Advert.field_specs.clear
|
334
326
|
Advert.connection.schema_cache.clear!
|
335
327
|
Advert.reset_column_information
|
336
|
-
|
328
|
+
|
329
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
337
330
|
declare_schema do
|
338
331
|
string :name, limit: 250, null: true
|
339
332
|
end
|
@@ -350,8 +343,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
350
343
|
# DeclareSchema extends the `belongs_to` macro so that it also declares the
|
351
344
|
# foreign-key field. It also generates an index on the field.
|
352
345
|
|
353
|
-
class Category < ActiveRecord::Base; end
|
354
|
-
class Advert < ActiveRecord::Base
|
346
|
+
class Category < ActiveRecord::Base; end # rubocop:disable Lint/ConstantDefinitionInBlock
|
347
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
355
348
|
declare_schema do
|
356
349
|
string :name, limit: 250, null: true
|
357
350
|
end
|
@@ -362,12 +355,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
362
355
|
migrate_up(<<~EOS.strip)
|
363
356
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
364
357
|
add_index :adverts, [:category_id], name: :index_adverts_on_category_id
|
365
|
-
#{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n"
|
358
|
+
#{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" unless current_adapter == 'sqlite3'}
|
366
359
|
EOS
|
367
|
-
.and
|
368
|
-
|
369
|
-
|
370
|
-
|
360
|
+
.and(migrate_down(<<~EOS.strip))
|
361
|
+
#{'remove_foreign_key :adverts, name: :index_adverts_on_category_id' unless current_adapter == 'sqlite3'}
|
362
|
+
remove_index :adverts, name: :index_adverts_on_category_id
|
363
|
+
remove_column :adverts, :category_id
|
371
364
|
EOS
|
372
365
|
)
|
373
366
|
|
@@ -376,8 +369,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
376
369
|
|
377
370
|
# If you specify a custom foreign key, the migration generator observes that:
|
378
371
|
|
379
|
-
class Category < ActiveRecord::Base; end
|
380
|
-
|
372
|
+
class Category < ActiveRecord::Base; end # rubocop:disable Lint/ConstantDefinitionInBlock
|
373
|
+
|
374
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
381
375
|
declare_schema { }
|
382
376
|
belongs_to :category, foreign_key: "c_id", class_name: 'Category'
|
383
377
|
end
|
@@ -386,8 +380,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
386
380
|
migrate_up(<<~EOS.strip)
|
387
381
|
add_column :adverts, :c_id, :integer, limit: 8, null: false
|
388
382
|
add_index :adverts, [:c_id], name: :index_adverts_on_c_id
|
389
|
-
#{
|
390
|
-
|
383
|
+
#{unless current_adapter == 'sqlite3'
|
384
|
+
"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" \
|
385
|
+
'add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id'
|
386
|
+
end}
|
391
387
|
EOS
|
392
388
|
)
|
393
389
|
|
@@ -397,8 +393,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
397
393
|
|
398
394
|
# You can avoid generating the index by specifying `index: false`
|
399
395
|
|
400
|
-
class Category < ActiveRecord::Base; end
|
401
|
-
|
396
|
+
class Category < ActiveRecord::Base; end # rubocop:disable Lint/ConstantDefinitionInBlock
|
397
|
+
|
398
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
402
399
|
declare_schema { }
|
403
400
|
belongs_to :category, index: false
|
404
401
|
end
|
@@ -406,8 +403,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
406
403
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
407
404
|
migrate_up(<<~EOS.strip)
|
408
405
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
409
|
-
#{
|
410
|
-
|
406
|
+
#{'add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id' unless current_adapter == 'sqlite3'}
|
407
|
+
EOS
|
411
408
|
)
|
412
409
|
|
413
410
|
Advert.field_specs.delete(:category_id)
|
@@ -416,10 +413,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
416
413
|
|
417
414
|
# You can specify the index name with index: 'name' [deprecated]
|
418
415
|
|
419
|
-
expect(
|
416
|
+
expect(DeclareSchema.deprecator).to(
|
417
|
+
receive(:warn).with(/belongs_to :category, index: 'name' is deprecated; use index: \{ name: 'name' \} instead/i)
|
418
|
+
)
|
419
|
+
|
420
|
+
class Category < ActiveRecord::Base; end # rubocop:disable Lint/ConstantDefinitionInBlock
|
420
421
|
|
421
|
-
class
|
422
|
-
class Advert < ActiveRecord::Base
|
422
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
423
423
|
declare_schema { }
|
424
424
|
belongs_to :category, index: 'my_index'
|
425
425
|
end
|
@@ -428,7 +428,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
428
428
|
migrate_up(<<~EOS.strip)
|
429
429
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
430
430
|
add_index :adverts, [:category_id], name: :my_index
|
431
|
-
#{
|
431
|
+
#{'add_foreign_key :adverts, :categories, column: :category_id, name: :my_index' unless current_adapter == 'sqlite3'}
|
432
432
|
EOS
|
433
433
|
)
|
434
434
|
|
@@ -438,8 +438,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
438
438
|
|
439
439
|
# You can specify the index name with index: { name: }'name', unique: true|false }
|
440
440
|
|
441
|
-
class Category < ActiveRecord::Base; end
|
442
|
-
|
441
|
+
class Category < ActiveRecord::Base; end # rubocop:disable Lint/ConstantDefinitionInBlock
|
442
|
+
|
443
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
443
444
|
declare_schema { }
|
444
445
|
belongs_to :category, index: { name: 'my_index', unique: false }
|
445
446
|
end
|
@@ -448,7 +449,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
448
449
|
migrate_up(<<~EOS.strip)
|
449
450
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
450
451
|
add_index :adverts, [:category_id], name: :my_index
|
451
|
-
#{
|
452
|
+
#{'add_foreign_key :adverts, :categories, column: :category_id, name: :my_index' unless current_adapter == 'sqlite3'}
|
452
453
|
EOS
|
453
454
|
)
|
454
455
|
|
@@ -461,7 +462,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
461
462
|
# `updated_at` and `created_at` can be declared with the shorthand `timestamps`.
|
462
463
|
# Similarly, `lock_version` can be declared with the "shorthand" `optimimistic_lock`.
|
463
464
|
|
464
|
-
class Advert < ActiveRecord::Base
|
465
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
465
466
|
declare_schema do
|
466
467
|
timestamps
|
467
468
|
optimistic_lock
|
@@ -474,10 +475,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
474
475
|
add_column :adverts, :updated_at, :datetime, null: true
|
475
476
|
add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
|
476
477
|
EOS
|
477
|
-
.and
|
478
|
-
|
479
|
-
|
480
|
-
|
478
|
+
.and(migrate_down(<<~EOS.strip))
|
479
|
+
remove_column :adverts, :lock_version
|
480
|
+
remove_column :adverts, :updated_at
|
481
|
+
remove_column :adverts, :created_at
|
481
482
|
EOS
|
482
483
|
)
|
483
484
|
|
@@ -489,10 +490,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
489
490
|
|
490
491
|
# You can add an index to a field definition
|
491
492
|
|
492
|
-
expect(
|
493
|
-
|
493
|
+
expect(DeclareSchema.deprecator).to(
|
494
|
+
receive(:warn).with(/belongs_to :category, index: 'name' is deprecated; use index: \{ name: 'name' \} instead/i)
|
495
|
+
)
|
496
|
+
expect(DeclareSchema.deprecator).to(
|
497
|
+
receive(:warn).with(/belongs_to :category, unique: true\|false is deprecated; use index: \{ unique: true\|false \} instead/i)
|
498
|
+
)
|
494
499
|
|
495
|
-
class Advert < ActiveRecord::Base
|
500
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
496
501
|
declare_schema do
|
497
502
|
string :title, index: true, limit: 250, null: true
|
498
503
|
end
|
@@ -505,7 +510,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
505
510
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
506
511
|
add_index :adverts, [:title], name: :index_adverts_on_title
|
507
512
|
add_index :adverts, [:category_id], name: :my_index
|
508
|
-
#{
|
513
|
+
#{'add_foreign_key :adverts, :categories, column: :category_id, name: :my_index' unless current_adapter == 'sqlite3'}
|
509
514
|
EOS
|
510
515
|
)
|
511
516
|
|
@@ -515,7 +520,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
515
520
|
|
516
521
|
# You can ask for a unique index (deprecated syntax; use index: { unique: true } instead).
|
517
522
|
|
518
|
-
class Advert < ActiveRecord::Base
|
523
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
519
524
|
declare_schema do
|
520
525
|
string :title, index: true, unique: true, null: true, limit: 250
|
521
526
|
end
|
@@ -532,7 +537,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
532
537
|
|
533
538
|
# You can specify the name for the index
|
534
539
|
|
535
|
-
class Advert < ActiveRecord::Base
|
540
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
536
541
|
declare_schema do
|
537
542
|
string :title, index: 'my_index', limit: 250, null: true
|
538
543
|
end
|
@@ -549,7 +554,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
549
554
|
|
550
555
|
# You can ask for an index outside of the fields block
|
551
556
|
|
552
|
-
class Advert < ActiveRecord::Base
|
557
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
553
558
|
declare_schema do
|
554
559
|
string :title, limit: 250, null: true
|
555
560
|
end
|
@@ -567,7 +572,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
567
572
|
|
568
573
|
# The available options for the index function are :unique, :name, :where, and :length.
|
569
574
|
|
570
|
-
class Advert < ActiveRecord::Base
|
575
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
571
576
|
index :title, unique: false, name: 'my_index', length: 10
|
572
577
|
end
|
573
578
|
|
@@ -582,7 +587,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
582
587
|
|
583
588
|
# You can create an index on more than one field
|
584
589
|
|
585
|
-
class Advert < ActiveRecord::Base
|
590
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
586
591
|
index [:title, :category_id]
|
587
592
|
end
|
588
593
|
|
@@ -603,7 +608,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
603
608
|
|
604
609
|
# The migration generator respects the `set_table_name` declaration, although as before, we need to explicitly tell the generator that we want a rename rather than a create and a drop.
|
605
610
|
|
606
|
-
class Advert < ActiveRecord::Base
|
611
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
607
612
|
self.table_name = "ads"
|
608
613
|
declare_schema do
|
609
614
|
string :title, limit: 250, null: true
|
@@ -618,20 +623,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
618
623
|
migrate_up(<<~EOS.strip)
|
619
624
|
rename_table :adverts, :ads
|
620
625
|
add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
|
621
|
-
add_column :ads, :body, :text#{', limit: 4294967295' if
|
626
|
+
add_column :ads, :body, :text#{', limit: 4294967295' if current_adapter == 'mysql2'}, null: true#{charset_and_collation}
|
627
|
+
EOS
|
628
|
+
.and(migrate_down(<<~EOS.strip))
|
629
|
+
remove_column :ads, :body
|
630
|
+
remove_column :ads, :title
|
631
|
+
rename_table :ads, :adverts
|
622
632
|
EOS
|
623
|
-
.and migrate_down(<<~EOS.strip)
|
624
|
-
remove_column :ads, :body
|
625
|
-
remove_column :ads, :title
|
626
|
-
rename_table :ads, :adverts
|
627
|
-
EOS
|
628
633
|
)
|
629
634
|
|
630
635
|
# Set the table name back to what it should be and confirm we're in sync:
|
631
636
|
|
632
637
|
nuke_model_class(Advert)
|
633
638
|
|
634
|
-
class Advert < ActiveRecord::Base
|
639
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
635
640
|
self.table_name = "adverts"
|
636
641
|
end
|
637
642
|
|
@@ -643,7 +648,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
643
648
|
|
644
649
|
nuke_model_class(Advert)
|
645
650
|
|
646
|
-
class Advertisement < ActiveRecord::Base
|
651
|
+
class Advertisement < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
647
652
|
declare_schema do
|
648
653
|
string :title, limit: 250, null: true
|
649
654
|
text :body, null: true
|
@@ -654,14 +659,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
654
659
|
migrate_up(<<~EOS.strip)
|
655
660
|
rename_table :adverts, :advertisements
|
656
661
|
add_column :advertisements, :title, :string, limit: 250, null: true#{charset_and_collation}
|
657
|
-
add_column :advertisements, :body, :text#{', limit: 4294967295' if
|
662
|
+
add_column :advertisements, :body, :text#{', limit: 4294967295' if current_adapter == 'mysql2'}, null: true#{charset_and_collation}
|
658
663
|
remove_column :advertisements, :name
|
659
664
|
EOS
|
660
|
-
.and
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
+
.and(migrate_down(<<~EOS.strip))
|
666
|
+
add_column :advertisements, :name, :string, limit: 250, null: true#{charset_and_collation}
|
667
|
+
remove_column :advertisements, :body
|
668
|
+
remove_column :advertisements, :title
|
669
|
+
rename_table :advertisements, :adverts
|
665
670
|
EOS
|
666
671
|
)
|
667
672
|
|
@@ -677,10 +682,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
677
682
|
migrate_up(<<~EOS.strip)
|
678
683
|
drop_table :adverts
|
679
684
|
EOS
|
680
|
-
.and
|
681
|
-
|
682
|
-
|
683
|
-
|
685
|
+
.and(migrate_down(<<~EOS.strip))
|
686
|
+
create_table "adverts"#{table_options}, force: :cascade do |t|
|
687
|
+
t.string "name", limit: 250#{charset_and_collation}
|
688
|
+
end
|
684
689
|
EOS
|
685
690
|
)
|
686
691
|
|
@@ -690,7 +695,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
690
695
|
|
691
696
|
# Adding a subclass or two should introduce the 'type' column and no other changes
|
692
697
|
|
693
|
-
class Advert < ActiveRecord::Base
|
698
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
694
699
|
declare_schema do
|
695
700
|
text :body, null: true
|
696
701
|
string :title, default: "Untitled", limit: 250, null: true
|
@@ -699,9 +704,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
699
704
|
up = Generators::DeclareSchema::Migration::Migrator.run.first
|
700
705
|
ActiveRecord::Migration.class_eval(up)
|
701
706
|
|
702
|
-
class FancyAdvert < Advert
|
707
|
+
class FancyAdvert < Advert # rubocop:disable Lint/ConstantDefinitionInBlock
|
703
708
|
end
|
704
|
-
|
709
|
+
|
710
|
+
class SuperFancyAdvert < FancyAdvert # rubocop:disable Lint/ConstantDefinitionInBlock
|
705
711
|
end
|
706
712
|
|
707
713
|
expect(Generators::DeclareSchema::Migration::Migrator.run.first).to be_present
|
@@ -712,9 +718,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
712
718
|
add_column :adverts, :type, :string, limit: 250, null: true#{charset_and_collation}
|
713
719
|
add_index :adverts, [:type], name: :on_type
|
714
720
|
EOS
|
715
|
-
.and
|
716
|
-
|
717
|
-
|
721
|
+
.and(migrate_down(<<~EOS.strip))
|
722
|
+
remove_index :adverts, name: :on_type
|
723
|
+
remove_column :adverts, :type
|
718
724
|
EOS
|
719
725
|
)
|
720
726
|
end
|
@@ -734,17 +740,16 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
734
740
|
Advert.connection.schema_cache.clear!
|
735
741
|
Advert.reset_column_information
|
736
742
|
|
737
|
-
expect(Advert.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables)
|
738
|
-
|
743
|
+
expect(Advert.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables)
|
744
|
+
.to eq(["adverts"])
|
739
745
|
expect(Advert.columns.map(&:name).sort).to eq(["body", "id", "title"])
|
740
746
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to eq(["", ""])
|
741
747
|
|
742
|
-
|
743
748
|
### Rename a column and change the default
|
744
749
|
|
745
750
|
Advert.field_specs.clear
|
746
751
|
|
747
|
-
class Advert < ActiveRecord::Base
|
752
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
748
753
|
declare_schema do
|
749
754
|
string :name, default: "No Name", limit: 250, null: true
|
750
755
|
text :body, null: true
|
@@ -756,16 +761,16 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
756
761
|
rename_column :adverts, :title, :name
|
757
762
|
change_column :adverts, :name, :string, limit: 250, null: true, default: "No Name"#{charset_and_collation}
|
758
763
|
EOS
|
759
|
-
.and
|
760
|
-
|
761
|
-
|
764
|
+
.and(migrate_down(<<~EOS.strip))
|
765
|
+
change_column :adverts, :name, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
|
766
|
+
rename_column :adverts, :name, :title
|
762
767
|
EOS
|
763
768
|
)
|
764
769
|
|
765
770
|
### Rename a table and add a column
|
766
771
|
|
767
772
|
nuke_model_class(Advert)
|
768
|
-
class Ad < ActiveRecord::Base
|
773
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
769
774
|
declare_schema do
|
770
775
|
string :title, default: "Untitled", limit: 250
|
771
776
|
text :body, null: true
|
@@ -781,7 +786,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
781
786
|
EOS
|
782
787
|
)
|
783
788
|
|
784
|
-
class Advert < ActiveRecord::Base
|
789
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
785
790
|
declare_schema do
|
786
791
|
text :body, null: true
|
787
792
|
string :title, default: "Untitled", limit: 250, null: true
|
@@ -794,7 +799,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
794
799
|
|
795
800
|
nuke_model_class(Ad)
|
796
801
|
|
797
|
-
class Advert < ActiveRecord::Base
|
802
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
798
803
|
declare_schema do
|
799
804
|
text :body, null: true
|
800
805
|
end
|
@@ -808,13 +813,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
808
813
|
)
|
809
814
|
|
810
815
|
nuke_model_class(Advert)
|
811
|
-
ActiveRecord::Base.connection.execute("
|
816
|
+
ActiveRecord::Base.connection.execute("DROP TABLE #{ActiveRecord::Base.connection.quote_table_name('adverts')};")
|
812
817
|
|
813
818
|
## DSL
|
814
819
|
|
815
820
|
# The DSL allows lambdas and constants
|
816
821
|
|
817
|
-
class User < ActiveRecord::Base
|
822
|
+
class User < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
818
823
|
declare_schema do
|
819
824
|
string :company, limit: 250, ruby_default: -> { "BigCorp" }
|
820
825
|
end
|
@@ -828,14 +833,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
828
833
|
|
829
834
|
# DeclareSchema can accept a validates hash in the field options.
|
830
835
|
|
831
|
-
class Ad < ActiveRecord::Base
|
836
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
832
837
|
class << self
|
833
838
|
def validates(field_name, options)
|
834
839
|
end
|
835
840
|
end
|
836
841
|
end
|
837
842
|
expect(Ad).to receive(:validates).with(:company, presence: true, uniqueness: { case_sensitive: false })
|
838
|
-
class Ad < ActiveRecord::Base
|
843
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
839
844
|
declare_schema do
|
840
845
|
string :company, limit: 250, index: true, unique: true, validates: { presence: true, uniqueness: { case_sensitive: false } }
|
841
846
|
end
|
@@ -848,13 +853,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
848
853
|
# DeclareSchema supports has_and_belongs_to_many relationships and generates the intersection ("join") table
|
849
854
|
# with appropriate primary key, indexes, and foreign keys.
|
850
855
|
|
851
|
-
class Advertiser < ActiveRecord::Base
|
856
|
+
class Advertiser < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
852
857
|
declare_schema do
|
853
858
|
string :name, limit: 250
|
854
859
|
end
|
855
860
|
has_and_belongs_to_many :creatives
|
856
861
|
end
|
857
|
-
|
862
|
+
|
863
|
+
class Creative < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
858
864
|
declare_schema do
|
859
865
|
string :url, limit: 500
|
860
866
|
end
|
@@ -868,10 +874,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
868
874
|
t.integer :advertiser_id, limit: 8, null: false
|
869
875
|
t.integer :creative_id, limit: 8, null: false
|
870
876
|
end
|
871
|
-
create_table :creatives, id:
|
877
|
+
create_table :creatives, id: #{primary_key_type}#{create_table_charset_and_collation} do |t|
|
872
878
|
t.string :url, limit: 500, null: false#{charset_and_collation}
|
873
879
|
end
|
874
|
-
create_table :advertisers, id:
|
880
|
+
create_table :advertisers, id: #{primary_key_type}#{create_table_charset_and_collation} do |t|
|
875
881
|
t.string :name, limit: 250, null: false#{charset_and_collation}
|
876
882
|
end
|
877
883
|
add_index :advertisers_creatives, [:creative_id], name: :index_advertisers_creatives_on_creative_id
|
@@ -880,14 +886,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
880
886
|
EOS
|
881
887
|
else
|
882
888
|
migrate_up(<<~EOS.strip)
|
883
|
-
create_table :advertisers, id:
|
889
|
+
create_table :advertisers, id: #{primary_key_type}#{create_table_charset_and_collation} do |t|
|
884
890
|
t.string :name, limit: 250, null: false#{charset_and_collation}
|
885
891
|
end
|
886
892
|
create_table :advertisers_creatives, primary_key: [:advertiser_id, :creative_id]#{create_table_charset_and_collation} do |t|
|
887
893
|
t.integer :advertiser_id, limit: 8, null: false
|
888
894
|
t.integer :creative_id, limit: 8, null: false
|
889
895
|
end
|
890
|
-
create_table :creatives, id:
|
896
|
+
create_table :creatives, id: #{primary_key_type}#{create_table_charset_and_collation} do |t|
|
891
897
|
t.string :url, limit: 500, null: false#{charset_and_collation}
|
892
898
|
end
|
893
899
|
add_index :advertisers_creatives, [:creative_id], name: :index_advertisers_creatives_on_creative_id
|
@@ -903,19 +909,25 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
903
909
|
end
|
904
910
|
|
905
911
|
context 'models with the same parent foreign key relation' do
|
912
|
+
include_context 'skip if' do
|
913
|
+
let(:adapter) { 'sqlite3' }
|
914
|
+
end
|
915
|
+
|
906
916
|
before do
|
907
|
-
class Category < ActiveRecord::Base
|
917
|
+
class Category < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
908
918
|
declare_schema do
|
909
919
|
string :name, limit: 250, null: true
|
910
920
|
end
|
911
921
|
end
|
912
|
-
|
922
|
+
|
923
|
+
class Advertiser < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
913
924
|
declare_schema do
|
914
925
|
string :name, limit: 250, null: true
|
915
926
|
end
|
916
927
|
belongs_to :category, limit: 8
|
917
928
|
end
|
918
|
-
|
929
|
+
|
930
|
+
class Affiliate < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
919
931
|
declare_schema do
|
920
932
|
string :name, limit: 250, null: true
|
921
933
|
end
|
@@ -923,56 +935,27 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
923
935
|
end
|
924
936
|
end
|
925
937
|
|
938
|
+
let(:fixture_file_path) do
|
939
|
+
if ActiveSupport.version >= Gem::Version.new('7.0.0') && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.1.0')
|
940
|
+
"../../fixtures/migrations/#{current_adapter}/will_generate_unique_constraint_names_rails_7.txt"
|
941
|
+
else
|
942
|
+
"../../fixtures/migrations/#{current_adapter}/will_generate_unique_constraint_names_rails_6.txt"
|
943
|
+
end
|
944
|
+
end
|
945
|
+
|
926
946
|
it 'will generate unique constraint names' do
|
927
|
-
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
928
|
-
|
929
|
-
migrate_up(<<~EOS.strip)
|
930
|
-
create_table :affiliates, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
931
|
-
t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
932
|
-
t.integer :category_id, limit: 8, null: false
|
933
|
-
end
|
934
|
-
create_table :advertisers, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
935
|
-
t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
936
|
-
t.integer :category_id, limit: 8, null: false
|
937
|
-
end
|
938
|
-
create_table :categories, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
939
|
-
t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
940
|
-
end
|
941
|
-
add_index :affiliates, [:category_id], name: :index_affiliates_on_category_id
|
942
|
-
add_index :advertisers, [:category_id], name: :index_advertisers_on_category_id
|
943
|
-
add_foreign_key :affiliates, :categories, column: :category_id, name: :index_affiliates_on_category_id
|
944
|
-
add_foreign_key :advertisers, :categories, column: :category_id, name: :index_advertisers_on_category_id
|
945
|
-
EOS
|
946
|
-
else
|
947
|
-
migrate_up(<<~EOS.strip)
|
948
|
-
create_table :categories, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
949
|
-
t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
950
|
-
end
|
951
|
-
create_table :advertisers, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
952
|
-
t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
953
|
-
t.integer :category_id, limit: 8, null: false
|
954
|
-
end
|
955
|
-
create_table :affiliates, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
956
|
-
t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
957
|
-
t.integer :category_id, limit: 8, null: false
|
958
|
-
end
|
959
|
-
add_index :advertisers, [:category_id], name: :index_advertisers_on_category_id
|
960
|
-
add_index :affiliates, [:category_id], name: :index_affiliates_on_category_id
|
961
|
-
add_foreign_key :advertisers, :categories, column: :category_id, name: :index_advertisers_on_category_id
|
962
|
-
add_foreign_key :affiliates, :categories, column: :category_id, name: :index_affiliates_on_category_id
|
963
|
-
EOS
|
964
|
-
end
|
965
|
-
)
|
947
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(migrate_up(File.read(File.expand_path(fixture_file_path, __dir__)).chomp))
|
948
|
+
|
966
949
|
migrate
|
967
950
|
|
968
951
|
nuke_model_class(Advertiser)
|
969
952
|
nuke_model_class(Affiliate)
|
970
953
|
end
|
971
|
-
end
|
954
|
+
end
|
972
955
|
|
973
956
|
describe 'serialize' do
|
974
957
|
before do
|
975
|
-
class Ad < ActiveRecord::Base
|
958
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
976
959
|
@serialize_args = []
|
977
960
|
|
978
961
|
class << self
|
@@ -987,7 +970,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
987
970
|
|
988
971
|
describe 'untyped' do
|
989
972
|
it 'allows serialize: true' do
|
990
|
-
class Ad < ActiveRecord::Base
|
973
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
991
974
|
declare_schema do
|
992
975
|
text :allow_list, limit: 0xFFFF, serialize: true
|
993
976
|
end
|
@@ -997,7 +980,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
997
980
|
end
|
998
981
|
|
999
982
|
it 'converts defaults with .to_yaml' do
|
1000
|
-
class Ad < ActiveRecord::Base
|
983
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1001
984
|
declare_schema do
|
1002
985
|
string :allow_list, limit: 250, serialize: true, null: true, default: []
|
1003
986
|
string :allow_hash, limit: 250, serialize: true, null: true, default: {}
|
@@ -1015,7 +998,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1015
998
|
|
1016
999
|
describe 'Array' do
|
1017
1000
|
it 'allows serialize: Array' do
|
1018
|
-
class Ad < ActiveRecord::Base
|
1001
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1019
1002
|
declare_schema do
|
1020
1003
|
string :allow_list, limit: 250, serialize: Array, null: true
|
1021
1004
|
end
|
@@ -1025,7 +1008,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1025
1008
|
end
|
1026
1009
|
|
1027
1010
|
it 'allows Array defaults' do
|
1028
|
-
class Ad < ActiveRecord::Base
|
1011
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1029
1012
|
declare_schema do
|
1030
1013
|
string :allow_list, limit: 250, serialize: Array, null: true, default: [2]
|
1031
1014
|
string :allow_string, limit: 250, serialize: Array, null: true, default: ['abc']
|
@@ -1043,7 +1026,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1043
1026
|
|
1044
1027
|
describe 'Hash' do
|
1045
1028
|
it 'allows serialize: Hash' do
|
1046
|
-
class Ad < ActiveRecord::Base
|
1029
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1047
1030
|
declare_schema do
|
1048
1031
|
string :allow_list, limit: 250, serialize: Hash, null: true
|
1049
1032
|
end
|
@@ -1053,7 +1036,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1053
1036
|
end
|
1054
1037
|
|
1055
1038
|
it 'allows Hash defaults' do
|
1056
|
-
class Ad < ActiveRecord::Base
|
1039
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1057
1040
|
declare_schema do
|
1058
1041
|
string :allow_loc, limit: 250, serialize: Hash, null: true, default: { 'state' => 'CA' }
|
1059
1042
|
string :allow_hash, limit: 250, serialize: Hash, null: true, default: {}
|
@@ -1069,7 +1052,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1069
1052
|
|
1070
1053
|
describe 'JSON' do
|
1071
1054
|
it 'allows serialize: JSON' do
|
1072
|
-
class Ad < ActiveRecord::Base
|
1055
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1073
1056
|
declare_schema do
|
1074
1057
|
string :allow_list, limit: 250, serialize: JSON
|
1075
1058
|
end
|
@@ -1079,7 +1062,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1079
1062
|
end
|
1080
1063
|
|
1081
1064
|
it 'allows JSON defaults' do
|
1082
|
-
class Ad < ActiveRecord::Base
|
1065
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1083
1066
|
declare_schema do
|
1084
1067
|
string :allow_hash, limit: 250, serialize: JSON, null: true, default: { 'state' => 'CA' }
|
1085
1068
|
string :allow_empty_array, limit: 250, serialize: JSON, null: true, default: []
|
@@ -1095,7 +1078,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1095
1078
|
end
|
1096
1079
|
end
|
1097
1080
|
|
1098
|
-
class ValueClass
|
1081
|
+
class ValueClass # rubocop:disable Lint/ConstantDefinitionInBlock
|
1099
1082
|
delegate :present?, :inspect, to: :@value
|
1100
1083
|
|
1101
1084
|
def initialize(value)
|
@@ -1119,7 +1102,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1119
1102
|
|
1120
1103
|
describe 'custom coder' do
|
1121
1104
|
it 'allows serialize: ValueClass' do
|
1122
|
-
class Ad < ActiveRecord::Base
|
1105
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1123
1106
|
declare_schema do
|
1124
1107
|
string :allow_list, limit: 250, serialize: ValueClass
|
1125
1108
|
end
|
@@ -1129,7 +1112,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1129
1112
|
end
|
1130
1113
|
|
1131
1114
|
it 'allows ValueClass defaults' do
|
1132
|
-
class Ad < ActiveRecord::Base
|
1115
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1133
1116
|
declare_schema do
|
1134
1117
|
string :allow_hash, limit: 250, serialize: ValueClass, null: true, default: ValueClass.new([2])
|
1135
1118
|
string :allow_empty_array, limit: 250, serialize: ValueClass, null: true, default: ValueClass.new([])
|
@@ -1145,7 +1128,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1145
1128
|
|
1146
1129
|
it 'disallows serialize: with a non-string column type' do
|
1147
1130
|
expect do
|
1148
|
-
class Ad < ActiveRecord::Base
|
1131
|
+
class Ad < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1149
1132
|
declare_schema do
|
1150
1133
|
integer :allow_list, limit: 8, serialize: true
|
1151
1134
|
end
|
@@ -1168,7 +1151,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1168
1151
|
end
|
1169
1152
|
end
|
1170
1153
|
|
1171
|
-
class Advert < ActiveRecord::Base
|
1154
|
+
class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1172
1155
|
declare_schema do
|
1173
1156
|
string :name, limit: 250, null: true
|
1174
1157
|
integer :category_id, limit: 8
|
@@ -1180,7 +1163,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1180
1163
|
end
|
1181
1164
|
|
1182
1165
|
it 'passes through optional: when given' do
|
1183
|
-
class AdvertBelongsTo < ActiveRecord::Base
|
1166
|
+
class AdvertBelongsTo < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1184
1167
|
self.table_name = 'adverts'
|
1185
1168
|
declare_schema { }
|
1186
1169
|
reset_column_information
|
@@ -1191,7 +1174,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1191
1174
|
|
1192
1175
|
describe 'contradictory settings' do # contradictory settings are ok--for example, during migration
|
1193
1176
|
it 'passes through optional: true, null: false' do
|
1194
|
-
class AdvertBelongsTo < ActiveRecord::Base
|
1177
|
+
class AdvertBelongsTo < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1195
1178
|
self.table_name = 'adverts'
|
1196
1179
|
declare_schema { }
|
1197
1180
|
reset_column_information
|
@@ -1202,7 +1185,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1202
1185
|
end
|
1203
1186
|
|
1204
1187
|
it 'passes through optional: false, null: true' do
|
1205
|
-
class AdvertBelongsTo < ActiveRecord::Base
|
1188
|
+
class AdvertBelongsTo < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1206
1189
|
self.table_name = 'adverts'
|
1207
1190
|
declare_schema { }
|
1208
1191
|
reset_column_information
|
@@ -1216,7 +1199,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1216
1199
|
[false, true].each do |nullable|
|
1217
1200
|
context "nullable=#{nullable}" do
|
1218
1201
|
it 'infers optional: from null:' do
|
1219
|
-
eval <<~EOS
|
1202
|
+
eval <<~EOS # rubocop:disable Style/EvalWithLocation,Security/Eval
|
1220
1203
|
class AdvertBelongsTo < ActiveRecord::Base
|
1221
1204
|
declare_schema { }
|
1222
1205
|
belongs_to :ad_category, null: #{nullable}
|
@@ -1227,7 +1210,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1227
1210
|
end
|
1228
1211
|
|
1229
1212
|
it 'infers null: from optional:' do
|
1230
|
-
eval <<~EOS
|
1213
|
+
eval <<~EOS # rubocop:disable Style/EvalWithLocation,Security/Eval
|
1231
1214
|
class AdvertBelongsTo < ActiveRecord::Base
|
1232
1215
|
declare_schema { }
|
1233
1216
|
belongs_to :ad_category, optional: #{nullable}
|
@@ -1240,8 +1223,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1240
1223
|
end
|
1241
1224
|
|
1242
1225
|
it 'deprecates limit:' do
|
1243
|
-
expect(
|
1244
|
-
eval <<~EOS
|
1226
|
+
expect(DeclareSchema.deprecator).to receive(:warn).with("belongs_to :ad_category, limit: is deprecated since it is now inferred")
|
1227
|
+
eval <<~EOS # rubocop:disable Style/EvalWithLocation
|
1245
1228
|
class UsingLimit < ActiveRecord::Base
|
1246
1229
|
declare_schema { }
|
1247
1230
|
belongs_to :ad_category, limit: 4
|
@@ -1252,18 +1235,21 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1252
1235
|
|
1253
1236
|
context 'when parent object PKs have different limits' do
|
1254
1237
|
before do
|
1255
|
-
class IdDefault < ActiveRecord::Base
|
1238
|
+
class IdDefault < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1256
1239
|
declare_schema { }
|
1257
1240
|
end
|
1258
|
-
|
1241
|
+
|
1242
|
+
class Id4 < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1259
1243
|
declare_schema id: :integer do
|
1260
1244
|
end
|
1261
1245
|
end
|
1262
|
-
|
1246
|
+
|
1247
|
+
class Id8 < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1263
1248
|
declare_schema id: :bigint do
|
1264
1249
|
end
|
1265
1250
|
end
|
1266
|
-
|
1251
|
+
|
1252
|
+
class Fk < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1267
1253
|
declare_schema { }
|
1268
1254
|
belongs_to :id_default
|
1269
1255
|
belongs_to :id4
|
@@ -1276,7 +1262,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1276
1262
|
|
1277
1263
|
create_id4_defaults = up.split("\n").grep(/create_table :id_defaults/).first
|
1278
1264
|
expect(create_id4_defaults).to be, up
|
1279
|
-
|
1265
|
+
case current_adapter
|
1266
|
+
when 'postgresql'
|
1267
|
+
expect(create_id4_defaults).to match(/, id: :bigserial/)
|
1268
|
+
else
|
1269
|
+
expect(create_id4_defaults).to match(/, id: :bigint/)
|
1270
|
+
end
|
1280
1271
|
|
1281
1272
|
create_id4s = up.split("\n").grep(/create_table :id4s/).first
|
1282
1273
|
expect(create_id4s).to be, up
|
@@ -1291,7 +1282,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1291
1282
|
up = Generators::DeclareSchema::Migration::Migrator.run.first
|
1292
1283
|
|
1293
1284
|
create_fks = up.split("\n").grep(/t\.integer /).map { |command| command.gsub(', null: false', '').gsub(/^ +/, '') }
|
1294
|
-
if
|
1285
|
+
if current_adapter == 'sqlite3'
|
1295
1286
|
create_fks.map! { |command| command.gsub(/limit: [a-z0-9]+/, 'limit: X') }
|
1296
1287
|
expect(create_fks).to eq([
|
1297
1288
|
't.integer :id_default_id, limit: X',
|
@@ -1317,20 +1308,22 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1317
1308
|
nuke_model_class(Fk)
|
1318
1309
|
ActiveRecord::Base.connection.schema_cache.clear!
|
1319
1310
|
|
1320
|
-
|
1321
|
-
class NewIdDefault < ActiveRecord::Base
|
1311
|
+
class NewIdDefault < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1322
1312
|
self.table_name = 'id_defaults'
|
1323
1313
|
declare_schema { }
|
1324
1314
|
end
|
1325
|
-
|
1315
|
+
|
1316
|
+
class NewId4 < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1326
1317
|
self.table_name = 'id4s'
|
1327
1318
|
declare_schema { }
|
1328
1319
|
end
|
1329
|
-
|
1320
|
+
|
1321
|
+
class NewId8 < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1330
1322
|
self.table_name = 'id8s'
|
1331
1323
|
declare_schema { }
|
1332
1324
|
end
|
1333
|
-
|
1325
|
+
|
1326
|
+
class NewFk < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
|
1334
1327
|
declare_schema { }
|
1335
1328
|
belongs_to :new_id_default
|
1336
1329
|
belongs_to :new_id4
|
@@ -1342,7 +1335,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1342
1335
|
up = Generators::DeclareSchema::Migration::Migrator.run.first
|
1343
1336
|
|
1344
1337
|
create_fks = up.split("\n").grep(/t\.integer /).map { |command| command.gsub(', null: false', '').gsub(/^ +/, '') }
|
1345
|
-
if
|
1338
|
+
if current_adapter == 'sqlite3'
|
1346
1339
|
create_fks.map! { |command| command.gsub(/limit: [a-z0-9]+/, 'limit: X') }
|
1347
1340
|
expect(create_fks).to eq([
|
1348
1341
|
't.integer :new_id_default_id, limit: X',
|
@@ -1364,7 +1357,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1364
1357
|
|
1365
1358
|
describe 'migration base class' do
|
1366
1359
|
it 'adapts to Rails 4' do
|
1367
|
-
class Advert < active_record_base_class.constantize
|
1360
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1368
1361
|
declare_schema do
|
1369
1362
|
string :title, limit: 100
|
1370
1363
|
end
|
@@ -1384,7 +1377,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1384
1377
|
|
1385
1378
|
context 'Does not generate migrations' do
|
1386
1379
|
it 'for aliased fields bigint -> integer limit 8' do
|
1387
|
-
class Advert < active_record_base_class.constantize
|
1380
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1388
1381
|
declare_schema do
|
1389
1382
|
bigint :price
|
1390
1383
|
end
|
@@ -1395,7 +1388,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1395
1388
|
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
1396
1389
|
expect(migrations.size).to eq(1), migrations.inspect
|
1397
1390
|
|
1398
|
-
class Advert < active_record_base_class.constantize
|
1391
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1399
1392
|
declare_schema do
|
1400
1393
|
integer :price, limit: 8
|
1401
1394
|
end
|
@@ -1409,7 +1402,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1409
1402
|
let(:default_schema_block) { nil }
|
1410
1403
|
let(:declare_model) do
|
1411
1404
|
-> do
|
1412
|
-
class Advert < active_record_base_class.constantize
|
1405
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1413
1406
|
declare_schema do
|
1414
1407
|
integer :price, limit: 8
|
1415
1408
|
end
|
@@ -1449,7 +1442,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1449
1442
|
|
1450
1443
|
context 'and the model sets default_schema: false' do
|
1451
1444
|
before do
|
1452
|
-
class Advert < active_record_base_class.constantize
|
1445
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1453
1446
|
declare_schema default_schema: false do
|
1454
1447
|
integer :price, limit: 8
|
1455
1448
|
end
|
@@ -1463,7 +1456,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1463
1456
|
|
1464
1457
|
context 'and the block has redundant fields' do
|
1465
1458
|
before do
|
1466
|
-
class Advert < active_record_base_class.constantize
|
1459
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1467
1460
|
declare_schema do
|
1468
1461
|
integer :price, limit: 8
|
1469
1462
|
timestamps
|
@@ -1480,7 +1473,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1480
1473
|
|
1481
1474
|
context 'index' do
|
1482
1475
|
before do
|
1483
|
-
class Advert < active_record_base_class.constantize
|
1476
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1484
1477
|
declare_schema { }
|
1485
1478
|
belongs_to :ad_category
|
1486
1479
|
end
|
@@ -1506,7 +1499,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1506
1499
|
|
1507
1500
|
context 'constraint' do
|
1508
1501
|
before do
|
1509
|
-
class Advert < active_record_base_class.constantize
|
1502
|
+
class Advert < active_record_base_class.constantize # rubocop:disable Lint/ConstantDefinitionInBlock
|
1510
1503
|
declare_schema { }
|
1511
1504
|
belongs_to :ad_category
|
1512
1505
|
end
|
@@ -1514,13 +1507,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1514
1507
|
|
1515
1508
|
it "when exactly equal, it is idempotent and doesn't raise" do
|
1516
1509
|
expect do
|
1517
|
-
Advert.constraint
|
1510
|
+
Advert.constraint(:ad_category_id, parent_table_name: 'ad_categories',
|
1511
|
+
constraint_name: :index_adverts_on_ad_category_id, parent_class_name: 'AdCategory')
|
1518
1512
|
end.to_not change { Advert.index_definitions.size }
|
1519
1513
|
end
|
1520
1514
|
|
1521
1515
|
it "when equivalent, it is idempotent and doesn't raise" do
|
1522
1516
|
expect do
|
1523
|
-
Advert.constraint
|
1517
|
+
Advert.constraint(:ad_category_id, parent_table_name: 'ad_categories', constraint_name: :constraint_1, parent_class_name: 'AdCategory')
|
1524
1518
|
end.to_not change { Advert.index_definitions.size }
|
1525
1519
|
end
|
1526
1520
|
end
|