declare_schema 0.5.0 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/declare_schema_build.yml +60 -0
- data/.gitignore +1 -0
- data/Appraisals +21 -4
- data/CHANGELOG.md +46 -0
- data/Gemfile +1 -2
- data/Gemfile.lock +4 -6
- data/README.md +3 -3
- data/Rakefile +17 -4
- data/bin/declare_schema +1 -1
- data/declare_schema.gemspec +1 -1
- data/gemfiles/rails_4_mysql.gemfile +22 -0
- data/gemfiles/{rails_4.gemfile → rails_4_sqlite.gemfile} +1 -2
- data/gemfiles/rails_5_mysql.gemfile +22 -0
- data/gemfiles/{rails_5.gemfile → rails_5_sqlite.gemfile} +1 -2
- data/gemfiles/rails_6_mysql.gemfile +22 -0
- data/gemfiles/{rails_6.gemfile → rails_6_sqlite.gemfile} +2 -3
- data/lib/declare_schema/command.rb +10 -3
- data/lib/declare_schema/model.rb +1 -1
- data/lib/declare_schema/model/field_spec.rb +18 -14
- data/lib/declare_schema/model/foreign_key_definition.rb +36 -25
- data/lib/declare_schema/model/table_options_definition.rb +8 -6
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migrator.rb +80 -34
- data/spec/lib/declare_schema/field_spec_spec.rb +69 -0
- data/spec/lib/declare_schema/generator_spec.rb +4 -2
- data/spec/lib/declare_schema/interactive_primary_key_spec.rb +8 -2
- data/spec/lib/declare_schema/migration_generator_spec.rb +286 -158
- data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +93 -0
- data/spec/lib/declare_schema/model/index_definition_spec.rb +4 -5
- data/spec/lib/declare_schema/model/table_options_definition_spec.rb +19 -29
- data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +17 -22
- data/spec/support/acceptance_spec_helpers.rb +3 -3
- metadata +15 -10
- data/.travis.yml +0 -37
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe 'DeclareSchema Model FieldSpec' do
|
4
|
+
before do
|
5
|
+
load File.expand_path('prepare_testapp.rb', __dir__)
|
6
|
+
end
|
7
|
+
|
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)
|
11
|
+
|
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
|
20
|
+
|
21
|
+
expect(subject.different_to?(subject.name, col)).to eq(false)
|
22
|
+
end
|
23
|
+
|
24
|
+
it '#different_to should return false for bigint == bigint' do
|
25
|
+
subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :bigint, null: false, position: 0)
|
26
|
+
|
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")
|
34
|
+
end
|
35
|
+
|
36
|
+
expect(subject.different_to?(subject.name, col)).to eq(false)
|
37
|
+
end
|
38
|
+
|
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)
|
41
|
+
|
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")
|
49
|
+
end
|
50
|
+
|
51
|
+
expect(subject.different_to?(subject.name, col)).to eq(false)
|
52
|
+
end
|
53
|
+
|
54
|
+
it '#different_to should return false for bigint == int8' do
|
55
|
+
subject = DeclareSchema::Model::FieldSpec.new(Object, :price, :bigint, null: false, position: 0)
|
56
|
+
|
57
|
+
case Rails::VERSION::MAJOR
|
58
|
+
when 4
|
59
|
+
cast_type = ActiveRecord::Type::Integer.new(limit: 8)
|
60
|
+
col = ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
|
61
|
+
else
|
62
|
+
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")
|
64
|
+
end
|
65
|
+
|
66
|
+
expect(subject.different_to?(subject.name, col)).to eq(false)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -30,7 +30,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
30
30
|
case Rails::VERSION::MAJOR
|
31
31
|
when 4, 5
|
32
32
|
expect_test_definition_to_eq('alpha/beta', <<~EOS)
|
33
|
-
require
|
33
|
+
require "test_helper"
|
34
34
|
|
35
35
|
class Alpha::BetaTest < ActiveSupport::TestCase
|
36
36
|
# test "the truth" do
|
@@ -101,7 +101,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
101
101
|
|
102
102
|
expect(File.exist?('db/schema.rb')).to be_truthy
|
103
103
|
|
104
|
-
|
104
|
+
if defined?(SQLite3)
|
105
|
+
expect(File.exist?("db/development.sqlite3") || File.exist?("db/test.sqlite3")).to be_truthy
|
106
|
+
end
|
105
107
|
|
106
108
|
module Alpha; end
|
107
109
|
require 'alpha/beta'
|
@@ -1,5 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'rails'
|
4
|
+
begin
|
5
|
+
require 'mysql2'
|
6
|
+
rescue LoadError
|
7
|
+
end
|
8
|
+
|
3
9
|
RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
4
10
|
before do
|
5
11
|
load File.expand_path('prepare_testapp.rb', __dir__)
|
@@ -31,8 +37,8 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
|
31
37
|
|
32
38
|
### migrate to
|
33
39
|
|
34
|
-
if Rails::VERSION::MAJOR >= 5
|
35
|
-
#
|
40
|
+
if Rails::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
|
41
|
+
# replace custom primary_key
|
36
42
|
class Foo < ActiveRecord::Base
|
37
43
|
fields do
|
38
44
|
end
|
@@ -1,12 +1,53 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rails'
|
4
|
+
begin
|
5
|
+
require 'mysql2'
|
6
|
+
rescue LoadError
|
7
|
+
end
|
4
8
|
|
5
9
|
RSpec.describe 'DeclareSchema Migration Generator' do
|
6
10
|
before do
|
7
11
|
load File.expand_path('prepare_testapp.rb', __dir__)
|
8
12
|
end
|
9
13
|
|
14
|
+
let(:charset_alter_table) do
|
15
|
+
if defined?(Mysql2)
|
16
|
+
<<~EOS
|
17
|
+
|
18
|
+
|
19
|
+
execute "ALTER TABLE `adverts` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
|
20
|
+
EOS
|
21
|
+
end
|
22
|
+
end
|
23
|
+
let(:text_limit) do
|
24
|
+
if defined?(Mysql2)
|
25
|
+
", limit: 4294967295"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
let(:charset_and_collation) do
|
29
|
+
if defined?(Mysql2)
|
30
|
+
', charset: "utf8mb4", collation: "utf8mb4_bin"'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
let(:datetime_precision) do
|
34
|
+
if defined?(Mysql2) && Rails::VERSION::MAJOR >= 5
|
35
|
+
', precision: 0'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
let(:table_options) do
|
39
|
+
if defined?(Mysql2)
|
40
|
+
", options: \"#{'ENGINE=InnoDB ' if Rails::VERSION::MAJOR == 5}DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\"" +
|
41
|
+
if Rails::VERSION::MAJOR >= 6
|
42
|
+
', charset: "utf8mb4", collation: "utf8mb4_bin"'
|
43
|
+
else
|
44
|
+
''
|
45
|
+
end
|
46
|
+
else
|
47
|
+
", id: :integer" unless Rails::VERSION::MAJOR < 5
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
10
51
|
# DeclareSchema - Migration Generator
|
11
52
|
it 'generates migrations' do
|
12
53
|
## The migration generator -- introduction
|
@@ -25,7 +66,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
25
66
|
|
26
67
|
class Advert < ActiveRecord::Base
|
27
68
|
fields do
|
28
|
-
name :string, limit:
|
69
|
+
name :string, limit: 250, null: true
|
29
70
|
end
|
30
71
|
end
|
31
72
|
|
@@ -33,8 +74,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
33
74
|
expect(migrations).to(
|
34
75
|
migrate_up(<<~EOS.strip)
|
35
76
|
create_table :adverts, id: :bigint do |t|
|
36
|
-
t.string :name, limit:
|
37
|
-
end
|
77
|
+
t.string :name, limit: 250#{charset_and_collation}
|
78
|
+
end#{charset_alter_table}
|
38
79
|
EOS
|
39
80
|
.and migrate_down("drop_table :adverts")
|
40
81
|
)
|
@@ -44,14 +85,18 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
44
85
|
expect(Advert.columns.map(&:name)).to eq(["id", "name"])
|
45
86
|
|
46
87
|
if Rails::VERSION::MAJOR < 5
|
47
|
-
# Rails 4
|
88
|
+
# Rails 4 drivers don't always create PK properly. Fix that by dropping and recreating.
|
48
89
|
ActiveRecord::Base.connection.execute("drop table adverts")
|
49
|
-
|
90
|
+
if defined?(Mysql2)
|
91
|
+
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(250)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin")
|
92
|
+
else
|
93
|
+
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(250))")
|
94
|
+
end
|
50
95
|
end
|
51
96
|
|
52
97
|
class Advert < ActiveRecord::Base
|
53
98
|
fields do
|
54
|
-
name :string, limit:
|
99
|
+
name :string, limit: 250, null: true
|
55
100
|
body :text, null: true
|
56
101
|
published_at :datetime, null: true
|
57
102
|
end
|
@@ -62,7 +107,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
62
107
|
|
63
108
|
expect(migrate).to(
|
64
109
|
migrate_up(<<~EOS.strip)
|
65
|
-
add_column :adverts, :body, :text
|
110
|
+
add_column :adverts, :body, :text#{text_limit}#{charset_and_collation}
|
66
111
|
add_column :adverts, :published_at, :datetime
|
67
112
|
EOS
|
68
113
|
.and migrate_down(<<~EOS.strip)
|
@@ -74,33 +119,33 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
74
119
|
Advert.field_specs.clear # not normally needed
|
75
120
|
class Advert < ActiveRecord::Base
|
76
121
|
fields do
|
77
|
-
name :string, limit:
|
122
|
+
name :string, limit: 250, null: true
|
78
123
|
body :text, null: true
|
79
124
|
end
|
80
125
|
end
|
81
126
|
|
82
127
|
expect(migrate).to(
|
83
128
|
migrate_up("remove_column :adverts, :published_at").and(
|
84
|
-
migrate_down("add_column :adverts, :published_at, :datetime")
|
129
|
+
migrate_down("add_column :adverts, :published_at, :datetime#{datetime_precision}")
|
85
130
|
)
|
86
131
|
)
|
87
132
|
|
88
133
|
nuke_model_class(Advert)
|
89
134
|
class Advert < ActiveRecord::Base
|
90
135
|
fields do
|
91
|
-
title :string, limit:
|
136
|
+
title :string, limit: 250, null: true
|
92
137
|
body :text, null: true
|
93
138
|
end
|
94
139
|
end
|
95
140
|
|
96
141
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
97
142
|
migrate_up(<<~EOS.strip)
|
98
|
-
add_column :adverts, :title, :string, limit:
|
143
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
99
144
|
remove_column :adverts, :name
|
100
145
|
EOS
|
101
146
|
.and migrate_down(<<~EOS.strip)
|
102
147
|
remove_column :adverts, :title
|
103
|
-
add_column :adverts, :name, :string, limit:
|
148
|
+
add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
|
104
149
|
EOS
|
105
150
|
)
|
106
151
|
|
@@ -120,24 +165,24 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
120
165
|
end
|
121
166
|
|
122
167
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
123
|
-
migrate_up("change_column :adverts, :title, :text").and(
|
124
|
-
migrate_down("change_column :adverts, :title, :string, limit:
|
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}")
|
125
170
|
)
|
126
171
|
)
|
127
172
|
|
128
173
|
class Advert < ActiveRecord::Base
|
129
174
|
fields do
|
130
|
-
title :string, default: "Untitled", limit:
|
175
|
+
title :string, default: "Untitled", limit: 250, null: true
|
131
176
|
body :text, null: true
|
132
177
|
end
|
133
178
|
end
|
134
179
|
|
135
180
|
expect(migrate).to(
|
136
181
|
migrate_up(<<~EOS.strip)
|
137
|
-
change_column :adverts, :title, :string, limit:
|
182
|
+
change_column :adverts, :title, :string, limit: 250, default: "Untitled"#{charset_and_collation}
|
138
183
|
EOS
|
139
184
|
.and migrate_down(<<~EOS.strip)
|
140
|
-
change_column :adverts, :title, :string, limit:
|
185
|
+
change_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
141
186
|
EOS
|
142
187
|
)
|
143
188
|
|
@@ -187,7 +232,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
187
232
|
# If a `limit` is given, it will only be used in MySQL, to choose the smallest TEXT field that will accommodate
|
188
233
|
# that limit (0xff for TINYTEXT, 0xffff for TEXT, 0xffffff for MEDIUMTEXT, 0xffffffff for LONGTEXT).
|
189
234
|
|
190
|
-
|
235
|
+
if defined?(SQLite3)
|
236
|
+
expect(::DeclareSchema::Model::FieldSpec.mysql_text_limits?).to be_falsey
|
237
|
+
end
|
238
|
+
|
191
239
|
class Advert < ActiveRecord::Base
|
192
240
|
fields do
|
193
241
|
notes :text
|
@@ -198,113 +246,107 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
198
246
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
199
247
|
migrate_up(<<~EOS.strip)
|
200
248
|
add_column :adverts, :price, :decimal
|
201
|
-
add_column :adverts, :notes, :text, null: false
|
202
|
-
add_column :adverts, :description, :text, null: false
|
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}
|
203
251
|
EOS
|
204
252
|
)
|
205
253
|
|
206
|
-
# (There is no limit on `add_column ... :description` above since these tests are run against SQLite.)
|
207
|
-
|
208
254
|
Advert.field_specs.delete :price
|
209
255
|
Advert.field_specs.delete :notes
|
210
256
|
Advert.field_specs.delete :description
|
211
257
|
|
212
258
|
# In MySQL, limits are applied, rounded up:
|
213
259
|
|
214
|
-
|
215
|
-
|
216
|
-
class Advert < ActiveRecord::Base
|
217
|
-
fields do
|
218
|
-
notes :text
|
219
|
-
description :text, limit: 200
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
224
|
-
migrate_up(<<~EOS.strip)
|
225
|
-
add_column :adverts, :notes, :text, null: false, limit: 4294967295
|
226
|
-
add_column :adverts, :description, :text, null: false, limit: 255
|
227
|
-
EOS
|
228
|
-
)
|
229
|
-
|
230
|
-
Advert.field_specs.delete :notes
|
231
|
-
|
232
|
-
# Limits that are too high for MySQL will raise an exception.
|
260
|
+
if defined?(Mysql2)
|
261
|
+
expect(::DeclareSchema::Model::FieldSpec.mysql_text_limits?).to be_truthy
|
233
262
|
|
234
|
-
::DeclareSchema::Model::FieldSpec::instance_variable_set(:@mysql_text_limits, true)
|
235
|
-
expect(::DeclareSchema::Model::FieldSpec.mysql_text_limits?).to be_truthy
|
236
|
-
expect do
|
237
263
|
class Advert < ActiveRecord::Base
|
238
264
|
fields do
|
239
265
|
notes :text
|
240
|
-
description :text, limit:
|
266
|
+
description :text, limit: 250
|
241
267
|
end
|
242
268
|
end
|
243
|
-
end.to raise_exception(ArgumentError, "limit of 4294967296 is too large for MySQL")
|
244
269
|
|
245
|
-
|
270
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
271
|
+
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}
|
274
|
+
EOS
|
275
|
+
)
|
246
276
|
|
247
|
-
|
277
|
+
Advert.field_specs.delete :notes
|
248
278
|
|
249
|
-
|
279
|
+
# Limits that are too high for MySQL will raise an exception.
|
250
280
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
281
|
+
expect do
|
282
|
+
class Advert < ActiveRecord::Base
|
283
|
+
fields do
|
284
|
+
notes :text
|
285
|
+
description :text, limit: 0x1_0000_0000
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end.to raise_exception(ArgumentError, "limit of 4294967296 is too large for MySQL")
|
258
289
|
|
259
|
-
|
290
|
+
Advert.field_specs.delete :notes
|
260
291
|
|
261
|
-
|
262
|
-
fields do
|
263
|
-
description :text
|
264
|
-
end
|
265
|
-
end
|
292
|
+
# And in MySQL, unstated text limits are treated as the maximum (LONGTEXT) limit.
|
266
293
|
|
267
|
-
|
268
|
-
migrate_up(<<~EOS.strip)
|
269
|
-
change_column :adverts, :description, :text, limit: 4294967295, null: false
|
270
|
-
EOS
|
271
|
-
.and migrate_down(<<~EOS.strip)
|
272
|
-
change_column :adverts, :description, :text
|
273
|
-
EOS
|
274
|
-
)
|
294
|
+
# To start, we'll set the database schema for `description` to match the above limit of 250.
|
275
295
|
|
276
|
-
|
277
|
-
|
296
|
+
Advert.connection.execute "ALTER TABLE adverts ADD COLUMN description TINYTEXT"
|
297
|
+
Advert.connection.schema_cache.clear!
|
298
|
+
Advert.reset_column_information
|
299
|
+
expect(Advert.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables).
|
300
|
+
to eq(["adverts"])
|
301
|
+
expect(Advert.columns.map(&:name)).to eq(["id", "body", "title", "description"])
|
278
302
|
|
279
|
-
|
303
|
+
# Now migrate to an unstated text limit:
|
280
304
|
|
281
|
-
|
282
|
-
|
283
|
-
|
305
|
+
class Advert < ActiveRecord::Base
|
306
|
+
fields do
|
307
|
+
description :text
|
308
|
+
end
|
284
309
|
end
|
285
|
-
end
|
286
310
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
311
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
312
|
+
migrate_up(<<~EOS.strip)
|
313
|
+
change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
|
314
|
+
EOS
|
315
|
+
.and migrate_down(<<~EOS.strip)
|
316
|
+
change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}#{charset_and_collation}
|
317
|
+
EOS
|
318
|
+
)
|
319
|
+
|
320
|
+
# And migrate to a stated text limit that is the same as the unstated one:
|
321
|
+
|
322
|
+
class Advert < ActiveRecord::Base
|
323
|
+
fields do
|
324
|
+
description :text, limit: 0xffffffff
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
329
|
+
migrate_up(<<~EOS.strip)
|
330
|
+
change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
|
331
|
+
EOS
|
332
|
+
.and migrate_down(<<~EOS.strip)
|
333
|
+
change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}#{charset_and_collation}
|
334
|
+
EOS
|
335
|
+
)
|
336
|
+
end
|
296
337
|
|
297
338
|
Advert.field_specs.clear
|
298
339
|
Advert.connection.schema_cache.clear!
|
299
340
|
Advert.reset_column_information
|
300
341
|
class Advert < ActiveRecord::Base
|
301
342
|
fields do
|
302
|
-
name :string, limit:
|
343
|
+
name :string, limit: 250, null: true
|
303
344
|
end
|
304
345
|
end
|
305
346
|
|
306
347
|
up = Generators::DeclareSchema::Migration::Migrator.run.first
|
307
348
|
ActiveRecord::Migration.class_eval up
|
349
|
+
|
308
350
|
Advert.connection.schema_cache.clear!
|
309
351
|
Advert.reset_column_information
|
310
352
|
|
@@ -316,7 +358,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
316
358
|
class Category < ActiveRecord::Base; end
|
317
359
|
class Advert < ActiveRecord::Base
|
318
360
|
fields do
|
319
|
-
name :string, limit:
|
361
|
+
name :string, limit: 250, null: true
|
320
362
|
end
|
321
363
|
belongs_to :category
|
322
364
|
end
|
@@ -326,11 +368,15 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
326
368
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
327
369
|
|
328
370
|
add_index :adverts, [:category_id], name: 'on_category_id'
|
371
|
+
|
372
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" if defined?(Mysql2)}
|
329
373
|
EOS
|
330
374
|
.and migrate_down(<<~EOS.strip)
|
331
375
|
remove_column :adverts, :category_id
|
332
376
|
|
333
377
|
remove_index :adverts, name: :on_category_id rescue ActiveRecord::StatementInvalid
|
378
|
+
|
379
|
+
#{"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" if defined?(Mysql2)}
|
334
380
|
EOS
|
335
381
|
)
|
336
382
|
|
@@ -350,6 +396,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
350
396
|
add_column :adverts, :c_id, :integer, limit: 8, null: false
|
351
397
|
|
352
398
|
add_index :adverts, [:c_id], name: 'on_c_id'
|
399
|
+
|
400
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
401
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
353
402
|
EOS
|
354
403
|
)
|
355
404
|
|
@@ -367,6 +416,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
367
416
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
368
417
|
migrate_up(<<~EOS.strip)
|
369
418
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
419
|
+
|
420
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
421
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
370
422
|
EOS
|
371
423
|
)
|
372
424
|
|
@@ -386,6 +438,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
386
438
|
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
387
439
|
|
388
440
|
add_index :adverts, [:category_id], name: 'my_index'
|
441
|
+
|
442
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
443
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
389
444
|
EOS
|
390
445
|
)
|
391
446
|
|
@@ -409,11 +464,17 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
409
464
|
add_column :adverts, :created_at, :datetime
|
410
465
|
add_column :adverts, :updated_at, :datetime
|
411
466
|
add_column :adverts, :lock_version, :integer, null: false, default: 1
|
467
|
+
|
468
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
469
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
412
470
|
EOS
|
413
471
|
.and migrate_down(<<~EOS.strip)
|
414
472
|
remove_column :adverts, :created_at
|
415
473
|
remove_column :adverts, :updated_at
|
416
474
|
remove_column :adverts, :lock_version
|
475
|
+
|
476
|
+
#{"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" +
|
477
|
+
"remove_foreign_key(\"adverts\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
417
478
|
EOS
|
418
479
|
)
|
419
480
|
|
@@ -427,15 +488,18 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
427
488
|
|
428
489
|
class Advert < ActiveRecord::Base
|
429
490
|
fields do
|
430
|
-
title :string, index: true, limit:
|
491
|
+
title :string, index: true, limit: 250, null: true
|
431
492
|
end
|
432
493
|
end
|
433
494
|
|
434
495
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
435
496
|
migrate_up(<<~EOS.strip)
|
436
|
-
add_column :adverts, :title, :string, limit:
|
497
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
437
498
|
|
438
499
|
add_index :adverts, [:title], name: 'on_title'
|
500
|
+
|
501
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
502
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
439
503
|
EOS
|
440
504
|
)
|
441
505
|
|
@@ -445,15 +509,18 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
445
509
|
|
446
510
|
class Advert < ActiveRecord::Base
|
447
511
|
fields do
|
448
|
-
title :string, index: true, unique: true, null: true, limit:
|
512
|
+
title :string, index: true, unique: true, null: true, limit: 250
|
449
513
|
end
|
450
514
|
end
|
451
515
|
|
452
516
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
453
517
|
migrate_up(<<~EOS.strip)
|
454
|
-
add_column :adverts, :title, :string, limit:
|
518
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
455
519
|
|
456
520
|
add_index :adverts, [:title], unique: true, name: 'on_title'
|
521
|
+
|
522
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
523
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
457
524
|
EOS
|
458
525
|
)
|
459
526
|
|
@@ -463,15 +530,18 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
463
530
|
|
464
531
|
class Advert < ActiveRecord::Base
|
465
532
|
fields do
|
466
|
-
title :string, index: 'my_index', limit:
|
533
|
+
title :string, index: 'my_index', limit: 250, null: true
|
467
534
|
end
|
468
535
|
end
|
469
536
|
|
470
537
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
471
538
|
migrate_up(<<~EOS.strip)
|
472
|
-
add_column :adverts, :title, :string, limit:
|
539
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
473
540
|
|
474
541
|
add_index :adverts, [:title], name: 'my_index'
|
542
|
+
|
543
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
544
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
475
545
|
EOS
|
476
546
|
)
|
477
547
|
|
@@ -485,9 +555,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
485
555
|
|
486
556
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
487
557
|
migrate_up(<<~EOS.strip)
|
488
|
-
add_column :adverts, :title, :string, limit:
|
558
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
489
559
|
|
490
560
|
add_index :adverts, [:title], name: 'on_title'
|
561
|
+
|
562
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
563
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
491
564
|
EOS
|
492
565
|
)
|
493
566
|
|
@@ -501,9 +574,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
501
574
|
|
502
575
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
503
576
|
migrate_up(<<~EOS.strip)
|
504
|
-
add_column :adverts, :title, :string, limit:
|
577
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
505
578
|
|
506
579
|
add_index :adverts, [:title], unique: true, name: 'my_index'
|
580
|
+
|
581
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
582
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
507
583
|
EOS
|
508
584
|
)
|
509
585
|
|
@@ -517,9 +593,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
517
593
|
|
518
594
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
519
595
|
migrate_up(<<~EOS.strip)
|
520
|
-
add_column :adverts, :title, :string, limit:
|
596
|
+
add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
|
521
597
|
|
522
598
|
add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
|
599
|
+
|
600
|
+
#{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
601
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
|
523
602
|
EOS
|
524
603
|
)
|
525
604
|
|
@@ -536,7 +615,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
536
615
|
class Advert < ActiveRecord::Base
|
537
616
|
self.table_name = "ads"
|
538
617
|
fields do
|
539
|
-
title :string, limit:
|
618
|
+
title :string, limit: 250, null: true
|
540
619
|
body :text, null: true
|
541
620
|
end
|
542
621
|
end
|
@@ -548,10 +627,16 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
548
627
|
migrate_up(<<~EOS.strip)
|
549
628
|
rename_table :adverts, :ads
|
550
629
|
|
551
|
-
add_column :ads, :title, :string, limit:
|
552
|
-
add_column :ads, :body, :text
|
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}
|
553
632
|
|
554
|
-
|
633
|
+
#{if defined?(SQLite3)
|
634
|
+
"add_index :ads, [:id], unique: true, name: 'PRIMARY'\n"
|
635
|
+
elsif defined?(Mysql2)
|
636
|
+
"execute \"ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
|
637
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
|
638
|
+
"add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")"
|
639
|
+
end}
|
555
640
|
EOS
|
556
641
|
.and migrate_down(<<~EOS.strip)
|
557
642
|
remove_column :ads, :title
|
@@ -559,14 +644,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
559
644
|
|
560
645
|
rename_table :ads, :adverts
|
561
646
|
|
562
|
-
|
647
|
+
#{if defined?(SQLite3)
|
648
|
+
"add_index :adverts, [:id], unique: true, name: 'PRIMARY'\n"
|
649
|
+
elsif defined?(Mysql2)
|
650
|
+
"execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
|
651
|
+
"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" +
|
652
|
+
"remove_foreign_key(\"adverts\", name: \"index_adverts_on_c_id\")"
|
653
|
+
end}
|
563
654
|
EOS
|
564
655
|
)
|
565
656
|
|
566
657
|
# Set the table name back to what it should be and confirm we're in sync:
|
567
658
|
|
568
|
-
Advert
|
569
|
-
|
659
|
+
nuke_model_class(Advert)
|
660
|
+
|
570
661
|
class Advert < ActiveRecord::Base
|
571
662
|
self.table_name = "adverts"
|
572
663
|
end
|
@@ -581,7 +672,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
581
672
|
|
582
673
|
class Advertisement < ActiveRecord::Base
|
583
674
|
fields do
|
584
|
-
title :string, limit:
|
675
|
+
title :string, limit: 250, null: true
|
585
676
|
body :text, null: true
|
586
677
|
end
|
587
678
|
end
|
@@ -590,20 +681,28 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
590
681
|
migrate_up(<<~EOS.strip)
|
591
682
|
rename_table :adverts, :advertisements
|
592
683
|
|
593
|
-
add_column :advertisements, :title, :string, limit:
|
594
|
-
add_column :advertisements, :body, :text
|
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}
|
595
686
|
remove_column :advertisements, :name
|
596
687
|
|
597
|
-
|
688
|
+
#{if defined?(SQLite3)
|
689
|
+
"add_index :advertisements, [:id], unique: true, name: 'PRIMARY'"
|
690
|
+
elsif defined?(Mysql2)
|
691
|
+
"execute \"ALTER TABLE advertisements DROP PRIMARY KEY, ADD PRIMARY KEY (id)\""
|
692
|
+
end}
|
598
693
|
EOS
|
599
694
|
.and migrate_down(<<~EOS.strip)
|
600
695
|
remove_column :advertisements, :title
|
601
696
|
remove_column :advertisements, :body
|
602
|
-
add_column :adverts, :name, :string, limit:
|
697
|
+
add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
|
603
698
|
|
604
699
|
rename_table :advertisements, :adverts
|
605
700
|
|
606
|
-
|
701
|
+
#{if defined?(SQLite3)
|
702
|
+
"add_index :adverts, [:id], unique: true, name: 'PRIMARY'"
|
703
|
+
elsif defined?(Mysql2)
|
704
|
+
"execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\""
|
705
|
+
end}
|
607
706
|
EOS
|
608
707
|
)
|
609
708
|
|
@@ -615,23 +714,15 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
615
714
|
|
616
715
|
# Dropping tables is where the automatic down-migration really comes in handy:
|
617
716
|
|
618
|
-
rails4_table_create = <<~EOS.strip
|
619
|
-
create_table "adverts", force: :cascade do |t|
|
620
|
-
t.string "name", limit: 255
|
621
|
-
end
|
622
|
-
EOS
|
623
|
-
|
624
|
-
rails5_table_create = <<~EOS.strip
|
625
|
-
create_table "adverts", id: :integer, force: :cascade do |t|
|
626
|
-
t.string "name", limit: 255
|
627
|
-
end
|
628
|
-
EOS
|
629
|
-
|
630
717
|
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
631
718
|
migrate_up(<<~EOS.strip)
|
632
719
|
drop_table :adverts
|
633
720
|
EOS
|
634
|
-
.and migrate_down(
|
721
|
+
.and migrate_down(<<~EOS.strip)
|
722
|
+
create_table "adverts"#{table_options}, force: :cascade do |t|
|
723
|
+
t.string "name", limit: 250#{charset_and_collation}
|
724
|
+
end
|
725
|
+
EOS
|
635
726
|
)
|
636
727
|
|
637
728
|
## STI
|
@@ -643,7 +734,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
643
734
|
class Advert < ActiveRecord::Base
|
644
735
|
fields do
|
645
736
|
body :text, null: true
|
646
|
-
title :string, default: "Untitled", limit:
|
737
|
+
title :string, default: "Untitled", limit: 250, null: true
|
647
738
|
end
|
648
739
|
end
|
649
740
|
up = Generators::DeclareSchema::Migration::Migrator.run.first
|
@@ -657,7 +748,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
657
748
|
up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
|
658
749
|
expect(migrations).to(
|
659
750
|
migrate_up(<<~EOS.strip)
|
660
|
-
add_column :adverts, :type, :string, limit:
|
751
|
+
add_column :adverts, :type, :string, limit: 250#{charset_and_collation}
|
661
752
|
|
662
753
|
add_index :adverts, [:type], name: 'on_type'
|
663
754
|
EOS
|
@@ -696,7 +787,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
696
787
|
|
697
788
|
class Advert < ActiveRecord::Base
|
698
789
|
fields do
|
699
|
-
name :string, default: "No Name", limit:
|
790
|
+
name :string, default: "No Name", limit: 250, null: true
|
700
791
|
body :text, null: true
|
701
792
|
end
|
702
793
|
end
|
@@ -704,11 +795,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
704
795
|
expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })).to(
|
705
796
|
migrate_up(<<~EOS.strip)
|
706
797
|
rename_column :adverts, :title, :name
|
707
|
-
change_column :adverts, :name, :string, limit:
|
798
|
+
change_column :adverts, :name, :string, limit: 250, default: "No Name"#{charset_and_collation}
|
708
799
|
EOS
|
709
800
|
.and migrate_down(<<~EOS.strip)
|
710
801
|
rename_column :adverts, :name, :title
|
711
|
-
change_column :adverts, :title, :string, limit:
|
802
|
+
change_column :adverts, :title, :string, limit: 250, default: "Untitled"#{charset_and_collation}
|
712
803
|
EOS
|
713
804
|
)
|
714
805
|
|
@@ -717,7 +808,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
717
808
|
nuke_model_class(Advert)
|
718
809
|
class Ad < ActiveRecord::Base
|
719
810
|
fields do
|
720
|
-
title :string, default: "Untitled", limit:
|
811
|
+
title :string, default: "Untitled", limit: 250
|
721
812
|
body :text, null: true
|
722
813
|
created_at :datetime
|
723
814
|
end
|
@@ -728,16 +819,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
728
819
|
rename_table :adverts, :ads
|
729
820
|
|
730
821
|
add_column :ads, :created_at, :datetime, null: false
|
731
|
-
change_column :ads, :title, :string, limit:
|
822
|
+
change_column :ads, :title, :string, limit: 250, null: false, default: \"Untitled\"#{charset_and_collation}
|
732
823
|
|
733
|
-
|
824
|
+
#{if defined?(SQLite3)
|
825
|
+
"add_index :ads, [:id], unique: true, name: 'PRIMARY'"
|
826
|
+
elsif defined?(Mysql2)
|
827
|
+
'execute "ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)"'
|
828
|
+
end}
|
734
829
|
EOS
|
735
830
|
)
|
736
831
|
|
737
832
|
class Advert < ActiveRecord::Base
|
738
833
|
fields do
|
739
834
|
body :text, null: true
|
740
|
-
title :string, default: "Untitled", limit:
|
835
|
+
title :string, default: "Untitled", limit: 250, null: true
|
741
836
|
end
|
742
837
|
end
|
743
838
|
|
@@ -758,7 +853,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
758
853
|
migrate_up(<<~EOS.strip)
|
759
854
|
rename_column :adverts, :id, :advert_id
|
760
855
|
|
761
|
-
|
856
|
+
#{if defined?(SQLite3)
|
857
|
+
"add_index :adverts, [:advert_id], unique: true, name: 'PRIMARY'"
|
858
|
+
elsif defined?(Mysql2)
|
859
|
+
'execute "ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (advert_id)"'
|
860
|
+
end}
|
762
861
|
EOS
|
763
862
|
)
|
764
863
|
|
@@ -771,7 +870,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
771
870
|
|
772
871
|
class User < ActiveRecord::Base
|
773
872
|
fields do
|
774
|
-
company :string, limit:
|
873
|
+
company :string, limit: 250, ruby_default: -> { "BigCorp" }
|
775
874
|
end
|
776
875
|
end
|
777
876
|
expect(User.field_specs.keys).to eq(['company'])
|
@@ -790,7 +889,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
790
889
|
expect(Ad).to receive(:validates).with(:company, presence: true, uniqueness: { case_sensitive: false })
|
791
890
|
class Ad < ActiveRecord::Base
|
792
891
|
fields do
|
793
|
-
company :string, limit:
|
892
|
+
company :string, limit: 250, index: true, unique: true, validates: { presence: true, uniqueness: { case_sensitive: false } }
|
794
893
|
end
|
795
894
|
self.primary_key = "advert_id"
|
796
895
|
end
|
@@ -828,10 +927,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
828
927
|
it 'converts defaults with .to_yaml' do
|
829
928
|
class Ad < ActiveRecord::Base
|
830
929
|
fields do
|
831
|
-
allow_list :string, limit:
|
832
|
-
allow_hash :string, limit:
|
833
|
-
allow_string :string, limit:
|
834
|
-
allow_null :string, limit:
|
930
|
+
allow_list :string, limit: 250, serialize: true, null: true, default: []
|
931
|
+
allow_hash :string, limit: 250, serialize: true, null: true, default: {}
|
932
|
+
allow_string :string, limit: 250, serialize: true, null: true, default: ['abc']
|
933
|
+
allow_null :string, limit: 250, serialize: true, null: true, default: nil
|
835
934
|
end
|
836
935
|
end
|
837
936
|
|
@@ -846,7 +945,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
846
945
|
it 'allows serialize: Array' do
|
847
946
|
class Ad < ActiveRecord::Base
|
848
947
|
fields do
|
849
|
-
allow_list :string, limit:
|
948
|
+
allow_list :string, limit: 250, serialize: Array, null: true
|
850
949
|
end
|
851
950
|
end
|
852
951
|
|
@@ -856,10 +955,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
856
955
|
it 'allows Array defaults' do
|
857
956
|
class Ad < ActiveRecord::Base
|
858
957
|
fields do
|
859
|
-
allow_list :string, limit:
|
860
|
-
allow_string :string, limit:
|
861
|
-
allow_empty :string, limit:
|
862
|
-
allow_null :string, limit:
|
958
|
+
allow_list :string, limit: 250, serialize: Array, null: true, default: [2]
|
959
|
+
allow_string :string, limit: 250, serialize: Array, null: true, default: ['abc']
|
960
|
+
allow_empty :string, limit: 250, serialize: Array, null: true, default: []
|
961
|
+
allow_null :string, limit: 250, serialize: Array, null: true, default: nil
|
863
962
|
end
|
864
963
|
end
|
865
964
|
|
@@ -874,7 +973,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
874
973
|
it 'allows serialize: Hash' do
|
875
974
|
class Ad < ActiveRecord::Base
|
876
975
|
fields do
|
877
|
-
allow_list :string, limit:
|
976
|
+
allow_list :string, limit: 250, serialize: Hash, null: true
|
878
977
|
end
|
879
978
|
end
|
880
979
|
|
@@ -884,9 +983,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
884
983
|
it 'allows Hash defaults' do
|
885
984
|
class Ad < ActiveRecord::Base
|
886
985
|
fields do
|
887
|
-
allow_loc :string, limit:
|
888
|
-
allow_hash :string, limit:
|
889
|
-
allow_null :string, limit:
|
986
|
+
allow_loc :string, limit: 250, serialize: Hash, null: true, default: { 'state' => 'CA' }
|
987
|
+
allow_hash :string, limit: 250, serialize: Hash, null: true, default: {}
|
988
|
+
allow_null :string, limit: 250, serialize: Hash, null: true, default: nil
|
890
989
|
end
|
891
990
|
end
|
892
991
|
|
@@ -900,7 +999,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
900
999
|
it 'allows serialize: JSON' do
|
901
1000
|
class Ad < ActiveRecord::Base
|
902
1001
|
fields do
|
903
|
-
allow_list :string, limit:
|
1002
|
+
allow_list :string, limit: 250, serialize: JSON
|
904
1003
|
end
|
905
1004
|
end
|
906
1005
|
|
@@ -910,10 +1009,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
910
1009
|
it 'allows JSON defaults' do
|
911
1010
|
class Ad < ActiveRecord::Base
|
912
1011
|
fields do
|
913
|
-
allow_hash :string, limit:
|
914
|
-
allow_empty_array :string, limit:
|
915
|
-
allow_empty_hash :string, limit:
|
916
|
-
allow_null :string, limit:
|
1012
|
+
allow_hash :string, limit: 250, serialize: JSON, null: true, default: { 'state' => 'CA' }
|
1013
|
+
allow_empty_array :string, limit: 250, serialize: JSON, null: true, default: []
|
1014
|
+
allow_empty_hash :string, limit: 250, serialize: JSON, null: true, default: {}
|
1015
|
+
allow_null :string, limit: 250, serialize: JSON, null: true, default: nil
|
917
1016
|
end
|
918
1017
|
end
|
919
1018
|
|
@@ -950,7 +1049,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
950
1049
|
it 'allows serialize: ValueClass' do
|
951
1050
|
class Ad < ActiveRecord::Base
|
952
1051
|
fields do
|
953
|
-
allow_list :string, limit:
|
1052
|
+
allow_list :string, limit: 250, serialize: ValueClass
|
954
1053
|
end
|
955
1054
|
end
|
956
1055
|
|
@@ -960,9 +1059,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
960
1059
|
it 'allows ValueClass defaults' do
|
961
1060
|
class Ad < ActiveRecord::Base
|
962
1061
|
fields do
|
963
|
-
allow_hash :string, limit:
|
964
|
-
allow_empty_array :string, limit:
|
965
|
-
allow_null :string, limit:
|
1062
|
+
allow_hash :string, limit: 250, serialize: ValueClass, null: true, default: ValueClass.new([2])
|
1063
|
+
allow_empty_array :string, limit: 250, serialize: ValueClass, null: true, default: ValueClass.new([])
|
1064
|
+
allow_null :string, limit: 250, serialize: ValueClass, null: true, default: nil
|
966
1065
|
end
|
967
1066
|
end
|
968
1067
|
|
@@ -1003,7 +1102,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1003
1102
|
|
1004
1103
|
class Advert < ActiveRecord::Base
|
1005
1104
|
fields do
|
1006
|
-
name :string, limit:
|
1105
|
+
name :string, limit: 250, null: true
|
1007
1106
|
category_id :integer, limit: 8
|
1008
1107
|
nullable_category_id :integer, limit: 8, null: true
|
1009
1108
|
end
|
@@ -1093,4 +1192,33 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1093
1192
|
expect(base_class).to eq("(Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)")
|
1094
1193
|
end
|
1095
1194
|
end
|
1195
|
+
|
1196
|
+
context 'Does not generate migrations' do
|
1197
|
+
it 'for aliased fields bigint -> integer limit 8' do
|
1198
|
+
if Rails::VERSION::MAJOR >= 5 || !ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
|
1199
|
+
class Advert < active_record_base_class.constantize
|
1200
|
+
fields do
|
1201
|
+
price :bigint
|
1202
|
+
end
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
generate_migrations '-n', '-m'
|
1206
|
+
|
1207
|
+
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
1208
|
+
expect(migrations.size).to eq(1), migrations.inspect
|
1209
|
+
|
1210
|
+
if defined?(Mysql2) && Rails::VERSION::MAJOR < 5
|
1211
|
+
ActiveRecord::Base.connection.execute("ALTER TABLE adverts ADD PRIMARY KEY (id)")
|
1212
|
+
end
|
1213
|
+
|
1214
|
+
class Advert < active_record_base_class.constantize
|
1215
|
+
fields do
|
1216
|
+
price :integer, limit: 8
|
1217
|
+
end
|
1218
|
+
end
|
1219
|
+
|
1220
|
+
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
1221
|
+
end
|
1222
|
+
end
|
1223
|
+
end
|
1096
1224
|
end
|