schema_plus_foreign_keys 0.1.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/prs.yml +134 -0
  3. data/.gitignore +1 -0
  4. data/.simplecov +20 -0
  5. data/Gemfile +4 -1
  6. data/README.md +25 -18
  7. data/Rakefile +3 -1
  8. data/gemfiles/Gemfile.base +1 -1
  9. data/gemfiles/activerecord-5.2/Gemfile.base +2 -1
  10. data/gemfiles/activerecord-5.2/Gemfile.mysql2 +2 -2
  11. data/gemfiles/activerecord-5.2/Gemfile.postgresql +2 -2
  12. data/gemfiles/activerecord-5.2/Gemfile.sqlite3 +3 -3
  13. data/gemfiles/activerecord-6.0/Gemfile.base +4 -0
  14. data/gemfiles/activerecord-6.0/Gemfile.mysql2 +10 -0
  15. data/gemfiles/activerecord-6.0/Gemfile.postgresql +10 -0
  16. data/gemfiles/{activerecord-4.2.0 → activerecord-6.0}/Gemfile.sqlite3 +3 -3
  17. data/lib/schema_plus/foreign_keys/active_record/base.rb +2 -0
  18. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract/schema_creation.rb +2 -11
  19. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb +7 -47
  20. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/foreign_key_definition.rb +4 -35
  21. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb +15 -16
  22. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/postgresql_adapter.rb +8 -6
  23. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +8 -6
  24. data/lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb +7 -46
  25. data/lib/schema_plus/foreign_keys/active_record/migration/command_recorder.rb +4 -2
  26. data/lib/schema_plus/foreign_keys/middleware/dumper.rb +4 -2
  27. data/lib/schema_plus/foreign_keys/middleware/migration.rb +13 -5
  28. data/lib/schema_plus/foreign_keys/middleware/model.rb +2 -0
  29. data/lib/schema_plus/foreign_keys/middleware/mysql.rb +3 -1
  30. data/lib/schema_plus/foreign_keys/middleware/sql.rb +2 -16
  31. data/lib/schema_plus/foreign_keys/version.rb +3 -1
  32. data/lib/schema_plus/foreign_keys.rb +2 -1
  33. data/lib/schema_plus_foreign_keys.rb +2 -0
  34. data/schema_dev.yml +4 -6
  35. data/schema_plus_foreign_keys.gemspec +10 -9
  36. data/spec/deprecation_spec.rb +11 -110
  37. data/spec/foreign_key_definition_spec.rb +5 -3
  38. data/spec/foreign_key_spec.rb +22 -20
  39. data/spec/migration_spec.rb +35 -59
  40. data/spec/named_schemas_spec.rb +16 -14
  41. data/spec/schema_dumper_spec.rb +13 -13
  42. data/spec/spec_helper.rb +4 -2
  43. data/spec/support/reference.rb +3 -2
  44. metadata +31 -89
  45. data/.travis.yml +0 -33
  46. data/gemfiles/activerecord-4.2.0/Gemfile.base +0 -3
  47. data/gemfiles/activerecord-4.2.0/Gemfile.mysql2 +0 -10
  48. data/gemfiles/activerecord-4.2.0/Gemfile.postgresql +0 -10
  49. data/gemfiles/activerecord-4.2.1/Gemfile.base +0 -3
  50. data/gemfiles/activerecord-4.2.1/Gemfile.mysql2 +0 -10
  51. data/gemfiles/activerecord-4.2.1/Gemfile.postgresql +0 -10
  52. data/gemfiles/activerecord-4.2.1/Gemfile.sqlite3 +0 -10
  53. data/gemfiles/activerecord-4.2.6/Gemfile.base +0 -3
  54. data/gemfiles/activerecord-4.2.6/Gemfile.mysql2 +0 -10
  55. data/gemfiles/activerecord-4.2.6/Gemfile.postgresql +0 -10
  56. data/gemfiles/activerecord-4.2.6/Gemfile.sqlite3 +0 -10
  57. data/gemfiles/activerecord-5.0/Gemfile.base +0 -3
  58. data/gemfiles/activerecord-5.0/Gemfile.mysql2 +0 -10
  59. data/gemfiles/activerecord-5.0/Gemfile.postgresql +0 -10
  60. data/gemfiles/activerecord-5.0/Gemfile.sqlite3 +0 -10
  61. data/gemfiles/activerecord-5.1/Gemfile.base +0 -3
  62. data/gemfiles/activerecord-5.1/Gemfile.mysql2 +0 -10
  63. data/gemfiles/activerecord-5.1/Gemfile.postgresql +0 -10
  64. data/gemfiles/activerecord-5.1/Gemfile.sqlite3 +0 -10
@@ -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, :force => true do |t|
12
+ create_table :users, force: true do |t|
11
13
  t.string :login
12
14
  end
