schema_auto_foreign_keys 0.1.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/prs.yml +173 -0
  3. data/.simplecov +19 -0
  4. data/Gemfile +2 -1
  5. data/README.md +20 -8
  6. data/Rakefile +1 -1
  7. data/gemfiles/Gemfile.base +1 -1
  8. data/gemfiles/activerecord-5.2/Gemfile.base +4 -0
  9. data/gemfiles/activerecord-5.2/Gemfile.mysql2 +10 -0
  10. data/gemfiles/activerecord-5.2/Gemfile.postgresql +10 -0
  11. data/gemfiles/{activerecord-4.2.0 → activerecord-5.2}/Gemfile.sqlite3 +3 -3
  12. data/gemfiles/activerecord-6.0/Gemfile.base +4 -0
  13. data/gemfiles/activerecord-6.0/Gemfile.mysql2 +10 -0
  14. data/gemfiles/activerecord-6.0/Gemfile.postgresql +10 -0
  15. data/gemfiles/{activerecord-4.2.1 → activerecord-6.0}/Gemfile.sqlite3 +3 -3
  16. data/gemfiles/activerecord-6.1/Gemfile.base +4 -0
  17. data/gemfiles/activerecord-6.1/Gemfile.mysql2 +10 -0
  18. data/gemfiles/activerecord-6.1/Gemfile.postgresql +10 -0
  19. data/gemfiles/activerecord-6.1/Gemfile.sqlite3 +10 -0
  20. data/gemfiles/activerecord-7.0/Gemfile.base +4 -0
  21. data/gemfiles/activerecord-7.0/Gemfile.mysql2 +10 -0
  22. data/gemfiles/activerecord-7.0/Gemfile.postgresql +10 -0
  23. data/gemfiles/activerecord-7.0/Gemfile.sqlite3 +10 -0
  24. data/lib/schema_auto_foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
  25. data/lib/schema_auto_foreign_keys/middleware/migration.rb +3 -3
  26. data/lib/schema_auto_foreign_keys/middleware/schema.rb +1 -1
  27. data/lib/schema_auto_foreign_keys/version.rb +1 -1
  28. data/lib/schema_auto_foreign_keys.rb +2 -3
  29. data/schema_auto_foreign_keys.gemspec +8 -8
  30. data/schema_dev.yml +8 -3
  31. data/spec/migration_spec.rb +76 -72
  32. data/spec/schema_spec.rb +7 -7
  33. data/spec/spec_helper.rb +2 -3
  34. metadata +50 -64
  35. data/.travis.yml +0 -21
  36. data/gemfiles/activerecord-4.2.0/Gemfile.base +0 -3
  37. data/gemfiles/activerecord-4.2.0/Gemfile.mysql2 +0 -10
  38. data/gemfiles/activerecord-4.2.0/Gemfile.postgresql +0 -10
  39. data/gemfiles/activerecord-4.2.1/Gemfile.base +0 -3
  40. data/gemfiles/activerecord-4.2.1/Gemfile.mysql2 +0 -10
  41. data/gemfiles/activerecord-4.2.1/Gemfile.postgresql +0 -10
@@ -7,7 +7,7 @@ describe ActiveRecord::Migration do
7
7
  define_schema do
8
8
 
9
9
  create_table :users do |t|
10
- t.string :login, :index => { :unique => true }
10
+ t.string :login, index: { unique: true}
11
11
  end
12
12
 
13
13
  create_table :members do |t|
@@ -17,8 +17,8 @@ describe ActiveRecord::Migration do
17
17
  create_table :comments do |t|
18
18
  t.string :content
19
19
  t.integer :user
20
- t.integer :user_id
21
- t.foreign_key :user_id, :users, :primary_key => :id
20
+ t.bigint :user_id
21
+ t.foreign_key :users, column: :user_id
22
22
  end
23
23
 
24
24
  create_table :posts do |t|
