foreigner 1.4.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/{README.rdoc → README.md} +43 -39
- data/Rakefile +1 -1
- data/lib/foreigner.rb +1 -0
- data/lib/foreigner/connection_adapters/abstract/foreign_key_definition.rb +3 -0
- data/lib/foreigner/connection_adapters/abstract/schema_statements.rb +18 -4
- data/lib/foreigner/connection_adapters/mysql2_adapter.rb +3 -7
- data/lib/foreigner/connection_adapters/postgresql_adapter.rb +2 -2
- data/lib/foreigner/connection_adapters/sql2003.rb +20 -11
- data/lib/foreigner/migration/command_recorder.rb +2 -2
- data/lib/foreigner/schema_dumper.rb +4 -4
- data/test/foreigner/connection_adapters/mysql2_adapter_test.rb +4 -4
- data/test/foreigner/connection_adapters/sql2003_test.rb +31 -19
- data/test/foreigner/migration/command_recorder_test.rb +27 -16
- data/test/foreigner/schema_dumper_test.rb +5 -5
- data/test/helper.rb +6 -6
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d68267917004a48382056d76a3a0057f170089d0
|
4
|
+
data.tar.gz: fa071b2e0de7ff5aa27f50598a2633c12e75d744
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ff99f300a776fbc3868614a116c375f63ce89e2cf4735890a9a01dd70085a3b9815118ac6a64005f3a1561ca9772e828441e49b7c9854b936dedd99bd8a10ac
|
7
|
+
data.tar.gz: ae4c8dbc8af8d9ab0b67b5bfa6b3592acb58229a1d27b4a420233e57dce7c5de6f33e5ba725ba79af9fbb5f626fef080c53e0eb72cd7bfc01010a8ed966dc497
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Foreigner
|
2
|
+
[](https://travis-ci.org/matthuhiggins/foreigner) [](https://codeclimate.com/github/matthuhiggins/foreigner)
|
3
3
|
|
4
|
-
Foreigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to schema.rb
|
4
|
+
Foreigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to `schema.rb`.
|
5
5
|
|
6
6
|
The following adapters are supported:
|
7
7
|
|
@@ -9,82 +9,86 @@ The following adapters are supported:
|
|
9
9
|
* postgres
|
10
10
|
* sqlite (foreign key methods are a no-op)
|
11
11
|
|
12
|
-
|
12
|
+
## Installation
|
13
13
|
|
14
14
|
Add the following to your Gemfile:
|
15
|
-
|
15
|
+
```ruby
|
16
16
|
gem 'foreigner'
|
17
|
-
|
18
|
-
|
17
|
+
```
|
18
|
+
## API Examples
|
19
19
|
|
20
20
|
Foreigner adds two methods to migrations.
|
21
21
|
|
22
|
-
* add_foreign_key(from_table, to_table, options)
|
23
|
-
* remove_foreign_key(from_table, options)
|
22
|
+
* `add_foreign_key(from_table, to_table, options)`
|
23
|
+
* `remove_foreign_key(from_table, options)`
|
24
24
|
|
25
|
-
(Options are documented in connection_adapters/abstract/schema_definitions.rb):
|
25
|
+
(Options are documented in `connection_adapters/abstract/schema_definitions.rb`):
|
26
26
|
|
27
27
|
For example, given the following model:
|
28
|
-
|
28
|
+
```ruby
|
29
29
|
class Comment < ActiveRecord::Base
|
30
30
|
belongs_to :post
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
class Post < ActiveRecord::Base
|
34
34
|
has_many :comments, dependent: :delete_all
|
35
35
|
end
|
36
|
-
|
36
|
+
```
|
37
37
|
You should add a foreign key in your migration:
|
38
|
-
|
38
|
+
```ruby
|
39
39
|
add_foreign_key(:comments, :posts)
|
40
|
-
|
41
|
-
The
|
42
|
-
|
40
|
+
```
|
41
|
+
The `:dependent` option can be moved from the `has_many` definition to the foreign key:
|
42
|
+
```ruby
|
43
43
|
add_foreign_key(:comments, :posts, dependent: :delete)
|
44
|
-
|
45
|
-
If the column is named article_id instead of post_id
|
46
|
-
|
44
|
+
```
|
45
|
+
If the column is named `article_id` instead of `post_id`, use the `:column` option:
|
46
|
+
```ruby
|
47
47
|
add_foreign_key(:comments, :posts, column: 'article_id')
|
48
|
-
|
48
|
+
```
|
49
49
|
A name can be specified for the foreign key constraint:
|
50
|
-
|
50
|
+
```ruby
|
51
51
|
add_foreign_key(:comments, :posts, name: 'comment_article_foreign_key')
|
52
|
+
```
|
53
|
+
The `:column` and `:name` options create a foreign key with a custom name. In order to remove it you need to specify `:name`:
|
54
|
+
```ruby
|
55
|
+
remove_foreign_key(:comments, name: 'comment_article_foreign_key')
|
56
|
+
```
|
57
|
+
## Change Table Methods
|
52
58
|
|
53
|
-
|
54
|
-
|
55
|
-
Foreigner adds extra methods to create_table and change_table.
|
59
|
+
Foreigner adds extra methods to `create_table` and `change_table`.
|
56
60
|
|
57
61
|
Create a new table with a foreign key:
|
58
|
-
|
62
|
+
```ruby
|
59
63
|
create_table :products do |t|
|
60
64
|
t.string :name
|
61
65
|
t.integer :factory_id
|
62
66
|
t.foreign_key :factories
|
63
67
|
end
|
64
|
-
|
68
|
+
```
|
65
69
|
Add a missing foreign key to comments:
|
66
|
-
|
70
|
+
```ruby
|
67
71
|
change_table :comments do |t|
|
68
72
|
t.foreign_key :posts, dependent: :delete
|
69
73
|
end
|
70
|
-
|
74
|
+
```
|
71
75
|
Remove an unwanted foreign key:
|
72
|
-
|
76
|
+
```ruby
|
73
77
|
change_table :comments do |t|
|
74
78
|
t.remove_foreign_key :users
|
75
79
|
end
|
80
|
+
```
|
81
|
+
## Database-specific options
|
76
82
|
|
77
|
-
|
78
|
-
|
79
|
-
Database specific options will never be supported by foreigner. You can add them using :options:
|
80
|
-
|
83
|
+
Database-specific options will never be supported by foreigner. You can add them using `:options`:
|
84
|
+
```ruby
|
81
85
|
add_foreign_key(:comments, :posts, options: 'ON UPDATE DEFERRED')
|
86
|
+
```
|
87
|
+
## Foreigner Add-ons
|
82
88
|
|
83
|
-
|
84
|
-
|
85
|
-
* {immigrant}[https://github.com/jenseng/immigrant] - generate a migration that includes all missing foreign keys.
|
86
|
-
* {sqlserver-foreigner}[https://github.com/cleblanc87/sqlserver-foreigner] - A plugin for SQL Server.
|
89
|
+
* [immigrant](https://github.com/jenseng/immigrant) - generate a migration that includes all missing foreign keys.
|
90
|
+
* [sqlserver-foreigner](https://github.com/cleblanc87/sqlserver-foreigner) - A plugin for SQL Server.
|
87
91
|
|
88
|
-
|
92
|
+
## License
|
89
93
|
|
90
94
|
Copyright (c) 2012 Matthew Higgins, released under the MIT license
|
data/Rakefile
CHANGED
data/lib/foreigner.rb
CHANGED
@@ -7,7 +7,7 @@ module Foreigner
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
module AbstractAdapter
|
12
12
|
def create_table(table_name, *args, &block)
|
13
13
|
definition = nil
|
@@ -26,6 +26,20 @@ module Foreigner
|
|
26
26
|
false
|
27
27
|
end
|
28
28
|
|
29
|
+
# Checks to see if a foreign key exists on a table for a given constraint.
|
30
|
+
#
|
31
|
+
# # Check a foreign key exists
|
32
|
+
# foreign_key_exists?(:suppliers, :companies)
|
33
|
+
#
|
34
|
+
# # Check a foreign key with a custom name exists
|
35
|
+
# foreign_key_exists?(:suppliers, name: "fk_company_id"
|
36
|
+
#
|
37
|
+
# # Check a foreign key on a column
|
38
|
+
# foreign_key_exists?(:suppliers, column: "company_id"
|
39
|
+
#
|
40
|
+
def foreign_key_exists?(table_name, column_name, options = {})
|
41
|
+
end
|
42
|
+
|
29
43
|
# Adds a new foreign key to the +from_table+, referencing the primary key of +to_table+
|
30
44
|
#
|
31
45
|
# The foreign key will be named after the from and to tables unless you pass
|
@@ -37,20 +51,20 @@ module Foreigner
|
|
37
51
|
# generates
|
38
52
|
# ALTER TABLE `comments` ADD CONSTRAINT
|
39
53
|
# `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
|
40
|
-
#
|
54
|
+
#
|
41
55
|
# ====== Creating a named foreign key
|
42
56
|
# add_foreign_key(:comments, :posts, name: 'comments_belongs_to_posts')
|
43
57
|
# generates
|
44
58
|
# ALTER TABLE `comments` ADD CONSTRAINT
|
45
59
|
# `comments_belongs_to_posts` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
|
46
|
-
#
|
60
|
+
#
|
47
61
|
# ====== Creating a cascading foreign_key on a custom column
|
48
62
|
# add_foreign_key(:people, :people, column: 'best_friend_id', dependent: :nullify)
|
49
63
|
# generates
|
50
64
|
# ALTER TABLE `people` ADD CONSTRAINT
|
51
65
|
# `people_best_friend_id_fk` FOREIGN KEY (`best_friend_id`) REFERENCES `people` (`id`)
|
52
66
|
# ON DELETE SET NULL
|
53
|
-
#
|
67
|
+
#
|
54
68
|
# === Supported options
|
55
69
|
# [:column]
|
56
70
|
# Specify the column name on the from_table that references the to_table. By default this is guessed
|
@@ -4,15 +4,11 @@ module Foreigner
|
|
4
4
|
include Foreigner::ConnectionAdapters::Sql2003
|
5
5
|
|
6
6
|
def remove_foreign_key_sql(table, options)
|
7
|
-
|
8
|
-
foreign_key_name = foreign_key_name(table, options[:column], options)
|
9
|
-
else
|
10
|
-
foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
|
11
|
-
end
|
7
|
+
foreign_key_name = decipher_foreign_key_name(table, options)
|
12
8
|
|
13
9
|
"DROP FOREIGN KEY #{quote_column_name(foreign_key_name)}"
|
14
10
|
end
|
15
|
-
|
11
|
+
|
16
12
|
def foreign_keys(table_name)
|
17
13
|
fk_info = select_all %{
|
18
14
|
SELECT fk.referenced_table_name as 'to_table'
|
@@ -28,7 +24,7 @@ module Foreigner
|
|
28
24
|
create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"]
|
29
25
|
|
30
26
|
fk_info.map do |row|
|
31
|
-
options = {:
|
27
|
+
options = {column: row['column'], name: row['name'], primary_key: row['primary_key']}
|
32
28
|
|
33
29
|
if create_table_info =~ /CONSTRAINT #{quote_column_name(row['name'])} FOREIGN KEY .* REFERENCES .* ON DELETE (CASCADE|SET NULL|RESTRICT)/
|
34
30
|
options[:dependent] = case $1
|
@@ -17,9 +17,9 @@ module Foreigner
|
|
17
17
|
AND t3.nspname = ANY (current_schemas(false))
|
18
18
|
ORDER BY c.conname
|
19
19
|
}
|
20
|
-
|
20
|
+
|
21
21
|
fk_info.map do |row|
|
22
|
-
options = {:
|
22
|
+
options = {column: row['column'], name: row['name'], primary_key: row['primary_key']}
|
23
23
|
|
24
24
|
options[:dependent] = case row['dependency']
|
25
25
|
when 'c' then :delete
|
@@ -14,6 +14,12 @@ module Foreigner
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
def foreign_key_exists?(from_table, options)
|
18
|
+
foreign_key_name = decipher_foreign_key_name(from_table, options)
|
19
|
+
|
20
|
+
foreign_keys(from_table).any? { |fk| fk.name == foreign_key_name }
|
21
|
+
end
|
22
|
+
|
17
23
|
def add_foreign_key(from_table, to_table, options = {})
|
18
24
|
sql = "ALTER TABLE #{quote_table_name(from_table)} #{add_foreign_key_sql(from_table, to_table, options)}"
|
19
25
|
execute(sql)
|
@@ -21,7 +27,7 @@ module Foreigner
|
|
21
27
|
|
22
28
|
def add_foreign_key_sql(from_table, to_table, options = {})
|
23
29
|
column = options[:column] || "#{to_table.to_s.singularize}_id"
|
24
|
-
foreign_key_name = foreign_key_name(from_table, column
|
30
|
+
foreign_key_name = options.key?(:name) ? options[:name].to_s : foreign_key_name(from_table, column)
|
25
31
|
primary_key = options[:primary_key] || "id"
|
26
32
|
dependency = dependency_sql(options[:dependent])
|
27
33
|
|
@@ -40,21 +46,24 @@ module Foreigner
|
|
40
46
|
end
|
41
47
|
|
42
48
|
def remove_foreign_key_sql(table, options)
|
43
|
-
|
44
|
-
foreign_key_name = foreign_key_name(table, options[:column], options)
|
45
|
-
else
|
46
|
-
foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
|
47
|
-
end
|
48
|
-
|
49
|
+
foreign_key_name = decipher_foreign_key_name(table, options)
|
49
50
|
"DROP CONSTRAINT #{quote_column_name(foreign_key_name)}"
|
50
51
|
end
|
51
52
|
|
52
53
|
private
|
53
|
-
def foreign_key_name(
|
54
|
-
|
55
|
-
|
54
|
+
def foreign_key_name(from_table, column)
|
55
|
+
"#{from_table}_#{column}_fk"
|
56
|
+
end
|
57
|
+
|
58
|
+
def foreign_key_column(to_table)
|
59
|
+
"#{to_table.to_s.singularize}_id"
|
60
|
+
end
|
61
|
+
|
62
|
+
def decipher_foreign_key_name(from_table, options)
|
63
|
+
if Hash === options
|
64
|
+
options.key?(:name) ? options[:name].to_s : foreign_key_name(from_table, options[:column])
|
56
65
|
else
|
57
|
-
|
66
|
+
foreign_key_name(from_table, foreign_key_column(options))
|
58
67
|
end
|
59
68
|
end
|
60
69
|
|
@@ -14,9 +14,9 @@ module Foreigner
|
|
14
14
|
add_options ||= {}
|
15
15
|
|
16
16
|
if add_options[:name]
|
17
|
-
options = {:
|
17
|
+
options = {name: add_options[:name]}
|
18
18
|
elsif add_options[:column]
|
19
|
-
options = {:
|
19
|
+
options = {column: add_options[:column]}
|
20
20
|
else
|
21
21
|
options = to_table
|
22
22
|
end
|
@@ -10,16 +10,16 @@ module Foreigner
|
|
10
10
|
def dump_foreign_key(foreign_key)
|
11
11
|
statement_parts = [ ('add_foreign_key ' + remove_prefix_and_suffix(foreign_key.from_table).inspect) ]
|
12
12
|
statement_parts << remove_prefix_and_suffix(foreign_key.to_table).inspect
|
13
|
-
statement_parts << (':
|
13
|
+
statement_parts << ('name: ' + foreign_key.options[:name].inspect)
|
14
14
|
|
15
15
|
if foreign_key.options[:column] != "#{remove_prefix_and_suffix(foreign_key.to_table).singularize}_id"
|
16
|
-
statement_parts << (':
|
16
|
+
statement_parts << ('column: ' + foreign_key.options[:column].inspect)
|
17
17
|
end
|
18
18
|
if foreign_key.options[:primary_key] != 'id'
|
19
|
-
statement_parts << (':
|
19
|
+
statement_parts << ('primary_key: ' + foreign_key.options[:primary_key].inspect)
|
20
20
|
end
|
21
21
|
if foreign_key.options[:dependent].present?
|
22
|
-
statement_parts << (':
|
22
|
+
statement_parts << ('dependent: ' + foreign_key.options[:dependent].inspect)
|
23
23
|
end
|
24
24
|
|
25
25
|
statement_parts.join(', ')
|
@@ -17,18 +17,18 @@ class Foreigner::Mysql2AdapterTest < Foreigner::UnitTest
|
|
17
17
|
@adapter.remove_foreign_key_sql(:suppliers, :companies)
|
18
18
|
)
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
test 'remove_foreign_key_sql by name' do
|
22
22
|
assert_equal(
|
23
23
|
"DROP FOREIGN KEY `belongs_to_supplier`",
|
24
|
-
@adapter.remove_foreign_key_sql(:suppliers, :
|
24
|
+
@adapter.remove_foreign_key_sql(:suppliers, name: "belongs_to_supplier")
|
25
25
|
)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
test 'remove_foreign_key_sql by column' do
|
29
29
|
assert_equal(
|
30
30
|
"DROP FOREIGN KEY `suppliers_ship_to_id_fk`",
|
31
|
-
@adapter.remove_foreign_key_sql(:suppliers, :
|
31
|
+
@adapter.remove_foreign_key_sql(:suppliers, column: "ship_to_id")
|
32
32
|
)
|
33
33
|
end
|
34
34
|
end
|
@@ -20,55 +20,67 @@ class Foreigner::Sql2003Test < Foreigner::UnitTest
|
|
20
20
|
assert @adapter.instance_variable_get(:@disable_referential_integrity)
|
21
21
|
end
|
22
22
|
|
23
|
+
test 'foreign_key_exists' do
|
24
|
+
@adapter.expects(:foreign_keys).with(:mommas).at_least_once.returns [Foreigner::ConnectionAdapters::ForeignKeyDefinition.new(:mommas, :babies, name: 'mommas_baby_id_fk')]
|
25
|
+
|
26
|
+
assert @adapter.foreign_key_exists?(:mommas, :babies)
|
27
|
+
assert @adapter.foreign_key_exists?(:mommas, name: 'mommas_baby_id_fk')
|
28
|
+
assert @adapter.foreign_key_exists?(:mommas, column: 'baby_id')
|
29
|
+
|
30
|
+
refute @adapter.foreign_key_exists?(:mommas, name: 'mommas_foo_id')
|
31
|
+
refute @adapter.foreign_key_exists?(:mommas, column: 'son_id')
|
32
|
+
refute @adapter.foreign_key_exists?(:mommas, :houses)
|
33
|
+
end
|
34
|
+
|
23
35
|
test 'add_without_options' do
|
24
36
|
assert_equal(
|
25
37
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
|
26
38
|
@adapter.add_foreign_key(:employees, :companies)
|
27
39
|
)
|
28
40
|
end
|
29
|
-
|
41
|
+
|
30
42
|
test 'add_with_name' do
|
31
43
|
assert_equal(
|
32
44
|
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
|
33
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
45
|
+
@adapter.add_foreign_key(:employees, :companies, name: 'favorite_company_fk')
|
34
46
|
)
|
35
47
|
end
|
36
|
-
|
48
|
+
|
37
49
|
test 'add_with_column' do
|
38
50
|
assert_equal(
|
39
51
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_last_employer_id_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
|
40
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
41
|
-
)
|
52
|
+
@adapter.add_foreign_key(:employees, :companies, column: 'last_employer_id')
|
53
|
+
)
|
42
54
|
end
|
43
|
-
|
55
|
+
|
44
56
|
test 'add_with_column_and_name' do
|
45
57
|
assert_equal(
|
46
58
|
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
|
47
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
59
|
+
@adapter.add_foreign_key(:employees, :companies, column: 'last_employer_id', name: 'favorite_company_fk')
|
48
60
|
)
|
49
61
|
end
|
50
|
-
|
62
|
+
|
51
63
|
test 'add_with_delete_dependency' do
|
52
64
|
assert_equal(
|
53
65
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
54
66
|
"ON DELETE CASCADE",
|
55
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
67
|
+
@adapter.add_foreign_key(:employees, :companies, dependent: :delete)
|
56
68
|
)
|
57
69
|
end
|
58
|
-
|
70
|
+
|
59
71
|
test 'add_with_nullify_dependency' do
|
60
72
|
assert_equal(
|
61
73
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
62
74
|
"ON DELETE SET NULL",
|
63
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
75
|
+
@adapter.add_foreign_key(:employees, :companies, dependent: :nullify)
|
64
76
|
)
|
65
77
|
end
|
66
|
-
|
78
|
+
|
67
79
|
test 'add_with_restrict_dependency' do
|
68
80
|
assert_equal(
|
69
81
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
70
82
|
"ON DELETE RESTRICT",
|
71
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
83
|
+
@adapter.add_foreign_key(:employees, :companies, dependent: :restrict)
|
72
84
|
)
|
73
85
|
end
|
74
86
|
|
@@ -76,28 +88,28 @@ class Foreigner::Sql2003Test < Foreigner::UnitTest
|
|
76
88
|
assert_equal(
|
77
89
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
78
90
|
"on delete foo",
|
79
|
-
@adapter.add_foreign_key(:employees, :companies, :
|
91
|
+
@adapter.add_foreign_key(:employees, :companies, options: 'on delete foo')
|
80
92
|
)
|
81
93
|
end
|
82
|
-
|
94
|
+
|
83
95
|
test 'remove_by_table' do
|
84
96
|
assert_equal(
|
85
97
|
"ALTER TABLE `suppliers` DROP CONSTRAINT `suppliers_company_id_fk`",
|
86
98
|
@adapter.remove_foreign_key(:suppliers, :companies)
|
87
99
|
)
|
88
100
|
end
|
89
|
-
|
101
|
+
|
90
102
|
test 'remove_by_name' do
|
91
103
|
assert_equal(
|
92
104
|
"ALTER TABLE `suppliers` DROP CONSTRAINT `belongs_to_supplier`",
|
93
|
-
@adapter.remove_foreign_key(:suppliers, :
|
105
|
+
@adapter.remove_foreign_key(:suppliers, name: "belongs_to_supplier")
|
94
106
|
)
|
95
107
|
end
|
96
|
-
|
108
|
+
|
97
109
|
test 'remove_by_column' do
|
98
110
|
assert_equal(
|
99
111
|
"ALTER TABLE `suppliers` DROP CONSTRAINT `suppliers_ship_to_id_fk`",
|
100
|
-
@adapter.remove_foreign_key(:suppliers, :
|
112
|
+
@adapter.remove_foreign_key(:suppliers, column: "ship_to_id")
|
101
113
|
)
|
102
114
|
end
|
103
115
|
end
|
@@ -5,36 +5,47 @@ ActiveRecord::Migration::CommandRecorder.class_eval do
|
|
5
5
|
end
|
6
6
|
|
7
7
|
class Foreigner::CommandRecorderTest < Foreigner::UnitTest
|
8
|
-
|
8
|
+
setup do
|
9
9
|
@recorder = ActiveRecord::Migration::CommandRecorder.new
|
10
10
|
end
|
11
11
|
|
12
12
|
test 'invert_add_foreign_key' do
|
13
|
-
@recorder.
|
14
|
-
|
15
|
-
|
13
|
+
@recorder.revert do
|
14
|
+
@recorder.add_foreign_key(:employees, :companies)
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_equal [
|
18
|
+
[:remove_foreign_key, [:employees, :companies]]
|
19
|
+
], @recorder.commands
|
16
20
|
end
|
17
21
|
|
18
22
|
test 'invert_add_foreign_key with column' do
|
19
|
-
@recorder.
|
20
|
-
|
21
|
-
|
23
|
+
@recorder.revert do
|
24
|
+
@recorder.add_foreign_key(:employees, :companies, column: :place_id)
|
25
|
+
end
|
26
|
+
|
27
|
+
assert_equal [
|
28
|
+
[:remove_foreign_key, [:employees, {column: :place_id}]]
|
29
|
+
], @recorder.commands
|
22
30
|
end
|
23
31
|
|
24
32
|
test 'invert_add_foreign_key with name' do
|
25
|
-
@recorder.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
@recorder.revert do
|
34
|
+
@recorder.add_foreign_key(:employees, :companies, name: 'the_best_fk', column: :place_id)
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal [
|
38
|
+
[:remove_foreign_key, [:employees, {name: 'the_best_fk'}]]
|
39
|
+
], @recorder.commands
|
32
40
|
end
|
33
41
|
|
34
42
|
test 'remove_foreign_key is irreversible' do
|
35
|
-
@recorder.remove_foreign_key(:employees, :companies)
|
36
43
|
assert_raise ActiveRecord::IrreversibleMigration do
|
37
|
-
@recorder.
|
44
|
+
@recorder.revert do
|
45
|
+
@recorder.remove_foreign_key(:employees, :companies)
|
46
|
+
end
|
38
47
|
end
|
48
|
+
# @recorder.inverse
|
49
|
+
# end
|
39
50
|
end
|
40
51
|
end
|
@@ -28,10 +28,10 @@ class Foreigner::SchemaDumperTest < Foreigner::UnitTest
|
|
28
28
|
end
|
29
29
|
|
30
30
|
test 'dump_foreign_key' do
|
31
|
-
assert_dump "add_foreign_key \"foos\", \"bars\", :
|
32
|
-
assert_dump "add_foreign_key \"foos\", \"bars\", :
|
33
|
-
assert_dump "add_foreign_key \"foos\", \"bars\", :
|
34
|
-
assert_dump "add_foreign_key \"foos\", \"bars\", :
|
31
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", name: \"lulz\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'bar_id', primary_key: 'id', name: 'lulz')
|
32
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", name: \"lulz\", primary_key: \"uuid\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'bar_id', primary_key: 'uuid', name: 'lulz')
|
33
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", name: \"lulz\", dependent: :delete", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'bar_id', primary_key: 'id', name: 'lulz', dependent: :delete)
|
34
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", name: \"lulz\", column: \"mamma_id\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'mamma_id', primary_key: 'id', name: 'lulz')
|
35
35
|
end
|
36
36
|
|
37
37
|
test 'all tables' do
|
@@ -52,7 +52,7 @@ class Foreigner::SchemaDumperTest < Foreigner::UnitTest
|
|
52
52
|
begin
|
53
53
|
ActiveRecord::Base.table_name_prefix = 'pre_'
|
54
54
|
ActiveRecord::Base.table_name_suffix = '_suf'
|
55
|
-
assert_dump "add_foreign_key \"foos\", \"bars\", :
|
55
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", name: \"lulz\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('pre_foos_suf', 'pre_bars_suf', column: 'bar_id', primary_key: 'id', name: 'lulz')
|
56
56
|
ensure
|
57
57
|
ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
|
58
58
|
end
|
data/test/helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'bundler/setup'
|
2
|
-
Bundler.require
|
2
|
+
Bundler.require :default, :test
|
3
3
|
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
require 'mocha'
|
4
|
+
require 'active_support/test_case'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'mocha/setup'
|
7
7
|
|
8
8
|
# Foreigner::Adapter.registered.values.each do |file_name|
|
9
9
|
# require file_name
|
@@ -14,7 +14,7 @@ module TestAdapterMethods
|
|
14
14
|
sql_statements << sql
|
15
15
|
sql
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def quote_table_name(name)
|
19
19
|
quote_column_name(name).gsub('.', '`.`')
|
20
20
|
end
|
@@ -27,7 +27,7 @@ module TestAdapterMethods
|
|
27
27
|
@sql_statements ||= []
|
28
28
|
end
|
29
29
|
|
30
|
-
def drop_table(name, options = {})
|
30
|
+
def drop_table(name, options = {})
|
31
31
|
end
|
32
32
|
|
33
33
|
def disable_referential_integrity
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreigner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Higgins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -42,12 +42,11 @@ description: Adds helpers to migrations and dumps foreign keys to schema.rb
|
|
42
42
|
email: developer@matthewhiggins.com
|
43
43
|
executables: []
|
44
44
|
extensions: []
|
45
|
-
extra_rdoc_files:
|
46
|
-
- README.rdoc
|
45
|
+
extra_rdoc_files: []
|
47
46
|
files:
|
48
47
|
- MIT-LICENSE
|
49
48
|
- Rakefile
|
50
|
-
- README.
|
49
|
+
- README.md
|
51
50
|
- lib/foreigner/adapter.rb
|
52
51
|
- lib/foreigner/connection_adapters/abstract/foreign_key_definition.rb
|
53
52
|
- lib/foreigner/connection_adapters/abstract/schema_definitions.rb
|
@@ -85,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
84
|
requirements:
|
86
85
|
- - '>='
|
87
86
|
- !ruby/object:Gem::Version
|
88
|
-
version: 1.
|
87
|
+
version: 1.9.2
|
89
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
89
|
requirements:
|
91
90
|
- - '>='
|