foreigner 0.9.2 → 1.0.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.
- data/README.rdoc +19 -41
- data/lib/foreigner/adapter.rb +22 -0
- data/lib/foreigner/connection_adapters/abstract/schema_definitions.rb +20 -25
- data/lib/foreigner/connection_adapters/{mysql_adapter.rb → mysql2_adapter.rb} +5 -5
- data/lib/foreigner/connection_adapters/postgresql_adapter.rb +11 -29
- data/lib/foreigner/connection_adapters/{sql_2003.rb → sql2003.rb} +22 -4
- data/lib/foreigner/migration/command_recorder.rb +21 -0
- data/lib/foreigner/railtie.rb +24 -0
- data/lib/foreigner/schema_dumper.rb +4 -4
- data/lib/foreigner.rb +15 -44
- data/test/helper.rb +34 -1
- data/test/mysql2_adapter_test.rb +26 -0
- data/test/{mysql_adapter_test.rb → sql2003_test.rb} +16 -30
- metadata +25 -11
data/README.rdoc
CHANGED
@@ -1,33 +1,28 @@
|
|
1
1
|
= Foreigner
|
2
2
|
|
3
|
-
|
4
|
-
methods to your migrations for adding and removing foreign key constraints.
|
3
|
+
Foreigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to schema.rb.
|
5
4
|
|
6
|
-
|
7
|
-
work on databases that do not support foreign keys, such as sqlite3.
|
5
|
+
The following adapters are supported:
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
* mysql2
|
8
|
+
* postgres
|
9
|
+
* sqlite (foreign key methods are a no-op)
|
12
10
|
|
13
|
-
|
11
|
+
== Installation
|
14
12
|
|
15
|
-
|
13
|
+
Add the following to your Gemfile:
|
16
14
|
|
17
15
|
gem 'foreigner'
|
18
16
|
|
19
|
-
== API
|
17
|
+
== API Examples
|
20
18
|
|
21
|
-
|
22
|
-
(Options are documented in connection_adapters/abstract/schema_definitions.rb):
|
19
|
+
Foreigner adds two methods to migrations.
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
foreign_keys(table_name)
|
21
|
+
* add_foreign_key(from_table, to_table, options)
|
22
|
+
* remove_foreign_key(from_table, options)
|
27
23
|
|
28
|
-
|
24
|
+
(Options are documented in connection_adapters/abstract/schema_definitions.rb):
|
29
25
|
|
30
|
-
The most common use of foreign keys is to reference a table that a model belongs to.
|
31
26
|
For example, given the following model:
|
32
27
|
|
33
28
|
class Comment < ActiveRecord::Base
|
@@ -54,9 +49,9 @@ A name can be specified for the foreign key constraint:
|
|
54
49
|
|
55
50
|
add_foreign_key(:comments, :posts, :name => 'comment_article_foreign_key')
|
56
51
|
|
57
|
-
== Change Table
|
52
|
+
== Change Table Methods
|
58
53
|
|
59
|
-
Foreigner adds extra
|
54
|
+
Foreigner adds extra methods to change_table.
|
60
55
|
|
61
56
|
Add a missing foreign key to comments:
|
62
57
|
|
@@ -64,29 +59,12 @@ Add a missing foreign key to comments:
|
|
64
59
|
t.foreign_key :posts, :dependent => :delete
|
65
60
|
end
|
66
61
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
== Additional t.references option
|
71
|
-
|
72
|
-
Foreigner extends table.references with the :foreign_key option. Pass true, and the default
|
73
|
-
foreign key options are used:
|
74
|
-
|
75
|
-
change_table :comments do |t|
|
76
|
-
t.references :post, :foreign_key => true
|
77
|
-
end
|
78
|
-
|
79
|
-
An options hash can also be passed. It accepts the same options as add_foreign_key:
|
80
|
-
|
62
|
+
Remove an unwanted foreign key:
|
63
|
+
|
81
64
|
change_table :comments do |t|
|
82
|
-
t.
|
65
|
+
t.remove_foreign_key :users
|
83
66
|
end
|
84
67
|
|
85
|
-
|
86
|
-
|
87
|
-
== schema.rb
|
88
|
-
|
89
|
-
Similar to indexes, the foreign keys in your database are automatically dumped to schema.rb.
|
90
|
-
This allows you to use foreign keys without switching to the :sql schema.
|
68
|
+
== License
|
91
69
|
|
92
|
-
Copyright (c)
|
70
|
+
Copyright (c) 2011 Matthew Higgins, released under the MIT license
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Foreigner
|
2
|
+
class Adapter
|
3
|
+
class_attribute :registered
|
4
|
+
self.registered = {}
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def register(adapter_name, file_name)
|
8
|
+
registered[adapter_name] = file_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def load!
|
12
|
+
if registered.key?(configured_name)
|
13
|
+
require registered[configured_name]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def configured_name
|
18
|
+
ActiveRecord::Base.connection_pool.spec.config[:adapter]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -12,10 +12,10 @@ module Foreigner
|
|
12
12
|
end
|
13
13
|
|
14
14
|
module Table
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
|
17
|
+
included do
|
18
|
+
alias_method_chain :references, :foreign_keys
|
19
19
|
end
|
20
20
|
|
21
21
|
# Adds a new foreign key to the table. +to_table+ can be a single Symbol, or
|
@@ -38,37 +38,32 @@ module Foreigner
|
|
38
38
|
#
|
39
39
|
# ===== Examples
|
40
40
|
# ====== Remove the suppliers_company_id_fk in the suppliers table.
|
41
|
-
#
|
41
|
+
# change_table :suppliers do |t|
|
42
|
+
# t.remove_foreign_key :companies
|
43
|
+
# end
|
42
44
|
# ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
|
43
|
-
#
|
45
|
+
# change_table :accounts do |t|
|
46
|
+
# t.remove_foreign_key :column => :branch_id
|
47
|
+
# end
|
44
48
|
# ====== Remove the foreign key named party_foreign_key in the accounts table.
|
45
|
-
#
|
49
|
+
# change_table :accounts do |t|
|
50
|
+
# t.remove_index :name => :party_foreign_key
|
51
|
+
# end
|
46
52
|
def remove_foreign_key(options = {})
|
47
53
|
@base.remove_foreign_key(@table_name, options)
|
48
54
|
end
|
49
55
|
|
50
|
-
#
|
51
|
-
# If :foreign_key is true, a foreign key constraint is added to the table.
|
52
|
-
# You can also specify a hash, which is passed as foreign key options.
|
53
|
-
#
|
54
|
-
# ===== Examples
|
55
|
-
# ====== Add goat_id column and a foreign key to the goats table.
|
56
|
-
# t.references(:goat, :foreign_key => true)
|
57
|
-
# ====== Add goat_id column and a cascading foreign key to the goats table.
|
58
|
-
# t.references(:goat, :foreign_key => {:dependent => :delete})
|
59
|
-
#
|
60
|
-
# Note: No foreign key is created if :polymorphic => true is used.
|
56
|
+
# Deprecated
|
61
57
|
def references_with_foreign_keys(*args)
|
62
58
|
options = args.extract_options!
|
63
|
-
polymorphic = options[:polymorphic]
|
64
|
-
fk_options = options.delete(:foreign_key)
|
65
59
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
args.each { |to_table| foreign_key(to_table, fk_options) }
|
60
|
+
if fk_options = options.delete(:foreign_key)
|
61
|
+
p ActiveSupport::Deprecation.send(:deprecation_message, caller,
|
62
|
+
":foreign_key in t.references is deprecated. " \
|
63
|
+
"Use t.foreign_key instead")
|
71
64
|
end
|
65
|
+
|
66
|
+
references_without_foreign_keys(*(args.dup << options))
|
72
67
|
end
|
73
68
|
end
|
74
69
|
end
|
@@ -1,16 +1,16 @@
|
|
1
1
|
module Foreigner
|
2
2
|
module ConnectionAdapters
|
3
|
-
module
|
3
|
+
module Mysql2Adapter
|
4
4
|
include Foreigner::ConnectionAdapters::Sql2003
|
5
5
|
|
6
|
-
def
|
6
|
+
def remove_foreign_key_sql(table, options)
|
7
7
|
if Hash === options
|
8
8
|
foreign_key_name = foreign_key_name(table, options[:column], options)
|
9
9
|
else
|
10
10
|
foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
"DROP FOREIGN KEY #{quote_column_name(foreign_key_name)}"
|
14
14
|
end
|
15
15
|
|
16
16
|
def foreign_keys(table_name)
|
@@ -44,10 +44,10 @@ module Foreigner
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
[:
|
47
|
+
[:Mysql2Adapter, :JdbcAdapter].each do |adapter|
|
48
48
|
begin
|
49
49
|
ActiveRecord::ConnectionAdapters.const_get(adapter).class_eval do
|
50
|
-
include Foreigner::ConnectionAdapters::
|
50
|
+
include Foreigner::ConnectionAdapters::Mysql2Adapter
|
51
51
|
end
|
52
52
|
rescue
|
53
53
|
end
|
@@ -3,43 +3,25 @@ module Foreigner
|
|
3
3
|
module PostgreSQLAdapter
|
4
4
|
include Foreigner::ConnectionAdapters::Sql2003
|
5
5
|
|
6
|
-
def remove_foreign_key(table, options)
|
7
|
-
if Hash === options
|
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
|
12
|
-
|
13
|
-
execute "ALTER TABLE #{quote_table_name(table)} DROP CONSTRAINT #{quote_column_name(foreign_key_name)}"
|
14
|
-
end
|
15
|
-
|
16
6
|
def foreign_keys(table_name)
|
17
7
|
fk_info = select_all %{
|
18
|
-
SELECT
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
JOIN information_schema.referential_constraints rc
|
27
|
-
USING (constraint_catalog, constraint_schema, constraint_name)
|
28
|
-
JOIN information_schema.constraint_column_usage ccu
|
29
|
-
USING (constraint_catalog, constraint_schema, constraint_name)
|
30
|
-
WHERE tc.constraint_type = 'FOREIGN KEY'
|
31
|
-
AND tc.constraint_catalog = '#{@config[:database]}'
|
32
|
-
AND tc.table_name = '#{table_name}'
|
33
|
-
AND tc.table_schema = ANY (current_schemas(false))
|
8
|
+
SELECT t2.relname AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confdeltype AS dependency
|
9
|
+
FROM pg_constraint c
|
10
|
+
JOIN pg_class t1 ON c.conrelid = t1.oid
|
11
|
+
JOIN pg_class t2 ON c.confrelid = t2.oid
|
12
|
+
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
|
13
|
+
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
|
14
|
+
WHERE c.contype = 'f' AND t1.relname = '#{table_name}'
|
15
|
+
ORDER BY c.conname
|
34
16
|
}
|
35
17
|
|
36
18
|
fk_info.map do |row|
|
37
19
|
options = {:column => row['column'], :name => row['name'], :primary_key => row['primary_key']}
|
38
20
|
|
39
21
|
options[:dependent] = case row['dependency']
|
40
|
-
when '
|
41
|
-
when '
|
42
|
-
when '
|
22
|
+
when 'c' then :delete
|
23
|
+
when 'n' then :nullify
|
24
|
+
when 'r' then :restrict
|
43
25
|
end
|
44
26
|
|
45
27
|
ForeignKeyDefinition.new(table_name, row['to_table'], options)
|
@@ -4,22 +4,40 @@ module Foreigner
|
|
4
4
|
def supports_foreign_keys?
|
5
5
|
true
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def add_foreign_key(from_table, to_table, options = {})
|
9
|
+
sql = "ALTER TABLE #{quote_table_name(from_table)} #{add_foreign_key_sql(from_table, to_table, options)}"
|
10
|
+
execute(sql)
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_foreign_key_sql(from_table, to_table, options = {})
|
9
14
|
column = options[:column] || "#{to_table.to_s.singularize}_id"
|
10
15
|
foreign_key_name = foreign_key_name(from_table, column, options)
|
11
16
|
primary_key = options[:primary_key] || "id"
|
12
17
|
dependency = dependency_sql(options[:dependent])
|
13
18
|
|
14
19
|
sql =
|
15
|
-
"ALTER TABLE #{quote_table_name(from_table)} " +
|
16
20
|
"ADD CONSTRAINT #{quote_column_name(foreign_key_name)} " +
|
17
21
|
"FOREIGN KEY (#{quote_column_name(column)}) " +
|
18
22
|
"REFERENCES #{quote_table_name(ActiveRecord::Migrator.proper_table_name(to_table))}(#{primary_key})"
|
19
23
|
sql << " #{dependency}" if dependency.present?
|
20
24
|
sql << " #{options[:options]}" if options[:options]
|
21
|
-
|
22
|
-
|
25
|
+
|
26
|
+
sql
|
27
|
+
end
|
28
|
+
|
29
|
+
def remove_foreign_key(table, options)
|
30
|
+
execute "ALTER TABLE #{quote_table_name(table)} #{remove_foreign_key_sql(table, options)}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def remove_foreign_key_sql(table, options)
|
34
|
+
if Hash === options
|
35
|
+
foreign_key_name = foreign_key_name(table, options[:column], options)
|
36
|
+
else
|
37
|
+
foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
|
38
|
+
end
|
39
|
+
|
40
|
+
"DROP CONSTRAINT #{quote_column_name(foreign_key_name)}"
|
23
41
|
end
|
24
42
|
|
25
43
|
private
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Foreigner
|
2
|
+
module Migration
|
3
|
+
module CommandRecorder
|
4
|
+
def add_foreign_key(*args)
|
5
|
+
record(:add_foreign_key, args)
|
6
|
+
end
|
7
|
+
|
8
|
+
def remove_foreign_key(*args)
|
9
|
+
record(:remove_foreign_key, args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def invert_add_foreign_key(*args)
|
13
|
+
[:remove_foreign_key, *args]
|
14
|
+
end
|
15
|
+
|
16
|
+
def invert_remove_foreign_key(*args)
|
17
|
+
[:add_foreign_key, *args]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Foreigner
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer 'foreigner.load_adapter' do
|
4
|
+
ActiveSupport.on_load :active_record do
|
5
|
+
ActiveRecord::ConnectionAdapters.module_eval do
|
6
|
+
include Foreigner::ConnectionAdapters::SchemaStatements
|
7
|
+
include Foreigner::ConnectionAdapters::SchemaDefinitions
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::SchemaDumper.class_eval do
|
11
|
+
include Foreigner::SchemaDumper
|
12
|
+
end
|
13
|
+
|
14
|
+
if defined?(ActiveRecord::Migration::CommandRecorder)
|
15
|
+
ActiveRecord::Migration::CommandRecorder.class_eval do
|
16
|
+
include Foreigner::Migration::CommandRecorder
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Foreigner::Adapter.load!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module Foreigner
|
2
2
|
module SchemaDumper
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
alias_method_chain :tables, :foreign_keys
|
7
7
|
end
|
8
8
|
|
9
9
|
def tables_with_foreign_keys(stream)
|
data/lib/foreigner.rb
CHANGED
@@ -1,53 +1,24 @@
|
|
1
|
-
require 'foreigner/connection_adapters/abstract/schema_statements'
|
2
|
-
require 'foreigner/connection_adapters/abstract/schema_definitions'
|
3
|
-
require 'foreigner/connection_adapters/sql_2003'
|
4
|
-
require 'foreigner/schema_dumper'
|
5
|
-
|
6
1
|
module Foreigner
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
def register(adapter_name, file_name)
|
13
|
-
adapters[adapter_name] = file_name
|
14
|
-
end
|
15
|
-
|
16
|
-
def load_adapter!
|
17
|
-
ActiveRecord::ConnectionAdapters.module_eval do
|
18
|
-
include Foreigner::ConnectionAdapters::SchemaStatements
|
19
|
-
include Foreigner::ConnectionAdapters::SchemaDefinitions
|
20
|
-
end
|
2
|
+
extend ActiveSupport::Autoload
|
3
|
+
autoload :Adapter
|
4
|
+
autoload :SchemaDumper
|
21
5
|
|
22
|
-
|
23
|
-
|
24
|
-
|
6
|
+
module ConnectionAdapters
|
7
|
+
extend ActiveSupport::Autoload
|
8
|
+
autoload :Sql2003
|
25
9
|
|
26
|
-
|
27
|
-
|
28
|
-
|
10
|
+
autoload_under 'abstract' do
|
11
|
+
autoload :SchemaDefinitions
|
12
|
+
autoload :SchemaStatements
|
29
13
|
end
|
14
|
+
end
|
30
15
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
16
|
+
module Migration
|
17
|
+
autoload :CommandRecorder, 'foreigner/migration/command_recorder'
|
34
18
|
end
|
35
19
|
end
|
36
20
|
|
37
|
-
Foreigner.register '
|
38
|
-
Foreigner.register '
|
39
|
-
Foreigner.register 'postgresql', 'foreigner/connection_adapters/postgresql_adapter'
|
21
|
+
Foreigner::Adapter.register 'mysql2', 'foreigner/connection_adapters/mysql2_adapter'
|
22
|
+
Foreigner::Adapter.register 'postgresql', 'foreigner/connection_adapters/postgresql_adapter'
|
40
23
|
|
41
|
-
|
42
|
-
module Foreigner
|
43
|
-
class Railtie < Rails::Railtie
|
44
|
-
initializer 'foreigner.load_adapter' do
|
45
|
-
ActiveSupport.on_load :active_record do
|
46
|
-
Foreigner.load_adapter!
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
else
|
52
|
-
Foreigner.load_adapter!
|
53
|
-
end
|
24
|
+
require 'foreigner/railtie'
|
data/test/helper.rb
CHANGED
@@ -2,4 +2,37 @@ require 'rubygems'
|
|
2
2
|
require 'test/unit'
|
3
3
|
require 'rails/all'
|
4
4
|
|
5
|
-
require 'foreigner'
|
5
|
+
require 'foreigner'
|
6
|
+
|
7
|
+
Foreigner::Adapter.registered.values.each do |file_name|
|
8
|
+
require file_name
|
9
|
+
end
|
10
|
+
|
11
|
+
module Foreigner
|
12
|
+
class UnitTest < ActiveSupport::TestCase
|
13
|
+
private
|
14
|
+
def execute(sql, name = nil)
|
15
|
+
sql
|
16
|
+
end
|
17
|
+
|
18
|
+
def quote_column_name(name)
|
19
|
+
"`#{name}`"
|
20
|
+
end
|
21
|
+
|
22
|
+
def quote_table_name(name)
|
23
|
+
quote_column_name(name).gsub('.', '`.`')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class IntegrationTest < ActiveSupport::TestCase
|
28
|
+
def with_migration(&blk)
|
29
|
+
migration = Class.new(ActiveRecord::Migration)
|
30
|
+
|
31
|
+
migration.singleton_class do
|
32
|
+
define_method(:up, &blk)
|
33
|
+
end
|
34
|
+
|
35
|
+
migration
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Mysql2AdapterTest < Foreigner::AdapterTest
|
4
|
+
include Foreigner::ConnectionAdapters::Mysql2Adapter
|
5
|
+
|
6
|
+
test 'remove_by_table' do
|
7
|
+
assert_equal(
|
8
|
+
"DROP FOREIGN KEY `suppliers_company_id_fk`",
|
9
|
+
remove_foreign_key_sql(:suppliers, :companies)
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'remove_by_name' do
|
14
|
+
assert_equal(
|
15
|
+
"DROP FOREIGN KEY `belongs_to_supplier`",
|
16
|
+
remove_foreign_key_sql(:suppliers, :name => "belongs_to_supplier")
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'remove_by_column' do
|
21
|
+
assert_equal(
|
22
|
+
"DROP FOREIGN KEY `suppliers_ship_to_id_fk`",
|
23
|
+
remove_foreign_key_sql(:suppliers, :column => "ship_to_id")
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
@@ -1,38 +1,37 @@
|
|
1
1
|
require 'helper'
|
2
|
-
require 'foreigner/connection_adapters/mysql_adapter'
|
3
2
|
|
4
|
-
class
|
5
|
-
include Foreigner::ConnectionAdapters::
|
3
|
+
class Sql2003Test < Foreigner::AdapterTest
|
4
|
+
include Foreigner::ConnectionAdapters::Sql2003
|
6
5
|
|
7
|
-
|
6
|
+
test 'add_without_options' do
|
8
7
|
assert_equal(
|
9
8
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
|
10
9
|
add_foreign_key(:employees, :companies)
|
11
10
|
)
|
12
11
|
end
|
13
12
|
|
14
|
-
|
13
|
+
test 'add_with_name' do
|
15
14
|
assert_equal(
|
16
15
|
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
|
17
16
|
add_foreign_key(:employees, :companies, :name => 'favorite_company_fk')
|
18
17
|
)
|
19
18
|
end
|
20
19
|
|
21
|
-
|
20
|
+
test 'add_with_column' do
|
22
21
|
assert_equal(
|
23
22
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_last_employer_id_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
|
24
23
|
add_foreign_key(:employees, :companies, :column => 'last_employer_id')
|
25
24
|
)
|
26
25
|
end
|
27
26
|
|
28
|
-
|
27
|
+
test 'add_with_column_and_name' do
|
29
28
|
assert_equal(
|
30
29
|
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
|
31
30
|
add_foreign_key(:employees, :companies, :column => 'last_employer_id', :name => 'favorite_company_fk')
|
32
31
|
)
|
33
32
|
end
|
34
33
|
|
35
|
-
|
34
|
+
test 'add_with_delete_dependency' do
|
36
35
|
assert_equal(
|
37
36
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
38
37
|
"ON DELETE CASCADE",
|
@@ -40,7 +39,7 @@ class MysqlAdapterTest < ActiveRecord::TestCase
|
|
40
39
|
)
|
41
40
|
end
|
42
41
|
|
43
|
-
|
42
|
+
test 'add_with_nullify_dependency' do
|
44
43
|
assert_equal(
|
45
44
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
46
45
|
"ON DELETE SET NULL",
|
@@ -48,7 +47,7 @@ class MysqlAdapterTest < ActiveRecord::TestCase
|
|
48
47
|
)
|
49
48
|
end
|
50
49
|
|
51
|
-
|
50
|
+
test 'add_with_restrict_dependency' do
|
52
51
|
assert_equal(
|
53
52
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
54
53
|
"ON DELETE RESTRICT",
|
@@ -56,7 +55,7 @@ class MysqlAdapterTest < ActiveRecord::TestCase
|
|
56
55
|
)
|
57
56
|
end
|
58
57
|
|
59
|
-
|
58
|
+
test 'add_with_options' do
|
60
59
|
assert_equal(
|
61
60
|
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
62
61
|
"on delete foo",
|
@@ -64,37 +63,24 @@ class MysqlAdapterTest < ActiveRecord::TestCase
|
|
64
63
|
)
|
65
64
|
end
|
66
65
|
|
67
|
-
|
66
|
+
test 'remove_by_table' do
|
68
67
|
assert_equal(
|
69
|
-
"ALTER TABLE `suppliers` DROP
|
68
|
+
"ALTER TABLE `suppliers` DROP CONSTRAINT `suppliers_company_id_fk`",
|
70
69
|
remove_foreign_key(:suppliers, :companies)
|
71
70
|
)
|
72
71
|
end
|
73
72
|
|
74
|
-
|
73
|
+
test 'remove_by_name' do
|
75
74
|
assert_equal(
|
76
|
-
"ALTER TABLE `suppliers` DROP
|
75
|
+
"ALTER TABLE `suppliers` DROP CONSTRAINT `belongs_to_supplier`",
|
77
76
|
remove_foreign_key(:suppliers, :name => "belongs_to_supplier")
|
78
77
|
)
|
79
78
|
end
|
80
79
|
|
81
|
-
|
80
|
+
test 'remove_by_column' do
|
82
81
|
assert_equal(
|
83
|
-
"ALTER TABLE `suppliers` DROP
|
82
|
+
"ALTER TABLE `suppliers` DROP CONSTRAINT `suppliers_ship_to_id_fk`",
|
84
83
|
remove_foreign_key(:suppliers, :column => "ship_to_id")
|
85
84
|
)
|
86
85
|
end
|
87
|
-
|
88
|
-
private
|
89
|
-
def execute(sql, name = nil)
|
90
|
-
sql
|
91
|
-
end
|
92
|
-
|
93
|
-
def quote_column_name(name)
|
94
|
-
"`#{name}`"
|
95
|
-
end
|
96
|
-
|
97
|
-
def quote_table_name(name)
|
98
|
-
quote_column_name(name).gsub('.', '`.`')
|
99
|
-
end
|
100
86
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: foreigner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Matthew Higgins
|
@@ -10,11 +10,21 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-17 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
|
-
dependencies:
|
16
|
-
|
17
|
-
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 3.0.0
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description: Adds helpers to migrations and dumps foreign keys to schema.rb
|
18
28
|
email: developer@matthewhiggins.com
|
19
29
|
executables: []
|
20
30
|
|
@@ -26,15 +36,19 @@ files:
|
|
26
36
|
- MIT-LICENSE
|
27
37
|
- Rakefile
|
28
38
|
- README.rdoc
|
39
|
+
- lib/foreigner/adapter.rb
|
29
40
|
- lib/foreigner/connection_adapters/abstract/schema_definitions.rb
|
30
41
|
- lib/foreigner/connection_adapters/abstract/schema_statements.rb
|
31
|
-
- lib/foreigner/connection_adapters/
|
42
|
+
- lib/foreigner/connection_adapters/mysql2_adapter.rb
|
32
43
|
- lib/foreigner/connection_adapters/postgresql_adapter.rb
|
33
|
-
- lib/foreigner/connection_adapters/
|
44
|
+
- lib/foreigner/connection_adapters/sql2003.rb
|
45
|
+
- lib/foreigner/migration/command_recorder.rb
|
46
|
+
- lib/foreigner/railtie.rb
|
34
47
|
- lib/foreigner/schema_dumper.rb
|
35
48
|
- lib/foreigner.rb
|
36
49
|
- test/helper.rb
|
37
|
-
- test/
|
50
|
+
- test/mysql2_adapter_test.rb
|
51
|
+
- test/sql2003_test.rb
|
38
52
|
has_rdoc: true
|
39
53
|
homepage: http://github.com/matthuhiggins/foreigner
|
40
54
|
licenses: []
|
@@ -49,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
63
|
requirements:
|
50
64
|
- - ">="
|
51
65
|
- !ruby/object:Gem::Version
|
52
|
-
version: 1.8.
|
66
|
+
version: 1.8.7
|
53
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
68
|
none: false
|
55
69
|
requirements:
|
@@ -59,9 +73,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
73
|
requirements: []
|
60
74
|
|
61
75
|
rubyforge_project: foreigner
|
62
|
-
rubygems_version: 1.6.
|
76
|
+
rubygems_version: 1.6.2
|
63
77
|
signing_key:
|
64
78
|
specification_version: 3
|
65
|
-
summary: Foreign
|
79
|
+
summary: Foreign Keys for Rails
|
66
80
|
test_files: []
|
67
81
|
|