@@ -28,10 +28,13 @@ describe ActiveRecord::Migration do
28
28
  class User < ::ActiveRecord::Base ; end
29
29
  class Post < ::ActiveRecord::Base ; end
30
30
  class Comment < ::ActiveRecord::Base ; end
31
+ User.reset_column_information
32
+ Post.reset_column_information
33
+ Comment.reset_column_information
31
34
  end
32
35
 
33
36
  around(:each) do |example|
34
- with_fk_config(:auto_create => true, :auto_index => true) { example.run }
37
+ with_fk_config(auto_create: true, auto_index: true) { example.run }
35
38
  end
36
39
 
37
40
  context "when table is created" do
@@ -42,14 +45,14 @@ describe ActiveRecord::Migration do
42
45
 
43
46
  it "creates auto foreign keys" do
44
47
  create_table(@model) do |t|
45
- t.integer :user_id
48
+ t.bigint :user_id
46
49
  end
47
50
  expect(@model).to reference(:users, :id).on(:user_id)
48
51
  end
49
52
 
50
53
  it "respects explicit foreign key" do
51
54
  create_table(@model) do |t|
52
- t.integer :author_id, :foreign_key => { :references => :users }
55
+ t.bigint :author_id, foreign_key: { references: :users }
53
56
  end
54
57
  expect(@model).to reference(:users, :id).on(:author_id)
55
58
  expect(@model).to have_index.on(:author_id)
@@ -57,7 +60,7 @@ describe ActiveRecord::Migration do
57
60
 
58
61
  it "suppresses auto foreign key" do
59
62
  create_table(@model) do |t|
60
- t.integer :member_id, :foreign_key => false
63
+ t.bigint :member_id, foreign_key: false
61
64
  end
62
65
  expect(@model).not_to reference.on(:member_id)
63
66
  expect(@model).not_to have_index.on(:member_id)
@@ -65,7 +68,7 @@ describe ActiveRecord::Migration do
65
68
 
66
69
  it "suppresses auto foreign key using shortcut" do
67
70
  create_table(@model) do |t|
68
- t.integer :member_id, :references => nil
71
+ t.bigint :member_id, references: nil
69
72
  end
70
73
  expect(@model).not_to reference.on(:member_id)
71
74
  expect(@model).not_to have_index.on(:member_id)
@@ -85,12 +88,12 @@ describe ActiveRecord::Migration do
85
88
  end
86
89
 
87
90
  it "does not create a foreign_key if polymorphic" do
88
- create_reference(reftype, :post, :polymorphic => true)
91
+ create_reference(reftype, :post, polymorphic: true)
89
92
  expect(@model).not_to reference(:posts, :id).on(:post_id)
90
93
  end
91
94
 
92
- it "does not create a foreign_key with :foreign_key => false" do
93
- create_reference(reftype, :post, :foreign_key => false)
95
+ it "does not create a foreign_key with foreign_key: false" do
96
+ create_reference(reftype, :post, foreign_key: false)
94
97
  expect(@model).not_to reference(:posts, :id).on(:post_id)
95
98
  end
96
99
 
@@ -100,25 +103,25 @@ describe ActiveRecord::Migration do
100
103
  end
101
104
 
102
105
  it "should create exactly one index explicitly (#157)" do
103
- create_reference(reftype, :post, :index => true)
106
+ create_reference(reftype, :post, index: true)
104
107
  expect(@model).to have_index.on(:post_id)
105
108
  end
106
109
 
107
110
  it "should respect :unique (#157)" do
108
- create_reference(reftype, :post, :index => :unique)
111
+ create_reference(reftype, :post, index: :unique)
109
112
  expect(@model).to have_unique_index.on(:post_id)
110
113
  end
111
114
 
112
115
  it "should create a two-column index if polymophic and index requested" do
113
- create_reference(reftype, :post, :polymorphic => true, :index => true)
116
+ create_reference(reftype, :post, polymorphic: true, index: true)
114
117
  expect(@model).to have_index.on([:post_id, :post_type])
