declare_schema 0.6.1 → 0.7.1

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.
@@ -25,7 +25,7 @@ RSpec.describe DeclareSchema::FieldDeclarationDsl do
25
25
  end
26
26
 
27
27
  it 'stores limits' do
28
- expect(TestModel.field_specs['name'].limit).to eq(127)
28
+ expect(TestModel.field_specs['name'].limit).to eq(127), TestModel.field_specs['name'].inspect
29
29
  end
30
30
 
31
31
  # TODO: fill out remaining tests
@@ -1,69 +1,173 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe 'DeclareSchema Model FieldSpec' do
3
+ begin
4
+ require 'mysql2'
5
+ rescue LoadError
6
+ end
7
+
8
+ RSpec.describe DeclareSchema::Model::FieldSpec do
9
+ let(:model) { double('model', table_options: {}) }
10
+ let(:col_spec) { double('col_spec', sql_type: 'varchar') }
11
+
4
12
  before do
5
13
  load File.expand_path('prepare_testapp.rb', __dir__)
14
+
15
+ if Rails::VERSION::MAJOR < 5
16
+ allow(col_spec).to receive(:type_cast_from_database, &:itself)
17
+ end
6
18
  end
7
19
 
8
- context 'There are no model columns to change' do
9
- it '#different_to should return false for int8 == int8' do
10
- subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :integer, limit: 8, null: false, position: 0)
20
+ describe '#initialize' do
21
+ it 'normalizes option order' do
22
+ subject = described_class.new(model, :price, :integer, anonymize_using: 'x', null: false, position: 0, limit: 4)
23
+ expect(subject.options.keys).to eq([:limit, :null, :anonymize_using])
24
+ end
25
+ end
11
26
 
12
- case Rails::VERSION::MAJOR
13
- when 4
14
- cast_type = ActiveRecord::Type::Integer.new(limit: 8)
15
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
16
- else
17
- sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: "integer(8)", type: :integer, limit: 8)
18
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
27
+ describe '#schema_attributes' do
28
+ describe 'integer 4' do
29
+ it 'returns schema attributes' do
30
+ subject = described_class.new(model, :price, :integer, limit: 4, null: false, position: 0)
31
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 4, null: false)
19
32
  end
20
-
21
- expect(subject.different_to?(subject.name, col)).to eq(false)
22
33
  end
23
34
 
24
- it '#different_to should return false for bigint == bigint' do
25
- subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :bigint, null: false, position: 0)
35
+ describe 'integer 8' do
36
+ it 'returns schema attributes' do
37
+ subject = described_class.new(model, :price, :integer, limit: 8, null: true, position: 2)
38
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: true)
39
+ end
40
+ end
26
41
 
27
- case Rails::VERSION::MAJOR
28
- when 4
29
- cast_type = ActiveRecord::Type::BigInteger.new(limit: 8)
30
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "bigint(20)", false)
31
- else
32
- sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: "bigint(20)", type: :integer, limit: 8)
33
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
42
+ describe 'bigint' do
43
+ it 'returns schema attributes' do
44
+ subject = described_class.new(model, :price, :bigint, null: false, position: 2)
45
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: false)
34
46
  end
47
+ end
35
48
 
36
- expect(subject.different_to?(subject.name, col)).to eq(false)
49
+ describe 'string' do
50
+ it 'returns schema attributes (including charset/collation iff mysql)' do
51
+ subject = described_class.new(model, :title, :string, limit: 100, null: true, charset: 'utf8mb4', position: 0)
52
+ if defined?(Mysql2)
53
+ expect(subject.schema_attributes(col_spec)).to eq(type: :string, limit: 100, null: true, charset: 'utf8mb4', collation: 'utf8mb4_bin')
54
+ else
55
+ expect(subject.schema_attributes(col_spec)).to eq(type: :string, limit: 100, null: true)
56
+ end
57
+ end
37
58
  end
38
59
 
