schema_plus_foreign_keys 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +6 -0
- data/README.md +25 -19
- data/gemfiles/activerecord-4.2.0/Gemfile.postgresql +2 -2
- data/gemfiles/activerecord-4.2.1/Gemfile.postgresql +2 -2
- data/gemfiles/activerecord-4.2.6/Gemfile.postgresql +2 -2
- data/gemfiles/activerecord-5.0/Gemfile.base +1 -1
- data/gemfiles/activerecord-5.0/Gemfile.postgresql +2 -2
- data/gemfiles/activerecord-5.1/Gemfile.base +3 -0
- data/gemfiles/activerecord-5.1/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-5.1/Gemfile.postgresql +10 -0
- data/gemfiles/activerecord-5.1/Gemfile.sqlite3 +10 -0
- data/gemfiles/activerecord-5.2/Gemfile.base +3 -0
- data/gemfiles/activerecord-5.2/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-5.2/Gemfile.postgresql +10 -0
- data/gemfiles/activerecord-5.2/Gemfile.sqlite3 +10 -0
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb +4 -4
- data/lib/schema_plus/foreign_keys/middleware/sql.rb +1 -1
- data/lib/schema_plus/foreign_keys/version.rb +1 -1
- data/schema_dev.yml +2 -0
- data/schema_plus_foreign_keys.gemspec +1 -1
- data/spec/deprecation_spec.rb +6 -6
- data/spec/foreign_key_spec.rb +5 -5
- data/spec/migration_spec.rb +58 -52
- data/spec/named_schemas_spec.rb +4 -4
- data/spec/schema_dumper_spec.rb +23 -22
- metadata +13 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 237f5ea4424fd65009b719b0f147d91f0bf3ddecd81cfca4ec385b45cfbea3a4
|
4
|
+
data.tar.gz: 82818ed615ce5ded6b395a5f9cef23e77560e316fa00b88579d2791c7b7a9d19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a46e7c27b80fa7877ec1c788e8566e574b724d53ad676e5ae443c79668a545e569bb0dbf9ec28fcc137e44fc64b67b455a721d0b9d76cdff8cd90e6862bd9fe
|
7
|
+
data.tar.gz: 250f274ce03d0ceea35dccc7e1c9b4cc3775a6802a2a83f64028abd3e38b59cbf17fc502c282c1923389c6ff885c284f33f77c5b99f42d5dbcdb63c79fc08483
|
data/.travis.yml
CHANGED
@@ -19,6 +19,12 @@ gemfile:
|
|
19
19
|
- gemfiles/activerecord-5.0/Gemfile.mysql2
|
20
20
|
- gemfiles/activerecord-5.0/Gemfile.postgresql
|
21
21
|
- gemfiles/activerecord-5.0/Gemfile.sqlite3
|
22
|
+
- gemfiles/activerecord-5.1/Gemfile.mysql2
|
23
|
+
- gemfiles/activerecord-5.1/Gemfile.postgresql
|
24
|
+
- gemfiles/activerecord-5.1/Gemfile.sqlite3
|
25
|
+
- gemfiles/activerecord-5.2/Gemfile.mysql2
|
26
|
+
- gemfiles/activerecord-5.2/Gemfile.postgresql
|
27
|
+
- gemfiles/activerecord-5.2/Gemfile.sqlite3
|
22
28
|
env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
|
23
29
|
addons:
|
24
30
|
postgresql: '9.4'
|
data/README.md
CHANGED
@@ -31,41 +31,44 @@ gem.add_dependency "schema_plus_foreign_keys" # in a .gemspec
|
|
31
31
|
To declare a foreign key constraint for a column, use the `:foreign_key`
|
32
32
|
option. The same options can be used with `t.integer`, `t.references`, `t.belongs_to`, `t.foreign_key`, `change_column`, and `add_foreign_key`:
|
33
33
|
|
34
|
-
t.
|
35
|
-
t.
|
36
|
-
t.
|
34
|
+
t.references :author, foreign_key: true # create a foreign_key to table "authors"
|
35
|
+
t.references :author, foreign_key: {} # create a foreign_key to table "authors"
|
36
|
+
t.references :author, foreign_key: false # don't create a constraint (this is the default)
|
37
37
|
t.integer :author, foreign_key: true # create a foreign_key to table "authors"
|
38
38
|
|
39
|
-
t.
|
39
|
+
t.references :parent, foreign_key: true # special column parent_id defaults to referencing its own table
|
40
40
|
|
41
41
|
Specify the target table and its primary key using the `:references` and `:primary_key`:
|
42
42
|
|
43
|
-
t.
|
43
|
+
t.references :author, foreign_key: { references: :authors } # the default
|
44
44
|
t.integer :author, foreign_key: { references: :authors } # the default
|
45
|
-
t.
|
46
|
-
t.
|
47
|
-
t.
|
48
|
-
t.
|
49
|
-
t.
|
45
|
+
t.references :author, foreign_key: { references: :people } # choose table name
|
46
|
+
t.references :author, foreign_key: { primary_key: :ssn] } # choose primary key
|
47
|
+
t.references :author, foreign_key: { references: :people, primary_key: :ssn] } # choose both
|
48
|
+
t.references :author, foreign_key: { references: [:people, :ssn] } # shortcut for both
|
49
|
+
t.references :author, foreign_key: { references: nil } # same as foreign_key: false
|
50
50
|
|
51
|
-
You can also specify other attributes:
|
51
|
+
You can also specify other attributes that are supported by Rails:
|
52
52
|
|
53
|
-
t.
|
54
|
-
t.
|
55
|
-
t.
|
56
|
-
|
57
|
-
|
53
|
+
t.references :author, foreign_key: { name: "my_fk" } # override default auto-generated constraint name
|
54
|
+
t.references :author, foreign_key: { on_delete: :cascade }
|
55
|
+
t.references :author, foreign_key: { on_update: :set_null }
|
56
|
+
|
57
|
+
In addition, support for deferrable constraints is implemented:
|
58
|
+
|
59
|
+
t.references :author, foreign_key: { deferrable: true }
|
60
|
+
t.references :author, foreign_key: { deferrable: :initially_deferred }
|
58
61
|
|
59
62
|
Of course the options can be combined:
|
60
63
|
|
61
|
-
t.
|
64
|
+
t.references :author, foreign_key: { references: :people, primary_key: :ssn, name: "my_fk", on_delet: :no_action }
|
62
65
|
|
63
66
|
|
64
67
|
As a shorthand, all options except `:name` can be specified without placing
|
65
68
|
them in a `foreign_key` hash, e.g.
|
66
69
|
|
67
|
-
t.
|
68
|
-
t.
|
70
|
+
t.references :author, on_delete: :cascade # shorthand for foreign_key: { on_delete: :cascade }
|
71
|
+
t.references :author, references: :people # shorthand for foreign_key: { references: :people }
|
69
72
|
|
70
73
|
To remove a foreign key constraint, you can either change the column, specifying `foreign_key: false`, or use `migration.remove_foreign_key(table, column)`
|
71
74
|
|
@@ -145,11 +148,14 @@ SchemaPlus::ForeignKeys is tested on:
|
|
145
148
|
* ruby **2.3.1** with activerecord **4.2.1**, using **mysql2**, **sqlite3** or **postgresql**
|
146
149
|
* ruby **2.3.1** with activerecord **4.2.6**, using **mysql2**, **sqlite3** or **postgresql**
|
147
150
|
* ruby **2.3.1** with activerecord **5.0**, using **mysql2**, **sqlite3** or **postgresql**
|
151
|
+
* ruby **2.3.1** with activerecord **5.1**, using **mysql2**, **sqlite3** or **postgresql**
|
152
|
+
* ruby **2.3.1** with activerecord **5.2**, using **mysql2**, **sqlite3** or **postgresql**
|
148
153
|
|
149
154
|
<!-- SCHEMA_DEV: MATRIX - end -->
|
150
155
|
|
151
156
|
## History
|
152
157
|
|
158
|
+
* 0.1.8 - Compatibility with ActiveRecord 5.1 and ActiveRecord 5.2.
|
153
159
|
* 0.1.7 - Compatibility with ActiveRecord 5.0.
|
154
160
|
* 0.1.6 - Missing require
|
155
161
|
* 0.1.5 - Explicit gem dependencies
|
@@ -34,17 +34,17 @@ module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
|
34
34
|
# SchemaPlus::ForeignKeys also supports creation of foreign key constraints analogously, using Migration::ClassMethods#add_foreign_key or TableDefinition#foreign_key or as part of the column definition, for example:
|
35
35
|
#
|
36
36
|
# create_table :posts do |t| # not DRY
|
37
|
-
# t.
|
37
|
+
# t.references :author
|
38
38
|
# end
|
39
39
|
# add_foreign_key :posts, :author_id, :references => :authors
|
40
40
|
#
|
41
41
|
# create_table :posts do |t| # DRYer
|
42
|
-
# t.
|
42
|
+
# t.references :author
|
43
43
|
# t.foreign_key :author_id, :references => :authors
|
44
44
|
# end
|
45
45
|
#
|
46
46
|
# create_table :posts do |t| # Dryest
|
47
|
-
# t.
|
47
|
+
# t.references :author, :foreign_key => true
|
48
48
|
# end
|
49
49
|
#
|
50
50
|
# <b>NOTE:</b> In the standard configuration, SchemaPlus::ForeignKeys automatically
|
@@ -60,7 +60,7 @@ module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
|
60
60
|
# basis by passing Config options to Migration::ClassMethods#create_table, such as
|
61
61
|
#
|
62
62
|
# create_table :students, :foreign_keys => {:auto_create => false} do
|
63
|
-
# t.
|
63
|
+
# t.references :student
|
64
64
|
# end
|
65
65
|
#
|
66
66
|
module TableDefinition
|
@@ -2,7 +2,7 @@ module SchemaPlus::ForeignKeys
|
|
2
2
|
module Middleware
|
3
3
|
module Sql
|
4
4
|
module Table
|
5
|
-
if Gem::Requirement.new('< 5.0
|
5
|
+
if Gem::Requirement.new('< 5.0').satisfied_by?(::ActiveRecord.version)
|
6
6
|
def after(env)
|
7
7
|
foreign_keys = if env.table_definition.foreign_keys.is_a? Array
|
8
8
|
env.table_definition.foreign_keys
|
data/schema_dev.yml
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
|
-
gem.add_dependency "activerecord", ">= 4.2", "< 5.
|
21
|
+
gem.add_dependency "activerecord", ">= 4.2", "< 5.3"
|
22
22
|
gem.add_dependency "schema_plus_core"
|
23
23
|
gem.add_dependency "schema_plus_compatibility", "~> 0.2"
|
24
24
|
gem.add_dependency "valuable"
|
data/spec/deprecation_spec.rb
CHANGED
@@ -9,7 +9,7 @@ describe 'Deprecations' do
|
|
9
9
|
define_schema do
|
10
10
|
create_table :posts
|
11
11
|
create_table :comments do |t|
|
12
|
-
t.
|
12
|
+
t.references :post
|
13
13
|
end
|
14
14
|
end
|
15
15
|
class Comment < ::ActiveRecord::Base ; end
|
@@ -28,7 +28,7 @@ describe 'Deprecations' do
|
|
28
28
|
define_schema do
|
29
29
|
create_table :posts
|
30
30
|
create_table :comments do |t|
|
31
|
-
t.
|
31
|
+
t.references :post, foreign_key: true
|
32
32
|
end
|
33
33
|
end
|
34
34
|
class Comment < ::ActiveRecord::Base ; end
|
@@ -89,7 +89,7 @@ describe 'Deprecations' do
|
|
89
89
|
define_schema do
|
90
90
|
create_table :posts
|
91
91
|
create_table :comments do |t|
|
92
|
-
t.
|
92
|
+
t.references :post, foreign_key: true
|
93
93
|
end
|
94
94
|
end
|
95
95
|
class Comment < ::ActiveRecord::Base ; end
|
@@ -126,7 +126,7 @@ describe 'Deprecations' do
|
|
126
126
|
define_schema do
|
127
127
|
create_table :posts
|
128
128
|
create_table :comments do |t|
|
129
|
-
t.
|
129
|
+
t.references :post, references: :posts, on_delete: :set_null
|
130
130
|
end
|
131
131
|
end
|
132
132
|
expect(definition.on_delete).to eq(:nullify)
|
@@ -141,7 +141,7 @@ describe 'Deprecations' do
|
|
141
141
|
define_schema do
|
142
142
|
create_table :posts, primary_key: :funky
|
143
143
|
create_table :comments do |t|
|
144
|
-
t.
|
144
|
+
t.references :post
|
145
145
|
t.foreign_key :post_id, :posts, :funky
|
146
146
|
end
|
147
147
|
end
|
@@ -153,7 +153,7 @@ describe 'Deprecations' do
|
|
153
153
|
define_schema do
|
154
154
|
create_table :posts, primary_key: :funky
|
155
155
|
create_table :comments do |t|
|
156
|
-
t.
|
156
|
+
t.references :post
|
157
157
|
t.foreign_key :post_id, :posts, :funky, :town
|
158
158
|
end
|
159
159
|
end
|
data/spec/foreign_key_spec.rb
CHANGED
@@ -11,7 +11,7 @@ describe "Foreign Key" do
|
|
11
11
|
t.string :login
|
12
12
|
end
|
13
13
|
create_table :comments, :force => true do |t|
|
14
|
-
t.
|
14
|
+
t.references :user
|
15
15
|
t.foreign_key :user_id, :users
|
16
16
|
end
|
17
17
|
end
|
@@ -41,13 +41,13 @@ describe "Foreign Key" do
|
|
41
41
|
|
42
42
|
create_table :posts, :force => true do |t|
|
43
43
|
t.text :body
|
44
|
-
t.
|
45
|
-
t.
|
44
|
+
t.references :user
|
45
|
+
t.references :author
|
46
46
|
end
|
47
47
|
|
48
48
|
create_table :comments, :force => true do |t|
|
49
49
|
t.text :body
|
50
|
-
t.
|
50
|
+
t.references :post
|
51
51
|
t.foreign_key :post_id, :posts
|
52
52
|
end
|
53
53
|
end
|
@@ -150,7 +150,7 @@ describe "Foreign Key" do
|
|
150
150
|
before(:each) do
|
151
151
|
migration.suppress_messages do
|
152
152
|
migration.create_table :references, :force => true do |t|
|
153
|
-
t.
|
153
|
+
t.references :post, :foreign_key => false
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
data/spec/migration_spec.rb
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe ActiveRecord::Migration do
|
5
|
+
let(:column_type) {
|
6
|
+
Gem::Requirement.new('< 5.1').satisfied_by?(::ActiveRecord.version) ? :integer : :bigint
|
7
|
+
}
|
5
8
|
|
6
9
|
before(:each) do
|
7
10
|
define_schema do
|
@@ -17,7 +20,7 @@ describe ActiveRecord::Migration do
|
|
17
20
|
create_table :comments, :force => true do |t|
|
18
21
|
t.string :content
|
19
22
|
t.integer :user
|
20
|
-
t.
|
23
|
+
t.references :user
|
21
24
|
t.foreign_key :user_id, :users, :primary_key => :id
|
22
25
|
end
|
23
26
|
|
@@ -40,21 +43,21 @@ describe ActiveRecord::Migration do
|
|
40
43
|
sql = []
|
41
44
|
allow(@model.connection).to receive(:execute) { |str| sql << str }
|
42
45
|
recreate_table(@model) do |t|
|
43
|
-
t.
|
46
|
+
t.send column_type, :user, :foreign_key => true
|
44
47
|
end
|
45
48
|
expect(sql.join('; ')).to match(/PRAGMA FOREIGN_KEYS = ON.*CREATE TABLE "posts"/)
|
46
49
|
end
|
47
50
|
|
48
51
|
it "should create foreign key with default reference" do
|
49
52
|
recreate_table(@model) do |t|
|
50
|
-
t.
|
53
|
+
t.send column_type, :user, :foreign_key => true
|
51
54
|
end
|
52
55
|
expect(@model).to reference(:users, :id).on(:user)
|
53
56
|
end
|
54
57
|
|
55
58
|
it "should create foreign key with default column" do
|
56
59
|
recreate_table(@model) do |t|
|
57
|
-
t.
|
60
|
+
t.references :user
|
58
61
|
t.foreign_key :users
|
59
62
|
end
|
60
63
|
expect(@model).to reference(:users, :id).on(:user_id)
|
@@ -62,7 +65,7 @@ describe ActiveRecord::Migration do
|
|
62
65
|
|
63
66
|
it "should create foreign key with different reference" do
|
64
67
|
recreate_table(@model) do |t|
|
65
|
-
t.
|
68
|
+
t.references :author, :foreign_key => { :references => :users }
|
66
69
|
end
|
67
70
|
expect(@model).to reference(:users, :id).on(:author_id)
|
68
71
|
end
|
@@ -71,7 +74,7 @@ describe ActiveRecord::Migration do
|
|
71
74
|
hash = { :references => :users }
|
72
75
|
hash_original = hash.dup
|
73
76
|
recreate_table(@model) do |t|
|
74
|
-
t.
|
77
|
+
t.references :author, :foreign_key => hash
|
75
78
|
end
|
76
79
|
expect(hash).to eq(hash_original)
|
77
80
|
end
|
@@ -87,40 +90,40 @@ describe ActiveRecord::Migration do
|
|
87
90
|
|
88
91
|
it "should create foreign key with different reference using shortcut" do
|
89
92
|
recreate_table(@model) do |t|
|
90
|
-
t.
|
93
|
+
t.references :author, :references => :users
|
91
94
|
end
|
92
95
|
expect(@model).to reference(:users, :id).on(:author_id)
|
93
96
|
end
|
94
97
|
|
95
98
|
it "should create foreign key with default name" do
|
96
99
|
recreate_table @model do |t|
|
97
|
-
t.
|
100
|
+
t.references :user, :foreign_key => true
|
98
101
|
end
|
99
102
|
expect(@model).to reference(:users, :id).with_name("fk_#{@model.table_name}_user_id")
|
100
103
|
end
|
101
104
|
|
102
105
|
it "should create foreign key with specified name" do
|
103
106
|
recreate_table @model do |t|
|
104
|
-
t.
|
107
|
+
t.references :user, :foreign_key => { :name => "wugga" }
|
105
108
|
end
|
106
109
|
expect(@model).to reference(:users, :id).with_name("wugga")
|
107
110
|
end
|
108
111
|
|
109
112
|
it "handles very long names" do
|
110
113
|
table = ("ta"*15)
|
111
|
-
column = ("co"*15
|
114
|
+
column = ("co"*15)
|
112
115
|
expect {
|
113
116
|
ActiveRecord::Migration.create_table table do |t|
|
114
|
-
t.
|
117
|
+
t.references column, references: :members, index: false
|
115
118
|
end
|
116
119
|
}.not_to raise_error
|
117
|
-
expect(ActiveRecord::Base.connection.foreign_keys(table).first.column).to eq(column)
|
120
|
+
expect(ActiveRecord::Base.connection.foreign_keys(table).first.column).to eq(column + "_id")
|
118
121
|
end
|
119
122
|
|
120
123
|
it "should allow multiple foreign keys to be made" do
|
121
124
|
recreate_table(@model) do |t|
|
122
|
-
t.
|
123
|
-
t.
|
125
|
+
t.references :user, :references => :users
|
126
|
+
t.references :updater, :references => :users
|
124
127
|
end
|
125
128
|
expect(@model).to reference(:users, :id).on(:user_id)
|
126
129
|
expect(@model).to reference(:users, :id).on(:updater_id)
|
@@ -128,14 +131,14 @@ describe ActiveRecord::Migration do
|
|
128
131
|
|
129
132
|
it "should suppress foreign key" do
|
130
133
|
recreate_table(@model) do |t|
|
131
|
-
t.
|
134
|
+
t.references :member, :foreign_key => false
|
132
135
|
end
|
133
136
|
expect(@model).not_to reference.on(:member_id)
|
134
137
|
end
|
135
138
|
|
136
139
|
it "should suppress foreign key using shortcut" do
|
137
140
|
recreate_table(@model) do |t|
|
138
|
-
t.
|
141
|
+
t.references :member, :references => nil
|
139
142
|
end
|
140
143
|
expect(@model).not_to reference.on(:member_id)
|
141
144
|
end
|
@@ -177,7 +180,7 @@ describe ActiveRecord::Migration do
|
|
177
180
|
|
178
181
|
it "should create foreign key to the same table on parent_id" do
|
179
182
|
recreate_table(@model) do |t|
|
180
|
-
t.
|
183
|
+
t.references :parent, foreign_key: true
|
181
184
|
end
|
182
185
|
expect(@model).to reference(@model.table_name, :id).on(:parent_id)
|
183
186
|
end
|
@@ -195,14 +198,14 @@ describe ActiveRecord::Migration do
|
|
195
198
|
|
196
199
|
it "should create and detect on_update #{action.inspect}", if_action_supported do
|
197
200
|
recreate_table @model do |t|
|
198
|
-
t.
|
201
|
+
t.references :user, :foreign_key => { :on_update => action }
|
199
202
|
end
|
200
203
|
expect(@model).to reference.on(:user_id).on_update(action)
|
201
204
|
end
|
202
205
|
|
203
206
|
it "should create and detect on_update #{action.inspect} using shortcut", if_action_supported do
|
204
207
|
recreate_table @model do |t|
|
205
|
-
t.
|
208
|
+
t.references :user, :on_update => action
|
206
209
|
end
|
207
210
|
expect(@model).to reference.on(:user_id).on_update(action)
|
208
211
|
end
|
@@ -210,21 +213,21 @@ describe ActiveRecord::Migration do
|
|
210
213
|
it "should raise a not-implemented error for on_update => #{action}", if_action_unsupported do
|
211
214
|
expect {
|
212
215
|
recreate_table @model do |t|
|
213
|
-
t.
|
216
|
+
t.references :user, :foreign_key => { :on_update => action }
|
214
217
|
end
|
215
218
|
}.to raise_error(NotImplementedError)
|
216
219
|
end
|
217
220
|
|
218
221
|
it "should create and detect on_delete #{action.inspect}", if_action_supported do
|
219
222
|
recreate_table @model do |t|
|
220
|
-
t.
|
223
|
+
t.references :user, :foreign_key => { :on_delete => action }
|
221
224
|
end
|
222
225
|
expect(@model).to reference.on(:user_id).on_delete(action)
|
223
226
|
end
|
224
227
|
|
225
228
|
it "should create and detect on_delete #{action.inspect} using shortcut", if_action_supported do
|
226
229
|
recreate_table @model do |t|
|
227
|
-
t.
|
230
|
+
t.references :user, :on_delete => action
|
228
231
|
end
|
229
232
|
expect(@model).to reference.on(:user_id).on_delete(action)
|
230
233
|
end
|
@@ -232,7 +235,7 @@ describe ActiveRecord::Migration do
|
|
232
235
|
it "should raise a not-implemented error for on_delete => #{action}", if_action_unsupported do
|
233
236
|
expect {
|
234
237
|
recreate_table @model do |t|
|
235
|
-
t.
|
238
|
+
t.references :user, :foreign_key => { :on_delete => action }
|
236
239
|
end
|
237
240
|
}.to raise_error(NotImplementedError)
|
238
241
|
end
|
@@ -242,7 +245,7 @@ describe ActiveRecord::Migration do
|
|
242
245
|
[false, true, :initially_deferred].each do |status|
|
243
246
|
it "should create and detect deferrable #{status.inspect}", :mysql => :skip do
|
244
247
|
recreate_table @model do |t|
|
245
|
-
t.
|
248
|
+
t.references :user, :on_delete => :cascade, :deferrable => status
|
246
249
|
end
|
247
250
|
expect(@model).to reference.on(:user_id).deferrable(status)
|
248
251
|
end
|
@@ -251,7 +254,7 @@ describe ActiveRecord::Migration do
|
|
251
254
|
it "should use default on_delete action" do
|
252
255
|
with_fk_config(:on_delete => :cascade) do
|
253
256
|
recreate_table @model do |t|
|
254
|
-
t.
|
257
|
+
t.references :user, foreign_key: true
|
255
258
|
end
|
256
259
|
expect(@model).to reference.on(:user_id).on_delete(:cascade)
|
257
260
|
end
|
@@ -260,7 +263,7 @@ describe ActiveRecord::Migration do
|
|
260
263
|
it "should override on_update action per table" do
|
261
264
|
with_fk_config(:on_update => :cascade) do
|
262
265
|
recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
|
263
|
-
t.
|
266
|
+
t.references :user, foreign_key: true
|
264
267
|
end
|
265
268
|
expect(@model).to reference.on(:user_id).on_update(:restrict)
|
266
269
|
end
|
@@ -269,7 +272,7 @@ describe ActiveRecord::Migration do
|
|
269
272
|
it "should override on_delete action per table" do
|
270
273
|
with_fk_config(:on_delete => :cascade) do
|
271
274
|
recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
|
272
|
-
t.
|
275
|
+
t.references :user, foreign_key: true
|
273
276
|
end
|
274
277
|
expect(@model).to reference.on(:user_id).on_delete(:restrict)
|
275
278
|
end
|
@@ -278,7 +281,7 @@ describe ActiveRecord::Migration do
|
|
278
281
|
it "should override on_update action per column" do
|
279
282
|
with_fk_config(:on_update => :cascade) do
|
280
283
|
recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
|
281
|
-
t.
|
284
|
+
t.references :user, :foreign_key => { :on_update => :nullify }
|
282
285
|
end
|
283
286
|
expect(@model).to reference.on(:user_id).on_update(:nullify)
|
284
287
|
end
|
@@ -287,7 +290,7 @@ describe ActiveRecord::Migration do
|
|
287
290
|
it "should override on_delete action per column" do
|
288
291
|
with_fk_config(:on_delete => :cascade) do
|
289
292
|
recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
|
290
|
-
t.
|
293
|
+
t.references :user, :foreign_key => { :on_delete => :nullify }
|
291
294
|
end
|
292
295
|
expect(@model).to reference.on(:user_id).on_delete(:nullify)
|
293
296
|
end
|
@@ -296,7 +299,7 @@ describe ActiveRecord::Migration do
|
|
296
299
|
it "should raise an error for an invalid on_update action" do
|
297
300
|
expect {
|
298
301
|
recreate_table @model do |t|
|
299
|
-
t.
|
302
|
+
t.references :user, :foreign_key => { :on_update => :invalid }
|
300
303
|
end
|
301
304
|
}.to raise_error(ArgumentError)
|
302
305
|
end
|
@@ -304,7 +307,7 @@ describe ActiveRecord::Migration do
|
|
304
307
|
it "should raise an error for an invalid on_delete action" do
|
305
308
|
expect {
|
306
309
|
recreate_table @model do |t|
|
307
|
-
t.
|
310
|
+
t.references :user, :foreign_key => { :on_delete => :invalid }
|
308
311
|
end
|
309
312
|
}.to raise_error(ArgumentError)
|
310
313
|
end
|
@@ -315,12 +318,15 @@ describe ActiveRecord::Migration do
|
|
315
318
|
before(:each) do
|
316
319
|
@model = Post
|
317
320
|
end
|
318
|
-
|
321
|
+
# Disabling bulk tests on mysql since the mysql adapter does not implement it for add_foreign_key in rails 5.1+
|
322
|
+
bulk_tests = RSpec.configuration.exclusion_filter[:mysql] == :skip ? [false] : [true, false]
|
323
|
+
|
324
|
+
bulk_tests.each do |bulk|
|
319
325
|
suffix = bulk ? ' with :bulk option' : ""
|
320
326
|
|
321
327
|
it "should create a foreign key constraint"+suffix, :sqlite3 => :skip do
|
322
328
|
change_table(@model, :bulk => bulk) do |t|
|
323
|
-
t.
|
329
|
+
t.references :user, foreign_key: true
|
324
330
|
end
|
325
331
|
expect(@model).to reference(:users, :id).on(:user_id)
|
326
332
|
end
|
@@ -332,7 +338,7 @@ describe ActiveRecord::Migration do
|
|
332
338
|
migration = Class.new ::ActiveRecord::Migration.latest_version do
|
333
339
|
define_method(:change) {
|
334
340
|
change_table("comments", :bulk => bulk) do |t|
|
335
|
-
t.
|
341
|
+
t.references :user, foreign_key: true
|
336
342
|
end
|
337
343
|
}
|
338
344
|
end
|
@@ -346,7 +352,7 @@ describe ActiveRecord::Migration do
|
|
346
352
|
|
347
353
|
it "should create a foreign key constraint using :references"+suffix, :sqlite3 => :skip do
|
348
354
|
change_table(@model, :bulk => bulk) do |t|
|
349
|
-
t.
|
355
|
+
t.send column_type, :user_id, foreign_key: true
|
350
356
|
end
|
351
357
|
expect(@model).to reference(:users, :id).on(:user_id)
|
352
358
|
end
|
@@ -367,19 +373,19 @@ describe ActiveRecord::Migration do
|
|
367
373
|
end
|
368
374
|
|
369
375
|
it "should create foreign key" do
|
370
|
-
add_column(:post_id,
|
376
|
+
add_column(:post_id, column_type, foreign_key: true) do
|
371
377
|
expect(@model).to reference(:posts, :id).on(:post_id)
|
372
378
|
end
|
373
379
|
end
|
374
380
|
|
375
381
|
it "should create foreign key to explicitly given table" do
|
376
|
-
add_column(:author_id,
|
382
|
+
add_column(:author_id, column_type, :foreign_key => { :references => :users }) do
|
377
383
|
expect(@model).to reference(:users, :id).on(:author_id)
|
378
384
|
end
|
379
385
|
end
|
380
386
|
|
381
387
|
it "should create foreign key to explicitly given table using shortcut" do
|
382
|
-
add_column(:author_id,
|
388
|
+
add_column(:author_id, column_type, :references => :users) do
|
383
389
|
expect(@model).to reference(:users, :id).on(:author_id)
|
384
390
|
end
|
385
391
|
end
|
@@ -391,14 +397,14 @@ describe ActiveRecord::Migration do
|
|
391
397
|
end
|
392
398
|
|
393
399
|
it "should create foreign key to the same table on parent_id" do
|
394
|
-
add_column(:parent_id,
|
400
|
+
add_column(:parent_id, column_type, foreign_key: true) do
|
395
401
|
expect(@model).to reference(@model.table_name, :id).on(:parent_id)
|
396
402
|
end
|
397
403
|
end
|
398
404
|
|
399
405
|
it "should use default on_update action" do
|
400
406
|
SchemaPlus::ForeignKeys.config.on_update = :cascade
|
401
|
-
add_column(:post_id,
|
407
|
+
add_column(:post_id, column_type, foreign_key: true) do
|
402
408
|
expect(@model).to reference.on(:post_id).on_update(:cascade)
|
403
409
|
end
|
404
410
|
SchemaPlus::ForeignKeys.config.on_update = nil
|
@@ -406,7 +412,7 @@ describe ActiveRecord::Migration do
|
|
406
412
|
|
407
413
|
it "should use default on_delete action" do
|
408
414
|
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
409
|
-
add_column(:post_id,
|
415
|
+
add_column(:post_id, column_type, foreign_key: true) do
|
410
416
|
expect(@model).to reference.on(:post_id).on_delete(:cascade)
|
411
417
|
end
|
412
418
|
SchemaPlus::ForeignKeys.config.on_delete = nil
|
@@ -415,14 +421,14 @@ describe ActiveRecord::Migration do
|
|
415
421
|
it "should allow to overwrite default actions" do
|
416
422
|
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
417
423
|
SchemaPlus::ForeignKeys.config.on_update = :restrict
|
418
|
-
add_column(:post_id,
|
424
|
+
add_column(:post_id, column_type, :foreign_key => { :on_update => :nullify, :on_delete => :nullify}) do
|
419
425
|
expect(@model).to reference.on(:post_id).on_delete(:nullify).on_update(:nullify)
|
420
426
|
end
|
421
427
|
SchemaPlus::ForeignKeys.config.on_delete = nil
|
422
428
|
end
|
423
429
|
|
424
430
|
it "should create foreign key with default name" do
|
425
|
-
add_column(:post_id,
|
431
|
+
add_column(:post_id, column_type, foreign_key: true) do
|
426
432
|
expect(@model).to reference(:posts, :id).with_name("fk_#{@model.table_name}_post_id")
|
427
433
|
end
|
428
434
|
end
|
@@ -456,7 +462,7 @@ describe ActiveRecord::Migration do
|
|
456
462
|
|
457
463
|
before(:each) do
|
458
464
|
recreate_table @model do |t|
|
459
|
-
t.
|
465
|
+
t.references :user, foreign_key: true
|
460
466
|
end
|
461
467
|
end
|
462
468
|
|
@@ -465,22 +471,22 @@ describe ActiveRecord::Migration do
|
|
465
471
|
end
|
466
472
|
|
467
473
|
it "should drop foreign key if it is no longer valid" do
|
468
|
-
change_column :user_id,
|
474
|
+
change_column :user_id, column_type, :foreign_key => { :references => :members }
|
469
475
|
expect(@model).not_to reference(:users)
|
470
476
|
end
|
471
477
|
|
472
478
|
it "should drop foreign key if requested to do so" do
|
473
|
-
change_column :user_id,
|
479
|
+
change_column :user_id, column_type, :foreign_key => { :references => nil }
|
474
480
|
expect(@model).not_to reference(:users)
|
475
481
|
end
|
476
482
|
|
477
483
|
it "should reference pointed table afterwards if new one is created" do
|
478
|
-
change_column :user_id,
|
484
|
+
change_column :user_id, column_type, :foreign_key => { :references => :members }
|
479
485
|
expect(@model).to reference(:members)
|
480
486
|
end
|
481
487
|
|
482
488
|
it "should maintain foreign key if it's unaffected by change" do
|
483
|
-
change_column :user_id,
|
489
|
+
change_column :user_id, column_type, :default => 0
|
484
490
|
expect(@model).to reference(:users)
|
485
491
|
end
|
486
492
|
|
@@ -503,7 +509,7 @@ describe ActiveRecord::Migration do
|
|
503
509
|
before(:each) do
|
504
510
|
@model = Comment
|
505
511
|
recreate_table @model do |t|
|
506
|
-
t.
|
512
|
+
t.references :post, foreign_key: true
|
507
513
|
end
|
508
514
|
end
|
509
515
|
|
@@ -529,7 +535,7 @@ describe ActiveRecord::Migration do
|
|
529
535
|
before(:each) do
|
530
536
|
@model = Comment
|
531
537
|
recreate_table @model do |t|
|
532
|
-
t.
|
538
|
+
t.references :user, foreign_key: true
|
533
539
|
t.integer :xyz, :index => true
|
534
540
|
end
|
535
541
|
ActiveRecord::Migration.suppress_messages do
|
@@ -549,8 +555,8 @@ describe ActiveRecord::Migration do
|
|
549
555
|
before(:each) do
|
550
556
|
@model = Comment
|
551
557
|
recreate_table @model do |t|
|
552
|
-
t.
|
553
|
-
t.
|
558
|
+
t.references :user
|
559
|
+
t.references :member
|
554
560
|
end
|
555
561
|
ActiveRecord::Migration.suppress_messages do
|
556
562
|
ActiveRecord::Migration.rename_table @model.table_name, :newname
|
data/spec/named_schemas_spec.rb
CHANGED
@@ -57,7 +57,7 @@ describe "with multiple schemas" do
|
|
57
57
|
|
58
58
|
it "should not find foreign keys in other schema" do
|
59
59
|
connection.create_table :comments, :force => true do |t|
|
60
|
-
t.
|
60
|
+
t.references :user, :foreign_key => false
|
61
61
|
end
|
62
62
|
Comment.reset_column_information
|
63
63
|
expect(Comment.foreign_keys.length).to eq(0)
|
@@ -67,7 +67,7 @@ describe "with multiple schemas" do
|
|
67
67
|
|
68
68
|
it "should find foreign keys in this schema" do
|
69
69
|
connection.create_table :comments, :force => true do |t|
|
70
|
-
t.
|
70
|
+
t.references :user, :foreign_key => true
|
71
71
|
end
|
72
72
|
Comment.reset_column_information
|
73
73
|
expect(Comment.foreign_keys.map(&:column).flatten).to eq(["user_id"])
|
@@ -85,8 +85,8 @@ describe "with multiple schemas" do
|
|
85
85
|
create_table "schema_plus_test2.groups", :force => true do |t|
|
86
86
|
end
|
87
87
|
create_table "schema_plus_test2.members", :force => true do |t|
|
88
|
-
t.
|
89
|
-
t.
|
88
|
+
t.references :item, :foreign_key => true unless SchemaDev::Rspec::Helpers.mysql?
|
89
|
+
t.references :group, :foreign_key => { references: "schema_plus_test2.groups" }
|
90
90
|
end
|
91
91
|
end
|
92
92
|
class Group < ::ActiveRecord::Base
|
data/spec/schema_dumper_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|
2
2
|
require 'stringio'
|
3
3
|
|
4
4
|
describe "Schema dump" do
|
5
|
+
let(:column_type) { Gem::Requirement.new('< 5.1').satisfied_by?(::ActiveRecord.version) ? :integer : :bigint }
|
5
6
|
|
6
7
|
before(:each) do
|
7
8
|
ActiveRecord::Migration.suppress_messages do
|
@@ -11,13 +12,13 @@ describe "Schema dump" do
|
|
11
12
|
create_table :users, :force => true do |t|
|
12
13
|
t.string :login
|
13
14
|
t.datetime :deleted_at
|
14
|
-
t.
|
15
|
+
t.references :first_post, index: { unique: true }
|
15
16
|
end
|
16
17
|
|
17
18
|
create_table :posts, :force => true do |t|
|
18
19
|
t.text :body
|
19
|
-
t.
|
20
|
-
t.
|
20
|
+
t.references :user
|
21
|
+
t.references :first_comment
|
21
22
|
t.string :string_no_default
|
22
23
|
t.integer :short_id
|
23
24
|
t.string :str_short
|
@@ -34,8 +35,8 @@ describe "Schema dump" do
|
|
34
35
|
|
35
36
|
create_table :comments, :force => true do |t|
|
36
37
|
t.text :body
|
37
|
-
t.
|
38
|
-
t.
|
38
|
+
t.references :post
|
39
|
+
t.references :commenter
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
@@ -53,7 +54,7 @@ describe "Schema dump" do
|
|
53
54
|
|
54
55
|
it "should include foreign_key definition" do
|
55
56
|
with_foreign_key Post, :user_id, :users, :id do
|
56
|
-
expect(dump_posts).to match(%r{t.integer\s+"user_id".*foreign_key.*users})
|
57
|
+
expect(dump_posts).to match(%r{t.(integer|bigint)\s+"user_id".*foreign_key.*users})
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
@@ -65,7 +66,7 @@ describe "Schema dump" do
|
|
65
66
|
|
66
67
|
it "should respect foreign key's primary key" do
|
67
68
|
with_foreign_key Post, :user_id, :users, :first_post_id do
|
68
|
-
expect(dump_posts).to match(%r{t.integer\s+"user_id".*foreign_key.*:primary_key=>"first_post_id"})
|
69
|
+
expect(dump_posts).to match(%r{t.(integer|bigint)\s+"user_id".*foreign_key.*:primary_key=>"first_post_id"})
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
@@ -107,7 +108,7 @@ describe "Schema dump" do
|
|
107
108
|
|
108
109
|
it "should include foreign_key options" do
|
109
110
|
with_foreign_key Post, :user_id, :users, :id, :on_update => :cascade, :on_delete => :nullify do
|
110
|
-
expect(dump_posts).to match(%q[t.integer\s*"user_id",.*:foreign_key=>{:references=>"users", :name=>"fk_posts_user_id", :on_update=>:cascade, :on_delete=>:nullify}])
|
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}])
|
111
112
|
end
|
112
113
|
end
|
113
114
|
|
@@ -146,30 +147,30 @@ describe "Schema dump" do
|
|
146
147
|
connection.tables_only.each do |table| drop_table table, force: :cascade end
|
147
148
|
|
148
149
|
create_table :grade_systems, force: true do |t|
|
149
|
-
t.string
|
150
|
-
t.
|
151
|
-
t.
|
152
|
-
t.
|
150
|
+
t.string :name
|
151
|
+
t.references :school
|
152
|
+
t.references :parent
|
153
|
+
t.references :profile
|
153
154
|
end
|
154
155
|
|
155
156
|
create_table :schools, force: true do |t|
|
156
|
-
t.string
|
157
|
-
t.
|
157
|
+
t.string :name
|
158
|
+
t.references :default_grade_system
|
158
159
|
end
|
159
160
|
|
160
161
|
create_table :academic_years, force: true do |t|
|
161
|
-
t.string
|
162
|
-
t.
|
162
|
+
t.string :name
|
163
|
+
t.references :school
|
163
164
|
end
|
164
165
|
|
165
166
|
create_table :buildings, force: true do |t|
|
166
|
-
t.string
|
167
|
-
t.
|
167
|
+
t.string :name
|
168
|
+
t.references :school
|
168
169
|
end
|
169
170
|
|
170
171
|
create_table :profiles, force: true do |t|
|
171
|
-
t.
|
172
|
-
t.
|
172
|
+
t.references :school
|
173
|
+
t.references :building
|
173
174
|
end
|
174
175
|
|
175
176
|
end
|
@@ -217,7 +218,7 @@ describe "Schema dump" do
|
|
217
218
|
ActiveRecord::Migration.suppress_messages do
|
218
219
|
ActiveRecord::Migration.create_table model.table_name, :force => true do |t|
|
219
220
|
table_columns.each do |column|
|
220
|
-
t.column column.name, column.type
|
221
|
+
t.column column.name, column.type, limit: column.limit
|
221
222
|
end
|
222
223
|
columnsets.each do |columns, referenced_table_name, referenced_columns, options|
|
223
224
|
t.foreign_key columns, referenced_table_name, (options||{}).merge(primary_key: referenced_columns)
|
@@ -231,7 +232,7 @@ describe "Schema dump" do
|
|
231
232
|
ActiveRecord::Migration.suppress_messages do
|
232
233
|
ActiveRecord::Migration.create_table model.table_name, :force => true do |t|
|
233
234
|
table_columns.each do |column|
|
234
|
-
t.column column.name, column.type
|
235
|
+
t.column column.name, column.type, limit: column.limit
|
235
236
|
end
|
236
237
|
end
|
237
238
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_plus_foreign_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ronen barzel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5.
|
22
|
+
version: '5.3'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '4.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
32
|
+
version: '5.3'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: schema_plus_core
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -202,6 +202,14 @@ files:
|
|
202
202
|
- gemfiles/activerecord-5.0/Gemfile.mysql2
|
203
203
|
- gemfiles/activerecord-5.0/Gemfile.postgresql
|
204
204
|
- gemfiles/activerecord-5.0/Gemfile.sqlite3
|
205
|
+
- gemfiles/activerecord-5.1/Gemfile.base
|
206
|
+
- gemfiles/activerecord-5.1/Gemfile.mysql2
|
207
|
+
- gemfiles/activerecord-5.1/Gemfile.postgresql
|
208
|
+
- gemfiles/activerecord-5.1/Gemfile.sqlite3
|
209
|
+
- gemfiles/activerecord-5.2/Gemfile.base
|
210
|
+
- gemfiles/activerecord-5.2/Gemfile.mysql2
|
211
|
+
- gemfiles/activerecord-5.2/Gemfile.postgresql
|
212
|
+
- gemfiles/activerecord-5.2/Gemfile.sqlite3
|
205
213
|
- lib/schema_plus/foreign_keys.rb
|
206
214
|
- lib/schema_plus/foreign_keys/active_record/base.rb
|
207
215
|
- lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract/schema_creation.rb
|
@@ -249,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
249
257
|
version: '0'
|
250
258
|
requirements: []
|
251
259
|
rubyforge_project:
|
252
|
-
rubygems_version: 2.
|
260
|
+
rubygems_version: 2.7.8
|
253
261
|
signing_key:
|
254
262
|
specification_version: 4
|
255
263
|
summary: Extended support for foreign key constraints in ActiveRecord
|