115
118
  end
116
119
 
117
120
  protected
118
121
 
119
- def create_reference(reftype, column_name, *args)
122
+ def create_reference(reftype, column_name, **kwargs)
120
123
  create_table(@model) do |t|
121
- t.send reftype, column_name, *args
124
+ t.send reftype, column_name, **kwargs.merge(type: :bigint)
122
125
  end
123
126
  end
124
127
 
@@ -127,9 +130,9 @@ describe ActiveRecord::Migration do
127
130
 
128
131
  it "creates auto-index on foreign keys only" do
129
132
  create_table(@model) do |t|
130
- t.integer :user_id
131
- t.integer :application_id, :references => nil
132
- t.integer :state
133
+ t.bigint :user_id
134
+ t.bigint :application_id, references: nil
135
+ t.bigint :state
133
136
  end
134
137
  expect(@model).to have_index.on(:user_id)
135
138
  expect(@model).not_to have_index.on(:application_id)
@@ -141,52 +144,52 @@ describe ActiveRecord::Migration do
141
144
  column = ("co"*15 + "_id")
142
145
  expect {
143
146
  ActiveRecord::Migration.create_table table do |t|
144
- t.integer column, foreign_key: { references: :members, name: "verylong" }
147
+ t.bigint column, foreign_key: { references: :members, name: "verylong" }
145
148
  end
146
149
  }.not_to raise_error
147
150
  expect(ActiveRecord::Base.connection.indexes(table).first.columns.first).to eq column
148
151
  end
149
152
 
150
153
  it "overrides foreign key auto_create positively" do
151
- with_fk_config(:auto_create => false) do
152
- create_table @model, :foreign_keys => {:auto_create => true} do |t|
153
- t.integer :user_id
154
+ with_fk_config(auto_create: false) do
155
+ create_table @model, foreign_keys: { auto_create: true } do |t|
156
+ t.bigint :user_id
154
157
  end
155
158
  expect(@model).to reference(:users, :id).on(:user_id)
156
159
  end
157
160
  end
158
161
 
159
162
  it "overrides foreign key auto_create negatively" do
160
- with_fk_config(:auto_create => true) do
161
- create_table @model, :foreign_keys => {:auto_create => false} do |t|
162
- t.integer :user_id
163
+ with_fk_config(auto_create: true) do
164
+ create_table @model, foreign_keys: { auto_create: false } do |t|
165
+ t.bigint :user_id
163
166
  end
164
167
  expect(@model).not_to reference.on(:user_id)
165
168
  end
166
169
  end
167
170
 
168
171
  it "overrides foreign key auto_index positively" do
169
- with_fk_config(:auto_index => false) do
170
- create_table @model, :foreign_keys => {:auto_index => true} do |t|
171
- t.integer :user_id
172
+ with_fk_config(auto_index: false) do
173
+ create_table @model, foreign_keys: { auto_index: true } do |t|
174
+ t.bigint :user_id
172
175
  end
173
176
  expect(@model).to have_index.on(:user_id)
174
177
  end
175
178
  end
176
179
 
177
- it "overrides foreign key auto_index negatively", :mysql => :skip do
178
- with_fk_config(:auto_index => true) do
179
- create_table @model, :foreign_keys => {:auto_index => false} do |t|
180
- t.integer :user_id
180
+ it "overrides foreign key auto_index negatively", mysql: :skip do
181
+ with_fk_config(auto_index: true) do
182
+ create_table @model, foreign_keys: { auto_index: false } do |t|
183
+ t.bigint :user_id
181
184
  end
182
185
  expect(@model).not_to have_index.on(:user_id)
183
186
  end
184
187
  end
185
188
 
186
- it "disables auto-index for a column", :mysql => :skip do
187
- with_fk_config(:auto_index => true) do
189
+ it "disables auto-index for a column", mysql: :skip do
190
+ with_fk_config(auto_index: true) do
188
191
  create_table @model do |t|
