automatic_foreign_key 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
22
  .rvmrc
23
+ .bundle
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ 1.3.0
2
+ * allow to disable automatic FK creation
1
3
  1.2.0
2
4
  * added install generator
3
5
  * fixed doubled auto-index inside Schema.define
@@ -1,41 +1,42 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- automatic_foreign_key (1.2.0)
4
+ automatic_foreign_key (1.3.0)
5
5
  activerecord (>= 2)
6
- redhillonrails_core (~> 1.1.2)
6
+ redhillonrails_core (>= 1.2.0)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activemodel (3.0.3)
12
- activesupport (= 3.0.3)
11
+ activemodel (3.0.9)
12
+ activesupport (= 3.0.9)
13
13
  builder (~> 2.1.2)
14
- i18n (~> 0.4)
15
- activerecord (3.0.3)
16
- activemodel (= 3.0.3)
17
- activesupport (= 3.0.3)
18
- arel (~> 2.0.2)
14
+ i18n (~> 0.5.0)
15
+ activerecord (3.0.9)
16
+ activemodel (= 3.0.9)
17
+ activesupport (= 3.0.9)
18
+ arel (~> 2.0.10)
19
19
  tzinfo (~> 0.3.23)
20
- activesupport (3.0.3)
21
- arel (2.0.7)
20
+ activesupport (3.0.9)
21
+ arel (2.0.10)
22
22
  builder (2.1.2)
23
23
  diff-lcs (1.1.2)
24
24
  i18n (0.5.0)
25
25
  mysql (2.8.1)
26
- mysql2 (0.2.6)
26
+ mysql2 (0.2.11)
27
27
  pg (0.10.0)
28
- redhillonrails_core (1.1.2)
29
- activerecord
30
- rspec (2.4.0)
31
- rspec-core (~> 2.4.0)
32
- rspec-expectations (~> 2.4.0)
33
- rspec-mocks (~> 2.4.0)
34
- rspec-core (2.4.0)
35
- rspec-expectations (2.4.0)
28
+ rake (0.9.2)
29
+ redhillonrails_core (1.2.0)
30
+ activerecord (>= 2)
31
+ rspec (2.6.0)
32
+ rspec-core (~> 2.6.0)
33
+ rspec-expectations (~> 2.6.0)
34
+ rspec-mocks (~> 2.6.0)
35
+ rspec-core (2.6.4)
36
+ rspec-expectations (2.6.0)
36
37
  diff-lcs (~> 1.1.2)
37
- rspec-mocks (2.4.0)
38
- tzinfo (0.3.24)
38
+ rspec-mocks (2.6.0)
39
+ tzinfo (0.3.29)
39
40
 
40
41
  PLATFORMS
41
42
  ruby
@@ -43,6 +44,7 @@ PLATFORMS
43
44
  DEPENDENCIES
44
45
  automatic_foreign_key!
45
46
  mysql
46
- mysql2
47
+ mysql2 (~> 0.2.11)
47
48
  pg
48
- rspec (~> 2.4.0)
49
+ rake
50
+ rspec (~> 2.6.0)
@@ -9,13 +9,24 @@ options to the schema column definition statements.
9
9
 
10
10
  === Installation
11
11
 
12
- As a gem
13
-
14
- gem install automatic_foreign_key
12
+ gem install automatic_foreign_key
13
+ rails generate automatic_foreign_key:install
15
14
 
16
- ...or as a plugin
15
+ === Rails 3.1 compatibility
17
16
 
18
- script/plugin install http://github.com/mlomnicki/automatic_foreign_key.git
17
+ You need at least 1.3.0 version.
18
+ Also explicit reference to redhillonrails_core 2.0.0.pre must be set.
19
+
20
+ gem "automatic_foreign_key", "~> 1.3.0"
21
+ gem "redhillonrails_core", "~> 2.0.0.pre"
22
+
23
+ === Rails 3.0 compatibility
24
+
25
+ Fully compatible with Rails 3.
26
+
27
+ === Rails 2.x compatibility
28
+
29
+ Only generator is not compatible
19
30
 
20
31
  === Usage
21
32
 
