declare_schema 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +13 -13
- data/lib/declare_schema/model.rb +7 -4
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migrator.rb +1 -1
- data/spec/lib/declare_schema/api_spec.rb +6 -7
- data/spec/lib/declare_schema/generator_spec.rb +51 -10
- data/spec/lib/declare_schema/interactive_primary_key_spec.rb +3 -3
- data/spec/lib/declare_schema/migration_generator_spec.rb +317 -202
- data/spec/spec_helper.rb +4 -0
- data/spec/support/acceptance_spec_helpers.rb +57 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b82febb09d3ef72dd3e219c6900abd790e6401e9b15c9ca05c8cd9fc20c5bf4
|
4
|
+
data.tar.gz: 3f2520292af5845df795dba3f26ee62385d1166da51ce5c6a980d1fdc52460a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb726c72430d5b44d94239c3d00abfa42a1c4e2a9c23ad316bd66ec6567857a81c6f7766136bf3d4d78d1df7a43edec2da7e0162cabd60080892b29a2f6b69ce
|
7
|
+
data.tar.gz: 4f39c4534895f33f5d30181ef18b09c2fd9eb535e9da14b0ce502c9117cf28e58404e4c6b2254ffb322a3faea99f0433de6450b9fca96a0585bc87d25047c7b8
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,10 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
4
4
|
|
5
5
|
Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [0.3.1] - 2020-11-13
|
8
|
+
### Fixed
|
9
|
+
- When passing `belongs_to` to Rails, suppress the `optional:` option in Rails 4, since that option was added in Rails 5.
|
10
|
+
|
7
11
|
## [0.3.0] - 2020-11-02
|
8
12
|
### Added
|
9
13
|
- Added support for `belongs_to optional:`.
|
@@ -43,6 +47,7 @@ using the appropriate Rails configuration attributes.
|
|
43
47
|
### Added
|
44
48
|
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
|
45
49
|
|
50
|
+
[0.3.1]: https://github.com/Invoca/declare_schema/compare/v0.3.0...v0.3.1
|
46
51
|
[0.3.0]: https://github.com/Invoca/declare_schema/compare/v0.2.0...v0.3.0
|
47
52
|
[0.2.0]: https://github.com/Invoca/declare_schema/compare/v0.1.3...v0.2.0
|
48
53
|
[0.1.3]: https://github.com/Invoca/declare_schema/compare/v0.1.2...v0.1.3
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
declare_schema (0.3.
|
4
|
+
declare_schema (0.3.1)
|
5
5
|
rails (>= 4.2)
|
6
6
|
|
7
7
|
GEM
|
@@ -54,7 +54,7 @@ GEM
|
|
54
54
|
thor (>= 0.14.0)
|
55
55
|
arel (9.0.0)
|
56
56
|
ast (2.4.1)
|
57
|
-
bootsnap (1.
|
57
|
+
bootsnap (1.5.0)
|
58
58
|
msgpack (~> 1.0)
|
59
59
|
builder (3.2.4)
|
60
60
|
byebug (11.1.3)
|
@@ -134,19 +134,19 @@ GEM
|
|
134
134
|
actionpack (>= 5.0)
|
135
135
|
railties (>= 5.0)
|
136
136
|
rexml (3.2.4)
|
137
|
-
rspec (3.
|
138
|
-
rspec-core (~> 3.
|
139
|
-
rspec-expectations (~> 3.
|
140
|
-
rspec-mocks (~> 3.
|
141
|
-
rspec-core (3.
|
142
|
-
rspec-support (~> 3.
|
143
|
-
rspec-expectations (3.
|
137
|
+
rspec (3.10.0)
|
138
|
+
rspec-core (~> 3.10.0)
|
139
|
+
rspec-expectations (~> 3.10.0)
|
140
|
+
rspec-mocks (~> 3.10.0)
|
141
|
+
rspec-core (3.10.0)
|
142
|
+
rspec-support (~> 3.10.0)
|
143
|
+
rspec-expectations (3.10.0)
|
144
144
|
diff-lcs (>= 1.2.0, < 2.0)
|
145
|
-
rspec-support (~> 3.
|
146
|
-
rspec-mocks (3.
|
145
|
+
rspec-support (~> 3.10.0)
|
146
|
+
rspec-mocks (3.10.0)
|
147
147
|
diff-lcs (>= 1.2.0, < 2.0)
|
148
|
-
rspec-support (~> 3.
|
149
|
-
rspec-support (3.
|
148
|
+
rspec-support (~> 3.10.0)
|
149
|
+
rspec-support (3.10.0)
|
150
150
|
rubocop (0.91.0)
|
151
151
|
parallel (~> 1.10)
|
152
152
|
parser (>= 2.7.1.1)
|
data/lib/declare_schema/model.rb
CHANGED
@@ -106,7 +106,7 @@ module DeclareSchema
|
|
106
106
|
end
|
107
107
|
|
108
108
|
# Extend belongs_to so that it creates a FieldSpec for the foreign key
|
109
|
-
def belongs_to(name, scope = nil, **options
|
109
|
+
def belongs_to(name, scope = nil, **options)
|
110
110
|
column_options = {}
|
111
111
|
|
112
112
|
column_options[:null] = if options.has_key?(:null)
|
@@ -128,13 +128,17 @@ module DeclareSchema
|
|
128
128
|
|
129
129
|
fk = options[:foreign_key]&.to_s || "#{name}_id"
|
130
130
|
|
131
|
-
if !options.has_key?(:optional)
|
131
|
+
if !options.has_key?(:optional)
|
132
132
|
options[:optional] = column_options[:null] # infer :optional from :null
|
133
133
|
end
|
134
134
|
|
135
135
|
fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
|
136
136
|
|
137
|
-
|
137
|
+
if Rails::VERSION::MAJOR >= 5
|
138
|
+
super
|
139
|
+
else
|
140
|
+
super(name, scope, options.except(:optional))
|
141
|
+
end
|
138
142
|
|
139
143
|
refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
|
140
144
|
fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
|
@@ -145,7 +149,6 @@ module DeclareSchema
|
|
145
149
|
index([foreign_type, fkey], index_options) if index_options[:name] != false
|
146
150
|
else
|
147
151
|
index(fkey, index_options) if index_options[:name] != false
|
148
|
-
options[:constraint_name] = options
|
149
152
|
constraint(fkey, fk_options) if fk_options[:constraint_name] != false
|
150
153
|
end
|
151
154
|
end
|
@@ -413,7 +413,7 @@ module Generators
|
|
413
413
|
col_name = old_names[c] || c
|
414
414
|
col = db_columns[col_name]
|
415
415
|
spec = model.field_specs[c]
|
416
|
-
if spec.different_to?(col) # TODO: DRY this up to a diff function that returns the differences. It's different if it has differences. -Colin
|
416
|
+
if spec.different_to?(col) # TODO: TECH-4814 DRY this up to a diff function that returns the differences. It's different if it has differences. -Colin
|
417
417
|
change_spec = fk_field_options(model, c)
|
418
418
|
change_spec[:limit] ||= spec.limit if (spec.sql_type != :text ||
|
419
419
|
::DeclareSchema::Model::FieldSpec.mysql_text_limits?) &&
|
@@ -10,14 +10,14 @@ RSpec.describe 'DeclareSchema API' do
|
|
10
10
|
|
11
11
|
describe 'example models' do
|
12
12
|
it 'generates a model' do
|
13
|
-
|
13
|
+
generate_model 'advert', 'title:string', 'body:text'
|
14
14
|
|
15
15
|
# The above will generate the test, fixture and a model file like this:
|
16
16
|
# model_declaration = Rails::Generators.invoke('declare_schema:model', ['advert2', 'title:string', 'body:text'])
|
17
17
|
# expect(model_declaration.first).to eq([["Advert"], nil, "app/models/advert.rb", nil,
|
18
18
|
# [["AdvertTest"], "test/models/advert_test.rb", nil, "test/fixtures/adverts.yml"]])
|
19
19
|
|
20
|
-
|
20
|
+
expect_model_definition_to_eq('advert', <<~EOS)
|
21
21
|
class Advert < #{active_record_base_class}
|
22
22
|
|
23
23
|
fields do
|
@@ -27,7 +27,8 @@ RSpec.describe 'DeclareSchema API' do
|
|
27
27
|
|
28
28
|
end
|
29
29
|
EOS
|
30
|
-
|
30
|
+
|
31
|
+
clean_up_model('advert2')
|
31
32
|
|
32
33
|
# The migration generator uses this information to create a migration.
|
33
34
|
# The following creates and runs the migration:
|
@@ -36,13 +37,11 @@ RSpec.describe 'DeclareSchema API' do
|
|
36
37
|
|
37
38
|
# We're now ready to start demonstrating the API
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
$LOAD_PATH << "#{TESTAPP_PATH}/app/models"
|
40
|
+
load_models
|
42
41
|
|
43
42
|
unless Rails::VERSION::MAJOR >= 6
|
44
43
|
# TODO: get this to work on Travis for Rails 6
|
45
|
-
|
44
|
+
generate_migrations '-n', '-m'
|
46
45
|
end
|
47
46
|
|
48
47
|
require 'advert'
|
@@ -6,22 +6,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it "generates nested models" do
|
9
|
-
|
9
|
+
generate_model 'alpha/beta', 'one:string', 'two:integer'
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
expect(File.read('app/models/alpha/beta.rb')).to eq(<<~EOS)
|
11
|
+
expect_model_definition_to_eq('alpha/beta', <<~EOS)
|
14
12
|
class Alpha::Beta < #{active_record_base_class}
|
15
|
-
|
13
|
+
|
16
14
|
fields do
|
17
15
|
one :string, limit: 255
|
18
16
|
two :integer
|
19
17
|
end
|
20
|
-
|
18
|
+
|
21
19
|
end
|
22
20
|
EOS
|
23
21
|
|
24
|
-
|
22
|
+
expect_model_definition_to_eq('alpha', <<~EOS)
|
25
23
|
module Alpha
|
26
24
|
def self.table_name_prefix
|
27
25
|
'alpha_'
|
@@ -29,9 +27,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
29
27
|
end
|
30
28
|
EOS
|
31
29
|
|
32
|
-
|
30
|
+
expect_test_definition_to_eq('alpha/beta', <<~EOS)
|
33
31
|
require 'test_helper'
|
34
|
-
|
32
|
+
|
35
33
|
class Alpha::BetaTest < ActiveSupport::TestCase
|
36
34
|
# test "the truth" do
|
37
35
|
# assert true
|
@@ -39,7 +37,50 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
39
37
|
end
|
40
38
|
EOS
|
41
39
|
|
42
|
-
|
40
|
+
case Rails::VERSION::MAJOR
|
41
|
+
when 4
|
42
|
+
expect_test_fixture_to_eq('alpha/beta', <<~EOS)
|
43
|
+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
44
|
+
|
45
|
+
# This model initially had no columns defined. If you add columns to the
|
46
|
+
# model remove the '{}' from the fixture names and add the columns immediately
|
47
|
+
# below each fixture, per the syntax in the comments below
|
48
|
+
#
|
49
|
+
one: {}
|
50
|
+
# column: value
|
51
|
+
#
|
52
|
+
two: {}
|
53
|
+
# column: value
|
54
|
+
EOS
|
55
|
+
when 5
|
56
|
+
expect_test_fixture_to_eq('alpha/beta', <<~EOS)
|
57
|
+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
58
|
+
|
59
|
+
# This model initially had no columns defined. If you add columns to the
|
60
|
+
# model remove the '{}' from the fixture names and add the columns immediately
|
61
|
+
# below each fixture, per the syntax in the comments below
|
62
|
+
#
|
63
|
+
one: {}
|
64
|
+
# column: value
|
65
|
+
#
|
66
|
+
two: {}
|
67
|
+
# column: value
|
68
|
+
EOS
|
69
|
+
when 6
|
70
|
+
expect_test_fixture_to_eq('alpha/beta', <<~EOS)
|
71
|
+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
72
|
+
|
73
|
+
# This model initially had no columns defined. If you add columns to the
|
74
|
+
# model remove the '{}' from the fixture names and add the columns immediately
|
75
|
+
# below each fixture, per the syntax in the comments below
|
76
|
+
#
|
77
|
+
one: {}
|
78
|
+
# column: value
|
79
|
+
#
|
80
|
+
two: {}
|
81
|
+
# column: value
|
82
|
+
EOS
|
83
|
+
end
|
43
84
|
|
44
85
|
$LOAD_PATH << "#{TESTAPP_PATH}/app/models"
|
45
86
|
|
@@ -12,7 +12,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
|
12
12
|
self.primary_key = "foo_id"
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
generate_migrations '-n', '-m'
|
16
16
|
expect(Foo.primary_key).to eq('foo_id')
|
17
17
|
|
18
18
|
### migrate from
|
@@ -24,7 +24,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
puts "\n\e[45m Please enter 'id' (no quotes) at the next prompt \e[0m"
|
27
|
-
|
27
|
+
generate_migrations '-n', '-m'
|
28
28
|
expect(Foo.primary_key).to eq('id')
|
29
29
|
|
30
30
|
nuke_model_class(Foo)
|
@@ -39,7 +39,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
puts "\n\e[45m Please enter 'drop id' (no quotes) at the next prompt \e[0m"
|
42
|
-
|
42
|
+
generate_migrations '-n', '-m'
|
43
43
|
expect(Foo.primary_key).to eq('foo_id')
|
44
44
|
|
45
45
|
### ensure it doesn't cause further migrations
|
@@ -11,14 +11,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
11
11
|
it 'generates migrations' do
|
12
12
|
## The migration generator -- introduction
|
13
13
|
|
14
|
-
|
15
|
-
expect(up_down).to eq(["", ""])
|
14
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("").and migrate_down("")
|
16
15
|
|
17
16
|
class Advert < ActiveRecord::Base
|
18
17
|
end
|
19
18
|
|
20
|
-
|
21
|
-
expect(up_down).to eq(["", ""])
|
19
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("").and migrate_down("")
|
22
20
|
|
23
21
|
Generators::DeclareSchema::Migration::Migrator.ignore_tables = ["green_fishes"]
|
24
22
|
|
@@ -30,13 +28,17 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
30
28
|
name :string, limit: 255, null: true
|
31
29
|
end
|
32
30
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
31
|
+
|
32
|
+
up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
|
33
|
+
expect(migrations).to(
|
34
|
+
migrate_up(<<~EOS.strip)
|
35
|
+
create_table :adverts, id: :bigint do |t|
|
36
|
+
t.string :name, limit: 255
|
37
|
+
end
|
38
|
+
EOS
|
39
|
+
.and migrate_down("drop_table :adverts")
|
40
|
+
)
|
41
|
+
end
|
40
42
|
|
41
43
|
ActiveRecord::Migration.class_eval(up)
|
42
44
|
expect(Advert.columns.map(&:name)).to eq(["id", "name"])
|
@@ -48,19 +50,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
48
50
|
published_at :datetime, null: true
|
49
51
|
end
|
50
52
|
end
|
51
|
-
up, down = migrate
|
52
|
-
expect(up).to eq(<<~EOS.strip)
|
53
|
-
add_column :adverts, :body, :text
|
54
|
-
add_column :adverts, :published_at, :datetime
|
55
53
|
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
expect(migrate).to(
|
55
|
+
migrate_up(<<~EOS.strip)
|
56
|
+
add_column :adverts, :body, :text
|
57
|
+
add_column :adverts, :published_at, :datetime
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
59
|
+
add_index :adverts, [:id], unique: true, name: 'PRIMARY_KEY'
|
60
|
+
EOS
|
61
|
+
.and migrate_down(<<~EOS.strip)
|
62
|
+
remove_column :adverts, :body
|
63
|
+
remove_column :adverts, :published_at
|
64
|
+
EOS
|
65
|
+
)
|
66
|
+
# TODO: ^ TECH-4975 add_index should not be there
|
64
67
|
|
65
68
|
Advert.field_specs.clear # not normally needed
|
66
69
|
class Advert < ActiveRecord::Base
|
@@ -70,9 +73,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
70
73
|
end
|
71
74
|
end
|
72
75
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
+
expect(migrate).to(
|
77
|
+
migrate_up("remove_column :adverts, :published_at").and(
|
78
|
+
migrate_down("add_column :adverts, :published_at, :datetime")
|
79
|
+
)
|
80
|
+
)
|
76
81
|
|
77
82
|
nuke_model_class(Advert)
|
78
83
|
class Advert < ActiveRecord::Base
|
@@ -82,20 +87,22 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
82
87
|
end
|
83
88
|
end
|
84
89
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
90
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
91
|
+
migrate_up(<<~EOS.strip)
|
92
|
+
add_column :adverts, :title, :string, limit: 255
|
93
|
+
remove_column :adverts, :name
|
94
|
+
EOS
|
95
|
+
.and migrate_down(<<~EOS.strip)
|
96
|
+
remove_column :adverts, :title
|
97
|
+
add_column :adverts, :name, :string, limit: 255
|
98
|
+
EOS
|
99
|
+
)
|
95
100
|
|
96
|
-
|
97
|
-
|
98
|
-
|
101
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { name: :title })).to(
|
102
|
+
migrate_up("rename_column :adverts, :name, :title").and(
|
103
|
+
migrate_down("rename_column :adverts, :title, :name")
|
104
|
+
)
|
105
|
+
)
|
99
106
|
|
100
107
|
migrate
|
101
108
|
|
@@ -106,9 +113,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
106
113
|
end
|
107
114
|
end
|
108
115
|
|
109
|
-
|
110
|
-
|
111
|
-
|
116
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
117
|
+
migrate_up("change_column :adverts, :title, :text").and(
|
118
|
+
migrate_down("change_column :adverts, :title, :string, limit: 255")
|
119
|
+
)
|
120
|
+
)
|
112
121
|
|
113
122
|
class Advert < ActiveRecord::Base
|
114
123
|
fields do
|
@@ -117,11 +126,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
117
126
|
end
|
118
127
|
end
|
119
128
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
129
|
+
expect(migrate).to(
|
130
|
+
migrate_up(<<~EOS.strip)
|
131
|
+
change_column :adverts, :title, :string, limit: 255, default: "Untitled"
|
132
|
+
EOS
|
133
|
+
.and migrate_down(<<~EOS.strip)
|
134
|
+
change_column :adverts, :title, :string, limit: 255
|
135
|
+
EOS
|
136
|
+
)
|
125
137
|
|
126
138
|
### Limits
|
127
139
|
|
@@ -131,8 +143,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
131
143
|
end
|
132
144
|
end
|
133
145
|
|
134
|
-
up = Generators::DeclareSchema::Migration::Migrator.run.
|
135
|
-
|
146
|
+
up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
|
147
|
+
expect(migrations).to migrate_up("add_column :adverts, :price, :integer, limit: 2")
|
148
|
+
end
|
136
149
|
|
137
150
|
# Now run the migration, then change the limit:
|
138
151
|
|
@@ -143,9 +156,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
143
156
|
end
|
144
157
|
end
|
145
158
|
|
146
|
-
|
147
|
-
|
148
|
-
|
159
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
160
|
+
migrate_up(<<~EOS.strip)
|
161
|
+
change_column :adverts, :price, :integer, limit: 3
|
162
|
+
EOS
|
163
|
+
.and migrate_down(<<~EOS.strip)
|
164
|
+
change_column :adverts, :price, :integer, limit: 2
|
165
|
+
EOS
|
166
|
+
)
|
149
167
|
|
150
168
|
# Note that limit on a decimal column is ignored (use :scale and :precision)
|
151
169
|
|
@@ -156,8 +174,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
156
174
|
end
|
157
175
|
end
|
158
176
|
|
159
|
-
|
160
|
-
expect(up).to eq("add_column :adverts, :price, :decimal")
|
177
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("add_column :adverts, :price, :decimal")
|
161
178
|
|
162
179
|
# Limits are generally not needed for `text` fields, because by default, `text` fields will use the maximum size
|
163
180
|
# allowed for that database type (0xffffffff for LONGTEXT in MySQL unlimited in Postgres, 1 billion in Sqlite).
|
@@ -172,12 +189,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
172
189
|
end
|
173
190
|
end
|
174
191
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
192
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
193
|
+
migrate_up(<<~EOS.strip)
|
194
|
+
add_column :adverts, :price, :decimal
|
195
|
+
add_column :adverts, :notes, :text, null: false
|
196
|
+
add_column :adverts, :description, :text, null: false
|
197
|
+
EOS
|
198
|
+
)
|
181
199
|
|
182
200
|
# (There is no limit on `add_column ... :description` above since these tests are run against SQLite.)
|
183
201
|
|
@@ -196,11 +214,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
196
214
|
end
|
197
215
|
end
|
198
216
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
217
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
218
|
+
migrate_up(<<~EOS.strip)
|
219
|
+
add_column :adverts, :notes, :text, null: false, limit: 4294967295
|
220
|
+
add_column :adverts, :description, :text, null: false, limit: 255
|
221
|
+
EOS
|
222
|
+
)
|
204
223
|
|
205
224
|
Advert.field_specs.delete :notes
|
206
225
|
|
@@ -239,9 +258,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
239
258
|
end
|
240
259
|
end
|
241
260
|
|
242
|
-
|
243
|
-
|
244
|
-
|
261
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
262
|
+
migrate_up(<<~EOS.strip)
|
263
|
+
change_column :adverts, :description, :text, limit: 4294967295, null: false
|
264
|
+
EOS
|
265
|
+
.and migrate_down(<<~EOS.strip)
|
266
|
+
change_column :adverts, :description, :text
|
267
|
+
EOS
|
268
|
+
)
|
245
269
|
|
246
270
|
# TODO TECH-4814: The above test should have this output:
|
247
271
|
# TODO => "change_column :adverts, :description, :text, limit: 255
|
@@ -254,9 +278,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
254
278
|
end
|
255
279
|
end
|
256
280
|
|
257
|
-
|
258
|
-
|
259
|
-
|
281
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
282
|
+
migrate_up(<<~EOS.strip)
|
283
|
+
change_column :adverts, :description, :text, limit: 4294967295, null: false
|
284
|
+
EOS
|
285
|
+
.and migrate_down(<<~EOS.strip)
|
286
|
+
change_column :adverts, :description, :text
|
287
|
+
EOS
|
288
|
+
)
|
260
289
|
::DeclareSchema::Model::FieldSpec::instance_variable_set(:@mysql_text_limits, false)
|
261
290
|
|
262
291
|
Advert.field_specs.clear
|
@@ -286,18 +315,21 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
286
315
|
belongs_to :category
|
287
316
|
end
|
288
317
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
318
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
319
|
+
migrate_up(<<~EOS.strip)
|
320
|
+
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
321
|
+
|
322
|
+
add_index :adverts, [:category_id], name: 'on_category_id'
|
323
|
+
EOS
|
324
|
+
.and migrate_down(<<~EOS.strip)
|
325
|
+
remove_column :adverts, :category_id
|
326
|
+
|
327
|
+
remove_index :adverts, name: :on_category_id rescue ActiveRecord::StatementInvalid
|
328
|
+
EOS
|
329
|
+
)
|
298
330
|
|
299
331
|
Advert.field_specs.delete(:category_id)
|
300
|
-
Advert.index_specs.delete_if {|spec| spec.fields==["category_id"]}
|
332
|
+
Advert.index_specs.delete_if { |spec| spec.fields==["category_id"] }
|
301
333
|
|
302
334
|
# If you specify a custom foreign key, the migration generator observes that:
|
303
335
|
|
@@ -306,11 +338,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
306
338
|
fields { }
|
307
339
|
belongs_to :category, foreign_key: "c_id", class_name: 'Category'
|
308
340
|
end
|
309
|
-
|
310
|
-
expect(
|
311
|
-
|
312
|
-
|
313
|
-
|
341
|
+
|
342
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
343
|
+
migrate_up(<<~EOS.strip)
|
344
|
+
add_column :adverts, :c_id, :integer, limit: 8, null: false
|
345
|
+
|
346
|
+
add_index :adverts, [:c_id], name: 'on_c_id'
|
347
|
+
EOS
|
348
|
+
)
|
314
349
|
|
315
350
|
Advert.field_specs.delete(:c_id)
|
316
351
|
Advert.index_specs.delete_if { |spec| spec.fields == ["c_id"] }
|
@@ -322,8 +357,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
322
357
|
fields { }
|
323
358
|
belongs_to :category, index: false
|
324
359
|
end
|
325
|
-
|
326
|
-
expect(
|
360
|
+
|
361
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
362
|
+
migrate_up(<<~EOS.strip)
|
363
|
+
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
364
|
+
EOS
|
365
|
+
)
|
327
366
|
|
328
367
|
Advert.field_specs.delete(:category_id)
|
329
368
|
Advert.index_specs.delete_if { |spec| spec.fields == ["category_id"] }
|
@@ -335,11 +374,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
335
374
|
fields { }
|
336
375
|
belongs_to :category, index: 'my_index'
|
337
376
|
end
|
338
|
-
|
339
|
-
expect(
|
340
|
-
|
341
|
-
|
342
|
-
|
377
|
+
|
378
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
379
|
+
migrate_up(<<~EOS.strip)
|
380
|
+
add_column :adverts, :category_id, :integer, limit: 8, null: false
|
381
|
+
|
382
|
+
add_index :adverts, [:category_id], name: 'my_index'
|
383
|
+
EOS
|
384
|
+
)
|
343
385
|
|
344
386
|
Advert.field_specs.delete(:category_id)
|
345
387
|
Advert.index_specs.delete_if { |spec| spec.fields == ["category_id"] }
|
@@ -355,17 +397,19 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
355
397
|
optimistic_lock
|
356
398
|
end
|
357
399
|
end
|
358
|
-
|
359
|
-
expect(
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
400
|
+
|
401
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
402
|
+
migrate_up(<<~EOS.strip)
|
403
|
+
add_column :adverts, :created_at, :datetime
|
404
|
+
add_column :adverts, :updated_at, :datetime
|
405
|
+
add_column :adverts, :lock_version, :integer, null: false, default: 1
|
406
|
+
EOS
|
407
|
+
.and migrate_down(<<~EOS.strip)
|
408
|
+
remove_column :adverts, :created_at
|
409
|
+
remove_column :adverts, :updated_at
|
410
|
+
remove_column :adverts, :lock_version
|
411
|
+
EOS
|
412
|
+
)
|
369
413
|
|
370
414
|
Advert.field_specs.delete(:updated_at)
|
371
415
|
Advert.field_specs.delete(:created_at)
|
@@ -380,11 +424,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
380
424
|
title :string, index: true, limit: 255, null: true
|
381
425
|
end
|
382
426
|
end
|
383
|
-
|
384
|
-
expect(
|
385
|
-
|
386
|
-
|
387
|
-
|
427
|
+
|
428
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
429
|
+
migrate_up(<<~EOS.strip)
|
430
|
+
add_column :adverts, :title, :string, limit: 255
|
431
|
+
|
432
|
+
add_index :adverts, [:title], name: 'on_title'
|
433
|
+
EOS
|
434
|
+
)
|
388
435
|
|
389
436
|
Advert.index_specs.delete_if { |spec| spec.fields==["title"] }
|
390
437
|
|
@@ -395,11 +442,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
395
442
|
title :string, index: true, unique: true, null: true, limit: 255
|
396
443
|
end
|
397
444
|
end
|
398
|
-
|
399
|
-
expect(
|
400
|
-
|
401
|
-
|
402
|
-
|
445
|
+
|
446
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
447
|
+
migrate_up(<<~EOS.strip)
|
448
|
+
add_column :adverts, :title, :string, limit: 255
|
449
|
+
|
450
|
+
add_index :adverts, [:title], unique: true, name: 'on_title'
|
451
|
+
EOS
|
452
|
+
)
|
403
453
|
|
404
454
|
Advert.index_specs.delete_if { |spec| spec.fields == ["title"] }
|
405
455
|
|
@@ -410,11 +460,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
410
460
|
title :string, index: 'my_index', limit: 255, null: true
|
411
461
|
end
|
412
462
|
end
|
413
|
-
|
414
|
-
expect(
|
415
|
-
|
416
|
-
|
417
|
-
|
463
|
+
|
464
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
465
|
+
migrate_up(<<~EOS.strip)
|
466
|
+
add_column :adverts, :title, :string, limit: 255
|
467
|
+
|
468
|
+
add_index :adverts, [:title], name: 'my_index'
|
469
|
+
EOS
|
470
|
+
)
|
418
471
|
|
419
472
|
Advert.index_specs.delete_if { |spec| spec.fields==["title"] }
|
420
473
|
|
@@ -423,11 +476,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
423
476
|
class Advert < ActiveRecord::Base
|
424
477
|
index :title
|
425
478
|
end
|
426
|
-
|
427
|
-
expect(
|
428
|
-
|
429
|
-
|
430
|
-
|
479
|
+
|
480
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
481
|
+
migrate_up(<<~EOS.strip)
|
482
|
+
add_column :adverts, :title, :string, limit: 255
|
483
|
+
|
484
|
+
add_index :adverts, [:title], name: 'on_title'
|
485
|
+
EOS
|
486
|
+
)
|
431
487
|
|
432
488
|
Advert.index_specs.delete_if { |spec| spec.fields == ["title"] }
|
433
489
|
|
@@ -436,11 +492,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
436
492
|
class Advert < ActiveRecord::Base
|
437
493
|
index :title, unique: true, name: 'my_index'
|
438
494
|
end
|
439
|
-
|
440
|
-
expect(
|
441
|
-
|
442
|
-
|
443
|
-
|
495
|
+
|
496
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
497
|
+
migrate_up(<<~EOS.strip)
|
498
|
+
add_column :adverts, :title, :string, limit: 255
|
499
|
+
|
500
|
+
add_index :adverts, [:title], unique: true, name: 'my_index'
|
501
|
+
EOS
|
502
|
+
)
|
444
503
|
|
445
504
|
Advert.index_specs.delete_if { |spec| spec.fields == ["title"] }
|
446
505
|
|
@@ -449,11 +508,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
449
508
|
class Advert < ActiveRecord::Base
|
450
509
|
index [:title, :category_id]
|
451
510
|
end
|
452
|
-
|
453
|
-
expect(
|
454
|
-
|
455
|
-
|
456
|
-
|
511
|
+
|
512
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
513
|
+
migrate_up(<<~EOS.strip)
|
514
|
+
add_column :adverts, :title, :string, limit: 255
|
515
|
+
|
516
|
+
add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
|
517
|
+
EOS
|
518
|
+
)
|
457
519
|
|
458
520
|
Advert.index_specs.delete_if { |spec| spec.fields==["title", "category_id"] }
|
459
521
|
|
@@ -476,19 +538,24 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
476
538
|
Advert.connection.schema_cache.clear!
|
477
539
|
Advert.reset_column_information
|
478
540
|
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
541
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run("adverts" => "ads")).to(
|
542
|
+
migrate_up(<<~EOS.strip)
|
543
|
+
rename_table :adverts, :ads
|
544
|
+
|
545
|
+
add_column :ads, :title, :string, limit: 255
|
546
|
+
add_column :ads, :body, :text
|
547
|
+
|
548
|
+
add_index :ads, [:id], unique: true, name: 'PRIMARY_KEY'
|
549
|
+
EOS
|
550
|
+
.and migrate_down(<<~EOS.strip)
|
551
|
+
remove_column :ads, :title
|
552
|
+
remove_column :ads, :body
|
553
|
+
|
554
|
+
rename_table :ads, :adverts
|
555
|
+
|
556
|
+
add_index :adverts, [:id], unique: true, name: 'PRIMARY_KEY'
|
557
|
+
EOS
|
558
|
+
)
|
492
559
|
|
493
560
|
# Set the table name back to what it should be and confirm we're in sync:
|
494
561
|
|
@@ -512,21 +579,27 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
512
579
|
body :text, null: true
|
513
580
|
end
|
514
581
|
end
|
515
|
-
|
516
|
-
expect(
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
582
|
+
|
583
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run("adverts" => "advertisements")).to(
|
584
|
+
migrate_up(<<~EOS.strip)
|
585
|
+
rename_table :adverts, :advertisements
|
586
|
+
|
587
|
+
add_column :advertisements, :title, :string, limit: 255
|
588
|
+
add_column :advertisements, :body, :text
|
589
|
+
remove_column :advertisements, :name
|
590
|
+
|
591
|
+
add_index :advertisements, [:id], unique: true, name: 'PRIMARY_KEY'
|
592
|
+
EOS
|
593
|
+
.and migrate_down(<<~EOS.strip)
|
594
|
+
remove_column :advertisements, :title
|
595
|
+
remove_column :advertisements, :body
|
596
|
+
add_column :adverts, :name, :string, limit: 255
|
597
|
+
|
598
|
+
rename_table :advertisements, :adverts
|
599
|
+
|
600
|
+
add_index :adverts, [:id], unique: true, name: 'PRIMARY_KEY'
|
601
|
+
EOS
|
602
|
+
)
|
530
603
|
|
531
604
|
### Drop a table
|
532
605
|
|
@@ -536,9 +609,28 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
536
609
|
|
537
610
|
# Dropping tables is where the automatic down-migration really comes in handy:
|
538
611
|
|
539
|
-
|
540
|
-
|
541
|
-
|
612
|
+
rails4_table_create = <<~EOS.strip
|
613
|
+
create_table "adverts", id: false, force: :cascade do |t|
|
614
|
+
t.integer "id", limit: 8
|
615
|
+
t.string "name", limit: 255
|
616
|
+
end
|
617
|
+
|
618
|
+
add_index "adverts", ["id"], name: "PRIMARY_KEY", unique: true
|
619
|
+
EOS
|
620
|
+
|
621
|
+
rails5_table_create = <<~EOS.strip
|
622
|
+
create_table "adverts", id: :integer, force: :cascade do |t|
|
623
|
+
t.string "name", limit: 255
|
624
|
+
t.index ["id"], name: "PRIMARY_KEY", unique: true
|
625
|
+
end
|
626
|
+
EOS
|
627
|
+
|
628
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run).to(
|
629
|
+
migrate_up(<<~EOS.strip)
|
630
|
+
drop_table :adverts
|
631
|
+
EOS
|
632
|
+
.and migrate_down(Rails::VERSION::MAJOR >= 5 ? rails5_table_create : rails4_table_create)
|
633
|
+
)
|
542
634
|
|
543
635
|
## STI
|
544
636
|
|
@@ -559,15 +651,21 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
559
651
|
end
|
560
652
|
class SuperFancyAdvert < FancyAdvert
|
561
653
|
end
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
654
|
+
|
655
|
+
up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
|
656
|
+
expect(migrations).to(
|
657
|
+
migrate_up(<<~EOS.strip)
|
658
|
+
add_column :adverts, :type, :string, limit: 255
|
659
|
+
|
660
|
+
add_index :adverts, [:type], name: 'on_type'
|
661
|
+
EOS
|
662
|
+
.and migrate_down(<<~EOS.strip)
|
663
|
+
remove_column :adverts, :type
|
664
|
+
|
665
|
+
remove_index :adverts, name: :on_type rescue ActiveRecord::StatementInvalid
|
666
|
+
EOS
|
667
|
+
)
|
668
|
+
end
|
571
669
|
|
572
670
|
Advert.field_specs.delete(:type)
|
573
671
|
nuke_model_class(SuperFancyAdvert)
|
@@ -600,16 +698,17 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
600
698
|
body :text, null: true
|
601
699
|
end
|
602
700
|
end
|
603
|
-
up, down = Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })
|
604
|
-
expect(up).to eq(<<~EOS.strip)
|
605
|
-
rename_column :adverts, :title, :name
|
606
|
-
change_column :adverts, :name, :string, limit: 255, default: \"No Name\"
|
607
|
-
EOS
|
608
701
|
|
609
|
-
expect(
|
610
|
-
|
611
|
-
|
612
|
-
|
702
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })).to(
|
703
|
+
migrate_up(<<~EOS.strip)
|
704
|
+
rename_column :adverts, :title, :name
|
705
|
+
change_column :adverts, :name, :string, limit: 255, default: "No Name"
|
706
|
+
EOS
|
707
|
+
.and migrate_down(<<~EOS.strip)
|
708
|
+
rename_column :adverts, :name, :title
|
709
|
+
change_column :adverts, :title, :string, limit: 255, default: "Untitled"
|
710
|
+
EOS
|
711
|
+
)
|
613
712
|
|
614
713
|
### Rename a table and add a column
|
615
714
|
|
@@ -621,13 +720,17 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
621
720
|
created_at :datetime
|
622
721
|
end
|
623
722
|
end
|
624
|
-
|
625
|
-
expect(
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
723
|
+
|
724
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads)).to(
|
725
|
+
migrate_up(<<~EOS.strip)
|
726
|
+
rename_table :adverts, :ads
|
727
|
+
|
728
|
+
add_column :ads, :created_at, :datetime, null: false
|
729
|
+
change_column :ads, :title, :string, limit: 255, null: false, default: \"Untitled\"
|
730
|
+
|
731
|
+
add_index :ads, [:id], unique: true, name: 'PRIMARY_KEY'
|
732
|
+
EOS
|
733
|
+
)
|
631
734
|
|
632
735
|
class Advert < ActiveRecord::Base
|
633
736
|
fields do
|
@@ -648,11 +751,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
648
751
|
end
|
649
752
|
self.primary_key = "advert_id"
|
650
753
|
end
|
651
|
-
|
652
|
-
expect(
|
653
|
-
|
654
|
-
|
655
|
-
|
754
|
+
|
755
|
+
expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { id: :advert_id })).to(
|
756
|
+
migrate_up(<<~EOS.strip)
|
757
|
+
rename_column :adverts, :id, :advert_id
|
758
|
+
|
759
|
+
add_index :adverts, [:advert_id], unique: true, name: 'PRIMARY_KEY'
|
760
|
+
EOS
|
761
|
+
)
|
656
762
|
|
657
763
|
nuke_model_class(Advert)
|
658
764
|
ActiveRecord::Base.connection.execute("drop table `adverts`;")
|
@@ -691,7 +797,16 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
691
797
|
expect(Ad.field_specs['company'].options[:validates].inspect).to eq("{:presence=>true, :uniqueness=>{:case_sensitive=>false}}")
|
692
798
|
end
|
693
799
|
|
694
|
-
|
800
|
+
context "for Rails #{Rails::VERSION::MAJOR}" do
|
801
|
+
if Rails::VERSION::MAJOR >= 5
|
802
|
+
let(:optional_true) { { optional: true } }
|
803
|
+
let(:optional_false) { { optional: false } }
|
804
|
+
else
|
805
|
+
let(:optional_true) { {} }
|
806
|
+
let(:optional_false) { {} }
|
807
|
+
end
|
808
|
+
let(:optional_flag) { { false => optional_false, true => optional_true } }
|
809
|
+
|
695
810
|
describe 'belongs_to' do
|
696
811
|
before do
|
697
812
|
unless defined?(AdCategory)
|
@@ -718,10 +833,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
718
833
|
reset_column_information
|
719
834
|
belongs_to :ad_category, optional: true
|
720
835
|
end
|
721
|
-
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(
|
836
|
+
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional_true)
|
722
837
|
end
|
723
838
|
|
724
|
-
describe 'contradictory settings' do # contradictory settings are ok during migration
|
839
|
+
describe 'contradictory settings' do # contradictory settings are ok--for example, during migration
|
725
840
|
it 'passes through optional: true, null: false' do
|
726
841
|
class AdvertBelongsTo < ActiveRecord::Base
|
727
842
|
self.table_name = 'adverts'
|
@@ -729,7 +844,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
729
844
|
reset_column_information
|
730
845
|
belongs_to :ad_category, optional: true, null: false
|
731
846
|
end
|
732
|
-
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(
|
847
|
+
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional_true)
|
733
848
|
expect(AdvertBelongsTo.field_specs['ad_category_id'].options&.[](:null)).to eq(false)
|
734
849
|
end
|
735
850
|
|
@@ -740,7 +855,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
740
855
|
reset_column_information
|
741
856
|
belongs_to :ad_category, optional: false, null: true
|
742
857
|
end
|
743
|
-
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(
|
858
|
+
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional_false)
|
744
859
|
expect(AdvertBelongsTo.field_specs['ad_category_id'].options&.[](:null)).to eq(true)
|
745
860
|
end
|
746
861
|
end
|
@@ -754,7 +869,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
754
869
|
belongs_to :ad_category, null: #{nullable}
|
755
870
|
end
|
756
871
|
EOS
|
757
|
-
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(
|
872
|
+
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional_flag[nullable])
|
758
873
|
expect(AdvertBelongsTo.field_specs['ad_category_id'].options&.[](:null)).to eq(nullable)
|
759
874
|
end
|
760
875
|
|
@@ -765,7 +880,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
765
880
|
belongs_to :ad_category, optional: #{nullable}
|
766
881
|
end
|
767
882
|
EOS
|
768
|
-
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(
|
883
|
+
expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional_flag[nullable])
|
769
884
|
expect(AdvertBelongsTo.field_specs['ad_category_id'].options&.[](:null)).to eq(nullable)
|
770
885
|
end
|
771
886
|
end
|
@@ -781,7 +896,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
781
896
|
end
|
782
897
|
end
|
783
898
|
|
784
|
-
|
899
|
+
generate_migrations '-n', '-m'
|
785
900
|
|
786
901
|
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
787
902
|
expect(migrations.size).to eq(1), migrations.inspect
|
data/spec/spec_helper.rb
CHANGED
@@ -4,7 +4,11 @@ require "bundler/setup"
|
|
4
4
|
require "declare_schema"
|
5
5
|
require "climate_control"
|
6
6
|
|
7
|
+
require_relative "./support/acceptance_spec_helpers"
|
8
|
+
|
7
9
|
RSpec.configure do |config|
|
10
|
+
config.include AcceptanceSpecHelpers
|
11
|
+
|
8
12
|
# Enable flags like --only-failures and --next-failure
|
9
13
|
config.example_status_persistence_file_path = ".rspec_status"
|
10
14
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AcceptanceSpecHelpers
|
4
|
+
def generate_model(model_name, *fields)
|
5
|
+
Rails::Generators.invoke('declare_schema:model', [model_name, *fields])
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate_migrations(*flags)
|
9
|
+
Rails::Generators.invoke('declare_schema:migration', flags)
|
10
|
+
end
|
11
|
+
|
12
|
+
def expect_model_definition_to_eq(model, expectation)
|
13
|
+
expect_file_to_eq("#{TESTAPP_PATH}/app/models/#{model}.rb", expectation)
|
14
|
+
end
|
15
|
+
|
16
|
+
def expect_test_definition_to_eq(model, expectation)
|
17
|
+
expect_file_to_eq("#{TESTAPP_PATH}/test/models/#{model}_test.rb", expectation)
|
18
|
+
end
|
19
|
+
|
20
|
+
def expect_test_fixture_to_eq(model, expectation)
|
21
|
+
expect_file_to_eq("#{TESTAPP_PATH}/test/fixtures/#{model}.yml", expectation)
|
22
|
+
end
|
23
|
+
|
24
|
+
def expect_file_to_eq(file_path, expectation)
|
25
|
+
expect(File.exist?(file_path)).to be_truthy
|
26
|
+
expect(File.read(file_path)).to eq(expectation)
|
27
|
+
end
|
28
|
+
|
29
|
+
def clean_up_model(model)
|
30
|
+
system("rm -rf #{TESTAPP_PATH}/app/models/#{model}.rb #{TESTAPP_PATH}/test/models/#{model}.rb #{TESTAPP_PATH}/test/fixtures/#{model}.rb")
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_models
|
34
|
+
Rails.application.config.autoload_paths += ["#{TESTAPP_PATH}/app/models"]
|
35
|
+
$LOAD_PATH << "#{TESTAPP_PATH}/app/models"
|
36
|
+
end
|
37
|
+
|
38
|
+
def migrate_up(expected_value)
|
39
|
+
MigrationUpEquals.new(expected_value)
|
40
|
+
end
|
41
|
+
|
42
|
+
def migrate_down(expected_value)
|
43
|
+
MigrationDownEquals.new(expected_value)
|
44
|
+
end
|
45
|
+
|
46
|
+
class MigrationUpEquals < RSpec::Matchers::BuiltIn::Eq
|
47
|
+
def matches?(subject)
|
48
|
+
super(subject[0])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class MigrationDownEquals < RSpec::Matchers::BuiltIn::Eq
|
53
|
+
def matches?(subject)
|
54
|
+
super(subject[1])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: declare_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Invoca Development adapted from hobo_fields by Tom Locke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- spec/lib/declare_schema/prepare_testapp.rb
|
80
80
|
- spec/lib/generators/declare_schema/migration/migrator_spec.rb
|
81
81
|
- spec/spec_helper.rb
|
82
|
+
- spec/support/acceptance_spec_helpers.rb
|
82
83
|
- test_responses.txt
|
83
84
|
homepage: https://github.com/Invoca/declare_schema
|
84
85
|
licenses: []
|