189
- t.integer :user_id, :index => false
192
+ t.bigint :user_id, index: false
190
193
  end
191
194
  expect(@model).not_to have_index.on(:user_id)
192
195
  end
@@ -194,7 +197,7 @@ describe ActiveRecord::Migration do
194
197
 
195
198
  end
196
199
 
197
- context "when table is changed", :sqlite3 => :skip do
200
+ context "when table is changed", sqlite3: :skip do
198
201
  before(:each) do
199
202
  @model = Post
200
203
  end
@@ -202,8 +205,10 @@ describe ActiveRecord::Migration do
202
205
  suffix = bulk ? ' with :bulk option' : ""
203
206
 
204
207
  it "auto creates a foreign key constraint"+suffix do
205
- change_table(@model, :bulk => bulk) do |t|
206
- t.integer :user_id
208
+ # https://github.com/rails/rails/pull/35958 was never backported to Rails 5.2
209
+ skip("Not supported on MySQL with Rails 5.2") if bulk && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::Base.connection.adapter_name == "Mysql2"
210
+ change_table(@model, bulk: bulk) do |t|
211
+ t.bigint :user_id
207
212
  end
208
213
  expect(@model).to reference(:users, :id).on(:user_id)
209
214
  end
@@ -211,14 +216,14 @@ describe ActiveRecord::Migration do
211
216
  context "migrate down" do
212
217
  it "removes an auto foreign key and index"+suffix do
213
218
  create_table Comment do |t|
214
- t.integer :user_id
219
+ t.bigint :user_id
215
220
  end
216
221
  expect(Comment).to reference(:users, :id).on(:user_id)
217
222
  expect(Comment).to have_index.on(:user_id)
218
- migration = Class.new ::ActiveRecord::Migration do
223
+ migration = Class.new ::ActiveRecord::Migration::Current do
219
224
  define_method(:change) {
220
- change_table("comments", :bulk => bulk) do |t|
221
- t.integer :user_id
225
+ change_table("comments", bulk: bulk) do |t|
226
+ t.bigint :user_id
222
227
  end
223
228
  }
224
229
  end
@@ -231,83 +236,83 @@ describe ActiveRecord::Migration do
231
236
  end
232
237
  end
233
238
 
234
- context "when table is renamed", :postgresql => :only do
239
+ context "when table is renamed", postgresql: :only do
235
240
 
236
241
  before(:each) do
237
242
  @model = Comment
238
243
  create_table @model do |t|
239
- t.integer :user_id
240
- t.integer :xyz, :index => true
244
+ t.bigint :user_id
245
+ t.bigint :xyz, index: true
241
246
  end
242
247
  ActiveRecord::Migration.rename_table @model.table_name, :newname
243
248
  end
244
249
 
245
250
  it "should rename fk indexes" do
246
- index = ActiveRecord::Base.connection.indexes(:newname).find(&its.columns == ['user_id'])
251
+ index = ActiveRecord::Base.connection.indexes(:newname).find { |it| it.columns == ['user_id'] }
247
252
  expect(index.name).to match(/^fk__newname_/)
248
253
  end
249
254
 
250
255
  end
251
256
 
252
- context "when column is added", :sqlite3 => :skip do
257
+ context "when column is added", sqlite3: :skip do
253
258
 
254
259
  before(:each) do
255
260
  @model = Comment
256
261
  end
257
262
 
258
263
  it "auto creates foreign key" do
259
- add_column(:post_id, :integer) do
264
+ add_column(:post_id, :bigint) do
260
265
  expect(@model).to reference(:posts, :id).on(:post_id)
261
266
  end
262
267
  end
263
268
 
264
269
  it "respects explicit foreign key" do
265
- add_column(:author_id, :integer, :foreign_key => { :references => :users }) do
270
+ add_column(:author_id, :bigint, foreign_key: { references: :users }) do
266
271
  expect(@model).to reference(:users, :id).on(:author_id)
267
272
  end
268
273
  end