@@ -83,7 +94,7 @@ would be misinterpreted as a foreign-key (say for example if you named the prima
83
94
 
84
95
  There is also a generator for creating foreign keys on a database that currently has none:
85
96
 
86
- ruby script/generate foreign_key_migration
97
+ ruby rails generate automatic_foreign_key:migration
87
98
 
88
99
  The plugin fully supports and understands the following active-record
89
100
  configuration properties:
@@ -92,7 +103,7 @@ configuration properties:
92
103
  * <code>config.active_record.table_name_prefix</code>
93
104
  * <code>config.active_record.table_name_suffix</code>
94
105
 
95
- === Auto Indices
106
+ === Auto Indices
96
107
 
97
108
  It's very common to create an index on foreign key. You can instruct
98
109
  AutomaticForeignKey to add an index after adding foreign key.
@@ -114,7 +125,7 @@ If you want to pass some options for index use hash params.
114
125
  Auto indexing option is useless for MySQL users as their RDBMS adds indices on foreign
115
126
  keys by default. However PostgreSQL users may have fun with that feature.
116
127
 
117
- === Column Indices
128
+ === Column Indices
118
129
 
119
130
  You can create an index on any column by specifying the +:index+ option.
120
131
 
@@ -149,20 +160,19 @@ For customization purposes create config/initializers/automatic_foreign_key.rb f
149
160
  config.auto_index = true # create indices on FKs by default
150
161
  config.on_update = :cascade # cascade as default on_update action
151
162
  config.on_delete = :restrict # restrict as default on_delete action
152
- end
153
163
 
154
- === Rails 3 compatibility
155
-
156
- Automatic foreign key is fully compatible with Rails 3.
157
-
158
- === Rails 2.x compatibility
159
-
160
- Everything but generator is compatible
164
+ config.disable = false # set to true if you want to disable auto foreign keys
165
+ end
161
166
 
162
167
  === Dependencies
163
168
 
164
169
  * RedHill on Rails Core (redhillonrails_core).
165
170
 
171
+ === Support
172
+
173
+ Don't hesitate to ask questions on our mailing list.
174
+ http://groups.google.com/group/rails-db
175
+
166
176
  === NOTE
167
177
 
168
178
  * Code was created by harukizaemon(http://github.com/harukizaemon) but is not supported currently by him.
@@ -170,5 +180,5 @@ For customization purposes create config/initializers/automatic_foreign_key.rb f
170
180
 
171
181
  === License
172
182
 
173
- This plugin is copyright 2010 by Michał Łomnicki and is released
183
+ This plugin is copyright 2011 by Michał Łomnicki and is released
174
184
  under the MIT license.
data/Rakefile CHANGED
@@ -29,15 +29,16 @@ Rake::RDocTask.new do |rdoc|
29
29
  rdoc.rdoc_files.include('lib/**/*.rb')
30
30
  end
31
31
 
32
+ DBNAME = "afk_unittest"
32
33
  namespace :postgresql do
33
34
  desc 'Build the PostgreSQL test databases'
34
35
  task :build_databases do
35
- %x( createdb -E UTF8 afk_unittest )
36
+ %x( createdb -E UTF8 #{DBNAME} )
36
37
  end
37
38
 
38
39
  desc 'Drop the PostgreSQL test databases'
39
40
  task :drop_databases do
40
- %x( dropdb afk_unittest )
41
+ %x( dropdb #{DBNAME} )
41
42
  end
42
43
 
43
44
  desc 'Rebuild the PostgreSQL test databases'
@@ -52,12 +53,12 @@ MYSQL_DB_USER = 'afk'
52
53
  namespace :mysql do
53
54
  desc 'Build the MySQL test databases'
54
55
  task :build_databases do
55
- %x( echo "create DATABASE afk_unittest DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci " | mysql --user=#{MYSQL_DB_USER})
56
+ %x( echo "create DATABASE #{DBNAME} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci " | mysql --user=#{MYSQL_DB_USER})
56
57
  end
57
58
 
58
- desc 'Drop the MySQL test databases'
59
+ desc 'Drop the MySQL test databases'
59
60
  task :drop_databases do
60
- %x( mysqladmin --user=#{MYSQL_DB_USER} -f drop afk_unittest )
61
+ %x( mysqladmin --user=#{MYSQL_DB_USER} -f drop #{DBNAME} )
61
62
  end
62
63
 
63
64
  desc 'Rebuild the MySQL test databases'
@@ -21,11 +21,12 @@ constraints when creating tables or adding columns. It uses SQL-92 syntax and as
21
21
  s.require_paths = ["lib"]
22
22
 
23
23
  s.add_dependency("activerecord", ">= 2")
24
- s.add_dependency("redhillonrails_core", "~> 1.1.2")
24
+ s.add_dependency("redhillonrails_core", ">= 1.2.0")
25
25
 
26
- s.add_development_dependency("rspec", "~> 2.4.0")
26
+ s.add_development_dependency("rspec", "~> 2.6.0")
27
27
  s.add_development_dependency("pg")
28
28
  s.add_development_dependency("mysql")
29
- s.add_development_dependency("mysql2")
29
+ s.add_development_dependency("mysql2", "~> 0.2.11")
30
+ s.add_development_dependency("rake")
30
31
  end
31
32
 
@@ -19,16 +19,21 @@ module AutomaticForeignKey
19
19
 
20
20
  end
21
21
 
22
- # Default FK update action
22
+ # Default FK update action
23
23
  mattr_accessor :on_update
24
-
25
- # Default FK delete action
24
+
25
+ # Default FK delete action
26
26
  mattr_accessor :on_delete
27
27
 
28
28
  # Create an index after creating FK (default false)
29
29
  mattr_accessor :auto_index
30
30
  @@auto_index = nil
31
31
 
32
+ # Disable automatic foreign key creation.
33
+ # Useful for disabling automatic foreign keys in development env
34
+ # but enabling in test and production.
35
+ mattr_accessor :disable
36
+
32
37
  def self.setup(&block)
33
38
  yield self
34
39
  end
@@ -20,16 +20,18 @@ module AutomaticForeignKey::ActiveRecord::ConnectionAdapters
20
20
 
21
21
  def column_with_automatic_foreign_key(name, type, options = {})
22
22
  column_without_automatic_foreign_key(name, type, options)
23
- references = ActiveRecord::Base.references(self.name, name, options)
24
- if references
25
- AutomaticForeignKey.set_default_update_and_delete_actions!(options)
26
- foreign_key(name, references.first, references.last, options)
27
- if index = afk_index_options(options)
28
- # append [column_name, index_options] pair
29
- self.indices << [name, AutomaticForeignKey.options_for_index(index)]
23
+ unless AutomaticForeignKey.disable
24
+ references = ActiveRecord::Base.references(self.name, name, options)
25
+ if references
26
+ AutomaticForeignKey.set_default_update_and_delete_actions!(options)
27
+ foreign_key(name, references.first, references.last, options)
28
+ if index = afk_index_options(options)
29
+ # append [column_name, index_options] pair
30
+ self.indices << [name, AutomaticForeignKey.options_for_index(index)]
31
+ end
32
+ elsif options[:index]
33
+ self.indices << [name, AutomaticForeignKey.options_for_index(options[:index])]
30
34
  end
31
- elsif options[:index]
32
- self.indices << [name, AutomaticForeignKey.options_for_index(options[:index])]
33
35
  end
34
36
  self
35
37
  end
@@ -29,13 +29,17 @@ module AutomaticForeignKey::ActiveRecord
29
29
  #
30
30
  def add_column(table_name, column_name, type, options = {})
31
31
  super
32
- handle_column_options(table_name, column_name, options)
32
+ unless AutomaticForeignKey.disable
33
+ handle_column_options(table_name, column_name, options)
34
+ end
33
35
  end
34
36
 
35
37
  def change_column(table_name, column_name, type, options = {})
36
38
  super
37
- remove_foreign_key_if_exists(table_name, column_name)
38
- handle_column_options(table_name, column_name, options)
39
+ unless AutomaticForeignKey.disable
40
+ remove_foreign_key_if_exists(table_name, column_name)
41
+ handle_column_options(table_name, column_name, options)
42
+ end
39
43
  end
40
44
 
41
45
  protected
@@ -1,3 +1,3 @@
1
1
  module AutomaticForeignKey
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -12,5 +12,8 @@ AutomaticForeignKey.setup do |config|
12
12
  # It's relevant for PostgreSQL only.
13
13
  # MySQL engines auto-index foreign keys by default
14
14
  # config.auto_index = true
15
+ #
16
+ # Disable automatic foreign key creation.
17
+ # config.disable = true
15
18
 
16
19
  end
@@ -54,7 +54,7 @@ describe ActiveRecord::Migration do
54
54
  :state => { :index => {:with => :city} } )
55
55
  @model.should have_index.on([:state, :city])
56
56
  end
57
-
57
+
58
58
  it "should auto-index foreign keys only" do
59
59
  AutomaticForeignKey.auto_index = true
60
60
  create_table(@model, :user_id => {},
@@ -182,17 +182,6 @@ describe ActiveRecord::Migration do
182
182
  AutomaticForeignKey.on_delete = nil
183
183
  end
184
184
 
185
- protected
186
- def add_column(column_name, *args)
187
- table = @model.table_name
188
- ActiveRecord::Migration.suppress_messages do
189
- ActiveRecord::Migration.add_column(table, column_name, *args)
190
- @model.reset_column_information
191
- yield if block_given?
192
- ActiveRecord::Migration.remove_column(table, column_name)
193
- end
194
- end
195
-
196
185
  end
197
186
 
198
187
  context "when column is changed" do
@@ -225,18 +214,40 @@ describe ActiveRecord::Migration do
225
214
  end
226
215
 
227
216
  end
228
-
229
- protected
230
- def change_column(column_name, *args)
231
- table = @model.table_name
232
- ActiveRecord::Migration.suppress_messages do
233
- ActiveRecord::Migration.change_column(table, column_name, *args)
234
- @model.reset_column_information
217
+
218
+ context "when disabled" do
219
+
220
+ before do
221
+ @model = Post
222
+ end
223
+
224
+ around do |example|
225
+ disable(&example)
226
+ end
227
+
228
+ it "should not create foreign key for created table" do
229
+ create_table(@model, :user_id => {})
230
+ @model.should_not reference.on(:user_id)
231
+ end
232
+
233
+ it "should not create foreign for added column" do
234
+ create_table(@model, {})
235
+ add_column(:user_id, :integer) do
236
+ @model.should_not reference.on(:user_id)
237
+ end
238
+ end
239
+
240
+ it "should not create foreign key for changed column" do
241
+ create_table(@model, :user_id => { :references => nil })
242
+ change_column(:user_id, :integer)
243
+ @model.should_not reference.on(:user_id)
235
244
  end
245
+
236
246
  end
237
247
 
238
248
  end
239
-
249
+
250
+ protected
240
251
  def foreign_key(model, column)
241
252
  columns = Array(column).collect(&:to_s)
242
253
  model.foreign_keys.detect { |fk| fk.table_name == model.table_name && fk.column_names == columns }
@@ -253,5 +264,33 @@ describe ActiveRecord::Migration do
253
264
  end
254
265
  end
255
266
 
267
+ def add_column(column_name, *args)
268
+ table = @model.table_name
269
+ ActiveRecord::Migration.suppress_messages do
270
+ ActiveRecord::Migration.add_column(table, column_name, *args)
271
+ @model.reset_column_information
272
+ yield if block_given?
273
+ ActiveRecord::Migration.remove_column(table, column_name)
274
+ end
275
+ end
276
+
277
+ def change_column(column_name, *args)
278
+ table = @model.table_name
279
+ ActiveRecord::Migration.suppress_messages do
280
+ ActiveRecord::Migration.change_column(table, column_name, *args)
281
+ @model.reset_column_information
282
+ end
283
+ end
284
+
285
+ def disable(&block)
286
+ old_value = AutomaticForeignKey.disable
287
+ AutomaticForeignKey.disable = true
288
+ begin
289
+ yield
290
+ ensure
291
+ AutomaticForeignKey.disable = old_value
292
+ end
293
+ end
294
+
256
295
  end
257
296
 
metadata CHANGED
@@ -1,114 +1,103 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: automatic_foreign_key
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 2
8
- - 0
9
- version: 1.2.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
12
- - "Micha\xC5\x82 \xC5\x81omnicki"
7
+ authors:
8
+ - Michał Łomnicki
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-01-23 00:00:00 +01:00
12
+ date: 2011-07-22 00:00:00.000000000 +02:00
18
13
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
21
16
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &79958930 !ruby/object:Gem::Requirement
24
18
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 2
30
- version: "2"
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '2'
31
23
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: redhillonrails_core
35
24
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *79958930
26
+ - !ruby/object:Gem::Dependency
27
+ name: redhillonrails_core
28
+ requirement: &79958130 !ruby/object:Gem::Requirement
37
29
  none: false
38
- requirements:
39
- - - ~>
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 1
43
- - 1
44
- - 2
45
- version: 1.1.2
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.0
46
34
  type: :runtime
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: rspec
50
35
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *79958130
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &79957220 !ruby/object:Gem::Requirement
52
40
  none: false
53
- requirements:
41
+ requirements:
54
42
  - - ~>
55
- - !ruby/object:Gem::Version
56
- segments:
57
- - 2
58
- - 4
59
- - 0
60
- version: 2.4.0
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
61
45
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: pg
65
46
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
47
+ version_requirements: *79957220
48
+ - !ruby/object:Gem::Dependency
49
+ name: pg
50
+ requirement: &79956760 !ruby/object:Gem::Requirement
67
51
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- segments:
72
- - 0
73
- version: "0"
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
74
56
  type: :development
75
- version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
77
- name: mysql
78
57
  prerelease: false
79
- requirement: &id005 !ruby/object:Gem::Requirement
58
+ version_requirements: *79956760
59
+ - !ruby/object:Gem::Dependency
60
+ name: mysql
61
+ requirement: &79956040 !ruby/object:Gem::Requirement
80
62
  none: false
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- segments:
85
- - 0
86
- version: "0"
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
87
67
  type: :development
88
- version_requirements: *id005
89
- - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ version_requirements: *79956040
70
+ - !ruby/object:Gem::Dependency
90
71
  name: mysql2
72
+ requirement: &79955350 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.2.11
78
+ type: :development
91
79
  prerelease: false
92
- requirement: &id006 !ruby/object:Gem::Requirement
80
+ version_requirements: *79955350
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: &79954720 !ruby/object:Gem::Requirement
93
84
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- segments:
98
- - 0
99
- version: "0"
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
100
89
  type: :development
101
- version_requirements: *id006
102
- description: Automatic Foreign Key automatically generates foreign-key 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.
103
- email:
90
+ prerelease: false
91
+ version_requirements: *79954720
92
+ description: Automatic Foreign Key automatically generates foreign-key constraints
93
+ when creating tables or adding columns. It uses SQL-92 syntax and as such should
94
+ be compatible with most databases that support foreign-key constraints.
95
+ email:
104
96
  - michal.lomnicki@gmail.com
105
97
  executables: []
106
-
107
98
  extensions: []
108
-
109
99
  extra_rdoc_files: []
110
-
111
- files:
100
+ files:
112
101
  - .document
113
102
  - .gitignore
114
103
  - CHANGELOG
@@ -150,34 +139,32 @@ files:
150
139
  has_rdoc: true
151
140
  homepage: https://github.com/mlomnicki/automatic_foreign_key
152
141
  licenses: []
153
-
154
142
  post_install_message:
155
143
  rdoc_options: []
156
-
157
- require_paths:
144
+ require_paths:
158
145
  - lib
159
- required_ruby_version: !ruby/object:Gem::Requirement
146
+ required_ruby_version: !ruby/object:Gem::Requirement
160
147
  none: false
161
- requirements:
162
- - - ">="
163
- - !ruby/object:Gem::Version
164
- segments:
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ segments:
165
153
  - 0
166
- version: "0"
167
- required_rubygems_version: !ruby/object:Gem::Requirement
154
+ hash: 126095695
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
156
  none: false
169
- requirements:
170
- - - ">="
171
- - !ruby/object:Gem::Version
172
- segments:
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ segments:
173
162
  - 0
174
- version: "0"
163
+ hash: 126095695
175
164
  requirements: []
176
-
177
165
  rubyforge_project: automatic_foreign_key
178
- rubygems_version: 1.3.7
166
+ rubygems_version: 1.6.2
179
167
  signing_key:
180
168
  specification_version: 3
181
169
  summary: Automatically generate foreign-key constraints when creating tables
182
170
  test_files: []
183
-