foreigner 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -14,63 +14,60 @@ module Foreigner
|
|
14
14
|
module Table
|
15
15
|
def self.included(base)
|
16
16
|
base.class_eval do
|
17
|
-
include InstanceMethods
|
18
17
|
alias_method_chain :references, :foreign_keys
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
21
|
+
# Adds a new foreign key to the table. +to_table+ can be a single Symbol, or
|
22
|
+
# an Array of Symbols. See SchemaStatements#add_foreign_key
|
23
|
+
#
|
24
|
+
# ===== Examples
|
25
|
+
# ====== Creating a simple foreign key
|
26
|
+
# t.foreign_key(:people)
|
27
|
+
# ====== Defining the column
|
28
|
+
# t.foreign_key(:people, :column => :sender_id)
|
29
|
+
# ====== Creating a named foreign key
|
30
|
+
# t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
|
31
|
+
# ====== Defining the column of the +to_table+.
|
32
|
+
# t.foreign_key(:people, :column => :sender_id, :primary_key => :person_id)
|
33
|
+
def foreign_key(to_table, options = {})
|
34
|
+
@base.add_foreign_key(@table_name, to_table, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Remove the given foreign key from the table.
|
38
|
+
#
|
39
|
+
# ===== Examples
|
40
|
+
# ====== Remove the suppliers_company_id_fk in the suppliers table.
|
41
|
+
# t.remove_foreign_key :companies
|
42
|
+
# ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
|
43
|
+
# remove_foreign_key :column => :branch_id
|
44
|
+
# ====== Remove the foreign key named party_foreign_key in the accounts table.
|
45
|
+
# remove_index :name => :party_foreign_key
|
46
|
+
def remove_foreign_key(options = {})
|
47
|
+
@base.remove_foreign_key(@table_name, options)
|
48
|
+
end
|
38
49
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
# You can also specify a hash, which is passed as foreign key options.
|
55
|
-
#
|
56
|
-
# ===== Examples
|
57
|
-
# ====== Add goat_id column and a foreign key to the goats table.
|
58
|
-
# t.references(:goat, :foreign_key => true)
|
59
|
-
# ====== Add goat_id column and a cascading foreign key to the goats table.
|
60
|
-
# t.references(:goat, :foreign_key => {:dependent => :delete})
|
61
|
-
#
|
62
|
-
# Note: No foreign key is created if :polymorphic => true is used.
|
63
|
-
def references_with_foreign_keys(*args)
|
64
|
-
options = args.extract_options!
|
65
|
-
polymorphic = options[:polymorphic]
|
66
|
-
fk_options = options.delete(:foreign_key)
|
50
|
+
# Adds a :foreign_key option to Table.references.
|
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.
|
61
|
+
def references_with_foreign_keys(*args)
|
62
|
+
options = args.extract_options!
|
63
|
+
polymorphic = options[:polymorphic]
|
64
|
+
fk_options = options.delete(:foreign_key)
|
67
65
|
|
68
|
-
|
66
|
+
references_without_foreign_keys(*(args.dup << options))
|
69
67
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
end
|
68
|
+
if fk_options && !polymorphic
|
69
|
+
fk_options = {} if fk_options == true
|
70
|
+
args.each { |to_table| foreign_key(to_table, fk_options) }
|
74
71
|
end
|
75
72
|
end
|
76
73
|
end
|
@@ -2,44 +2,41 @@ module Foreigner
|
|
2
2
|
module SchemaDumper
|
3
3
|
def self.included(base)
|
4
4
|
base.class_eval do
|
5
|
-
include InstanceMethods
|
6
5
|
alias_method_chain :tables, :foreign_keys
|
7
6
|
end
|
8
7
|
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
foreign_keys(table, stream)
|
15
|
-
end
|
9
|
+
def tables_with_foreign_keys(stream)
|
10
|
+
tables_without_foreign_keys(stream)
|
11
|
+
@connection.tables.sort.each do |table|
|
12
|
+
foreign_keys(table, stream)
|
16
13
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
' ' + statement_parts.join(', ')
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def foreign_keys(table_name, stream)
|
18
|
+
if (foreign_keys = @connection.foreign_keys(table_name)).any?
|
19
|
+
add_foreign_key_statements = foreign_keys.map do |foreign_key|
|
20
|
+
statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
|
21
|
+
statement_parts << foreign_key.to_table.inspect
|
22
|
+
statement_parts << (':name => ' + foreign_key.options[:name].inspect)
|
23
|
+
|
24
|
+
if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
|
25
|
+
statement_parts << (':column => ' + foreign_key.options[:column].inspect)
|
26
|
+
end
|
27
|
+
if foreign_key.options[:primary_key] != 'id'
|
28
|
+
statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
|
29
|
+
end
|
30
|
+
if foreign_key.options[:dependent].present?
|
31
|
+
statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
|
37
32
|
end
|
38
33
|
|
39
|
-
|
40
|
-
stream.puts
|
34
|
+
' ' + statement_parts.join(', ')
|
41
35
|
end
|
36
|
+
|
37
|
+
stream.puts add_foreign_key_statements.sort.join("\n")
|
38
|
+
stream.puts
|
42
39
|
end
|
43
|
-
|
40
|
+
end
|
44
41
|
end
|
45
42
|
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreigner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 9
|
8
|
-
- 1
|
9
|
-
version: 0.9.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.9.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Matthew Higgins
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-04-12 00:00:00 -07:00
|
18
14
|
default_executable:
|
19
15
|
dependencies: []
|
20
16
|
|
@@ -53,25 +49,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
49
|
requirements:
|
54
50
|
- - ">="
|
55
51
|
- !ruby/object:Gem::Version
|
56
|
-
segments:
|
57
|
-
- 1
|
58
|
-
- 8
|
59
|
-
- 6
|
60
52
|
version: 1.8.6
|
61
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
54
|
none: false
|
63
55
|
requirements:
|
64
56
|
- - ">="
|
65
57
|
- !ruby/object:Gem::Version
|
66
|
-
segments:
|
67
|
-
- 1
|
68
|
-
- 3
|
69
|
-
- 5
|
70
58
|
version: 1.3.5
|
71
59
|
requirements: []
|
72
60
|
|
73
61
|
rubyforge_project: foreigner
|
74
|
-
rubygems_version: 1.
|
62
|
+
rubygems_version: 1.6.1
|
75
63
|
signing_key:
|
76
64
|
specification_version: 3
|
77
65
|
summary: Foreign keys for Rails
|