schema_plus 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/README.rdoc +11 -8
- data/gemfiles/Gemfile.rails-2.3.lock +65 -0
- data/gemfiles/Gemfile.rails-3.0.lock +111 -0
- data/gemfiles/Gemfile.rails-3.1 +1 -1
- data/gemfiles/Gemfile.rails-3.1.lock +123 -0
- data/lib/schema_plus.rb +1 -2
- data/lib/schema_plus/active_record/base.rb +3 -10
- data/lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb +11 -1
- data/lib/schema_plus/active_record/connection_adapters/column.rb +3 -2
- data/lib/schema_plus/active_record/connection_adapters/table_definition.rb +1 -1
- data/lib/schema_plus/active_record/foreign_keys.rb +177 -0
- data/lib/schema_plus/version.rb +1 -1
- data/runspecs +35 -16
- data/spec/migration_spec.rb +10 -8
- data/spec/rails3_migration_spec.rb +138 -0
- data/spec/references_spec.rb +1 -1
- data/spec/support/helpers.rb +4 -0
- metadata +102 -129
- data/lib/schema_plus/active_record/migration.rb +0 -220
data/spec/references_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
4
4
|
describe 'get_references method' do
|
5
5
|
|
6
6
|
before(:all) do
|
7
|
-
@target = ActiveRecord::Migration
|
7
|
+
@target = ActiveRecord::Migration.connection
|
8
8
|
@table_name = 'comments'
|
9
9
|
@column_name = 'post_id'
|
10
10
|
@destination_table = 'posts'
|
data/spec/support/helpers.rb
CHANGED
metadata
CHANGED
@@ -1,129 +1,126 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_plus
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
4
5
|
prerelease:
|
5
|
-
version: 0.1.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Ronen Barzel
|
9
|
-
-
|
9
|
+
- Michał Łomnicki
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
dependencies:
|
17
|
-
- !ruby/object:Gem::Dependency
|
13
|
+
date: 2011-09-28 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
18
16
|
name: rails
|
19
|
-
|
20
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &69439930 !ruby/object:Gem::Requirement
|
21
18
|
none: false
|
22
|
-
requirements:
|
23
|
-
- -
|
24
|
-
- !ruby/object:Gem::Version
|
25
|
-
version:
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
26
23
|
type: :runtime
|
27
|
-
version_requirements: *id001
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: valuable
|
30
24
|
prerelease: false
|
31
|
-
|
25
|
+
version_requirements: *69439930
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: valuable
|
28
|
+
requirement: &69439700 !ruby/object:Gem::Requirement
|
32
29
|
none: false
|
33
|
-
requirements:
|
34
|
-
- -
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version:
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
37
34
|
type: :runtime
|
38
|
-
version_requirements: *id002
|
39
|
-
- !ruby/object:Gem::Dependency
|
40
|
-
name: rake
|
41
35
|
prerelease: false
|
42
|
-
|
36
|
+
version_requirements: *69439700
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
requirement: &69439390 !ruby/object:Gem::Requirement
|
43
40
|
none: false
|
44
|
-
requirements:
|
41
|
+
requirements:
|
45
42
|
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
43
|
+
- !ruby/object:Gem::Version
|
47
44
|
version: 0.8.7
|
48
45
|
type: :development
|
49
|
-
version_requirements: *id003
|
50
|
-
- !ruby/object:Gem::Dependency
|
51
|
-
name: rspec
|
52
46
|
prerelease: false
|
53
|
-
|
47
|
+
version_requirements: *69439390
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
requirement: &69439120 !ruby/object:Gem::Requirement
|
54
51
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
version:
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
59
56
|
type: :development
|
60
|
-
version_requirements: *id004
|
61
|
-
- !ruby/object:Gem::Dependency
|
62
|
-
name: pg
|
63
57
|
prerelease: false
|
64
|
-
|
58
|
+
version_requirements: *69439120
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: pg
|
61
|
+
requirement: &69438750 !ruby/object:Gem::Requirement
|
65
62
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version:
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
70
67
|
type: :development
|
71
|
-
version_requirements: *id005
|
72
|
-
- !ruby/object:Gem::Dependency
|
73
|
-
name: mysql
|
74
68
|
prerelease: false
|
75
|
-
|
69
|
+
version_requirements: *69438750
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: mysql
|
72
|
+
requirement: &69438370 !ruby/object:Gem::Requirement
|
76
73
|
none: false
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version:
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
81
78
|
type: :development
|
82
|
-
version_requirements: *id006
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: sqlite3
|
85
79
|
prerelease: false
|
86
|
-
|
80
|
+
version_requirements: *69438370
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: sqlite3
|
83
|
+
requirement: &69437940 !ruby/object:Gem::Requirement
|
87
84
|
none: false
|
88
|
-
requirements:
|
89
|
-
- -
|
90
|
-
- !ruby/object:Gem::Version
|
91
|
-
version:
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
92
89
|
type: :development
|
93
|
-
version_requirements: *id007
|
94
|
-
- !ruby/object:Gem::Dependency
|
95
|
-
name: simplecov
|
96
90
|
prerelease: false
|
97
|
-
|
91
|
+
version_requirements: *69437940
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
name: simplecov
|
94
|
+
requirement: &69437700 !ruby/object:Gem::Requirement
|
98
95
|
none: false
|
99
|
-
requirements:
|
100
|
-
- -
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version:
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
103
100
|
type: :development
|
104
|
-
version_requirements: *id008
|
105
|
-
- !ruby/object:Gem::Dependency
|
106
|
-
name: simplecov-gem-adapter
|
107
101
|
prerelease: false
|
108
|
-
|
102
|
+
version_requirements: *69437700
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: simplecov-gem-adapter
|
105
|
+
requirement: &69437420 !ruby/object:Gem::Requirement
|
109
106
|
none: false
|
110
|
-
requirements:
|
111
|
-
- -
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
version:
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
114
111
|
type: :development
|
115
|
-
|
116
|
-
|
117
|
-
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: *69437420
|
114
|
+
description: ! 'SchemaPlus is an ActiveRecord extension that provides enhanced capabilities
|
115
|
+
for schema definition and querying, including: enhanced and more DRY index capabilities,
|
116
|
+
support and automation for foreign key constraints, and support for views.'
|
117
|
+
email:
|
118
118
|
- ronen@barzel.org
|
119
119
|
- michal.lomnicki@gmail.com
|
120
120
|
executables: []
|
121
|
-
|
122
121
|
extensions: []
|
123
|
-
|
124
122
|
extra_rdoc_files: []
|
125
|
-
|
126
|
-
files:
|
123
|
+
files:
|
127
124
|
- .gitignore
|
128
125
|
- .travis.yml
|
129
126
|
- Gemfile
|
@@ -131,8 +128,11 @@ files:
|
|
131
128
|
- README.rdoc
|
132
129
|
- Rakefile
|
133
130
|
- gemfiles/Gemfile.rails-2.3
|
131
|
+
- gemfiles/Gemfile.rails-2.3.lock
|
134
132
|
- gemfiles/Gemfile.rails-3.0
|
133
|
+
- gemfiles/Gemfile.rails-3.0.lock
|
135
134
|
- gemfiles/Gemfile.rails-3.1
|
135
|
+
- gemfiles/Gemfile.rails-3.1.lock
|
136
136
|
- init.rb
|
137
137
|
- lib/schema_plus.rb
|
138
138
|
- lib/schema_plus/active_record/base.rb
|
@@ -145,7 +145,7 @@ files:
|
|
145
145
|
- lib/schema_plus/active_record/connection_adapters/schema_statements.rb
|
146
146
|
- lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb
|
147
147
|
- lib/schema_plus/active_record/connection_adapters/table_definition.rb
|
148
|
-
- lib/schema_plus/active_record/
|
148
|
+
- lib/schema_plus/active_record/foreign_keys.rb
|
149
149
|
- lib/schema_plus/active_record/schema.rb
|
150
150
|
- lib/schema_plus/active_record/schema_dumper.rb
|
151
151
|
- lib/schema_plus/railtie.rb
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- spec/models/comment.rb
|
166
166
|
- spec/models/post.rb
|
167
167
|
- spec/models/user.rb
|
168
|
+
- spec/rails3_migration_spec.rb
|
168
169
|
- spec/references_spec.rb
|
169
170
|
- spec/schema/auto_schema.rb
|
170
171
|
- spec/schema/core_schema.rb
|
@@ -177,57 +178,29 @@ files:
|
|
177
178
|
- spec/support/matchers/reference.rb
|
178
179
|
- spec/support/reference.rb
|
179
180
|
- spec/views_spec.rb
|
180
|
-
has_rdoc: true
|
181
181
|
homepage: https://github.com/lomba/schema_plus
|
182
182
|
licenses: []
|
183
|
-
|
184
183
|
post_install_message:
|
185
184
|
rdoc_options: []
|
186
|
-
|
187
|
-
require_paths:
|
185
|
+
require_paths:
|
188
186
|
- lib
|
189
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
187
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
190
188
|
none: false
|
191
|
-
requirements:
|
192
|
-
- -
|
193
|
-
- !ruby/object:Gem::Version
|
194
|
-
version:
|
195
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - ! '>='
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
194
|
none: false
|
197
|
-
requirements:
|
198
|
-
- -
|
199
|
-
- !ruby/object:Gem::Version
|
200
|
-
version:
|
195
|
+
requirements:
|
196
|
+
- - ! '>='
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: '0'
|
201
199
|
requirements: []
|
202
|
-
|
203
200
|
rubyforge_project: schema_plus
|
204
|
-
rubygems_version: 1.
|
201
|
+
rubygems_version: 1.8.10
|
205
202
|
signing_key:
|
206
203
|
specification_version: 3
|
207
|
-
summary: Enhances ActiveRecord schema mechanism, including more DRY index creation
|
208
|
-
|
209
|
-
|
210
|
-
- spec/connections/mysql/connection.rb
|
211
|
-
- spec/connections/mysql2/connection.rb
|
212
|
-
- spec/connections/postgresql/connection.rb
|
213
|
-
- spec/connections/sqlite3/connection.rb
|
214
|
-
- spec/foreign_key_definition_spec.rb
|
215
|
-
- spec/foreign_key_spec.rb
|
216
|
-
- spec/index_definition_spec.rb
|
217
|
-
- spec/index_spec.rb
|
218
|
-
- spec/migration_spec.rb
|
219
|
-
- spec/models/comment.rb
|
220
|
-
- spec/models/post.rb
|
221
|
-
- spec/models/user.rb
|
222
|
-
- spec/references_spec.rb
|
223
|
-
- spec/schema/auto_schema.rb
|
224
|
-
- spec/schema/core_schema.rb
|
225
|
-
- spec/schema_dumper_spec.rb
|
226
|
-
- spec/schema_spec.rb
|
227
|
-
- spec/spec_helper.rb
|
228
|
-
- spec/support/helpers.rb
|
229
|
-
- spec/support/matchers/automatic_foreign_key_matchers.rb
|
230
|
-
- spec/support/matchers/have_index.rb
|
231
|
-
- spec/support/matchers/reference.rb
|
232
|
-
- spec/support/reference.rb
|
233
|
-
- spec/views_spec.rb
|
204
|
+
summary: Enhances ActiveRecord schema mechanism, including more DRY index creation
|
205
|
+
and support for foreign key constraints and views.
|
206
|
+
test_files: []
|
@@ -1,220 +0,0 @@
|
|
1
|
-
module SchemaPlus::ActiveRecord
|
2
|
-
# SchemaPlus extends ActiveRecord::Migration with several enhancements. See documentation at Migration::ClassMethods
|
3
|
-
module Migration
|
4
|
-
def self.included(base) #:nodoc:
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
end
|
7
|
-
|
8
|
-
#
|
9
|
-
# SchemaPlus extends ActiveRecord::Migration with the following enhancements.
|
10
|
-
#
|
11
|
-
module ClassMethods
|
12
|
-
|
13
|
-
# Create a new view, given its name and SQL definition
|
14
|
-
#
|
15
|
-
def create_view(view_name, definition)
|
16
|
-
connection.create_view(view_name, definition)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Drop the named view
|
20
|
-
def drop_view(view_name)
|
21
|
-
connection.drop_view(view_name)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Define a foreign key constraint. Valid options are :on_update,
|
25
|
-
# :on_delete, and :deferrable, with values as described at
|
26
|
-
# ConnectionAdapters::ForeignKeyDefinition
|
27
|
-
#
|
28
|
-
# (NOTE: Sqlite3 does not support altering a table to add foreign-key
|
29
|
-
# constraints; they must be included in the table specification when
|
30
|
-
# it's created. If you're using Sqlite3, this method will raise an
|
31
|
-
# error.)
|
32
|
-
def add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {})
|
33
|
-
connection.add_foreign_key(table_name, column_names, references_table_name, references_column_names, options)
|
34
|
-
end
|
35
|
-
|
36
|
-
# Remove a foreign key constraint
|
37
|
-
#
|
38
|
-
# (NOTE: Sqlite3 does not support altering a table to remove
|
39
|
-
# foreign-key constraints. If you're using Sqlite3, this method will
|
40
|
-
# raise an error.)
|
41
|
-
def remove_foreign_key(table_name, foreign_key_name)
|
42
|
-
connection.remove_foreign_key(table_name, foreign_key_name)
|
43
|
-
end
|
44
|
-
|
45
|
-
# Enhances ActiveRecord::Migration#add_column to support indexes and foreign keys, with automatic creation
|
46
|
-
#
|
47
|
-
# == Indexes
|
48
|
-
#
|
49
|
-
# The <tt>:index</tt> option takes a hash of parameters to pass to ActiveRecord::Migration.add_index. Thus
|
50
|
-
#
|
51
|
-
# add_column('books', 'isbn', :string, :index => {:name => "ISBN-index", :unique => true })
|
52
|
-
#
|
53
|
-
# is equivalent to:
|
54
|
-
#
|
55
|
-
# add_column('books', 'isbn', :string)
|
56
|
-
# add_index('books', ['isbn'], :name => "ISBN-index", :unique => true)
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# In order to support multi-column indexes, an special parameter <tt>:with</tt> may be specified, which takes another column name or an array of column names to include in the index. Thus
|
60
|
-
#
|
61
|
-
# add_column('contacts', 'phone_number', :string, :index => { :with => [:country_code, :area_code], :unique => true })
|
62
|
-
#
|
63
|
-
# is equivalent to:
|
64
|
-
#
|
65
|
-
# add_column('contacts', 'phone_number', :string)
|
66
|
-
# add_index('contacts', ['phone_number', 'country_code', 'area_code'], :unique => true)
|
67
|
-
#
|
68
|
-
#
|
69
|
-
# Some convenient shorthands are available:
|
70
|
-
#
|
71
|
-
# add_column('books', 'isbn', :index => true) # adds index with no extra options
|
72
|
-
# add_column('books', 'isbn', :index => :unique) # adds index with :unique => true
|
73
|
-
#
|
74
|
-
# == Foreign Key Constraints
|
75
|
-
#
|
76
|
-
# The +:references+ option takes the name of a table to reference in
|
77
|
-
# a foreign key constraint. For example:
|
78
|
-
#
|
79
|
-
# add_column('widgets', 'color', :integer, :references => 'colors')
|
80
|
-
#
|
81
|
-
# is equivalent to
|
82
|
-
#
|
83
|
-
# add_column('widgets', 'color', :integer)
|
84
|
-
# add_foreign_key('widgets', 'color', 'colors', 'id')
|
85
|
-
#
|
86
|
-
# The foreign column name defaults to +id+, but a different column
|
87
|
-
# can be specified using <tt>:references => [table_name,column_name]</tt>
|
88
|
-
#
|
89
|
-
# Additional options +:on_update+ and +:on_delete+ can be spcified,
|
90
|
-
# with values as described at ConnectionAdapters::ForeignKeyDefinition. For example:
|
91
|
-
#
|
92
|
-
# add_column('comments', 'post', :integer, :references => 'posts', :on_delete => :cascade)
|
93
|
-
#
|
94
|
-
# Global default values for +:on_update+ and +:on_delete+ can be
|
95
|
-
# specified in SchemaPlus.steup via, e.g., <tt>config.foreign_keys.on_update = :cascade</tt>
|
96
|
-
#
|
97
|
-
# == Automatic Foreign Key Constraints
|
98
|
-
#
|
99
|
-
# SchemaPlus supports the convention of naming foreign key columns
|
100
|
-
# with a suffix of +_id+. That is, if you define a column suffixed
|
101
|
-
# with +_id+, SchemaPlus assumes an implied :references to a table
|
102
|
-
# whose name is the column name prefix, pluralized. For example,
|
103
|
-
# these are equivalent:
|
104
|
-
#
|
105
|
-
# add_column('posts', 'author_id', :integer)
|
106
|
-
# add_column('posts', 'author_id', :integer, :references => 'authors')
|
107
|
-
#
|
108
|
-
# As a special case, if the column is named 'parent_id', SchemaPlus
|
109
|
-
# assumes it's a self reference, for a record that acts as a node of
|
110
|
-
# a tree. Thus, these are equivalent:
|
111
|
-
#
|
112
|
-
# add_column('sections', 'parent_id', :integer)
|
113
|
-
# add_column('sections', 'parent_id', :integer, :references => 'sections')
|
114
|
-
#
|
115
|
-
# If the implicit +:references+ value isn't what you want (e.g., the
|
116
|
-
# table name isn't pluralized), you can explicitly specify
|
117
|
-
# +:references+ and it will override the implicit value.
|
118
|
-
#
|
119
|
-
# If you don't want a foreign key constraint to be created, specify
|
120
|
-
# <tt>:references => nil</tt>.
|
121
|
-
# To disable automatic foreign key constraint creation globally, set
|
122
|
-
# <tt>config.foreign_keys.auto_create = false</tt> in
|
123
|
-
# SchemaPlus.steup.
|
124
|
-
#
|
125
|
-
# == Automatic Foreign Key Indexes
|
126
|
-
#
|
127
|
-
# Since efficient use of foreign key constraints requires that the
|
128
|
-
# referencing column be indexed, SchemaPlus will automatically create
|
129
|
-
# an index for the column if it created a foreign key. Thus
|
130
|
-
#
|
131
|
-
# add_column('widgets', 'color', :integer, :references => 'colors')
|
132
|
-
#
|
133
|
-
# is equivalent to:
|
134
|
-
#
|
135
|
-
# add_column('widgets', 'color', :integer, :references => 'colors', :index => true)
|
136
|
-
#
|
137
|
-
# If you want to pass options to the index, you can explcitly pass
|
138
|
-
# index options, such as <tt>:index => :unique</tt>.
|
139
|
-
#
|
140
|
-
# If you don't want an index to be created, specify
|
141
|
-
# <tt>:index => nil</tt>.
|
142
|
-
# To disable automatic foreign key index creation globally, set
|
143
|
-
# <tt>config.foreign_keys.auto_index = false</tt> in
|
144
|
-
# SchemaPlus.steup. (*Note*: If you're using MySQL, it will
|
145
|
-
# automatically create an index for foreign keys if you don't.)
|
146
|
-
#
|
147
|
-
def add_column(table_name, column_name, type, options = {})
|
148
|
-
super
|
149
|
-
handle_column_options(table_name, column_name, options)
|
150
|
-
end
|
151
|
-
|
152
|
-
# Enhances ActiveRecord::Migration#change_column to support indexes and foreign keys same as add_column.
|
153
|
-
def change_column(table_name, column_name, type, options = {})
|
154
|
-
super
|
155
|
-
remove_foreign_key_if_exists(table_name, column_name)
|
156
|
-
handle_column_options(table_name, column_name, options)
|
157
|
-
end
|
158
|
-
|
159
|
-
# Determines referenced table and column.
|
160
|
-
# Used in migrations.
|
161
|
-
#
|
162
|
-
# If auto_create is true:
|
163
|
-
# get_references('comments', 'post_id') # => ['posts', 'id']
|
164
|
-
#
|
165
|
-
# And if <tt>column_name</tt> is parent_id it references to the same table
|
166
|
-
# get_references('pages', 'parent_id') # => ['pages', 'id']
|
167
|
-
#
|
168
|
-
# If :references option is given, it is used (whether or not auto_create is true)
|
169
|
-
# get_references('widgets', 'main_page_id', :references => 'pages'))
|
170
|
-
# # => ['pages', 'id']
|
171
|
-
#
|
172
|
-
# Also the referenced id column may be specified:
|
173
|
-
# get_references('addresses', 'member_id', :references => ['users', 'uuid'])
|
174
|
-
# # => ['users', 'uuid']
|
175
|
-
def get_references(table_name, column_name, options = {}, config=nil) #:nodoc:
|
176
|
-
column_name = column_name.to_s
|
177
|
-
if options.has_key?(:references)
|
178
|
-
references = options[:references]
|
179
|
-
references = [references, :id] unless references.nil? || references.is_a?(Array)
|
180
|
-
references
|
181
|
-
elsif (config || SchemaPlus.config).foreign_keys.auto_create? && !ActiveRecord::Schema.defining?
|
182
|
-
if column_name == 'parent_id'
|
183
|
-
[table_name, :id]
|
184
|
-
elsif column_name =~ /^(.*)_id$/
|
185
|
-
determined_table_name = ActiveRecord::Base.pluralize_table_names ? $1.to_s.pluralize : $1
|
186
|
-
[determined_table_name, :id]
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
protected
|
192
|
-
def handle_column_options(table_name, column_name, options) #:nodoc:
|
193
|
-
if references = get_references(table_name, column_name, options)
|
194
|
-
if index = options.fetch(:index, SchemaPlus.config.foreign_keys.auto_index? && !ActiveRecord::Schema.defining?)
|
195
|
-
column_index(table_name, column_name, index)
|
196
|
-
end
|
197
|
-
add_foreign_key(table_name, column_name, references.first, references.last,
|
198
|
-
options.reverse_merge(:on_update => SchemaPlus.config.foreign_keys.on_update,
|
199
|
-
:on_delete => SchemaPlus.config.foreign_keys.on_delete))
|
200
|
-
elsif options[:index]
|
201
|
-
column_index(table_name, column_name, options[:index])
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
def column_index(table_name, column_name, options) #:nodoc:
|
206
|
-
options = {} if options == true
|
207
|
-
options = { :unique => true } if options == :unique
|
208
|
-
column_name = [column_name] + Array.wrap(options.delete(:with)).compact
|
209
|
-
add_index(table_name, column_name, options)
|
210
|
-
end
|
211
|
-
|
212
|
-
def remove_foreign_key_if_exists(table_name, column_name) #:nodoc:
|
213
|
-
foreign_keys = ActiveRecord::Base.connection.foreign_keys(table_name.to_s)
|
214
|
-
fk = foreign_keys.detect { |fk| fk.table_name == table_name.to_s && fk.column_names == Array(column_name).collect(&:to_s) }
|
215
|
-
remove_foreign_key(table_name, fk.name) if fk
|
216
|
-
end
|
217
|
-
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|