269
274
 
270
275
  it "doesn't create foreign key if column doesn't look like foreign key" do
271
- add_column(:views_count, :integer) do
276
+ add_column(:views_count, :bigint) do
272
277
  expect(@model).not_to reference.on(:views_count)
273
278
  end
274
279
  end
275
280
 
276
281
  it "doesn't create foreign key if declined explicitly" do
277
- add_column(:post_id, :integer, :foreign_key => false) do
282
+ add_column(:post_id, :bigint, foreign_key: false) do
278
283
  expect(@model).not_to reference.on(:post_id)
279
284
  end
280
285
  end
281
286
 
282
287
  it "shouldn't create foreign key if declined explicitly by shorthand" do
283
- add_column(:post_id, :integer, :references => nil) do
288
+ add_column(:post_id, :bigint, references: nil) do
284
289
  expect(@model).not_to reference.on(:post_id)
285
290
  end
286
291
  end
287
292
 
288
293
  it "creates auto index" do
289
- add_column(:post_id, :integer) do
294
+ add_column(:post_id, :bigint) do
290
295
  expect(@model).to have_index.on(:post_id)
291
296
  end
292
297
  end
293
298
 
294
299
  it "does not create auto-index for non-foreign keys" do
295
- add_column(:state, :integer) do
300
+ add_column(:state, :bigint) do
296
301
  expect(@model).not_to have_index.on(:state)
297
302
  end
298
303
  end
299
304
 
300
305
  # MySQL creates an index on foreign key and we can't override that
301
- it "doesn't create auto-index if declined explicitly", :mysql => :skip do
302
- add_column(:post_id, :integer, :index => false) do
306
+ it "doesn't create auto-index if declined explicitly", mysql: :skip do
307
+ add_column(:post_id, :bigint, index: false) do
303
308
  expect(@model).not_to have_index.on(:post_id)
304
309
  end
305
310
  end
306
311
 
307
312
  protected
308
- def add_column(column_name, *args)
313
+ def add_column(column_name, type, **kwargs)
309
314
  table = @model.table_name
310
- ActiveRecord::Migration.add_column(table, column_name, *args)
315
+ ActiveRecord::Migration.add_column(table, column_name, type, **kwargs)
311
316
  @model.reset_column_information
312
317
  yield if block_given?
313
318
  ActiveRecord::Migration.remove_column(table, column_name)
@@ -321,13 +326,13 @@ describe ActiveRecord::Migration do
321
326
  @model = Comment
322
327
  end
323
328
 
324
- context "with foreign keys", :sqlite3 => :skip do
329
+ context "with foreign keys", sqlite3: :skip do
325
330
 
326
331
  context "and initially references to users table" do
327
332
 
328
333
  before(:each) do
329
334
  create_table @model do |t|
330
- t.integer :user_id
335
+ t.bigint :user_id
331
336
  end
332
337
  end
333
338
 
@@ -336,13 +341,13 @@ describe ActiveRecord::Migration do
336
341
  end
337
342
 
338
343
  it "should drop foreign key if requested to do so" do
339
- change_column :user_id, :integer, :foreign_key => { :references => nil }
344
+ change_column :user_id, :bigint, foreign_key: { references: nil }
340
345
  expect(@model).not_to reference(:users)
341
346
  end
342
347
 
343
- it "should remove auto-created index if foreign key is removed", :mysql => :skip do
348
+ it "should remove auto-created index if foreign key is removed", mysql: :skip do
344
349
  expect(@model).to have_index.on(:user_id) # sanity check that index was auto-created
345
- change_column :user_id, :integer, :foreign_key => { :references => nil }
350
+ change_column :user_id, :bigint, foreign_key: { references: nil }
346
351
  expect(@model).not_to have_index.on(:user_id)
347
352
  end
348
353
 
@@ -351,7 +356,7 @@ describe ActiveRecord::Migration do
351
356
  context "if column defined without foreign key but with index" do
352
357
  before(:each) do