39
- it '#different_to should return false for int8 == bigint' do
40
- subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :integer, limit: 8, null: false, position: 0)
60
+ describe 'text' do
61
+ it 'returns schema attributes (including charset/collation iff mysql)' do
62
+ subject = described_class.new(model, :title, :text, limit: 200, null: true, charset: 'utf8mb4', position: 2)
63
+ if defined?(Mysql2)
64
+ expect(subject.schema_attributes(col_spec)).to eq(type: :text, limit: 255, null: true, charset: 'utf8mb4', collation: 'utf8mb4_bin')
65
+ else
66
+ expect(subject.schema_attributes(col_spec)).to eq(type: :text, null: true)
67
+ end
68
+ end
41
69
 
42
- case Rails::VERSION::MAJOR
43
- when 4
44
- cast_type = ActiveRecord::Type::BigInteger.new(limit: 8)
45
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "bigint(20)", false)
46
- else
47
- sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: "bigint(20)", type: :integer, limit: 8)
48
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
70
+ it 'allows a default to be set unless mysql' do
71
+ if defined?(Mysql2)
72
+ expect do
73
+ described_class.new(model, :title, :text, limit: 200, null: true, default: 'none', charset: 'utf8mb4', position: 2)
74
+ end.to raise_exception(DeclareSchema::MysqlTextMayNotHaveDefault)
75
+ else
76
+ subject = described_class.new(model, :title, :text, limit: 200, null: true, default: 'none', charset: 'utf8mb4', position: 2)
77
+ expect(subject.schema_attributes(col_spec)).to eq(type: :text, null: true, default: 'none')
78
+ end
49
79
  end
50
80
 
51
- expect(subject.different_to?(subject.name, col)).to eq(false)
81
+ describe 'decimal' do
82
+ it 'allows precision: and scale:' do
83
+ subject = described_class.new(model, :quantity, :decimal, precision: 8, scale: 10, null: true, position: 3)
84
+ expect(subject.schema_attributes(col_spec)).to eq(type: :decimal, precision: 8, scale: 10, null: true)
85
+ end
86
+
87
+ it 'requires precision:' do
88
+ expect_any_instance_of(described_class).to receive(:warn).with(/precision: required for :decimal type/)
89
+ described_class.new(model, :quantity, :decimal, scale: 10, null: true, position: 3)
90
+ end
91
+
92
+ it 'requires scale:' do
93
+ expect_any_instance_of(described_class).to receive(:warn).with(/scale: required for :decimal type/)
94
+ described_class.new(model, :quantity, :decimal, precision: 8, null: true, position: 3)
95
+ end
96
+ end
97
+
98
+ [:integer, :bigint, :string, :text, :binary, :datetime, :date, :time].each do |t|
99
+ describe t.to_s do
100
+ let(:extra) { t == :string ? { limit: 100 } : {} }
101
+
102
+ it 'does not allow precision:' do
103
+ expect_any_instance_of(described_class).to receive(:warn).with(/precision: only allowed for :decimal type/)
104
+ described_class.new(model, :quantity, t, { precision: 8, null: true, position: 3 }.merge(extra))
105
+ end unless t == :datetime
106
+
107
+ it 'does not allow scale:' do
108
+ expect_any_instance_of(described_class).to receive(:warn).with(/scale: only allowed for :decimal type/)
109
+ described_class.new(model, :quantity, t, { scale: 10, null: true, position: 3 }.merge(extra))
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ describe 'datetime' do
116
+ it 'keeps type as "datetime"' do
117
+ subject = described_class.new(model, :created_at, :datetime, null: false, position: 1)
118
+ expect(subject.schema_attributes(col_spec)).to eq(type: :datetime, null: false)
119
+ end
120
+ end
121
+
122
+ describe 'timestamp' do
123
+ it 'normalizes type to "datetime"' do
124
+ subject = described_class.new(model, :created_at, :timestamp, null: true, position: 2)
125
+ expect(subject.schema_attributes(col_spec)).to eq(type: :datetime, null: true)
126
+ end
52
127
  end
53
128
 
54
- it '#different_to should return false for bigint == int8' do
55
- subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :bigint, null: false, position: 0)
129
+ describe 'default:' do
130
+ let(:col_spec) { double('col_spec', sql_type: :integer) }
131
+
132
+ it 'typecasts default value' do
133
+ allow(col_spec).to receive(:type_cast_from_database) { |default| Integer(default) }
134
+ subject = described_class.new(model, :price, :integer, limit: 4, default: '42', null: true, position: 2)
135
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 4, default: 42, null: true)
136
+ end
137
+ end
138
+ end
56
139
 
