automatic_foreign_key 1.1.0 → 1.1.5
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 +5 -3
- data/VERSION +1 -1
- data/automatic_foreign_key.gemspec +6 -5
- data/lib/automatic_foreign_key/active_record/connection_adapters/schema_statements.rb +22 -0
- data/lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb +13 -1
- data/lib/automatic_foreign_key/active_record/migration.rb +2 -12
- data/lib/automatic_foreign_key.rb +10 -0
- data/spec/connections/mysql/connection.rb +0 -2
- data/spec/connections/postgresql/connection.rb +0 -2
- data/spec/migration_spec.rb +35 -4
- metadata +9 -4
data/README.rdoc
CHANGED
@@ -77,7 +77,7 @@ configuration properties:
|
|
77
77
|
* <code>config.active_record.table_name_prefix</code>
|
78
78
|
* <code>config.active_record.table_name_suffix</code>
|
79
79
|
|
80
|
-
=== Auto Indices
|
80
|
+
=== Auto Indices
|
81
81
|
|
82
82
|
It's very common to create an index on foreign key. You can instruct
|
83
83
|
AutomaticForeignKey to add an index after adding foreign key.
|
@@ -94,10 +94,12 @@ If you want to pass some options for index use hash params.
|
|
94
94
|
t.integer :order_id, :index => { :unique => true, :name => 'foo_index' }
|
95
95
|
end
|
96
96
|
|
97
|
-
|
97
|
+
==== NOTE
|
98
|
+
|
99
|
+
Auto indexing option is useless for MySQL users as their RDBMS adds indices on foreign
|
98
100
|
keys by default. However PostgreSQL users may have fun with that feature.
|
99
101
|
|
100
|
-
=== Configuration
|
102
|
+
=== Configuration
|
101
103
|
|
102
104
|
For customization purposes create config/initializers/automatic_foreign_key.rb file:
|
103
105
|
AutomaticForeignKey.auto_index = true # create indices on FKs by default
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.5
|
@@ -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.5"
|
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-
|
11
|
+
s.authors = ["Michał Łomnicki"]
|
12
|
+
s.date = %q{2010-09-07}
|
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}
|
@@ -29,6 +29,7 @@ constraints when creating tables or adding columns. It uses SQL-92 syntax and as
|
|
29
29
|
"install.rb",
|
30
30
|
"lib/automatic_foreign_key.rb",
|
31
31
|
"lib/automatic_foreign_key/active_record/base.rb",
|
32
|
+
"lib/automatic_foreign_key/active_record/connection_adapters/schema_statements.rb",
|
32
33
|
"lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb",
|
33
34
|
"lib/automatic_foreign_key/active_record/migration.rb",
|
34
35
|
"lib/generators/automatic_foreign_key/migration_generator.rb",
|
@@ -50,7 +51,7 @@ constraints when creating tables or adding columns. It uses SQL-92 syntax and as
|
|
50
51
|
s.homepage = %q{http://github.com/mlomnicki/automatic_foreign_key}
|
51
52
|
s.rdoc_options = ["--charset=UTF-8"]
|
52
53
|
s.require_paths = ["lib"]
|
53
|
-
s.rubygems_version = %q{1.3.
|
54
|
+
s.rubygems_version = %q{1.3.7}
|
54
55
|
s.summary = %q{Automatically generate foreign-key constraints when creating tables}
|
55
56
|
s.test_files = [
|
56
57
|
"spec/schema/schema.rb",
|
@@ -72,7 +73,7 @@ constraints when creating tables or adding columns. It uses SQL-92 syntax and as
|
|
72
73
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
73
74
|
s.specification_version = 3
|
74
75
|
|
75
|
-
if Gem::Version.new(Gem::
|
76
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
76
77
|
s.add_runtime_dependency(%q<redhillonrails_core>, [">= 1.0.4.1"])
|
77
78
|
s.add_runtime_dependency(%q<activerecord>, [">= 2.2"])
|
78
79
|
else
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module AutomaticForeignKey::ActiveRecord::ConnectionAdapters
|
2
|
+
module SchemaStatements
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :create_table, :automatic_foreign_key
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_table_with_automatic_foreign_key(table, options = {})
|
11
|
+
indices = nil
|
12
|
+
create_table_without_automatic_foreign_key(table, options) do |table_definition|
|
13
|
+
yield table_definition if block_given?
|
14
|
+
indices = table_definition.indices
|
15
|
+
end
|
16
|
+
indices.each do |column_name, index_options|
|
17
|
+
add_index(table, column_name, index_options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -1,7 +1,11 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext/module/attr_accessor_with_default'
|
3
|
+
|
1
4
|
module AutomaticForeignKey::ActiveRecord::ConnectionAdapters
|
2
5
|
module TableDefinition
|
3
6
|
def self.included(base)
|
4
7
|
base.class_eval do
|
8
|
+
attr_accessor_with_default :indices, Array.new
|
5
9
|
alias_method_chain :column, :automatic_foreign_key
|
6
10
|
alias_method_chain :primary_key, :automatic_foreign_key
|
7
11
|
end
|
@@ -14,7 +18,15 @@ module AutomaticForeignKey::ActiveRecord::ConnectionAdapters
|
|
14
18
|
def column_with_automatic_foreign_key(name, type, options = {})
|
15
19
|
column_without_automatic_foreign_key(name, type, options)
|
16
20
|
references = ActiveRecord::Base.references(self.name, name, options)
|
17
|
-
|
21
|
+
if references
|
22
|
+
AutomaticForeignKey.set_default_update_and_delete_actions!(options)
|
23
|
+
foreign_key(name, references.first, references.last, options)
|
24
|
+
end
|
25
|
+
index = options.fetch(:index, AutomaticForeignKey.auto_index)
|
26
|
+
if index
|
27
|
+
# append [column_name, index_options] pair
|
28
|
+
self.indices << [name, AutomaticForeignKey.options_for_index(index)]
|
29
|
+
end
|
18
30
|
self
|
19
31
|
end
|
20
32
|
|
@@ -33,20 +33,10 @@ module AutomaticForeignKey::ActiveRecord
|
|
33
33
|
index = options.fetch(:index, AutomaticForeignKey.auto_index)
|
34
34
|
references = ActiveRecord::Base.references(table_name, column_name, options)
|
35
35
|
if references
|
36
|
-
set_default_update_and_delete_actions!(options)
|
36
|
+
AutomaticForeignKey.set_default_update_and_delete_actions!(options)
|
37
37
|
add_foreign_key(table_name, column_name, references.first, references.last, options)
|
38
38
|
end
|
39
|
-
add_index(table_name, column_name, options_for_index(index)) if index
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
def options_for_index(index)
|
44
|
-
index.is_a?(Hash) ? index : {}
|
45
|
-
end
|
46
|
-
|
47
|
-
def set_default_update_and_delete_actions!(options)
|
48
|
-
options[:on_update] = options.fetch(:on_update, AutomaticForeignKey.on_update)
|
49
|
-
options[:on_delete] = options.fetch(:on_delete, AutomaticForeignKey.on_delete)
|
39
|
+
add_index(table_name, column_name, AutomaticForeignKey.options_for_index(index)) if index
|
50
40
|
end
|
51
41
|
|
52
42
|
end
|
@@ -13,6 +13,7 @@ module AutomaticForeignKey
|
|
13
13
|
|
14
14
|
module ConnectionAdapters
|
15
15
|
autoload :TableDefinition, 'automatic_foreign_key/active_record/connection_adapters/table_definition'
|
16
|
+
autoload :SchemaStatements, 'automatic_foreign_key/active_record/connection_adapters/schema_statements'
|
16
17
|
end
|
17
18
|
|
18
19
|
end
|
@@ -27,9 +28,18 @@ module AutomaticForeignKey
|
|
27
28
|
mattr_accessor :auto_index
|
28
29
|
@@auto_index = nil
|
29
30
|
|
31
|
+
def self.options_for_index(index)
|
32
|
+
index.is_a?(Hash) ? index : {}
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.set_default_update_and_delete_actions!(options)
|
36
|
+
options[:on_update] = options.fetch(:on_update, AutomaticForeignKey.on_update)
|
37
|
+
options[:on_delete] = options.fetch(:on_delete, AutomaticForeignKey.on_delete)
|
38
|
+
end
|
30
39
|
|
31
40
|
end
|
32
41
|
|
33
42
|
ActiveRecord::Base.send(:include, AutomaticForeignKey::ActiveRecord::Base)
|
34
43
|
ActiveRecord::Migration.send(:include, AutomaticForeignKey::ActiveRecord::Migration)
|
35
44
|
ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, AutomaticForeignKey::ActiveRecord::ConnectionAdapters::TableDefinition)
|
45
|
+
ActiveRecord::ConnectionAdapters::SchemaStatements.send(:include, AutomaticForeignKey::ActiveRecord::ConnectionAdapters::SchemaStatements)
|
data/spec/migration_spec.rb
CHANGED
@@ -15,13 +15,44 @@ end
|
|
15
15
|
describe ActiveRecord::Migration do
|
16
16
|
|
17
17
|
context "when table is created" do
|
18
|
+
before(:all) do
|
19
|
+
@model = Post
|
20
|
+
end
|
21
|
+
|
22
|
+
|
18
23
|
it "should create foreign keys" do
|
19
|
-
create_table(
|
24
|
+
create_table(@model, :user_id => {},
|
20
25
|
:author_id => { :references => :users },
|
21
26
|
:member_id => { :references => nil } )
|
22
|
-
|
23
|
-
|
24
|
-
|
27
|
+
@model.should reference(:users, :id).on(:user_id)
|
28
|
+
@model.should reference(:users, :id).on(:author_id)
|
29
|
+
@model.should_not reference.on(:member_id)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should use default on_cascade action" do
|
33
|
+
AutomaticForeignKey.on_update = :cascade
|
34
|
+
create_table(@model, :user_id => {})
|
35
|
+
AutomaticForeignKey.on_update = nil
|
36
|
+
@model.should reference.on(:user_id).on_update(:cascade)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should use default on_cascade action" do
|
40
|
+
AutomaticForeignKey.on_delete = :cascade
|
41
|
+
create_table(@model, :user_id => {})
|
42
|
+
AutomaticForeignKey.on_delete = nil
|
43
|
+
@model.should reference.on(:user_id).on_delete(:cascade)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should create an index if specified" do
|
47
|
+
create_table(@model, :state => { :index => true })
|
48
|
+
@model.should have_index.on(:state)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should auto-create index if specified in global options" do
|
52
|
+
AutomaticForeignKey.auto_index = nil
|
53
|
+
create_table(@model, :state => {})
|
54
|
+
@model.should have_index.on(:state)
|
55
|
+
AutomaticForeignKey.auto_index = false
|
25
56
|
end
|
26
57
|
|
27
58
|
end
|
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
|
+
- 5
|
9
|
+
version: 1.1.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Micha\xC5\x82 \xC5\x81omnicki"
|
@@ -14,13 +14,14 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-09-07 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
|
24
25
|
requirements:
|
25
26
|
- - ">="
|
26
27
|
- !ruby/object:Gem::Version
|
@@ -36,6 +37,7 @@ dependencies:
|
|
36
37
|
name: activerecord
|
37
38
|
prerelease: false
|
38
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
39
41
|
requirements:
|
40
42
|
- - ">="
|
41
43
|
- !ruby/object:Gem::Version
|
@@ -68,6 +70,7 @@ files:
|
|
68
70
|
- install.rb
|
69
71
|
- lib/automatic_foreign_key.rb
|
70
72
|
- lib/automatic_foreign_key/active_record/base.rb
|
73
|
+
- lib/automatic_foreign_key/active_record/connection_adapters/schema_statements.rb
|
71
74
|
- lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb
|
72
75
|
- lib/automatic_foreign_key/active_record/migration.rb
|
73
76
|
- lib/generators/automatic_foreign_key/migration_generator.rb
|
@@ -95,6 +98,7 @@ rdoc_options:
|
|
95
98
|
require_paths:
|
96
99
|
- lib
|
97
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
98
102
|
requirements:
|
99
103
|
- - ">="
|
100
104
|
- !ruby/object:Gem::Version
|
@@ -102,6 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
106
|
- 0
|
103
107
|
version: "0"
|
104
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
105
110
|
requirements:
|
106
111
|
- - ">="
|
107
112
|
- !ruby/object:Gem::Version
|
@@ -111,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
116
|
requirements: []
|
112
117
|
|
113
118
|
rubyforge_project:
|
114
|
-
rubygems_version: 1.3.
|
119
|
+
rubygems_version: 1.3.7
|
115
120
|
signing_key:
|
116
121
|
specification_version: 3
|
117
122
|
summary: Automatically generate foreign-key constraints when creating tables
|