schema_plus_foreign_keys 0.1.8 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/prs.yml +173 -0
- data/.gitignore +1 -0
- data/.simplecov +20 -0
- data/Gemfile +4 -1
- data/README.md +43 -27
- data/Rakefile +3 -1
- data/gemfiles/Gemfile.base +1 -1
- data/gemfiles/activerecord-5.2/Gemfile.base +2 -1
- data/gemfiles/activerecord-5.2/Gemfile.mysql2 +2 -2
- data/gemfiles/activerecord-5.2/Gemfile.postgresql +2 -2
- data/gemfiles/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.0 → 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-4.2.1 → 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.6 → 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 +7 -46
- 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 +7 -6
- data/schema_plus_foreign_keys.gemspec +9 -9
- data/spec/deprecation_spec.rb +11 -110
- data/spec/foreign_key_definition_spec.rb +5 -3
- data/spec/foreign_key_spec.rb +22 -20
- data/spec/migration_spec.rb +36 -60
- data/spec/named_schemas_spec.rb +16 -14
- data/spec/schema_dumper_spec.rb +15 -15
- data/spec/spec_helper.rb +5 -4
- data/spec/support/reference.rb +3 -2
- metadata +35 -99
- data/.travis.yml +0 -33
- 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/gemfiles/activerecord-5.0/Gemfile.sqlite3 +0 -10
- data/gemfiles/activerecord-5.1/Gemfile.base +0 -3
- data/gemfiles/activerecord-5.1/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-5.1/Gemfile.postgresql +0 -10
- data/gemfiles/activerecord-5.1/Gemfile.sqlite3 +0 -10
data/spec/deprecation_spec.rb
CHANGED
@@ -1,89 +1,11 @@
|
|
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.references :post
|
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.references :post, 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
|
@@ -100,26 +22,6 @@ 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/)
|
@@ -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
|
@@ -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, :
|
15
|
+
create_table :comments, force: true do |t|
|
14
16
|
t.references :user
|
15
|
-
t.foreign_key :
|
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
46
|
t.references :user
|
45
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
52
|
t.references :post
|
51
|
-
t.foreign_key :
|
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.references :post, :
|
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
|
data/spec/migration_spec.rb
CHANGED
@@ -1,30 +1,28 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require 'spec_helper'
|
3
5
|
|
4
6
|
describe ActiveRecord::Migration do
|
5
|
-
let(:column_type) {
|
6
|
-
Gem::Requirement.new('< 5.1').satisfied_by?(::ActiveRecord.version) ? :integer : :bigint
|
7
|
-
}
|
8
|
-
|
9
7
|
before(:each) do
|
10
8
|
define_schema do
|
11
9
|
|
12
|
-
create_table :users, :
|
13
|
-
t.string :login, :
|
10
|
+
create_table :users, force: true do |t|
|
11
|
+
t.string :login, index: { unique: true }
|
14
12
|
end
|
15
13
|
|
16
|
-
create_table :members, :
|
14
|
+
create_table :members, force: true do |t|
|
17
15
|
t.string :login
|
18
16
|
end
|
19
17
|
|
20
|
-
create_table :comments, :
|
18
|
+
create_table :comments, force: true do |t|
|
21
19
|
t.string :content
|
22
20
|
t.integer :user
|
23
21
|
t.references :user
|
24
|
-
t.foreign_key :
|
22
|
+
t.foreign_key :users, column: :user_id, primary_key: :id
|
25
23
|
end
|
26
24
|
|
27
|
-
create_table :posts, :
|
25
|
+
create_table :posts, force: true do |t|
|
28
26
|
t.string :content
|
29
27
|
end
|
30
28
|
end
|
@@ -39,18 +37,18 @@ describe ActiveRecord::Migration do
|
|
39
37
|
@model = Post
|
40
38
|
end
|
41
39
|
|
42
|
-
it "should enable foreign keys", :
|
40
|
+
it "should enable foreign keys", sqlite3: :only do
|
43
41
|
sql = []
|
44
42
|
allow(@model.connection).to receive(:execute) { |str| sql << str }
|
45
43
|
recreate_table(@model) do |t|
|
46
|
-
t.send
|
44
|
+
t.send :bigint, :user, foreign_key: true
|
47
45
|
end
|
48
46
|
expect(sql.join('; ')).to match(/PRAGMA FOREIGN_KEYS = ON.*CREATE TABLE "posts"/)
|
49
47
|
end
|
50
48
|
|
51
49
|
it "should create foreign key with default reference" do
|
52
50
|
recreate_table(@model) do |t|
|
53
|
-
t.send
|
51
|
+
t.send :bigint, :user, foreign_key: true
|
54
52
|
end
|
55
53
|
expect(@model).to reference(:users, :id).on(:user)
|
56
54
|
end
|
@@ -65,16 +63,16 @@ describe ActiveRecord::Migration do
|
|
65
63
|
|
66
64
|
it "should create foreign key with different reference" do
|
67
65
|
recreate_table(@model) do |t|
|
68
|
-
t.references :author, :
|
66
|
+
t.references :author, foreign_key: { references: :users }
|
69
67
|
end
|
70
68
|
expect(@model).to reference(:users, :id).on(:author_id)
|
71
69
|
end
|
72
70
|
|
73
71
|
it "should create foreign key without modifying input hash" do
|
74
|
-
hash = { :
|
72
|
+
hash = { references: :users }
|
75
73
|
hash_original = hash.dup
|
76
74
|
recreate_table(@model) do |t|
|
77
|
-
t.references :author, :
|
75
|
+
t.references :author, foreign_key: hash
|
78
76
|
end
|
79
77
|
expect(hash).to eq(hash_original)
|
80
78
|
end
|
@@ -99,7 +97,7 @@ describe ActiveRecord::Migration do
|
|
99
97
|
recreate_table @model do |t|
|
100
98
|
t.references :user, :foreign_key => true
|
101
99
|
end
|
102
|
-
expect(@model).to reference(:users, :id).with_name(
|
100
|
+
expect(@model).to reference(:users, :id).with_name(/fk_rails_\w+/)
|
103
101
|
end
|
104
102
|
|
105
103
|
it "should create foreign key with specified name" do
|
@@ -335,7 +333,7 @@ describe ActiveRecord::Migration do
|
|
335
333
|
it "should remove a foreign key constraint"+suffix, :sqlite3 => :skip do
|
336
334
|
Comment.reset_column_information
|
337
335
|
expect(Comment).to reference(:users, :id).on(:user_id)
|
338
|
-
migration = Class.new ::ActiveRecord::Migration
|
336
|
+
migration = Class.new ::ActiveRecord::Migration::Current do
|
339
337
|
define_method(:change) {
|
340
338
|
change_table("comments", :bulk => bulk) do |t|
|
341
339
|
t.references :user, foreign_key: true
|
@@ -352,7 +350,7 @@ describe ActiveRecord::Migration do
|
|
352
350
|
|
353
351
|
it "should create a foreign key constraint using :references"+suffix, :sqlite3 => :skip do
|
354
352
|
change_table(@model, :bulk => bulk) do |t|
|
355
|
-
t.send
|
353
|
+
t.send :bigint, :user_id, foreign_key: true
|
356
354
|
end
|
357
355
|
expect(@model).to reference(:users, :id).on(:user_id)
|
358
356
|
end
|
@@ -373,19 +371,19 @@ describe ActiveRecord::Migration do
|
|
373
371
|
end
|
374
372
|
|
375
373
|
it "should create foreign key" do
|
376
|
-
add_column(:post_id,
|
374
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
377
375
|
expect(@model).to reference(:posts, :id).on(:post_id)
|
378
376
|
end
|
379
377
|
end
|
380
378
|
|
381
379
|
it "should create foreign key to explicitly given table" do
|
382
|
-
add_column(:author_id,
|
380
|
+
add_column(:author_id, :bigint, :foreign_key => { :references => :users }) do
|
383
381
|
expect(@model).to reference(:users, :id).on(:author_id)
|
384
382
|
end
|
385
383
|
end
|
386
384
|
|
387
385
|
it "should create foreign key to explicitly given table using shortcut" do
|
388
|
-
add_column(:author_id,
|
386
|
+
add_column(:author_id, :bigint, :references => :users) do
|
389
387
|
expect(@model).to reference(:users, :id).on(:author_id)
|
390
388
|
end
|
391
389
|
end
|
@@ -397,14 +395,14 @@ describe ActiveRecord::Migration do
|
|
397
395
|
end
|
398
396
|
|
399
397
|
it "should create foreign key to the same table on parent_id" do
|
400
|
-
add_column(:parent_id,
|
398
|
+
add_column(:parent_id, :bigint, foreign_key: true) do
|
401
399
|
expect(@model).to reference(@model.table_name, :id).on(:parent_id)
|
402
400
|
end
|
403
401
|
end
|
404
402
|
|
405
403
|
it "should use default on_update action" do
|
406
404
|
SchemaPlus::ForeignKeys.config.on_update = :cascade
|
407
|
-
add_column(:post_id,
|
405
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
408
406
|
expect(@model).to reference.on(:post_id).on_update(:cascade)
|
409
407
|
end
|
410
408
|
SchemaPlus::ForeignKeys.config.on_update = nil
|
@@ -412,7 +410,7 @@ describe ActiveRecord::Migration do
|
|
412
410
|
|
413
411
|
it "should use default on_delete action" do
|
414
412
|
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
415
|
-
add_column(:post_id,
|
413
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
416
414
|
expect(@model).to reference.on(:post_id).on_delete(:cascade)
|
417
415
|
end
|
418
416
|
SchemaPlus::ForeignKeys.config.on_delete = nil
|
@@ -421,22 +419,22 @@ describe ActiveRecord::Migration do
|
|
421
419
|
it "should allow to overwrite default actions" do
|
422
420
|
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
423
421
|
SchemaPlus::ForeignKeys.config.on_update = :restrict
|
424
|
-
add_column(:post_id,
|
422
|
+
add_column(:post_id, :bigint, :foreign_key => { :on_update => :nullify, :on_delete => :nullify}) do
|
425
423
|
expect(@model).to reference.on(:post_id).on_delete(:nullify).on_update(:nullify)
|
426
424
|
end
|
427
425
|
SchemaPlus::ForeignKeys.config.on_delete = nil
|
428
426
|
end
|
429
427
|
|
430
428
|
it "should create foreign key with default name" do
|
431
|
-
add_column(:post_id,
|
432
|
-
expect(@model).to reference(:posts, :id).with_name(
|
429
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
430
|
+
expect(@model).to reference(:posts, :id).with_name(/fk_rails_\w+/)
|
433
431
|
end
|
434
432
|
end
|
435
433
|
|
436
434
|
protected
|
437
|
-
def add_column(column_name,
|
435
|
+
def add_column(column_name, type, **options)
|
438
436
|
table = @model.table_name
|
439
|
-
ActiveRecord::Migration.add_column(table, column_name,
|
437
|
+
ActiveRecord::Migration.add_column(table, column_name, type, **options)
|
440
438
|
@model.reset_column_information
|
441
439
|
yield if block_given?
|
442
440
|
ActiveRecord::Migration.remove_column(table, column_name)
|
@@ -471,22 +469,22 @@ describe ActiveRecord::Migration do
|
|
471
469
|
end
|
472
470
|
|
473
471
|
it "should drop foreign key if it is no longer valid" do
|
474
|
-
change_column :user_id,
|
472
|
+
change_column :user_id, :bigint, :foreign_key => { :references => :members }
|
475
473
|
expect(@model).not_to reference(:users)
|
476
474
|
end
|
477
475
|
|
478
476
|
it "should drop foreign key if requested to do so" do
|
479
|
-
change_column :user_id,
|
477
|
+
change_column :user_id, :bigint, :foreign_key => { :references => nil }
|
480
478
|
expect(@model).not_to reference(:users)
|
481
479
|
end
|
482
480
|
|
483
481
|
it "should reference pointed table afterwards if new one is created" do
|
484
|
-
change_column :user_id,
|
482
|
+
change_column :user_id, :bigint, :foreign_key => { :references => :members }
|
485
483
|
expect(@model).to reference(:members)
|
486
484
|
end
|
487
485
|
|
488
486
|
it "should maintain foreign key if it's unaffected by change" do
|
489
|
-
change_column :user_id,
|
487
|
+
change_column :user_id, :bigint, :default => 0
|
490
488
|
expect(@model).to reference(:users)
|
491
489
|
end
|
492
490
|
|
@@ -495,10 +493,10 @@ describe ActiveRecord::Migration do
|
|
495
493
|
end
|
496
494
|
|
497
495
|
protected
|
498
|
-
def change_column(column_name,
|
496
|
+
def change_column(column_name, type, **options)
|
499
497
|
table = @model.table_name
|
500
498
|
ActiveRecord::Migration.suppress_messages do
|
501
|
-
ActiveRecord::Migration.change_column(table, column_name,
|
499
|
+
ActiveRecord::Migration.change_column(table, column_name, type, **options)
|
502
500
|
@model.reset_column_information
|
503
501
|
end
|
504
502
|
end
|
@@ -529,27 +527,6 @@ describe ActiveRecord::Migration do
|
|
529
527
|
end
|
530
528
|
end
|
531
529
|
|
532
|
-
|
533
|
-
context "when table is renamed" do
|
534
|
-
|
535
|
-
before(:each) do
|
536
|
-
@model = Comment
|
537
|
-
recreate_table @model do |t|
|
538
|
-
t.references :user, foreign_key: true
|
539
|
-
t.integer :xyz, :index => true
|
540
|
-
end
|
541
|
-
ActiveRecord::Migration.suppress_messages do
|
542
|
-
ActiveRecord::Migration.rename_table @model.table_name, :newname
|
543
|
-
end
|
544
|
-
end
|
545
|
-
|
546
|
-
it "should rename foreign key constraints", :sqlite3 => :skip do
|
547
|
-
expect(ActiveRecord::Base.connection.foreign_keys(:newname).first.name).to match(/newname/)
|
548
|
-
end
|
549
|
-
|
550
|
-
end
|
551
|
-
|
552
|
-
|
553
530
|
context "when table with more than one fk constraint is renamed", :sqlite3 => :skip do
|
554
531
|
|
555
532
|
before(:each) do
|
@@ -571,17 +548,16 @@ describe ActiveRecord::Migration do
|
|
571
548
|
|
572
549
|
def recreate_table(model, opts={}, &block)
|
573
550
|
ActiveRecord::Migration.suppress_messages do
|
574
|
-
ActiveRecord::Migration.create_table model.table_name, opts.merge(:force => true), &block
|
551
|
+
ActiveRecord::Migration.create_table model.table_name, **opts.merge(:force => true), &block
|
575
552
|
end
|
576
553
|
model.reset_column_information
|
577
554
|
end
|
578
555
|
|
579
556
|
def change_table(model, opts={}, &block)
|
580
557
|
ActiveRecord::Migration.suppress_messages do
|
581
|
-
ActiveRecord::Migration.change_table model.table_name, opts, &block
|
558
|
+
ActiveRecord::Migration.change_table model.table_name, **opts, &block
|
582
559
|
end
|
583
560
|
model.reset_column_information
|
584
561
|
end
|
585
562
|
|
586
563
|
end
|
587
|
-
|
data/spec/named_schemas_spec.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
|
-
describe "with multiple schemas" do
|
5
|
+
describe "with multiple schemas", sqlite3: :skip do
|
4
6
|
def connection
|
5
7
|
ActiveRecord::Base.connection
|
6
8
|
end
|
@@ -22,7 +24,7 @@ describe "with multiple schemas" do
|
|
22
24
|
|
23
25
|
before(:each) do
|
24
26
|
define_schema do
|
25
|
-
create_table :users, :
|
27
|
+
create_table :users, force: true do |t|
|
26
28
|
t.string :login
|
27
29
|
end
|
28
30
|
end
|
@@ -56,8 +58,8 @@ describe "with multiple schemas" do
|
|
56
58
|
end
|
57
59
|
|
58
60
|
it "should not find foreign keys in other schema" do
|
59
|
-
connection.create_table :comments, :
|
60
|
-
t.references :user, :
|
61
|
+
connection.create_table :comments, force: true do |t|
|
62
|
+
t.references :user, foreign_key: false
|
61
63
|
end
|
62
64
|
Comment.reset_column_information
|
63
65
|
expect(Comment.foreign_keys.length).to eq(0)
|
@@ -66,8 +68,8 @@ describe "with multiple schemas" do
|
|
66
68
|
end
|
67
69
|
|
68
70
|
it "should find foreign keys in this schema" do
|
69
|
-
connection.create_table :comments, :
|
70
|
-
t.references :user, :
|
71
|
+
connection.create_table :comments, force: true do |t|
|
72
|
+
t.references :user, foreign_key: true
|
71
73
|
end
|
72
74
|
Comment.reset_column_information
|
73
75
|
expect(Comment.foreign_keys.map(&:column).flatten).to eq(["user_id"])
|
@@ -80,13 +82,13 @@ describe "with multiple schemas" do
|
|
80
82
|
context "foreign key migrations" do
|
81
83
|
before(:each) do
|
82
84
|
define_schema do
|
83
|
-
create_table "items", :
|
85
|
+
create_table "items", force: true do |t|
|
84
86
|
end
|
85
|
-
create_table "schema_plus_test2.groups", :
|
87
|
+
create_table "schema_plus_test2.groups", force: true do |t|
|
86
88
|
end
|
87
|
-
create_table "schema_plus_test2.members", :
|
88
|
-
t.references :item, :
|
89
|
-
t.references :group, :
|
89
|
+
create_table "schema_plus_test2.members", force: true do |t|
|
90
|
+
t.references :item, foreign_key: { name: 'fk_test2_members_items' } unless SchemaDev::Rspec::Helpers.mysql?
|
91
|
+
t.references :group, foreign_key: { name: 'fk_test2_members_groups', references: "schema_plus_test2.groups" }
|
90
92
|
end
|
91
93
|
end
|
92
94
|
class Group < ::ActiveRecord::Base
|
@@ -122,13 +124,13 @@ describe "with multiple schemas" do
|
|
122
124
|
expect(Member.foreign_keys.map(&:to_table)).to include "schema_plus_test2.groups"
|
123
125
|
end
|
124
126
|
|
125
|
-
it "should reference table in default schema", :
|
127
|
+
it "should reference table in default schema", mysql: :skip do
|
126
128
|
expect(Member.foreign_keys.map(&:to_table)).to include "items"
|
127
129
|
end
|
128
130
|
|
129
131
|
it "should include the schema in the constraint name" do
|
130
|
-
expected_names = ["
|
131
|
-
expected_names << "
|
132
|
+
expected_names = ["fk_test2_members_groups"]
|
133
|
+
expected_names << "fk_test2_members_items" unless SchemaDev::Rspec::Helpers.mysql?
|
132
134
|
expect(Member.foreign_keys.map(&:name).sort).to match_array(expected_names.sort)
|
133
135
|
end
|
134
136
|
end
|