140
+ describe '#schema_attributes' do
141
+ let(:col_spec) do
57
142
  case Rails::VERSION::MAJOR
58
143
  when 4
59
144
  cast_type = ActiveRecord::Type::Integer.new(limit: 8)
60
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
145
+ ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
61
146
  else
62
147
  sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: "integer(8)", type: :integer, limit: 8)
63
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
148
+ ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
64
149
  end
150
+ end
151
+
152
+ it 'returns the attributes except name, position, and non-SQL options' do
153
+ subject = described_class.new(model, :price, :bigint, null: true, default: 0, ruby_default: -> { }, encrypt_using: -> { }, position: 2)
154
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: true, default: 0)
155
+ end
156
+
157
+ it 'aliases :bigint and :integer limit: 8' do
158
+ int8 = described_class.new(model, :price, :integer, limit: 8, null: false, position: 0)
159
+ bigint = described_class.new(model, :price, :bigint, null: false, position: 0)
160
+
161
+ expected_attributes = { type: :integer, limit: 8, null: false }
162
+ expect(int8.schema_attributes(col_spec)).to eq(expected_attributes)
163
+ expect(bigint.schema_attributes(col_spec)).to eq(expected_attributes)
164
+ end
165
+ end
65
166
 
66
- expect(subject.different_to?(subject.name, col)).to eq(false)
167
+ describe '#sql_options' do
168
+ subject { described_class.new(model, :price, :integer, limit: 4, null: true, default: 0, position: 2, encrypt_using: ->(field) { field }) }
169
+ it 'excludes non-sql options' do
170
+ expect(subject.sql_options).to eq(limit: 4, null: true, default: 0)
67
171
  end
68
172
  end
69
173
  end
@@ -47,6 +47,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
47
47
  ", id: :integer" unless Rails::VERSION::MAJOR < 5
48
48
  end
49
49
  end
50
+ let(:lock_version_limit) do
51
+ if defined?(Mysql2)
52
+ ", limit: 4"
53
+ else
54
+ ''
55
+ end
56
+ end
50
57
 
51
58
  # DeclareSchema - Migration Generator
52
59
  it 'generates migrations' do
