schema_plus_foreign_keys 0.1.7 → 1.0.1.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/prs.yml +173 -0
- data/.gitignore +1 -0
- data/.simplecov +20 -0
- data/Gemfile +4 -1
- data/README.md +56 -35
- data/Rakefile +3 -1
- data/gemfiles/Gemfile.base +1 -1
- data/gemfiles/activerecord-5.2/Gemfile.base +4 -0
- data/gemfiles/activerecord-5.2/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-5.2/Gemfile.postgresql +10 -0
- data/gemfiles/{activerecord-4.2.0 → activerecord-5.2}/Gemfile.sqlite3 +3 -3
- data/gemfiles/activerecord-6.0/Gemfile.base +4 -0
- data/gemfiles/activerecord-6.0/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-6.0/Gemfile.postgresql +10 -0
- data/gemfiles/{activerecord-4.2.6 → activerecord-6.0}/Gemfile.sqlite3 +3 -3
- data/gemfiles/activerecord-6.1/Gemfile.base +4 -0
- data/gemfiles/activerecord-6.1/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-6.1/Gemfile.postgresql +10 -0
- data/gemfiles/{activerecord-5.0 → activerecord-6.1}/Gemfile.sqlite3 +3 -3
- data/gemfiles/activerecord-7.0/Gemfile.base +4 -0
- data/gemfiles/activerecord-7.0/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-7.0/Gemfile.postgresql +10 -0
- data/gemfiles/{activerecord-4.2.1 → activerecord-7.0}/Gemfile.sqlite3 +3 -3
- data/lib/schema_plus/foreign_keys/active_record/base.rb +2 -0
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract/schema_creation.rb +14 -12
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb +7 -47
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/foreign_key_definition.rb +4 -35
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb +15 -16
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/postgresql_adapter.rb +8 -6
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +8 -6
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb +10 -49
- data/lib/schema_plus/foreign_keys/active_record/migration/command_recorder.rb +4 -2
- data/lib/schema_plus/foreign_keys/middleware/dumper.rb +5 -3
- data/lib/schema_plus/foreign_keys/middleware/migration.rb +13 -5
- data/lib/schema_plus/foreign_keys/middleware/model.rb +2 -0
- data/lib/schema_plus/foreign_keys/middleware/mysql.rb +3 -1
- data/lib/schema_plus/foreign_keys/middleware/sql.rb +2 -16
- data/lib/schema_plus/foreign_keys/version.rb +3 -1
- data/lib/schema_plus/foreign_keys.rb +2 -2
- data/lib/schema_plus_foreign_keys.rb +2 -0
- data/schema_dev.yml +8 -5
- data/schema_plus_foreign_keys.gemspec +9 -9
- data/spec/deprecation_spec.rb +14 -113
- data/spec/foreign_key_definition_spec.rb +5 -3
- data/spec/foreign_key_spec.rb +26 -24
- data/spec/migration_spec.rb +73 -91
- data/spec/named_schemas_spec.rb +16 -14
- data/spec/schema_dumper_spec.rb +36 -35
- data/spec/spec_helper.rb +5 -4
- data/spec/support/reference.rb +3 -2
- metadata +41 -97
- data/.travis.yml +0 -27
- data/gemfiles/activerecord-4.2.0/Gemfile.base +0 -3
- data/gemfiles/activerecord-4.2.0/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-4.2.0/Gemfile.postgresql +0 -10
- data/gemfiles/activerecord-4.2.1/Gemfile.base +0 -3
- data/gemfiles/activerecord-4.2.1/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-4.2.1/Gemfile.postgresql +0 -10
- data/gemfiles/activerecord-4.2.6/Gemfile.base +0 -3
- data/gemfiles/activerecord-4.2.6/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-4.2.6/Gemfile.postgresql +0 -10
- data/gemfiles/activerecord-5.0/Gemfile.base +0 -3
- data/gemfiles/activerecord-5.0/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-5.0/Gemfile.postgresql +0 -10
data/spec/deprecation_spec.rb
CHANGED
@@ -1,95 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe 'Deprecations' do
|
4
6
|
|
5
7
|
let(:migration) { ActiveRecord::Migration }
|
6
8
|
|
7
|
-
describe "on add_foreign_key", sqlite3: :skip do
|
8
|
-
before(:each) do
|
9
|
-
define_schema do
|
10
|
-
create_table :posts
|
11
|
-
create_table :comments do |t|
|
12
|
-
t.integer :post_id
|
13
|
-
end
|
14
|
-
end
|
15
|
-
class Comment < ::ActiveRecord::Base ; end
|
16
|
-
end
|
17
|
-
|
18
|
-
it "deprecates 4-argument form" do
|
19
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/4-argument/)
|
20
|
-
migration.add_foreign_key "comments", "post_id", "posts", "id"
|
21
|
-
expect(Comment).to reference(:posts, :id).on(:post_id)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "on remove_foreign_key", sqlite3: :skip do
|
27
|
-
before(:each) do
|
28
|
-
define_schema do
|
29
|
-
create_table :posts
|
30
|
-
create_table :comments do |t|
|
31
|
-
t.integer :post_id, foreign_key: true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
class Comment < ::ActiveRecord::Base ; end
|
35
|
-
end
|
36
|
-
|
37
|
-
it "deprecates :column_names option" do
|
38
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/column_names/)
|
39
|
-
migration.remove_foreign_key "comments", "posts", column_names: "post_id"
|
40
|
-
Comment.reset_column_information
|
41
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "deprecates :references_column_names option" do
|
45
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_column_names.*primary_key/)
|
46
|
-
migration.remove_foreign_key "comments", "posts", references_column_names: "id"
|
47
|
-
Comment.reset_column_information
|
48
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "deprecates :references_table_name option" do
|
52
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_table_name.*to_table/)
|
53
|
-
migration.remove_foreign_key "comments", references_table_name: "posts"
|
54
|
-
Comment.reset_column_information
|
55
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "deprecates table-and-name form" do
|
59
|
-
Comment.reset_column_information
|
60
|
-
name = Comment.foreign_keys.first.name
|
61
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/name.*name: name/)
|
62
|
-
migration.remove_foreign_key "comments", name
|
63
|
-
Comment.reset_column_information
|
64
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "deprecates 3-argument form" do
|
68
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/3.*-argument/)
|
69
|
-
migration.remove_foreign_key "comments", "post_id", "posts"
|
70
|
-
Comment.reset_column_information
|
71
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "deprecates 4-argument form" do
|
75
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/4.*-argument/)
|
76
|
-
migration.remove_foreign_key "comments", "post_id", "posts", "id"
|
77
|
-
Comment.reset_column_information
|
78
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "raises error for 5 arguments" do
|
82
|
-
expect { migration.remove_foreign_key "zip", "a", "dee", "do", "da" }.to raise_error /Wrong number of arguments.*5/
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
9
|
describe "on foreign key definition" do
|
88
10
|
before(:each) do
|
89
11
|
define_schema do
|
90
12
|
create_table :posts
|
91
13
|
create_table :comments do |t|
|
92
|
-
t.
|
14
|
+
t.references :post, foreign_key: true
|
93
15
|
end
|
94
16
|
end
|
95
17
|
class Comment < ::ActiveRecord::Base ; end
|
@@ -100,33 +22,13 @@ describe 'Deprecations' do
|
|
100
22
|
Comment.foreign_keys.first
|
101
23
|
}
|
102
24
|
|
103
|
-
it "deprecates column_names" do
|
104
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/column_names/)
|
105
|
-
expect(definition.column_names).to eq(["post_id"])
|
106
|
-
end
|
107
|
-
|
108
|
-
it "deprecates references_column_names" do
|
109
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_column_names.*primary_key/)
|
110
|
-
expect(definition.references_column_names).to eq(["id"])
|
111
|
-
end
|
112
|
-
|
113
|
-
it "deprecates references_table_name" do
|
114
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_table_name.*to_table/)
|
115
|
-
expect(definition.references_table_name).to eq("posts")
|
116
|
-
end
|
117
|
-
|
118
|
-
it "deprecates table_name" do
|
119
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/table_name.*from_table/)
|
120
|
-
expect(definition.table_name).to eq("comments")
|
121
|
-
end
|
122
|
-
|
123
25
|
it "deprecates :set_null" do
|
124
26
|
expect(ActiveSupport::Deprecation).to receive(:warn).with(/set_null.*nullify/)
|
125
27
|
allow(ActiveSupport::Deprecation).to receive(:warn).with(/table_exists\? currently checks/)
|
126
28
|
define_schema do
|
127
29
|
create_table :posts
|
128
30
|
create_table :comments do |t|
|
129
|
-
t.
|
31
|
+
t.references :post, references: :posts, on_delete: :set_null
|
130
32
|
end
|
131
33
|
end
|
132
34
|
expect(definition.on_delete).to eq(:nullify)
|
@@ -135,17 +37,16 @@ describe 'Deprecations' do
|
|
135
37
|
end
|
136
38
|
|
137
39
|
describe "in table definition" do
|
138
|
-
it "
|
139
|
-
expect
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
40
|
+
it "raises error for 3 arguments" do
|
41
|
+
expect {
|
42
|
+
define_schema do
|
43
|
+
create_table :posts, primary_key: :funky
|
44
|
+
create_table :comments do |t|
|
45
|
+
t.references :post
|
46
|
+
t.foreign_key :post_id, :posts, :funky
|
47
|
+
end
|
146
48
|
end
|
147
|
-
|
148
|
-
expect(migration.foreign_keys("comments").first.primary_key).to eq("funky")
|
49
|
+
}.to raise_error /wrong number of arguments/i
|
149
50
|
end
|
150
51
|
|
151
52
|
it "raises error for 4 arguments" do
|
@@ -153,7 +54,7 @@ describe 'Deprecations' do
|
|
153
54
|
define_schema do
|
154
55
|
create_table :posts, primary_key: :funky
|
155
56
|
create_table :comments do |t|
|
156
|
-
t.
|
57
|
+
t.references :post
|
157
58
|
t.foreign_key :post_id, :posts, :funky, :town
|
158
59
|
end
|
159
60
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe "Foreign Key definition" do
|
4
6
|
|
5
7
|
let(:definition) {
|
6
|
-
options = {:
|
8
|
+
options = {name: "posts_user_fkey", column: :user, primary_key: :id}
|
7
9
|
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
8
10
|
}
|
9
11
|
|
@@ -12,13 +14,13 @@ describe "Foreign Key definition" do
|
|
12
14
|
end
|
13
15
|
|
14
16
|
it "dumps to sql with deferrable values" do
|
15
|
-
options = {:
|
17
|
+
options = {name: "posts_user_fkey", column: :user, primary_key: :id, deferrable: true}
|
16
18
|
deferred_definition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
17
19
|
expect(deferred_definition.to_sql).to eq(%Q{CONSTRAINT posts_user_fkey FOREIGN KEY (#{quote_column_name('user')}) REFERENCES #{quote_table_name('users')} (#{quote_column_name('id')}) DEFERRABLE})
|
18
20
|
end
|
19
21
|
|
20
22
|
it "dumps to sql with initially deferrable values" do
|
21
|
-
options = {:
|
23
|
+
options = {name: "posts_user_fkey", column: :user, primary_key: :id, deferrable: :initially_deferred}
|
22
24
|
initially_deferred_definition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
23
25
|
expect(initially_deferred_definition.to_sql).to eq(%Q{CONSTRAINT posts_user_fkey FOREIGN KEY (#{quote_column_name('user')}) REFERENCES #{quote_table_name('users')} (#{quote_column_name('id')}) DEFERRABLE INITIALLY DEFERRED})
|
24
26
|
end
|
data/spec/foreign_key_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe "Foreign Key" do
|
@@ -7,12 +9,12 @@ describe "Foreign Key" do
|
|
7
9
|
context "created with table" do
|
8
10
|
before(:each) do
|
9
11
|
define_schema do
|
10
|
-
create_table :users, :
|
12
|
+
create_table :users, force: true do |t|
|
11
13
|
t.string :login
|
12
14
|
end
|
13
|
-
create_table :comments, :
|
14
|
-
t.
|
15
|
-
t.foreign_key :
|
15
|
+
create_table :comments, force: true do |t|
|
16
|
+
t.references :user
|
17
|
+
t.foreign_key :users, column: :user_id
|
16
18
|
end
|
17
19
|
end
|
18
20
|
class User < ::ActiveRecord::Base ; end
|
@@ -34,21 +36,21 @@ describe "Foreign Key" do
|
|
34
36
|
|
35
37
|
before(:each) do
|
36
38
|
define_schema do
|
37
|
-
create_table :users, :
|
39
|
+
create_table :users, force: true do |t|
|
38
40
|
t.string :login
|
39
41
|
t.datetime :deleted_at
|
40
42
|
end
|
41
43
|
|
42
|
-
create_table :posts, :
|
44
|
+
create_table :posts, force: true do |t|
|
43
45
|
t.text :body
|
44
|
-
t.
|
45
|
-
t.
|
46
|
+
t.references :user
|
47
|
+
t.references :author
|
46
48
|
end
|
47
49
|
|
48
|
-
create_table :comments, :
|
50
|
+
create_table :comments, force: true do |t|
|
49
51
|
t.text :body
|
50
|
-
t.
|
51
|
-
t.foreign_key :
|
52
|
+
t.references :post
|
53
|
+
t.foreign_key :posts, column: :post_id
|
52
54
|
end
|
53
55
|
end
|
54
56
|
class User < ::ActiveRecord::Base ; end
|
@@ -58,12 +60,12 @@ describe "Foreign Key" do
|
|
58
60
|
end
|
59
61
|
|
60
62
|
|
61
|
-
context "works", :
|
63
|
+
context "works", sqlite3: :skip do
|
62
64
|
|
63
65
|
context "when is added", "posts(author_id)" do
|
64
66
|
|
65
67
|
before(:each) do
|
66
|
-
add_foreign_key(:posts, :users, :
|
68
|
+
add_foreign_key(:posts, :users, column: :author_id, on_update: :cascade, on_delete: :restrict)
|
67
69
|
end
|
68
70
|
|
69
71
|
it "references users(id)" do
|
@@ -90,7 +92,7 @@ describe "Foreign Key" do
|
|
90
92
|
|
91
93
|
context "when is dropped", "comments(post_id)" do
|
92
94
|
|
93
|
-
let(:foreign_key_name) { fk = Comment.foreign_keys.detect
|
95
|
+
let(:foreign_key_name) { fk = Comment.foreign_keys.detect { |it| it.column == 'post_id' } and fk.name }
|
94
96
|
|
95
97
|
before(:each) do
|
96
98
|
remove_foreign_key(:comments, name: foreign_key_name)
|
@@ -112,7 +114,7 @@ describe "Foreign Key" do
|
|
112
114
|
|
113
115
|
context "when drop using hash", "comments(post_id)" do
|
114
116
|
|
115
|
-
let(:foreign_key_name) { fk = Comment.foreign_keys.detect
|
117
|
+
let(:foreign_key_name) { fk = Comment.foreign_keys.detect { |it| it.column == 'post_id' } and fk.name }
|
116
118
|
|
117
119
|
it "finds by name" do
|
118
120
|
remove_foreign_key(:comments, name: foreign_key_name)
|
@@ -131,7 +133,7 @@ describe "Foreign Key" do
|
|
131
133
|
end
|
132
134
|
|
133
135
|
it "does not error with :if_exists" do
|
134
|
-
expect{remove_foreign_key(:comments, "posts", column: "nonesuch", :
|
136
|
+
expect{remove_foreign_key(:comments, "posts", column: "nonesuch", if_exists: true)}.to_not raise_error
|
135
137
|
end
|
136
138
|
end
|
137
139
|
|
@@ -149,8 +151,8 @@ describe "Foreign Key" do
|
|
149
151
|
context "when table name is a reserved word" do
|
150
152
|
before(:each) do
|
151
153
|
migration.suppress_messages do
|
152
|
-
migration.create_table :references, :
|
153
|
-
t.
|
154
|
+
migration.create_table :references, force: true do |t|
|
155
|
+
t.references :post, foreign_key: false
|
154
156
|
end
|
155
157
|
end
|
156
158
|
end
|
@@ -168,11 +170,11 @@ describe "Foreign Key" do
|
|
168
170
|
|
169
171
|
end
|
170
172
|
|
171
|
-
context "raises an exception", :
|
173
|
+
context "raises an exception", sqlite3: :only do
|
172
174
|
|
173
175
|
it "when attempting to add" do
|
174
176
|
expect {
|
175
|
-
add_foreign_key(:posts, :users, :
|
177
|
+
add_foreign_key(:posts, :users, column: :author_id, on_update: :cascade, on_delete: :restrict)
|
176
178
|
}.to raise_error(NotImplementedError)
|
177
179
|
end
|
178
180
|
|
@@ -186,18 +188,18 @@ describe "Foreign Key" do
|
|
186
188
|
end
|
187
189
|
|
188
190
|
protected
|
189
|
-
def add_foreign_key(*args)
|
191
|
+
def add_foreign_key(*args, **kwargs)
|
190
192
|
migration.suppress_messages do
|
191
|
-
migration.add_foreign_key(*args)
|
193
|
+
migration.add_foreign_key(*args, **kwargs)
|
192
194
|
end
|
193
195
|
User.reset_column_information
|
194
196
|
Post.reset_column_information
|
195
197
|
Comment.reset_column_information
|
196
198
|
end
|
197
199
|
|
198
|
-
def remove_foreign_key(*args)
|
200
|
+
def remove_foreign_key(*args, **kwargs)
|
199
201
|
migration.suppress_messages do
|
200
|
-
migration.remove_foreign_key(*args)
|
202
|
+
migration.remove_foreign_key(*args, **kwargs)
|
201
203
|
end
|
202
204
|
User.reset_column_information
|
203
205
|
Post.reset_column_information
|