13
- create_table :comments, :force => true do |t|
15
+ create_table :comments, force: true do |t|
14
16
  t.references :user
15
- t.foreign_key :user_id, :users
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, :force => true do |t|
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, :force => true do |t|
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, :force => true do |t|
50
+ create_table :comments, force: true do |t|
49
51
  t.text :body
50
52
  t.references :post
51
- t.foreign_key :post_id, :posts
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", :sqlite3 => :skip do
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, :column => :author_id, :on_update => :cascade, :on_delete => :restrict)
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(&its.column == 'post_id') and fk.name }
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(&its.column == 'post_id') and fk.name }
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", :if_exists => true)}.to_not raise_error
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, :force => true do |t|
153
- t.references :post, :foreign_key => false
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", :sqlite3 => :only do
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, :column => :author_id, :on_update => :cascade, :on_delete => :restrict)
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
@@ -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, :force => true do |t|
13
- t.string :login, :index => { :unique => true }
10
+ create_table :users, force: true do |t|
11
+ t.string :login, index: { unique: true }
14
12
  end
15
13
 
16
- create_table :members, :force => true do |t|
14
+ create_table :members, force: true do |t|
17
15
  t.string :login
18
16
  end
19
17
 
20
- create_table :comments, :force => true do |t|
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 :user_id, :users, :primary_key => :id
22
+ t.foreign_key :users, column: :user_id, primary_key: :id
25
23
  end
26
24
 
27
- create_table :posts, :force => true do |t|
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", :sqlite3 => :only do
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 column_type, :user, :foreign_key => true
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 column_type, :user, :foreign_key => true
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, :foreign_key => { :references => :users }
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 = { :references => :users }
72
+ hash = { references: :users }
75
73
  hash_original = hash.dup
76
74
  recreate_table(@model) do |t|
77
- t.references :author, :foreign_key => hash
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("fk_#{@model.table_name}_user_id")
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
@@ -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 column_type, :user_id, foreign_key: true
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, column_type, foreign_key: true) do
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, column_type, :foreign_key => { :references => :users }) do
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, column_type, :references => :users) do
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, column_type, foreign_key: true) do
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, column_type, foreign_key: true) do
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, column_type, foreign_key: true) do
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, column_type, :foreign_key => { :on_update => :nullify, :on_delete => :nullify}) do
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, column_type, foreign_key: true) do
432
- expect(@model).to reference(:posts, :id).with_name("fk_#{@model.table_name}_post_id")
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, *args)
435
+ def add_column(column_name, type, **options)
438
436
  table = @model.table_name
439
- ActiveRecord::Migration.add_column(table, column_name, *args)
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, column_type, :foreign_key => { :references => :members }
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, column_type, :foreign_key => { :references => nil }
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, column_type, :foreign_key => { :references => :members }
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, column_type, :default => 0
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, *args)
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, *args)
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
-
@@ -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, :force => true do |t|
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, :force => true do |t|
60
- t.references :user, :foreign_key => false
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, :force => true do |t|
70
- t.references :user, :foreign_key => true
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", :force => true do |t|
85
+ create_table "items", force: true do |t|
84
86
  end
85
- create_table "schema_plus_test2.groups", :force => true do |t|
87
+ create_table "schema_plus_test2.groups", force: true do |t|
86
88
  end
87
- create_table "schema_plus_test2.members", :force => true do |t|
88
- t.references :item, :foreign_key => true unless SchemaDev::Rspec::Helpers.mysql?
89
- t.references :group, :foreign_key => { references: "schema_plus_test2.groups" }
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", :mysql => :skip do
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 = ["fk_schema_plus_test2_members_group_id"]
131
- expected_names << "fk_schema_plus_test2_members_item_id" unless SchemaDev::Rspec::Helpers.mysql?
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
@@ -1,21 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'stringio'
3
5
 
4
6
  describe "Schema dump" do
5
- let(:column_type) { Gem::Requirement.new('< 5.1').satisfied_by?(::ActiveRecord.version) ? :integer : :bigint }
6
-
7
7
  before(:each) do
8
8
  ActiveRecord::Migration.suppress_messages do
9
9
  ActiveRecord::Schema.define do
10
10
  connection.tables_only.each do |table| drop_table table, force: :cascade end
11
11
 
12
- create_table :users, :force => true do |t|
12
+ create_table :users, force: true do |t|
13
13
  t.string :login
14
14
  t.datetime :deleted_at
15
15
  t.references :first_post, index: { unique: true }
16
16
  end
17
17
 
18
- create_table :posts, :force => true do |t|
18
+ create_table :posts, force: true do |t|
19
19
  t.text :body
20
20
  t.references :user
21
21
  t.references :first_comment
@@ -33,7 +33,7 @@ describe "Schema dump" do
33
33
  t.boolean :boolean_col
34
34
  end