@@ -74,7 +81,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
74
81
  expect(migrations).to(
75
82
  migrate_up(<<~EOS.strip)
76
83
  create_table :adverts, id: :bigint do |t|
77
- t.string :name, limit: 250#{charset_and_collation}
84
+ t.string :name, limit: 250, null: true#{charset_and_collation}
78
85
  end#{charset_alter_table}
79
86
  EOS
80
87
  .and migrate_down("drop_table :adverts")
@@ -107,8 +114,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
107
114
 
108
115
  expect(migrate).to(
109
116
  migrate_up(<<~EOS.strip)
110
- add_column :adverts, :body, :text#{text_limit}#{charset_and_collation}
111
- add_column :adverts, :published_at, :datetime
117
+ add_column :adverts, :body, :text#{text_limit}, null: true#{charset_and_collation}
118
+ add_column :adverts, :published_at, :datetime, null: true
112
119
  EOS
113
120
  .and migrate_down(<<~EOS.strip)
114
121
  remove_column :adverts, :body
@@ -126,7 +133,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
126
133
 
127
134
  expect(migrate).to(
128
135
  migrate_up("remove_column :adverts, :published_at").and(
129
- migrate_down("add_column :adverts, :published_at, :datetime#{datetime_precision}")
136
+ migrate_down("add_column :adverts, :published_at, :datetime#{datetime_precision}, null: true")
130
137
  )
131
138
  )
132
139
 
@@ -140,12 +147,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
140
147
 
141
148
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
142
149
  migrate_up(<<~EOS.strip)
143
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
150
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
144
151
  remove_column :adverts, :name
145
152
  EOS
146
153
  .and migrate_down(<<~EOS.strip)
147
154
  remove_column :adverts, :title
148
- add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
155
+ add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
149
156
  EOS
150
157
  )
151
158
 
@@ -165,8 +172,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
165
172
  end
166
173
 
167
174
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
168
- migrate_up("change_column :adverts, :title, :text#{text_limit}#{charset_and_collation}").and(
169
- migrate_down("change_column :adverts, :title, :string, limit: 250#{charset_and_collation}")
175
+ migrate_up("change_column :adverts, :title, :text#{text_limit}, null: true#{charset_and_collation}").and(
176
+ migrate_down("change_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}")
170
177
  )
171
178
  )
172
179
 
@@ -179,10 +186,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
179
186
 
180
187
  expect(migrate).to(
181
188
  migrate_up(<<~EOS.strip)
182
- change_column :adverts, :title, :string, limit: 250, default: "Untitled"#{charset_and_collation}
189
+ change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
183
190
  EOS
184
191
  .and migrate_down(<<~EOS.strip)
185
- change_column :adverts, :title, :string, limit: 250#{charset_and_collation}
192
+ change_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
186
193
  EOS
187
194
  )
188
195
 
@@ -195,7 +202,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
195
202
  end
196
203
 
197
204
  up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
198
- expect(migrations).to migrate_up("add_column :adverts, :price, :integer, limit: 2")
205
+ expect(migrations).to migrate_up("add_column :adverts, :price, :integer, limit: 2, null: true")
199
206
  end
200
207
 
201
208
  # Now run the migration, then change the limit:
@@ -209,24 +216,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
209
216
 
210
217
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
211
218
  migrate_up(<<~EOS.strip)
212
- change_column :adverts, :price, :integer, limit: 3
219
+ change_column :adverts, :price, :integer, limit: 3, null: true
213
220
  EOS
214
221
  .and migrate_down(<<~EOS.strip)
215
- change_column :adverts, :price, :integer, limit: 2
222
+ change_column :adverts, :price, :integer, limit: 2, null: true
216
223
  EOS
217
224
  )
218
225
 
219
- # Note that limit on a decimal column is ignored (use :scale and :precision)
220
-
221
226
  ActiveRecord::Migration.class_eval("remove_column :adverts, :price")
222
227
  class Advert < ActiveRecord::Base
223
228
  fields do
224
- price :decimal, null: true, limit: 4
229
+ price :decimal, precision: 4, scale: 1, null: true
225
230
  end
226
231
  end
227
232
 
228
- expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("add_column :adverts, :price, :decimal")
229
-
230
233
  # Limits are generally not needed for `text` fields, because by default, `text` fields will use the maximum size
231
234
  # allowed for that database type (0xffffffff for LONGTEXT in MySQL unlimited in Postgres, 1 billion in Sqlite).
232
235
  # If a `limit` is given, it will only be used in MySQL, to choose the smallest TEXT field that will accommodate
@@ -245,9 +248,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
245
248
 
246
249
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
247
250
  migrate_up(<<~EOS.strip)
248
- add_column :adverts, :price, :decimal
249
- add_column :adverts, :notes, :text, null: false#{text_limit}#{charset_and_collation}
250
- add_column :adverts, :description, :text, null: false#{', limit: 65535' if defined?(Mysql2)}#{charset_and_collation}
251
+ add_column :adverts, :price, :decimal, precision: 4, scale: 1, null: true
252
+ add_column :adverts, :notes, :text#{text_limit}, null: false#{charset_and_collation}
253
+ add_column :adverts, :description, :text#{', limit: 65535' if defined?(Mysql2)}, null: false#{charset_and_collation}
251
254
  EOS
252
255
  )
253
256
 
@@ -269,8 +272,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
269
272
 
270
273
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
271
274
  migrate_up(<<~EOS.strip)
272
- add_column :adverts, :notes, :text, null: false, limit: 4294967295#{charset_and_collation}
273
- add_column :adverts, :description, :text, null: false, limit: 255#{charset_and_collation}
275
+ add_column :adverts, :notes, :text, limit: 4294967295, null: false#{charset_and_collation}
276
+ add_column :adverts, :description, :text, limit: 255, null: false#{charset_and_collation}
274
277
  EOS
275
278
  )
276
279
 
