declare_schema 0.6.3 → 0.8.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,179 @@
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', type: :string) }
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
11
25
 
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")
19
- end
26
+ it 'raises exception on unknown field type' do
27
+ expect do
28
+ described_class.new(model, :location, :lat_long, position: 0)
29
+ end.to raise_exception(::DeclareSchema::UnknownTypeError, /:lat_long not found in /)
30
+ end
31
+ end
20
32
 
21
- expect(subject.different_to?(subject.name, col)).to eq(false)
33
+ describe '#schema_attributes' do
34
+ describe 'integer 4' do
35
+ it 'returns schema attributes' do
36
+ subject = described_class.new(model, :price, :integer, limit: 4, null: false, position: 0)
37
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 4, null: false)
38
+ end
22
39
  end
23
40
 
24
- it '#different_to should return false for bigint == bigint' do
25
- subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :bigint, null: false, position: 0)
41
+ describe 'integer 8' do
42
+ it 'returns schema attributes' do
43
+ subject = described_class.new(model, :price, :integer, limit: 8, null: true, position: 2)
44
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: true)
45
+ end
46
+ end
26
47
 
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")
48
+ describe 'bigint' do
49
+ it 'returns schema attributes' do
50
+ subject = described_class.new(model, :price, :bigint, null: false, position: 2)
51
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: false)
34
52
  end
53
+ end
35
54
 
36
- expect(subject.different_to?(subject.name, col)).to eq(false)
55
+ describe 'string' do
56
+ it 'returns schema attributes (including charset/collation iff mysql)' do
57
+ subject = described_class.new(model, :title, :string, limit: 100, null: true, charset: 'utf8mb4', position: 0)
58
+ if defined?(Mysql2)
59
+ expect(subject.schema_attributes(col_spec)).to eq(type: :string, limit: 100, null: true, charset: 'utf8mb4', collation: 'utf8mb4_bin')
60
+ else
61
+ expect(subject.schema_attributes(col_spec)).to eq(type: :string, limit: 100, null: true)
62
+ end
63
+ end
37
64
  end
38
65
 
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)
66
+ describe 'text' do
67
+ it 'returns schema attributes (including charset/collation iff mysql)' do
68
+ subject = described_class.new(model, :title, :text, limit: 200, null: true, charset: 'utf8mb4', position: 2)
69
+ if defined?(Mysql2)
70
+ expect(subject.schema_attributes(col_spec)).to eq(type: :text, limit: 255, null: true, charset: 'utf8mb4', collation: 'utf8mb4_bin')
71
+ else
72
+ expect(subject.schema_attributes(col_spec)).to eq(type: :text, null: true)
73
+ end
74
+ end
41
75
 
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")
76
+ it 'allows a default to be set unless mysql' do
77
+ if defined?(Mysql2)
78
+ expect do
79
+ described_class.new(model, :title, :text, limit: 200, null: true, default: 'none', charset: 'utf8mb4', position: 2)
80
+ end.to raise_exception(DeclareSchema::MysqlTextMayNotHaveDefault)
81
+ else
82
+ subject = described_class.new(model, :title, :text, limit: 200, null: true, default: 'none', charset: 'utf8mb4', position: 2)
83
+ expect(subject.schema_attributes(col_spec)).to eq(type: :text, null: true, default: 'none')
84
+ end
49
85
  end
50
86
 
51
- expect(subject.different_to?(subject.name, col)).to eq(false)
87
+ describe 'decimal' do
88
+ it 'allows precision: and scale:' do
89
+ subject = described_class.new(model, :quantity, :decimal, precision: 8, scale: 10, null: true, position: 3)
90
+ expect(subject.schema_attributes(col_spec)).to eq(type: :decimal, precision: 8, scale: 10, null: true)
91
+ end
92
+
93
+ it 'requires precision:' do
94
+ expect_any_instance_of(described_class).to receive(:warn).with(/precision: required for :decimal type/)
95
+ described_class.new(model, :quantity, :decimal, scale: 10, null: true, position: 3)
96
+ end
97
+
98
+ it 'requires scale:' do
99
+ expect_any_instance_of(described_class).to receive(:warn).with(/scale: required for :decimal type/)
100
+ described_class.new(model, :quantity, :decimal, precision: 8, null: true, position: 3)
101
+ end
102
+ end
103
+
104
+ [:integer, :bigint, :string, :text, :binary, :datetime, :date, :time].each do |t|
105
+ describe t.to_s do
106
+ let(:extra) { t == :string ? { limit: 100 } : {} }
107
+
108
+ it 'does not allow precision:' do
109
+ expect_any_instance_of(described_class).to receive(:warn).with(/precision: only allowed for :decimal type/)
110
+ described_class.new(model, :quantity, t, { precision: 8, null: true, position: 3 }.merge(extra))
111
+ end unless t == :datetime
112
+
113
+ it 'does not allow scale:' do
114
+ expect_any_instance_of(described_class).to receive(:warn).with(/scale: only allowed for :decimal type/)
115
+ described_class.new(model, :quantity, t, { scale: 10, null: true, position: 3 }.merge(extra))
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ describe 'datetime' do
122
+ it 'keeps type as "datetime"' do
123
+ subject = described_class.new(model, :created_at, :datetime, null: false, position: 1)
124
+ expect(subject.schema_attributes(col_spec)).to eq(type: :datetime, null: false)
125
+ end
52
126
  end