35
35
 
36
- create_table :comments, :force => true do |t|
36
+ create_table :comments, force: true do |t|
37
37
  t.text :body
38
38
  t.references :post
39
39
  t.references :commenter
@@ -59,7 +59,7 @@ describe "Schema dump" do
59
59
  end
60
60
 
61
61
  it "should include foreign_key name" do
62
- with_foreign_key Post, :user_id, :users, :id, :name => "yippee" do
62
+ with_foreign_key Post, :user_id, :users, :id, name: "yippee" do
63
63
  expect(dump_posts).to match(/user_id.*foreign_key.*users.*:name=>"yippee"/)
64
64
  end
65
65
  end
@@ -72,7 +72,7 @@ describe "Schema dump" do
72
72
 
73
73
 
74
74
  it "should include foreign_key exactly once" do
75
- with_foreign_key Post, :user_id, :users, :id, :name => "yippee" do
75
+ with_foreign_key Post, :user_id, :users, :id, name: "yippee" do
76
76
  expect(dump_posts.scan(/foreign_key.*yippee"/).length).to eq 1
77
77
  end
78
78
  end
@@ -98,7 +98,7 @@ describe "Schema dump" do
98
98
 
99
99
  it "should handle regexp in ignore_tables" do
100
100
  with_foreign_key Comment, :post_id, :posts, :id do
101
- dump = dump_schema(:ignore => /post/)
101
+ dump = dump_schema(ignore: /post/)
102
102
  expect(dump).to match(/create_table "comments"/)
103
103
  expect(dump).not_to match(/create_table "posts"/)
104
104
  end
@@ -107,12 +107,12 @@ describe "Schema dump" do
107
107
  end
108
108
 
109
109
  it "should include foreign_key options" do
110
- with_foreign_key Post, :user_id, :users, :id, :on_update => :cascade, :on_delete => :nullify do
111
- expect(dump_posts).to match(%q[t.(integer|bigint)\s*"user_id",.*:foreign_key=>{:references=>"users", :name=>"fk_posts_user_id", :on_update=>:cascade, :on_delete=>:nullify}])
110
+ with_foreign_key Post, :user_id, :users, :id, on_update: :cascade, on_delete: :nullify do
111
+ expect(dump_posts).to match(%q[t.(integer|bigint)\s*"user_id",.*:foreign_key=>{:references=>"users", :name=>"fk_rails_\w+", :on_update=>:cascade, :on_delete=>:nullify}])
112
112
  end
113
113
  end
114
114
 
115
- context "with cyclic foreign key constraints", :sqlite3 => :skip do
115
+ context "with cyclic foreign key constraints", sqlite3: :skip do
116
116
  before(:each) do
117
117
  ActiveRecord::Base.connection.add_foreign_key(Comment.table_name, User.table_name, column: :commenter_id)
118
118
  ActiveRecord::Base.connection.add_foreign_key(Comment.table_name, Post.table_name, column: :post_id)
@@ -216,12 +216,12 @@ describe "Schema dump" do
216
216
  def with_foreign_keys(model, columnsets)
217
217
  table_columns = model.columns.reject{|column| column.name == 'id'}
218
218
  ActiveRecord::Migration.suppress_messages do
219
- ActiveRecord::Migration.create_table model.table_name, :force => true do |t|
219
+ ActiveRecord::Migration.create_table model.table_name, force: true do |t|
220
220
  table_columns.each do |column|
221
221
  t.column column.name, column.type, limit: column.limit
222
222
  end
223
223
  columnsets.each do |columns, referenced_table_name, referenced_columns, options|
224
- t.foreign_key columns, referenced_table_name, (options||{}).merge(primary_key: referenced_columns)
224
+ t.foreign_key referenced_table_name, **(options||{}).merge(column: columns, primary_key: referenced_columns)
225
225
  end
226
226
  end
227
227
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simplecov'
2
- require 'simplecov-gem-profile'
3
- SimpleCov.start "gem"
4
+
5
+ SimpleCov.start
4
6
 
5
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SchemaPlus::Matchers
2
4
 
3
5
  class Reference
@@ -22,7 +24,7 @@ module SchemaPlus::Matchers
22
24
  @result.keep_if {|fk| fk.on_update == @on_update } if @on_update
23
25
  @result.keep_if {|fk| fk.on_delete == @on_delete } if @on_delete
24
26
  @result.keep_if {|fk| fk.deferrable == @deferrable } if @deferrable
25
- @result.keep_if {|fk| fk.name == @name } if @name
27
+ @result.keep_if {|fk| @name.is_a?(String) ? fk.name == @name : fk.name.match?(@name) } if @name
26
28
  !@result.empty?
27
29
  end
28
30
 
@@ -76,4 +78,3 @@ module SchemaPlus::Matchers
76
78
  end
77
79
 
78
80
  end
79
-