@@ -313,7 +316,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
313
316
  change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
314
317
  EOS
315
318
  .and migrate_down(<<~EOS.strip)
316
- change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}#{charset_and_collation}
319
+ change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}, null: true#{charset_and_collation}
317
320
  EOS
318
321
  )
319
322
 
@@ -330,7 +333,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
330
333
  change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
331
334
  EOS
332
335
  .and migrate_down(<<~EOS.strip)
333
- change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}#{charset_and_collation}
336
+ change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}, null: true#{charset_and_collation}
334
337
  EOS
335
338
  )
336
339
  end
@@ -369,14 +372,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
369
372
 
370
373
  add_index :adverts, [:category_id], name: 'on_category_id'
371
374
 
372
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" if defined?(Mysql2)}
375
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" if defined?(Mysql2)}
373
376
  EOS
374
377
  .and migrate_down(<<~EOS.strip)
375
378
  remove_column :adverts, :category_id
376
379
 
377
380
  remove_index :adverts, name: :on_category_id rescue ActiveRecord::StatementInvalid
378
381
 
379
- #{"remove_foreign_key('adverts', name: '')\n" if defined?(Mysql2)}
382
+ #{"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" if defined?(Mysql2)}
380
383
  EOS
381
384
  )
382
385
 
@@ -397,8 +400,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
397
400
 
398
401
  add_index :adverts, [:c_id], name: 'on_c_id'
399
402
 
400
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
401
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
403
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
404
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
402
405
  EOS
403
406
  )
404
407
 
@@ -417,8 +420,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
417
420
  migrate_up(<<~EOS.strip)
418
421
  add_column :adverts, :category_id, :integer, limit: 8, null: false
419
422
 
420
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
421
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
423
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
424
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
422
425
  EOS
423
426
  )
424
427
 
@@ -439,8 +442,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
439
442
 
440
443
  add_index :adverts, [:category_id], name: 'my_index'
441
444
 
442
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
443
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
445
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
446
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
444
447
  EOS
445
448
  )
446
449
 
@@ -461,20 +464,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
461
464
 
462
465
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
463
466
  migrate_up(<<~EOS.strip)
464
- add_column :adverts, :created_at, :datetime
465
- add_column :adverts, :updated_at, :datetime
466
- add_column :adverts, :lock_version, :integer, null: false, default: 1
467
+ add_column :adverts, :created_at, :datetime, null: true
468
+ add_column :adverts, :updated_at, :datetime, null: true
469
+ add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
467
470
 
468
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
469
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
471
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
472
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
470
473
  EOS
471
474
  .and migrate_down(<<~EOS.strip)
472
475
  remove_column :adverts, :created_at
473
476
  remove_column :adverts, :updated_at
474
477
  remove_column :adverts, :lock_version
475
478
 
476
- #{"remove_foreign_key('adverts', name: '')\n" +
477
- "remove_foreign_key('adverts', name: '')" if defined?(Mysql2)}
479
+ #{"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" +
480
+ "remove_foreign_key(\"adverts\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
478
481
  EOS
479
482
  )
480
483
 
@@ -494,12 +497,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
494
497
 
495
498
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
496
499
  migrate_up(<<~EOS.strip)
497
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
500
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
498
501
 
499
502
  add_index :adverts, [:title], name: 'on_title'
500
503
 
501
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
502
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
504
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
505
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
503
506
  EOS
504
507
  )
505
508
 
@@ -515,12 +518,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
515
518
 
516
519
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
517
520
  migrate_up(<<~EOS.strip)
518
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
521
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
519
522
 
520
523
  add_index :adverts, [:title], unique: true, name: 'on_title'
521
524
 
522
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
523
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
525
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
526
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
524
527
  EOS
525
528
  )
526
529
 
@@ -536,12 +539,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
536
539
 
537
540
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
538
541
  migrate_up(<<~EOS.strip)
539
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
542
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
540
543
 
541
544
  add_index :adverts, [:title], name: 'my_index'
542
545
 
543
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
544
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
546
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
547
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
545
548
  EOS
546
549
  )
547
550
 
@@ -555,12 +558,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
555
558
 
