schema_plus_foreign_keys 0.1.6 → 1.0.0
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 +134 -0
- data/.gitignore +1 -0
- data/.simplecov +20 -0
- data/Gemfile +4 -1
- data/README.md +49 -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/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 +20 -0
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb +9 -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 +14 -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 -15
- data/lib/schema_plus/foreign_keys/version.rb +3 -1
- data/lib/schema_plus/foreign_keys.rb +3 -1
- data/lib/schema_plus_foreign_keys.rb +2 -0
- data/schema_dev.yml +5 -4
- data/schema_plus_foreign_keys.gemspec +10 -8
- data/spec/deprecation_spec.rb +15 -112
- 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 +6 -4
- data/spec/support/reference.rb +3 -2
- metadata +48 -73
- data/.travis.yml +0 -24
- 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.1/Gemfile.sqlite3 +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/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
|
data/spec/migration_spec.rb
CHANGED
@@ -1,27 +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
|
-
|
6
7
|
before(:each) do
|
7
8
|
define_schema do
|
8
9
|
|
9
|
-
create_table :users, :
|
10
|
-
t.string :login, :
|
10
|
+
create_table :users, force: true do |t|
|
11
|
+
t.string :login, index: { unique: true }
|
11
12
|
end
|
12
13
|
|
13
|
-
create_table :members, :
|
14
|
+
create_table :members, force: true do |t|
|
14
15
|
t.string :login
|
15
16
|
end
|
16
17
|
|
17
|
-
create_table :comments, :
|
18
|
+
create_table :comments, force: true do |t|
|
18
19
|
t.string :content
|
19
20
|
t.integer :user
|
20
|
-
t.
|
21
|
-
t.foreign_key :
|
21
|
+
t.references :user
|
22
|
+
t.foreign_key :users, column: :user_id, primary_key: :id
|
22
23
|
end
|
23
24
|
|
24
|
-
create_table :posts, :
|
25
|
+
create_table :posts, force: true do |t|
|
25
26
|
t.string :content
|
26
27
|
end
|
27
28
|
end
|
@@ -36,25 +37,25 @@ describe ActiveRecord::Migration do
|
|
36
37
|
@model = Post
|
37
38
|
end
|
38
39
|
|
39
|
-
it "should enable foreign keys", :
|
40
|
+
it "should enable foreign keys", sqlite3: :only do
|
40
41
|
sql = []
|
41
42
|
allow(@model.connection).to receive(:execute) { |str| sql << str }
|
42
43
|
recreate_table(@model) do |t|
|
43
|
-
t.
|
44
|
+
t.send :bigint, :user, foreign_key: true
|
44
45
|
end
|
45
46
|
expect(sql.join('; ')).to match(/PRAGMA FOREIGN_KEYS = ON.*CREATE TABLE "posts"/)
|
46
47
|
end
|
47
48
|
|
48
49
|
it "should create foreign key with default reference" do
|
49
50
|
recreate_table(@model) do |t|
|
50
|
-
t.
|
51
|
+
t.send :bigint, :user, foreign_key: true
|
51
52
|
end
|
52
53
|
expect(@model).to reference(:users, :id).on(:user)
|
53
54
|
end
|
54
55
|
|
55
56
|
it "should create foreign key with default column" do
|
56
57
|
recreate_table(@model) do |t|
|
57
|
-
t.
|
58
|
+
t.references :user
|
58
59
|
t.foreign_key :users
|
59
60
|
end
|
60
61
|
expect(@model).to reference(:users, :id).on(:user_id)
|
@@ -62,16 +63,16 @@ describe ActiveRecord::Migration do
|
|
62
63
|
|
63
64
|
it "should create foreign key with different reference" do
|
64
65
|
recreate_table(@model) do |t|
|
65
|
-
t.
|
66
|
+
t.references :author, foreign_key: { references: :users }
|
66
67
|
end
|
67
68
|
expect(@model).to reference(:users, :id).on(:author_id)
|
68
69
|
end
|
69
70
|
|
70
71
|
it "should create foreign key without modifying input hash" do
|
71
|
-
hash = { :
|
72
|
+
hash = { references: :users }
|
72
73
|
hash_original = hash.dup
|
73
74
|
recreate_table(@model) do |t|
|
74
|
-
t.
|
75
|
+
t.references :author, foreign_key: hash
|
75
76
|
end
|
76
77
|
expect(hash).to eq(hash_original)
|
77
78
|
end
|
@@ -87,40 +88,40 @@ describe ActiveRecord::Migration do
|
|
87
88
|
|
88
89
|
it "should create foreign key with different reference using shortcut" do
|
89
90
|
recreate_table(@model) do |t|
|
90
|
-
t.
|
91
|
+
t.references :author, :references => :users
|
91
92
|
end
|
92
93
|
expect(@model).to reference(:users, :id).on(:author_id)
|
93
94
|
end
|
94
95
|
|
95
96
|
it "should create foreign key with default name" do
|
96
97
|
recreate_table @model do |t|
|
97
|
-
t.
|
98
|
+
t.references :user, :foreign_key => true
|
98
99
|
end
|
99
|
-
expect(@model).to reference(:users, :id).with_name(
|
100
|
+
expect(@model).to reference(:users, :id).with_name(/fk_rails_\w+/)
|
100
101
|
end
|
101
102
|
|
102
103
|
it "should create foreign key with specified name" do
|
103
104
|
recreate_table @model do |t|
|
104
|
-
t.
|
105
|
+
t.references :user, :foreign_key => { :name => "wugga" }
|
105
106
|
end
|
106
107
|
expect(@model).to reference(:users, :id).with_name("wugga")
|
107
108
|
end
|
108
109
|
|
109
110
|
it "handles very long names" do
|
110
111
|
table = ("ta"*15)
|
111
|
-
column = ("co"*15
|
112
|
+
column = ("co"*15)
|
112
113
|
expect {
|
113
114
|
ActiveRecord::Migration.create_table table do |t|
|
114
|
-
t.
|
115
|
+
t.references column, references: :members, index: false
|
115
116
|
end
|
116
117
|
}.not_to raise_error
|
117
|
-
expect(ActiveRecord::Base.connection.foreign_keys(table).first.column).to eq(column)
|
118
|
+
expect(ActiveRecord::Base.connection.foreign_keys(table).first.column).to eq(column + "_id")
|
118
119
|
end
|
119
120
|
|
120
121
|
it "should allow multiple foreign keys to be made" do
|
121
122
|
recreate_table(@model) do |t|
|
122
|
-
t.
|
123
|
-
t.
|
123
|
+
t.references :user, :references => :users
|
124
|
+
t.references :updater, :references => :users
|
124
125
|
end
|
125
126
|
expect(@model).to reference(:users, :id).on(:user_id)
|
126
127
|
expect(@model).to reference(:users, :id).on(:updater_id)
|
@@ -128,14 +129,14 @@ describe ActiveRecord::Migration do
|
|
128
129
|
|
129
130
|
it "should suppress foreign key" do
|
130
131
|
recreate_table(@model) do |t|
|
131
|
-
t.
|
132
|
+
t.references :member, :foreign_key => false
|
132
133
|
end
|
133
134
|
expect(@model).not_to reference.on(:member_id)
|
134
135
|
end
|
135
136
|
|
136
137
|
it "should suppress foreign key using shortcut" do
|
137
138
|
recreate_table(@model) do |t|
|
138
|
-
t.
|
139
|
+
t.references :member, :references => nil
|
139
140
|
end
|
140
141
|
expect(@model).not_to reference.on(:member_id)
|
141
142
|
end
|
@@ -177,7 +178,7 @@ describe ActiveRecord::Migration do
|
|
177
178
|
|
178
179
|
it "should create foreign key to the same table on parent_id" do
|
179
180
|
recreate_table(@model) do |t|
|
180
|
-
t.
|
181
|
+
t.references :parent, foreign_key: true
|
181
182
|
end
|
182
183
|
expect(@model).to reference(@model.table_name, :id).on(:parent_id)
|
183
184
|
end
|
@@ -195,14 +196,14 @@ describe ActiveRecord::Migration do
|
|
195
196
|
|
196
197
|
it "should create and detect on_update #{action.inspect}", if_action_supported do
|
197
198
|
recreate_table @model do |t|
|
198
|
-
t.
|
199
|
+
t.references :user, :foreign_key => { :on_update => action }
|
199
200
|
end
|
200
201
|
expect(@model).to reference.on(:user_id).on_update(action)
|
201
202
|
end
|
202
203
|
|
203
204
|
it "should create and detect on_update #{action.inspect} using shortcut", if_action_supported do
|
204
205
|
recreate_table @model do |t|
|
205
|
-
t.
|
206
|
+
t.references :user, :on_update => action
|
206
207
|
end
|
207
208
|
expect(@model).to reference.on(:user_id).on_update(action)
|
208
209
|
end
|
@@ -210,21 +211,21 @@ describe ActiveRecord::Migration do
|
|
210
211
|
it "should raise a not-implemented error for on_update => #{action}", if_action_unsupported do
|
211
212
|
expect {
|
212
213
|
recreate_table @model do |t|
|
213
|
-
t.
|
214
|
+
t.references :user, :foreign_key => { :on_update => action }
|
214
215
|
end
|
215
216
|
}.to raise_error(NotImplementedError)
|
216
217
|
end
|
217
218
|
|
218
219
|
it "should create and detect on_delete #{action.inspect}", if_action_supported do
|
219
220
|
recreate_table @model do |t|
|
220
|
-
t.
|
221
|
+
t.references :user, :foreign_key => { :on_delete => action }
|
221
222
|
end
|
222
223
|
expect(@model).to reference.on(:user_id).on_delete(action)
|
223
224
|
end
|
224
225
|
|
225
226
|
it "should create and detect on_delete #{action.inspect} using shortcut", if_action_supported do
|
226
227
|
recreate_table @model do |t|
|
227
|
-
t.
|
228
|
+
t.references :user, :on_delete => action
|
228
229
|
end
|
229
230
|
expect(@model).to reference.on(:user_id).on_delete(action)
|
230
231
|
end
|
@@ -232,7 +233,7 @@ describe ActiveRecord::Migration do
|
|
232
233
|
it "should raise a not-implemented error for on_delete => #{action}", if_action_unsupported do
|
233
234
|
expect {
|
234
235
|
recreate_table @model do |t|
|
235
|
-
t.
|
236
|
+
t.references :user, :foreign_key => { :on_delete => action }
|
236
237
|
end
|
237
238
|
}.to raise_error(NotImplementedError)
|
238
239
|
end
|
@@ -242,7 +243,7 @@ describe ActiveRecord::Migration do
|
|
242
243
|
[false, true, :initially_deferred].each do |status|
|
243
244
|
it "should create and detect deferrable #{status.inspect}", :mysql => :skip do
|
244
245
|
recreate_table @model do |t|
|
245
|
-
t.
|
246
|
+
t.references :user, :on_delete => :cascade, :deferrable => status
|
246
247
|
end
|
247
248
|
expect(@model).to reference.on(:user_id).deferrable(status)
|
248
249
|
end
|
@@ -251,7 +252,7 @@ describe ActiveRecord::Migration do
|
|
251
252
|
it "should use default on_delete action" do
|
252
253
|
with_fk_config(:on_delete => :cascade) do
|
253
254
|
recreate_table @model do |t|
|
254
|
-
t.
|
255
|
+
t.references :user, foreign_key: true
|
255
256
|
end
|
256
257
|
expect(@model).to reference.on(:user_id).on_delete(:cascade)
|
257
258
|
end
|
@@ -260,7 +261,7 @@ describe ActiveRecord::Migration do
|
|
260
261
|
it "should override on_update action per table" do
|
261
262
|
with_fk_config(:on_update => :cascade) do
|
262
263
|
recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
|
263
|
-
t.
|
264
|
+
t.references :user, foreign_key: true
|
264
265
|
end
|
265
266
|
expect(@model).to reference.on(:user_id).on_update(:restrict)
|
266
267
|
end
|
@@ -269,7 +270,7 @@ describe ActiveRecord::Migration do
|
|
269
270
|
it "should override on_delete action per table" do
|
270
271
|
with_fk_config(:on_delete => :cascade) do
|
271
272
|
recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
|
272
|
-
t.
|
273
|
+
t.references :user, foreign_key: true
|
273
274
|
end
|
274
275
|
expect(@model).to reference.on(:user_id).on_delete(:restrict)
|
275
276
|
end
|
@@ -278,7 +279,7 @@ describe ActiveRecord::Migration do
|
|
278
279
|
it "should override on_update action per column" do
|
279
280
|
with_fk_config(:on_update => :cascade) do
|
280
281
|
recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
|
281
|
-
t.
|
282
|
+
t.references :user, :foreign_key => { :on_update => :nullify }
|
282
283
|
end
|
283
284
|
expect(@model).to reference.on(:user_id).on_update(:nullify)
|
284
285
|
end
|
@@ -287,7 +288,7 @@ describe ActiveRecord::Migration do
|
|
287
288
|
it "should override on_delete action per column" do
|
288
289
|
with_fk_config(:on_delete => :cascade) do
|
289
290
|
recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
|
290
|
-
t.
|
291
|
+
t.references :user, :foreign_key => { :on_delete => :nullify }
|
291
292
|
end
|
292
293
|
expect(@model).to reference.on(:user_id).on_delete(:nullify)
|
293
294
|
end
|
@@ -296,7 +297,7 @@ describe ActiveRecord::Migration do
|
|
296
297
|
it "should raise an error for an invalid on_update action" do
|
297
298
|
expect {
|
298
299
|
recreate_table @model do |t|
|
299
|
-
t.
|
300
|
+
t.references :user, :foreign_key => { :on_update => :invalid }
|
300
301
|
end
|
301
302
|
}.to raise_error(ArgumentError)
|
302
303
|
end
|
@@ -304,7 +305,7 @@ describe ActiveRecord::Migration do
|
|
304
305
|
it "should raise an error for an invalid on_delete action" do
|
305
306
|
expect {
|
306
307
|
recreate_table @model do |t|
|
307
|
-
t.
|
308
|
+
t.references :user, :foreign_key => { :on_delete => :invalid }
|
308
309
|
end
|
309
310
|
}.to raise_error(ArgumentError)
|
310
311
|
end
|
@@ -315,12 +316,15 @@ describe ActiveRecord::Migration do
|
|
315
316
|
before(:each) do
|
316
317
|
@model = Post
|
317
318
|
end
|
318
|
-
|
319
|
+
# Disabling bulk tests on mysql since the mysql adapter does not implement it for add_foreign_key in rails 5.1+
|
320
|
+
bulk_tests = RSpec.configuration.exclusion_filter[:mysql] == :skip ? [false] : [true, false]
|
321
|
+
|
322
|
+
bulk_tests.each do |bulk|
|
319
323
|
suffix = bulk ? ' with :bulk option' : ""
|
320
324
|
|
321
325
|
it "should create a foreign key constraint"+suffix, :sqlite3 => :skip do
|
322
326
|
change_table(@model, :bulk => bulk) do |t|
|
323
|
-
t.
|
327
|
+
t.references :user, foreign_key: true
|
324
328
|
end
|
325
329
|
expect(@model).to reference(:users, :id).on(:user_id)
|
326
330
|
end
|
@@ -329,10 +333,10 @@ describe ActiveRecord::Migration do
|
|
329
333
|
it "should remove a foreign key constraint"+suffix, :sqlite3 => :skip do
|
330
334
|
Comment.reset_column_information
|
331
335
|
expect(Comment).to reference(:users, :id).on(:user_id)
|
332
|
-
migration = Class.new ::ActiveRecord::Migration do
|
336
|
+
migration = Class.new ::ActiveRecord::Migration.latest_version do
|
333
337
|
define_method(:change) {
|
334
338
|
change_table("comments", :bulk => bulk) do |t|
|
335
|
-
t.
|
339
|
+
t.references :user, foreign_key: true
|
336
340
|
end
|
337
341
|
}
|
338
342
|
end
|
@@ -346,7 +350,7 @@ describe ActiveRecord::Migration do
|
|
346
350
|
|
347
351
|
it "should create a foreign key constraint using :references"+suffix, :sqlite3 => :skip do
|
348
352
|
change_table(@model, :bulk => bulk) do |t|
|
349
|
-
t.
|
353
|
+
t.send :bigint, :user_id, foreign_key: true
|
350
354
|
end
|
351
355
|
expect(@model).to reference(:users, :id).on(:user_id)
|
352
356
|
end
|
@@ -367,19 +371,19 @@ describe ActiveRecord::Migration do
|
|
367
371
|
end
|
368
372
|
|
369
373
|
it "should create foreign key" do
|
370
|
-
add_column(:post_id, :
|
374
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
371
375
|
expect(@model).to reference(:posts, :id).on(:post_id)
|
372
376
|
end
|
373
377
|
end
|
374
378
|
|
375
379
|
it "should create foreign key to explicitly given table" do
|
376
|
-
add_column(:author_id, :
|
380
|
+
add_column(:author_id, :bigint, :foreign_key => { :references => :users }) do
|
377
381
|
expect(@model).to reference(:users, :id).on(:author_id)
|
378
382
|
end
|
379
383
|
end
|
380
384
|
|
381
385
|
it "should create foreign key to explicitly given table using shortcut" do
|
382
|
-
add_column(:author_id, :
|
386
|
+
add_column(:author_id, :bigint, :references => :users) do
|
383
387
|
expect(@model).to reference(:users, :id).on(:author_id)
|
384
388
|
end
|
385
389
|
end
|
@@ -391,14 +395,14 @@ describe ActiveRecord::Migration do
|
|
391
395
|
end
|
392
396
|
|
393
397
|
it "should create foreign key to the same table on parent_id" do
|
394
|
-
add_column(:parent_id, :
|
398
|
+
add_column(:parent_id, :bigint, foreign_key: true) do
|
395
399
|
expect(@model).to reference(@model.table_name, :id).on(:parent_id)
|
396
400
|
end
|
397
401
|
end
|
398
402
|
|
399
403
|
it "should use default on_update action" do
|
400
404
|
SchemaPlus::ForeignKeys.config.on_update = :cascade
|
401
|
-
add_column(:post_id, :
|
405
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
402
406
|
expect(@model).to reference.on(:post_id).on_update(:cascade)
|
403
407
|
end
|
404
408
|
SchemaPlus::ForeignKeys.config.on_update = nil
|
@@ -406,7 +410,7 @@ describe ActiveRecord::Migration do
|
|
406
410
|
|
407
411
|
it "should use default on_delete action" do
|
408
412
|
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
409
|
-
add_column(:post_id, :
|
413
|
+
add_column(:post_id, :bigint, foreign_key: true) do
|
410
414
|
expect(@model).to reference.on(:post_id).on_delete(:cascade)
|
411
415
|
end
|
412
416
|
SchemaPlus::ForeignKeys.config.on_delete = nil
|
@@ -415,22 +419,22 @@ describe ActiveRecord::Migration do
|
|
415
419
|
it "should allow to overwrite default actions" do
|
416
420
|
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
417
421
|
SchemaPlus::ForeignKeys.config.on_update = :restrict
|
418
|
-
add_column(:post_id, :
|
422
|
+
add_column(:post_id, :bigint, :foreign_key => { :on_update => :nullify, :on_delete => :nullify}) do
|
419
423
|
expect(@model).to reference.on(:post_id).on_delete(:nullify).on_update(:nullify)
|
420
424
|
end
|
421
425
|
SchemaPlus::ForeignKeys.config.on_delete = nil
|
422
426
|
end
|
423
427
|
|
424
428
|
it "should create foreign key with default name" do
|
425
|
-
add_column(:post_id, :
|
426
|
-
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+/)
|
427
431
|
end
|
428
432
|
end
|
429
433
|
|
430
434
|
protected
|
431
|
-
def add_column(column_name,
|
435
|
+
def add_column(column_name, type, **options)
|
432
436
|
table = @model.table_name
|
433
|
-
ActiveRecord::Migration.add_column(table, column_name,
|
437
|
+
ActiveRecord::Migration.add_column(table, column_name, type, **options)
|
434
438
|
@model.reset_column_information
|
435
439
|
yield if block_given?
|
436
440
|
ActiveRecord::Migration.remove_column(table, column_name)
|
@@ -456,7 +460,7 @@ describe ActiveRecord::Migration do
|
|
456
460
|
|
457
461
|
before(:each) do
|
458
462
|
recreate_table @model do |t|
|
459
|
-
t.
|
463
|
+
t.references :user, foreign_key: true
|
460
464
|
end
|
461
465
|
end
|
462
466
|
|
@@ -465,22 +469,22 @@ describe ActiveRecord::Migration do
|
|
465
469
|
end
|
466
470
|
|
467
471
|
it "should drop foreign key if it is no longer valid" do
|
468
|
-
change_column :user_id, :
|
472
|
+
change_column :user_id, :bigint, :foreign_key => { :references => :members }
|
469
473
|
expect(@model).not_to reference(:users)
|
470
474
|
end
|
471
475
|
|
472
476
|
it "should drop foreign key if requested to do so" do
|
473
|
-
change_column :user_id, :
|
477
|
+
change_column :user_id, :bigint, :foreign_key => { :references => nil }
|
474
478
|
expect(@model).not_to reference(:users)
|
475
479
|
end
|
476
480
|
|
477
481
|
it "should reference pointed table afterwards if new one is created" do
|
478
|
-
change_column :user_id, :
|
482
|
+
change_column :user_id, :bigint, :foreign_key => { :references => :members }
|
479
483
|
expect(@model).to reference(:members)
|
480
484
|
end
|
481
485
|
|
482
486
|
it "should maintain foreign key if it's unaffected by change" do
|
483
|
-
change_column :user_id, :
|
487
|
+
change_column :user_id, :bigint, :default => 0
|
484
488
|
expect(@model).to reference(:users)
|
485
489
|
end
|
486
490
|
|
@@ -489,10 +493,10 @@ describe ActiveRecord::Migration do
|
|
489
493
|
end
|
490
494
|
|
491
495
|
protected
|
492
|
-
def change_column(column_name,
|
496
|
+
def change_column(column_name, type, **options)
|
493
497
|
table = @model.table_name
|
494
498
|
ActiveRecord::Migration.suppress_messages do
|
495
|
-
ActiveRecord::Migration.change_column(table, column_name,
|
499
|
+
ActiveRecord::Migration.change_column(table, column_name, type, **options)
|
496
500
|
@model.reset_column_information
|
497
501
|
end
|
498
502
|
end
|
@@ -503,7 +507,7 @@ describe ActiveRecord::Migration do
|
|
503
507
|
before(:each) do
|
504
508
|
@model = Comment
|
505
509
|
recreate_table @model do |t|
|
506
|
-
t.
|
510
|
+
t.references :post, foreign_key: true
|
507
511
|
end
|
508
512
|
end
|
509
513
|
|
@@ -523,34 +527,13 @@ describe ActiveRecord::Migration do
|
|
523
527
|
end
|
524
528
|
end
|
525
529
|
|
526
|
-
|
527
|
-
context "when table is renamed" do
|
528
|
-
|
529
|
-
before(:each) do
|
530
|
-
@model = Comment
|
531
|
-
recreate_table @model do |t|
|
532
|
-
t.integer :user_id, foreign_key: true
|
533
|
-
t.integer :xyz, :index => true
|
534
|
-
end
|
535
|
-
ActiveRecord::Migration.suppress_messages do
|
536
|
-
ActiveRecord::Migration.rename_table @model.table_name, :newname
|
537
|
-
end
|
538
|
-
end
|
539
|
-
|
540
|
-
it "should rename foreign key constraints", :sqlite3 => :skip do
|
541
|
-
expect(ActiveRecord::Base.connection.foreign_keys(:newname).first.name).to match(/newname/)
|
542
|
-
end
|
543
|
-
|
544
|
-
end
|
545
|
-
|
546
|
-
|
547
530
|
context "when table with more than one fk constraint is renamed", :sqlite3 => :skip do
|
548
531
|
|
549
532
|
before(:each) do
|
550
533
|
@model = Comment
|
551
534
|
recreate_table @model do |t|
|
552
|
-
t.
|
553
|
-
t.
|
535
|
+
t.references :user
|
536
|
+
t.references :member
|
554
537
|
end
|
555
538
|
ActiveRecord::Migration.suppress_messages do
|
556
539
|
ActiveRecord::Migration.rename_table @model.table_name, :newname
|
@@ -565,17 +548,16 @@ describe ActiveRecord::Migration do
|
|
565
548
|
|
566
549
|
def recreate_table(model, opts={}, &block)
|
567
550
|
ActiveRecord::Migration.suppress_messages do
|
568
|
-
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
|
569
552
|
end
|
570
553
|
model.reset_column_information
|
571
554
|
end
|
572
555
|
|
573
556
|
def change_table(model, opts={}, &block)
|
574
557
|
ActiveRecord::Migration.suppress_messages do
|
575
|
-
ActiveRecord::Migration.change_table model.table_name, opts, &block
|
558
|
+
ActiveRecord::Migration.change_table model.table_name, **opts, &block
|
576
559
|
end
|
577
560
|
model.reset_column_information
|
578
561
|
end
|
579
562
|
|
580
563
|
end
|
581
|
-
|