353
358
  create_table @model do |t|
354
- t.integer :user_id, :foreign_key => false, :index => true
359
+ t.bigint :user_id, foreign_key: false, index: true
355
360
  end
356
361
  end
357
362
 
@@ -360,7 +365,7 @@ describe ActiveRecord::Migration do
360
365
  end
361
366
 
362
367
  it "adding foreign key should not fail due to attempt to auto-create existing index" do
363
- expect { change_column :user_id, :integer, :foreign_key => true }.to_not raise_error
368
+ expect { change_column :user_id, :bigint, foreign_key: true }.to_not raise_error
364
369
  end
365
370
  end
366
371
  end
@@ -369,7 +374,7 @@ describe ActiveRecord::Migration do
369
374
 
370
375
  it "doesn't auto-add foreign keys" do
371
376
  create_table @model do |t|
372
- t.integer :user_id, :foreign_key => false
377
+ t.bigint :user_id, foreign_key: false
373
378
  t.string :other_column
374
379
  end
375
380
  with_fk_auto_create do
@@ -381,23 +386,22 @@ describe ActiveRecord::Migration do
381
386
  end
382
387
 
383
388
  protected
384
- def change_column(column_name, *args)
389
+ def change_column(column_name, type, **kwargs)
385
390
  table = @model.table_name
386
- ActiveRecord::Migration.change_column(table, column_name, *args)
391
+ ActiveRecord::Migration.change_column(table, column_name, type, **kwargs)
387
392
  @model.reset_column_information
388
393
  end
389
394
 
390
395
  end
391
396
 
392
397
  def create_table(model, opts={}, &block)
393
- ActiveRecord::Migration.create_table model.table_name, opts.merge(:force => true), &block
398
+ ActiveRecord::Migration.create_table model.table_name, **opts.merge(force: true, id: :bigint), &block
394
399
  model.reset_column_information
395
400
  end
396
401
 
397
402
  def change_table(model, opts={}, &block)
398
- ActiveRecord::Migration.change_table model.table_name, opts, &block
403
+ ActiveRecord::Migration.change_table model.table_name, **opts, &block
399
404
  model.reset_column_information
400
405
  end
401
406
 
402
407
  end
403
-
data/spec/schema_spec.rb CHANGED
@@ -10,19 +10,19 @@ describe ActiveRecord::Schema do
10
10
  with_fk_config(auto_create: true, auto_index: true) do
11
11
  ActiveRecord::Schema.define do
12
12
 
13
- create_table :users, :force => :cascade do
13
+ create_table :users, force: :cascade do
14
14
  end
15
15
 
16
- create_table :colors, :force => :cascade do
16
+ create_table :colors, force: :cascade do
17
17
  end
18
18
 
19
- create_table :shoes, :force => :cascade do
19
+ create_table :shoes, force: :cascade do
20
20
  end
21
21
 
22
- create_table :posts, :force => true do |t|
23
- t.integer :user_id, :references => :users, :index => true
24
- t.integer :shoe_id, :references => :shoes # should not have an index (except mysql)
25
- t.integer :color_id # should not have a foreign key nor index
22
+ create_table :posts, force: true do |t|
23
+ t.bigint :user_id, references: :users, index: true
24
+ t.bigint :shoe_id, references: :shoes # should not have an index (except mysql)
25
+ t.bigint :color_id # should not have a foreign key nor index
26
26
  end
27
27
  end
28
28
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'simplecov'
2
- require 'simplecov-gem-profile'
3
- SimpleCov.start "gem"
2
+ SimpleCov.start unless SimpleCov.running
4
3
 
5
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -37,7 +36,7 @@ def with_fk_config(opts={}, &block)
37
36
  end
38
37
 
39
38
  def with_fk_auto_create(value = true, &block)
40
- with_fk_config(:auto_create => value, &block)
39
+ with_fk_config(auto_create: value, &block)
41
40
  end
42
41
 
43
42
  def define_schema(&block)