556
559
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
557
560
  migrate_up(<<~EOS.strip)
558
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
561
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
559
562
 
560
563
  add_index :adverts, [:title], name: 'on_title'
561
564
 
562
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
563
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
565
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
566
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
564
567
  EOS
565
568
  )
566
569
 
@@ -574,12 +577,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
574
577
 
575
578
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
576
579
  migrate_up(<<~EOS.strip)
577
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
580
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
578
581
 
579
582
  add_index :adverts, [:title], unique: true, name: 'my_index'
580
583
 
581
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
582
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
584
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
585
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
583
586
  EOS
584
587
  )
585
588
 
@@ -593,12 +596,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
593
596
 
594
597
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
595
598
  migrate_up(<<~EOS.strip)
596
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
599
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
597
600
 
598
601
  add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
599
602
 
600
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
601
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
603
+ #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
604
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
602
605
  EOS
603
606
  )
604
607
 
@@ -627,14 +630,15 @@ RSpec.describe 'DeclareSchema Migration Generator' do
627
630
  migrate_up(<<~EOS.strip)
628
631
  rename_table :adverts, :ads
629
632
 
630
- add_column :ads, :title, :string, limit: 250#{charset_and_collation}
631
- add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}#{charset_and_collation}
633
+ add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
634
+ add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
632
635
 
633
636
  #{if defined?(SQLite3)
634
637
  "add_index :ads, [:id], unique: true, name: 'PRIMARY'\n"
635
638
  elsif defined?(Mysql2)
636
639
  "execute \"ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
637
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\nexecute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \""
640
+ "add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
641
+ "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")"
638
642
  end}
639
643
  EOS
640
644
  .and migrate_down(<<~EOS.strip)
@@ -647,8 +651,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
647
651
  "add_index :adverts, [:id], unique: true, name: 'PRIMARY'\n"
648
652
  elsif defined?(Mysql2)
649
653
  "execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
650
- "remove_foreign_key('adverts', name: '')\n" +
651
- "remove_foreign_key('adverts', name: '')"
654
+ "remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" +
655
+ "remove_foreign_key(\"adverts\", name: \"index_adverts_on_c_id\")"
652
656
  end}
653
657
  EOS
654
658
  )
@@ -680,8 +684,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
680
684
  migrate_up(<<~EOS.strip)
681
685
  rename_table :adverts, :advertisements
682
686
 
683
- add_column :advertisements, :title, :string, limit: 250#{charset_and_collation}
684
- add_column :advertisements, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}#{charset_and_collation}
687
+ add_column :advertisements, :title, :string, limit: 250, null: true#{charset_and_collation}
688
+ add_column :advertisements, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
685
689
  remove_column :advertisements, :name
686
690
 
687
691
  #{if defined?(SQLite3)
@@ -693,7 +697,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
693
697
  .and migrate_down(<<~EOS.strip)
694
698
  remove_column :advertisements, :title
695
699
  remove_column :advertisements, :body
696
- add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
700
+ add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
697
701
 
698
702
  rename_table :advertisements, :adverts
699
703
 
@@ -747,7 +751,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
747
751
  up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
748
752
  expect(migrations).to(
749
753
  migrate_up(<<~EOS.strip)
750
- add_column :adverts, :type, :string, limit: 250#{charset_and_collation}
754
+ add_column :adverts, :type, :string, limit: 250, null: true#{charset_and_collation}
751
755
 
752
756
  add_index :adverts, [:type], name: 'on_type'
753
757
  EOS
@@ -794,11 +798,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
794
798
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })).to(
795
799
  migrate_up(<<~EOS.strip)
796
800
  rename_column :adverts, :title, :name
797
- change_column :adverts, :name, :string, limit: 250, default: "No Name"#{charset_and_collation}
801
+ change_column :adverts, :name, :string, limit: 250, null: true, default: "No Name"#{charset_and_collation}
798
802
  EOS
799
803
  .and migrate_down(<<~EOS.strip)
800
804
  rename_column :adverts, :name, :title
801
- change_column :adverts, :title, :string, limit: 250, default: "Untitled"#{charset_and_collation}
805
+ change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
802
806
  EOS
803
807
  )
804
808