automatic_foreign_key 1.1.5 → 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -102,9 +102,9 @@ keys by default. However PostgreSQL users may have fun with that feature.
|
|
102
102
|
=== Configuration
|
103
103
|
|
104
104
|
For customization purposes create config/initializers/automatic_foreign_key.rb file:
|
105
|
-
AutomaticForeignKey.auto_index = true # create indices on FKs by default
|
106
|
-
AutomaticForeignKey.on_update = :cascade # cascade as default on_update action
|
107
|
-
AutomaticForeignKey.on_delete = :
|
105
|
+
AutomaticForeignKey.auto_index = true # create indices on FKs by default
|
106
|
+
AutomaticForeignKey.on_update = :cascade # cascade as default on_update action
|
107
|
+
AutomaticForeignKey.on_delete = :restrict # restrict as default on_delete action
|
108
108
|
|
109
109
|
=== Rails 3 compatibility
|
110
110
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.6
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{automatic_foreign_key}
|
8
|
-
s.version = "1.1.
|
8
|
+
s.version = "1.1.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["
|
12
|
-
s.date = %q{2010-09-
|
11
|
+
s.authors = ["Micha\305\202 \305\201omnicki"]
|
12
|
+
s.date = %q{2010-09-13}
|
13
13
|
s.description = %q{Automatic Foreign Key automatically generates foreign-key
|
14
14
|
constraints when creating tables or adding columns. It uses SQL-92 syntax and as such should be compatible with most databases that support foreign-key constraints.}
|
15
15
|
s.email = %q{michal.lomnicki@gmail.com}
|
@@ -51,7 +51,7 @@ constraints when creating tables or adding columns. It uses SQL-92 syntax and as
|
|
51
51
|
s.homepage = %q{http://github.com/mlomnicki/automatic_foreign_key}
|
52
52
|
s.rdoc_options = ["--charset=UTF-8"]
|
53
53
|
s.require_paths = ["lib"]
|
54
|
-
s.rubygems_version = %q{1.3.
|
54
|
+
s.rubygems_version = %q{1.3.6}
|
55
55
|
s.summary = %q{Automatically generate foreign-key constraints when creating tables}
|
56
56
|
s.test_files = [
|
57
57
|
"spec/schema/schema.rb",
|
@@ -73,7 +73,7 @@ constraints when creating tables or adding columns. It uses SQL-92 syntax and as
|
|
73
73
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
74
74
|
s.specification_version = 3
|
75
75
|
|
76
|
-
if Gem::Version.new(Gem::
|
76
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
77
77
|
s.add_runtime_dependency(%q<redhillonrails_core>, [">= 1.0.4.1"])
|
78
78
|
s.add_runtime_dependency(%q<activerecord>, [">= 2.2"])
|
79
79
|
else
|
@@ -5,27 +5,31 @@ module AutomaticForeignKey::ActiveRecord::ConnectionAdapters
|
|
5
5
|
module TableDefinition
|
6
6
|
def self.included(base)
|
7
7
|
base.class_eval do
|
8
|
-
attr_accessor_with_default :indices, Array.new
|
9
8
|
alias_method_chain :column, :automatic_foreign_key
|
10
9
|
alias_method_chain :primary_key, :automatic_foreign_key
|
11
10
|
end
|
12
11
|
end
|
13
|
-
|
12
|
+
|
14
13
|
def primary_key_with_automatic_foreign_key(name, options = {})
|
15
14
|
column(name, :primary_key, options)
|
16
15
|
end
|
17
16
|
|
17
|
+
def indices
|
18
|
+
@indices ||= []
|
19
|
+
end
|
20
|
+
|
18
21
|
def column_with_automatic_foreign_key(name, type, options = {})
|
19
22
|
column_without_automatic_foreign_key(name, type, options)
|
20
23
|
references = ActiveRecord::Base.references(self.name, name, options)
|
21
24
|
if references
|
22
25
|
AutomaticForeignKey.set_default_update_and_delete_actions!(options)
|
23
26
|
foreign_key(name, references.first, references.last, options)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
if index = options.fetch(:index, AutomaticForeignKey.auto_index)
|
28
|
+
# append [column_name, index_options] pair
|
29
|
+
self.indices << [name, AutomaticForeignKey.options_for_index(index)]
|
30
|
+
end
|
31
|
+
elsif options[:index]
|
32
|
+
self.indices << [name, AutomaticForeignKey.options_for_index(options[:index])]
|
29
33
|
end
|
30
34
|
self
|
31
35
|
end
|
@@ -30,13 +30,16 @@ module AutomaticForeignKey::ActiveRecord
|
|
30
30
|
#
|
31
31
|
def add_column(table_name, column_name, type, options = {})
|
32
32
|
super
|
33
|
-
index = options.fetch(:index, AutomaticForeignKey.auto_index)
|
34
33
|
references = ActiveRecord::Base.references(table_name, column_name, options)
|
35
34
|
if references
|
36
35
|
AutomaticForeignKey.set_default_update_and_delete_actions!(options)
|
37
36
|
add_foreign_key(table_name, column_name, references.first, references.last, options)
|
37
|
+
if index = options.fetch(:index, AutomaticForeignKey.auto_index)
|
38
|
+
add_index(table_name, column_name, AutomaticForeignKey.options_for_index(index))
|
39
|
+
end
|
40
|
+
elsif options[:index]
|
41
|
+
add_index(table_name, column_name, AutomaticForeignKey.options_for_index(options[:index]))
|
38
42
|
end
|
39
|
-
add_index(table_name, column_name, AutomaticForeignKey.options_for_index(index)) if index
|
40
43
|
end
|
41
44
|
|
42
45
|
end
|
data/spec/migration_spec.rb
CHANGED
@@ -48,11 +48,15 @@ describe ActiveRecord::Migration do
|
|
48
48
|
@model.should have_index.on(:state)
|
49
49
|
end
|
50
50
|
|
51
|
-
it "should auto-
|
51
|
+
it "should auto-index foreign keys only" do
|
52
|
+
AutomaticForeignKey.auto_index = true
|
53
|
+
create_table(@model, :user_id => {},
|
54
|
+
:application_id => { :references => nil },
|
55
|
+
:state => {})
|
56
|
+
@model.should have_index.on(:user_id)
|
57
|
+
@model.should_not have_index.on(:application_id)
|
58
|
+
@model.should_not have_index.on(:state)
|
52
59
|
AutomaticForeignKey.auto_index = nil
|
53
|
-
create_table(@model, :state => {})
|
54
|
-
@model.should have_index.on(:state)
|
55
|
-
AutomaticForeignKey.auto_index = false
|
56
60
|
end
|
57
61
|
|
58
62
|
end
|
@@ -118,7 +122,7 @@ describe ActiveRecord::Migration do
|
|
118
122
|
end
|
119
123
|
end
|
120
124
|
|
121
|
-
it "should auto-
|
125
|
+
it "should auto-index if specified in global options" do
|
122
126
|
AutomaticForeignKey.auto_index = true
|
123
127
|
add_column(:post_id, :integer) do
|
124
128
|
@model.should have_index.on(:post_id)
|
@@ -126,6 +130,14 @@ describe ActiveRecord::Migration do
|
|
126
130
|
AutomaticForeignKey.auto_index = false
|
127
131
|
end
|
128
132
|
|
133
|
+
it "should auto-index foreign keys only" do
|
134
|
+
AutomaticForeignKey.auto_index = true
|
135
|
+
add_column(:state, :integer) do
|
136
|
+
@model.should_not have_index.on(:state)
|
137
|
+
end
|
138
|
+
AutomaticForeignKey.auto_index = false
|
139
|
+
end
|
140
|
+
|
129
141
|
it "should allow to overwrite auto_index options in column definition" do
|
130
142
|
AutomaticForeignKey.auto_index = true
|
131
143
|
add_column(:post_id, :integer, :index => false) do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 6
|
9
|
+
version: 1.1.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Micha\xC5\x82 \xC5\x81omnicki"
|
@@ -14,14 +14,13 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-13 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: redhillonrails_core
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
24
|
requirements:
|
26
25
|
- - ">="
|
27
26
|
- !ruby/object:Gem::Version
|
@@ -37,7 +36,6 @@ dependencies:
|
|
37
36
|
name: activerecord
|
38
37
|
prerelease: false
|
39
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
39
|
requirements:
|
42
40
|
- - ">="
|
43
41
|
- !ruby/object:Gem::Version
|
@@ -98,7 +96,6 @@ rdoc_options:
|
|
98
96
|
require_paths:
|
99
97
|
- lib
|
100
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
99
|
requirements:
|
103
100
|
- - ">="
|
104
101
|
- !ruby/object:Gem::Version
|
@@ -106,7 +103,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
103
|
- 0
|
107
104
|
version: "0"
|
108
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
-
none: false
|
110
106
|
requirements:
|
111
107
|
- - ">="
|
112
108
|
- !ruby/object:Gem::Version
|
@@ -116,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
112
|
requirements: []
|
117
113
|
|
118
114
|
rubyforge_project:
|
119
|
-
rubygems_version: 1.3.
|
115
|
+
rubygems_version: 1.3.6
|
120
116
|
signing_key:
|
121
117
|
specification_version: 3
|
122
118
|
summary: Automatically generate foreign-key constraints when creating tables
|