matthuhiggins-foreigner 0.1.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/MIT-LICENSE +20 -0
- data/README +106 -0
- data/Rakefile +23 -0
- data/lib/foreigner/connection_adapters/abstract/schema_definitions.rb +135 -0
- data/lib/foreigner/connection_adapters/abstract/schema_statements.rb +62 -0
- data/lib/foreigner/connection_adapters/mysql_adapter.rb +84 -0
- data/lib/foreigner/schema_dumper.rb +39 -0
- data/lib/foreigner.rb +28 -0
- data/test/helper.rb +9 -0
- data/test/mysql_adapter_test.rb +83 -0
- metadata +67 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
Foreigner
|
2
|
+
=========
|
3
|
+
|
4
|
+
Rails does not come with methods to add foreign keys. Foreigner introduces a few
|
5
|
+
methods to your migrations for adding and removing foreign key constraints.
|
6
|
+
|
7
|
+
Since each adapter implements the API, migrations using Foreigner will continue to
|
8
|
+
work on databases that do not support foreign keys, such as sqlite3.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
Install as a plugin:
|
14
|
+
|
15
|
+
ruby script/plugin install git://github.com/matthuhiggins/foreigner.git
|
16
|
+
|
17
|
+
Install as a gem by adding the following to environment.rb:
|
18
|
+
|
19
|
+
config.gem "matthuhiggins-foreigner", :lib => "foreigner"
|
20
|
+
|
21
|
+
API
|
22
|
+
---
|
23
|
+
|
24
|
+
An adapter implementing the Foreigner API implements three methods.
|
25
|
+
(Options are documented in connection_adapters/abstract/schema_definitions.rb):
|
26
|
+
|
27
|
+
add_foreign_key(from_table, to_table, options)
|
28
|
+
remove_foreign_key(from_table, options)
|
29
|
+
foreign_keys(table_name)
|
30
|
+
|
31
|
+
Example
|
32
|
+
-------
|
33
|
+
|
34
|
+
The most common use of foreign keys is to reference a table that a model belongs to.
|
35
|
+
For example, given the following model:
|
36
|
+
|
37
|
+
class Comment < ActiveRecord::Base
|
38
|
+
belongs_to :post
|
39
|
+
end
|
40
|
+
|
41
|
+
class Post < ActiveRecord::Base
|
42
|
+
has_many :comments, :dependent => :delete_all
|
43
|
+
end
|
44
|
+
|
45
|
+
You should add a foreign key in your migration:
|
46
|
+
|
47
|
+
add_foreign_key(:comments, :posts)
|
48
|
+
|
49
|
+
The :dependent option can be moved from the has_many definition to the foreign key:
|
50
|
+
|
51
|
+
add_foreign_key(:comments, :posts, :dependent => :delete)
|
52
|
+
|
53
|
+
If the column is named article_id instead of post_id, use the :column option:
|
54
|
+
|
55
|
+
add_foreign_key(:comments, :posts, :column => 'article_id')
|
56
|
+
|
57
|
+
Lastly, a name can be specified for the foreign key constraint:
|
58
|
+
|
59
|
+
add_foreign_key(:comments, :posts, :name => 'comment_article_foreign_key')
|
60
|
+
|
61
|
+
Create/Change Table Shorthand
|
62
|
+
-----------------------------
|
63
|
+
|
64
|
+
Foreigner adds extra behavior to create_table and change_table, which lets you define foreign keys using shorthand.
|
65
|
+
|
66
|
+
Create the comments table with a foreign key to posts:
|
67
|
+
|
68
|
+
create_table :comments do |t|
|
69
|
+
t.integer :post_id
|
70
|
+
t.foreign_key :posts
|
71
|
+
end
|
72
|
+
|
73
|
+
Add a missing foreign key to comments:
|
74
|
+
|
75
|
+
change_table :comments do |t|
|
76
|
+
t.foreign_key :posts, :dependent => :delete
|
77
|
+
end
|
78
|
+
|
79
|
+
t.foreign_key accepts the same options as add_foreign_key.
|
80
|
+
|
81
|
+
|
82
|
+
Additional t.references option
|
83
|
+
------------------------------
|
84
|
+
|
85
|
+
Foreigner extends table.references with the :foreign_key option. Pass true, and the default
|
86
|
+
foreign key options are used:
|
87
|
+
|
88
|
+
create_table :comments do |t|
|
89
|
+
t.references :post, :foreign_key => true
|
90
|
+
end
|
91
|
+
|
92
|
+
An options hash can also be passed. It accepts the same options as add_foreign_key:
|
93
|
+
|
94
|
+
change_table :comments do |t|
|
95
|
+
t.references :author, :foreign_key => {:dependent => :destroy}
|
96
|
+
end
|
97
|
+
|
98
|
+
By default, t.references will not generate a foreign key.
|
99
|
+
|
100
|
+
schema.rb
|
101
|
+
---------
|
102
|
+
|
103
|
+
Similar to indexes, the foreign keys in your database are automatically dumped to schema.rb.
|
104
|
+
This allows you to use foreign keys without fighting Rails!
|
105
|
+
|
106
|
+
Copyright (c) 2009 Matthew Higgins, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the foreigner plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the foreigner plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Foreigner'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
module Foreigner
|
2
|
+
class ForeignKeyDefinition < Struct.new(:from_table, :to_table, :options) #:nodoc:
|
3
|
+
end
|
4
|
+
|
5
|
+
module TableDefinition
|
6
|
+
class ForeignKey < Struct.new(:base, :to_table, :options)
|
7
|
+
def to_sql
|
8
|
+
base.foreign_key_definition(to_table, options)
|
9
|
+
end
|
10
|
+
alias to_s :to_sql
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.class_eval do
|
15
|
+
include InstanceMethods
|
16
|
+
alias_method_chain :references, :foreign_keys
|
17
|
+
alias_method_chain :to_sql, :foreign_keys
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
# Adds a :foreign_key option to TableDefinition.references.
|
23
|
+
# If :foreign_key is true, a foreign key constraint is added to the table.
|
24
|
+
# You can also specify a hash, which is passed as foreign key options.
|
25
|
+
#
|
26
|
+
# ===== Examples
|
27
|
+
# ====== Add goat_id column and a foreign key to the goats table.
|
28
|
+
# t.references(:goat, :foreign_key => true)
|
29
|
+
# ====== Add goat_id column and a cascading foreign key to the goats table.
|
30
|
+
# t.references(:goat, :foreign_key => {:dependent => :delete})
|
31
|
+
#
|
32
|
+
# Note: No foreign key is created if :polymorphic => true is used.
|
33
|
+
# Note: If no name is specified, the database driver creates one for you!
|
34
|
+
def references_with_foreign_keys(*args)
|
35
|
+
options = args.extract_options!
|
36
|
+
fk_options = options.delete(:foreign_key)
|
37
|
+
|
38
|
+
if fk_options && !options[:polymorphic]
|
39
|
+
fk_options = {} if options[:foreign_key] == true
|
40
|
+
args.each { |to_table| foreign_key(to_table, fk_options) }
|
41
|
+
end
|
42
|
+
|
43
|
+
references_without_foreign_keys(*(args << options))
|
44
|
+
end
|
45
|
+
|
46
|
+
# Defines a foreign key for the table. +to_table+ can be a single Symbol, or
|
47
|
+
# an Array of Symbols. See SchemaStatements#add_foreign_key
|
48
|
+
#
|
49
|
+
# ===== Examples
|
50
|
+
# ====== Creating a simple foreign key
|
51
|
+
# t.foreign_key(:people)
|
52
|
+
# ====== Defining the column
|
53
|
+
# t.foreign_key(:people, :column => :sender_id)
|
54
|
+
# ====== Creating a named foreign key
|
55
|
+
# t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
|
56
|
+
def foreign_key(to_table, options = {})
|
57
|
+
to_table = to_table.to_s.pluralize if ActiveRecord::Base.pluralize_table_names
|
58
|
+
foreign_keys << ForeignKey.new(@base, to_table, options)
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_sql_with_foreign_keys
|
62
|
+
sql = to_sql_without_foreign_keys
|
63
|
+
sql << ', ' << (foreign_keys * ', ') if foreign_keys.present?
|
64
|
+
sql
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def foreign_keys
|
69
|
+
@foreign_keys ||= []
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module Table
|
75
|
+
def self.included(base)
|
76
|
+
base.class_eval do
|
77
|
+
include InstanceMethods
|
78
|
+
alias_method_chain :references, :foreign_keys
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
module InstanceMethods
|
83
|
+
# Adds a new foreign key to the table. +to_table+ can be a single Symbol, or
|
84
|
+
# an Array of Symbols. See SchemaStatements#add_foreign_key
|
85
|
+
#
|
86
|
+
# ===== Examples
|
87
|
+
# ====== Creating a simple foreign key
|
88
|
+
# t.foreign_key(:people)
|
89
|
+
# ====== Defining the column
|
90
|
+
# t.foreign_key(:people, :column => :sender_id)
|
91
|
+
# ====== Creating a named foreign key
|
92
|
+
# t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
|
93
|
+
def foreign_key(to_table, options = {})
|
94
|
+
@base.add_foreign_key(@table_name, to_table, options)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Remove the given foreign key from the table.
|
98
|
+
#
|
99
|
+
# ===== Examples
|
100
|
+
# ====== Remove the suppliers_company_id_fk in the suppliers table.
|
101
|
+
# t.remove_foreign_key :companies
|
102
|
+
# ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
|
103
|
+
# remove_foreign_key :column => :branch_id
|
104
|
+
# ====== Remove the foreign key named party_foreign_key in the accounts table.
|
105
|
+
# remove_index :name => :party_foreign_key
|
106
|
+
def remove_foreign_key(options = {})
|
107
|
+
@base.remove_foreign_key(@table_name, options)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Adds a :foreign_key option to TableDefinition.references.
|
111
|
+
# If :foreign_key is true, a foreign key constraint is added to the table.
|
112
|
+
# You can also specify a hash, which is passed as foreign key options.
|
113
|
+
#
|
114
|
+
# ===== Examples
|
115
|
+
# ====== Add goat_id column and a foreign key to the goats table.
|
116
|
+
# t.references(:goat, :foreign_key => true)
|
117
|
+
# ====== Add goat_id column and a cascading foreign key to the goats table.
|
118
|
+
# t.references(:goat, :foreign_key => {:dependent => :delete})
|
119
|
+
#
|
120
|
+
# Note: No foreign key is created if :polymorphic => true is used.
|
121
|
+
def references_with_foreign_keys(*args)
|
122
|
+
options = args.extract_options!
|
123
|
+
polymorphic = options[:polymorphic]
|
124
|
+
fk_options = options.delete(:foreign_key)
|
125
|
+
|
126
|
+
references_without_foreign_keys(*(args << options))
|
127
|
+
|
128
|
+
if fk_options && !polymorphic
|
129
|
+
fk_options = {} if options[:foreign_key] == true
|
130
|
+
args.each { |to_table| foreign_key(to_table, fk_options) }
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Foreigner
|
2
|
+
module AdapterMethods
|
3
|
+
def supports_foreign_keys?
|
4
|
+
false
|
5
|
+
end
|
6
|
+
|
7
|
+
# Adds a new foreign key to the +from_table+, referencing the primary key of +to_table+
|
8
|
+
#
|
9
|
+
# The foreign key will be named after the from and to tables unless you pass
|
10
|
+
# <tt>:name</tt> as an option.
|
11
|
+
#
|
12
|
+
# ===== Examples
|
13
|
+
# ====== Creating a foreign key
|
14
|
+
# add_foreign_key(:comments, :posts)
|
15
|
+
# generates
|
16
|
+
# ALTER TABLE `comments` ADD CONSTRAINT
|
17
|
+
# `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
|
18
|
+
#
|
19
|
+
# ====== Creating a named foreign key
|
20
|
+
# add_foreign_key(:comments, :posts, :name => 'comments_belongs_to_posts')
|
21
|
+
# generates
|
22
|
+
# ALTER TABLE `comments` ADD CONSTRAINT
|
23
|
+
# `comments_belongs_to_posts` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
|
24
|
+
#
|
25
|
+
# ====== Creating a cascading foreign_key on a custom column
|
26
|
+
# add_foreign_key(:people, :people, :column => 'best_friend_id', :dependent => :nullify)
|
27
|
+
# generates
|
28
|
+
# ALTER TABLE `people` ADD CONSTRAINT
|
29
|
+
# `people_best_friend_id_fk` FOREIGN KEY (`best_friend_id`) REFERENCES `people` (`id`)
|
30
|
+
# ON DELETE SET NULL
|
31
|
+
#
|
32
|
+
# === Supported options
|
33
|
+
# [:column]
|
34
|
+
# Specify the column name on the from_table that references the to_table. By default this is guessed
|
35
|
+
# to be the singular name of the to_table with "_id" suffixed. So a to_table of :posts will use "post_id"
|
36
|
+
# as the default <tt>:column</tt>.
|
37
|
+
# [:name]
|
38
|
+
# Specify the name of the foreign key constraint. This defaults to use from_table and foreign key column.
|
39
|
+
# [:dependent]
|
40
|
+
# If set to <tt>:delete</tt>, the associated records in from_table are deleted when records in to_table table are deleted.
|
41
|
+
# If set to <tt>:nullify</tt>, the foreign key column is set to +NULL+.
|
42
|
+
def add_foreign_key(from_table, to_table, options = {})
|
43
|
+
end
|
44
|
+
|
45
|
+
# Remove the given foreign key from the table.
|
46
|
+
#
|
47
|
+
# ===== Examples
|
48
|
+
# ====== Remove the suppliers_company_id_fk in the suppliers table.
|
49
|
+
# remove_foreign_key :suppliers, :companies
|
50
|
+
# ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
|
51
|
+
# remove_foreign_key :accounts, :column => :branch_id
|
52
|
+
# ====== Remove the foreign key named party_foreign_key in the accounts table.
|
53
|
+
# remove_foreign_key :accounts, :name => :party_foreign_key
|
54
|
+
def remove_foreign_key(from_table, options)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Return the foreign keys for the schema_dumper
|
58
|
+
def foreign_keys(table_name)
|
59
|
+
[]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Foreigner
|
2
|
+
module MysqlAdapter
|
3
|
+
def supports_foreign_keys?
|
4
|
+
true
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_foreign_key(from_table, to_table, options = {})
|
8
|
+
column = options[:column] || "#{to_table.to_s.singularize}_id"
|
9
|
+
foreign_key_name = foreign_key_name(from_table, column, options)
|
10
|
+
|
11
|
+
sql =
|
12
|
+
"ALTER TABLE #{quote_table_name(from_table)} " +
|
13
|
+
"ADD CONSTRAINT #{quote_column_name(foreign_key_name)} " +
|
14
|
+
foreign_key_definition(to_table, options)
|
15
|
+
|
16
|
+
execute(sql)
|
17
|
+
end
|
18
|
+
|
19
|
+
def foreign_key_definition(to_table, options = {})
|
20
|
+
column = options[:column] || "#{to_table.to_s.singularize}_id"
|
21
|
+
dependency = dependency_sql(options[:dependent])
|
22
|
+
|
23
|
+
sql = "FOREIGN KEY (#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}(id)"
|
24
|
+
sql << " #{dependency}" unless dependency.blank?
|
25
|
+
sql
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove_foreign_key(table, options)
|
29
|
+
if Hash === options
|
30
|
+
foreign_key_name = foreign_key_name(table, options[:column], options)
|
31
|
+
else
|
32
|
+
foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
|
33
|
+
end
|
34
|
+
|
35
|
+
execute "ALTER TABLE #{quote_table_name(table)} DROP FOREIGN KEY #{quote_column_name(foreign_key_name)}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def foreign_keys(table_name)
|
39
|
+
foreign_keys = []
|
40
|
+
fk_info = select_all %{
|
41
|
+
SELECT fk.referenced_table_name as 'to_table'
|
42
|
+
,fk.column_name as 'column'
|
43
|
+
,fk.constraint_name as 'name'
|
44
|
+
FROM information_schema.key_column_usage fk
|
45
|
+
WHERE fk.referenced_column_name is not null
|
46
|
+
AND fk.table_schema = '#{@config[:database]}'
|
47
|
+
AND fk.table_name = '#{table_name}'
|
48
|
+
}
|
49
|
+
|
50
|
+
create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"]
|
51
|
+
|
52
|
+
fk_info.each do |row|
|
53
|
+
options = {:column => row['column'], :name => row['name']}
|
54
|
+
if create_table_info =~ /CONSTRAINT #{quote_column_name(row['name'])} FOREIGN KEY .* REFERENCES .* ON DELETE (CASCADE|SET NULL)/
|
55
|
+
if $1 == 'CASCADE'
|
56
|
+
options[:dependent] = :delete
|
57
|
+
elsif $1 == 'SET NULL'
|
58
|
+
options[:dependent] = :nullify
|
59
|
+
end
|
60
|
+
end
|
61
|
+
foreign_keys << ForeignKeyDefinition.new(table_name, row['to_table'], options)
|
62
|
+
end
|
63
|
+
|
64
|
+
foreign_keys
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def foreign_key_name(table, column, options = {})
|
69
|
+
if options[:name]
|
70
|
+
options[:name]
|
71
|
+
else
|
72
|
+
"#{table}_#{column}_fk"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def dependency_sql(dependency)
|
77
|
+
case dependency
|
78
|
+
when :nullify then "ON DELETE SET NULL"
|
79
|
+
when :delete then "ON DELETE CASCADE"
|
80
|
+
else ""
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Foreigner
|
2
|
+
module SchemaDumper
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
include InstanceMethods
|
6
|
+
alias_method_chain :tables, :foreign_keys
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
def tables_with_foreign_keys(stream)
|
12
|
+
tables_without_foreign_keys(stream)
|
13
|
+
@connection.tables.sort.each do |table|
|
14
|
+
foreign_keys(table, stream)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def foreign_keys(table_name, stream)
|
20
|
+
if (foreign_keys = @connection.foreign_keys(table_name)).any?
|
21
|
+
add_foreign_key_statements = foreign_keys.map do |foreign_key|
|
22
|
+
statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
|
23
|
+
statement_parts << foreign_key.to_table.inspect
|
24
|
+
statement_parts << (':column => ' + foreign_key.options[:column].inspect)
|
25
|
+
statement_parts << (':name => ' + foreign_key.options[:name].inspect)
|
26
|
+
if foreign_key.options[:dependent].present?
|
27
|
+
statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
|
28
|
+
end
|
29
|
+
|
30
|
+
' ' + statement_parts.join(', ')
|
31
|
+
end
|
32
|
+
|
33
|
+
stream.puts add_foreign_key_statements.sort.join("\n")
|
34
|
+
stream.puts
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/foreigner.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'foreigner/connection_adapters/abstract/schema_statements'
|
2
|
+
require 'foreigner/connection_adapters/abstract/schema_definitions'
|
3
|
+
require 'foreigner/connection_adapters/mysql_adapter'
|
4
|
+
require 'foreigner/schema_dumper'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module ConnectionAdapters
|
8
|
+
AbstractAdapter.class_eval do
|
9
|
+
include Foreigner::AdapterMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
MysqlAdapter.class_eval do
|
13
|
+
include Foreigner::MysqlAdapter
|
14
|
+
end
|
15
|
+
|
16
|
+
TableDefinition.class_eval do
|
17
|
+
include Foreigner::TableDefinition
|
18
|
+
end
|
19
|
+
|
20
|
+
Table.class_eval do
|
21
|
+
include Foreigner::Table
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
SchemaDumper.class_eval do
|
26
|
+
include Foreigner::SchemaDumper
|
27
|
+
end
|
28
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/test_case'
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/test_case'
|
7
|
+
require 'active_record/connection_adapters/mysql_adapter'
|
8
|
+
$:.unshift File.dirname(__FILE__) + '/../lib/'
|
9
|
+
require 'foreigner'
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class MysqlAdapterTest < ActiveRecord::TestCase
|
4
|
+
include Foreigner::MysqlAdapter
|
5
|
+
|
6
|
+
def test_add_without_options
|
7
|
+
assert_equal(
|
8
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
|
9
|
+
add_foreign_key(:employees, :companies)
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_add_with_name
|
14
|
+
assert_equal(
|
15
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
|
16
|
+
add_foreign_key(:employees, :companies, :name => 'favorite_company_fk')
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_add_with_column
|
21
|
+
assert_equal(
|
22
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `employees_last_employer_id_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
|
23
|
+
add_foreign_key(:employees, :companies, :column => 'last_employer_id')
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_add_with_column_and_name
|
28
|
+
assert_equal(
|
29
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
|
30
|
+
add_foreign_key(:employees, :companies, :column => 'last_employer_id', :name => 'favorite_company_fk')
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_add_with_delete_dependency
|
35
|
+
assert_equal(
|
36
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
37
|
+
"ON DELETE CASCADE",
|
38
|
+
add_foreign_key(:employees, :companies, :dependent => :delete)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_add_with_nullify_dependency
|
43
|
+
assert_equal(
|
44
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
45
|
+
"ON DELETE SET NULL",
|
46
|
+
add_foreign_key(:employees, :companies, :dependent => :nullify)
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_remove_by_table
|
51
|
+
assert_equal(
|
52
|
+
"ALTER TABLE `suppliers` DROP FOREIGN KEY `suppliers_company_id_fk`",
|
53
|
+
remove_foreign_key(:suppliers, :companies)
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_remove_by_name
|
58
|
+
assert_equal(
|
59
|
+
"ALTER TABLE `suppliers` DROP FOREIGN KEY `belongs_to_supplier`",
|
60
|
+
remove_foreign_key(:suppliers, :name => "belongs_to_supplier")
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_remove_by_column
|
65
|
+
assert_equal(
|
66
|
+
"ALTER TABLE `suppliers` DROP FOREIGN KEY `suppliers_ship_to_id_fk`",
|
67
|
+
remove_foreign_key(:suppliers, :column => "ship_to_id")
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
def execute(sql, name = nil)
|
73
|
+
sql
|
74
|
+
end
|
75
|
+
|
76
|
+
def quote_column_name(name)
|
77
|
+
"`#{name}`"
|
78
|
+
end
|
79
|
+
|
80
|
+
def quote_table_name(name)
|
81
|
+
quote_column_name(name).gsub('.', '`.`')
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matthuhiggins-foreigner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Higgins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Foreign keys for Rails migrations
|
17
|
+
email: developer@matthewhiggins.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- Rakefile
|
27
|
+
- README
|
28
|
+
- lib/foreigner.rb
|
29
|
+
- lib/foreigner
|
30
|
+
- lib/foreigner/schema_dumper.rb
|
31
|
+
- lib/foreigner/connection_adapters
|
32
|
+
- lib/foreigner/connection_adapters/mysql_adapter.rb
|
33
|
+
- lib/foreigner/connection_adapters/abstract/schema_definitions.rb
|
34
|
+
- lib/foreigner/connection_adapters/abstract/schema_statements.rb
|
35
|
+
- test/helper.rb
|
36
|
+
- test/mysql_adapter_test.rb
|
37
|
+
has_rdoc: false
|
38
|
+
homepage: http://github.com/matthuhiggins/foreigner/tree/master
|
39
|
+
licenses:
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --line-numbers
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: foreigner
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 1
|
65
|
+
summary: Foreign keys for Rails migrations
|
66
|
+
test_files: []
|
67
|
+
|