53
127
 
54
- it '#different_to should return false for bigint == int8' do
55
- subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :bigint, null: false, position: 0)
128
+ describe 'timestamp' do
129
+ it 'normalizes type to "datetime"' do
130
+ subject = described_class.new(model, :created_at, :timestamp, null: true, position: 2)
131
+ expect(subject.schema_attributes(col_spec)).to eq(type: :datetime, null: true)
132
+ end
133
+ end
56
134
 
135
+ describe 'default:' do
136
+ let(:col_spec) { double('col_spec', type: :integer) }
137
+
138
+ it 'typecasts default value' do
139
+ allow(col_spec).to receive(:type_cast_from_database) { |default| Integer(default) }
140
+ subject = described_class.new(model, :price, :integer, limit: 4, default: '42', null: true, position: 2)
141
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 4, default: 42, null: true)
142
+ end
143
+ end
144
+ end
145
+
146
+ describe '#schema_attributes' do
147
+ let(:col_spec) do
57
148
  case Rails::VERSION::MAJOR
58
149
  when 4
59
150
  cast_type = ActiveRecord::Type::Integer.new(limit: 8)
60
- col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
151
+ ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
61
152
  else
62
153
  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")
154
+ ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
64
155
  end
156
+ end
157
+
158
+ it 'returns the attributes except name, position, and non-SQL options' do
159
+ subject = described_class.new(model, :price, :bigint, null: true, default: 0, ruby_default: -> { }, encrypt_using: -> { }, position: 2)
160
+ expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: true, default: 0)
161
+ end
162
+
163
+ it 'aliases :bigint and :integer limit: 8' do
164
+ int8 = described_class.new(model, :price, :integer, limit: 8, null: false, position: 0)
165
+ bigint = described_class.new(model, :price, :bigint, null: false, position: 0)
166
+
167
+ expected_attributes = { type: :integer, limit: 8, null: false }
168
+ expect(int8.schema_attributes(col_spec)).to eq(expected_attributes)
169
+ expect(bigint.schema_attributes(col_spec)).to eq(expected_attributes)
170
+ end
171
+ end
65
172
 
66
- expect(subject.different_to?(subject.name, col)).to eq(false)
173
+ describe '#sql_options' do
174
+ subject { described_class.new(model, :price, :integer, limit: 4, null: true, default: 0, position: 2, encrypt_using: ->(field) { field }) }
175
+ it 'excludes non-sql options' do
176
+ expect(subject.sql_options).to eq(limit: 4, null: true, default: 0)
67
177
  end
68
178
  end
69
179
  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,7 +372,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
369
372
 
370
373
  add_index :adverts, [:category_id], name: 'on_category_id'
371
374
 
372
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_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
@@ -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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
401
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
421
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
443
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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,12 +464,12 @@ 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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
469
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
@@ -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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
502
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
523
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
544
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
563
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
582
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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
- #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
601
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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,15 +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
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
638
- "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_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\")"
639
642
  end}
640
643
  EOS
641
644
  .and migrate_down(<<~EOS.strip)
@@ -681,8 +684,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
681
684
  migrate_up(<<~EOS.strip)
682
685
  rename_table :adverts, :advertisements
683
686
 
684
- add_column :advertisements, :title, :string, limit: 250#{charset_and_collation}
685
- 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}
686
689
  remove_column :advertisements, :name
687
690
 
688
691
  #{if defined?(SQLite3)
@@ -694,7 +697,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
694
697
  .and migrate_down(<<~EOS.strip)
695
698
  remove_column :advertisements, :title
696
699
  remove_column :advertisements, :body
697
- add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
700
+ add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
698
701
 
699
702
  rename_table :advertisements, :adverts
700
703
 
@@ -748,7 +751,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
748
751
  up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
749
752
  expect(migrations).to(
750
753
  migrate_up(<<~EOS.strip)
751
- add_column :adverts, :type, :string, limit: 250#{charset_and_collation}
754
+ add_column :adverts, :type, :string, limit: 250, null: true#{charset_and_collation}
752
755
 
753
756
  add_index :adverts, [:type], name: 'on_type'
754
757
  EOS
@@ -795,11 +798,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
795
798
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })).to(
796
799
  migrate_up(<<~EOS.strip)
797
800
  rename_column :adverts, :title, :name
798
- 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}
799
802
  EOS
800
803
  .and migrate_down(<<~EOS.strip)
801
804
  rename_column :adverts, :name, :title
802
- 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}
803
806
  EOS
804
807
  )
805
808