immigrant 0.1.8 → 0.2.0
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.md +6 -5
- data/lib/generators/immigration_generator.rb +1 -2
- data/lib/immigrant.rb +12 -41
- data/lib/immigrant/compat.rb +57 -0
- data/lib/immigrant/compat/3.0.rb +11 -0
- data/lib/immigrant/compat/3.1.rb +12 -0
- data/lib/immigrant/compat/4.0.rb +22 -0
- data/lib/immigrant/compat/4.2.rb +60 -0
- data/lib/immigrant/compat/foreigner_extensions.rb +5 -0
- data/lib/immigrant/loader.rb +5 -4
- data/test/compat.rb +8 -0
- data/test/helper.rb +3 -8
- data/test/immigrant_test.rb +23 -23
- metadata +11 -21
- data/lib/immigrant/foreign_key_definition.rb +0 -26
data/README.md
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
# Immigrant
|
2
2
|
[<img src="https://secure.travis-ci.org/jenseng/immigrant.png?rvm=1.9.3" />](http://travis-ci.org/jenseng/immigrant)
|
3
3
|
|
4
|
-
Immigrant gives
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
Like Foreigner, Immigrant requires Rails 3.0 or greater.
|
4
|
+
Immigrant gives Rails a foreign key migration generator so you can
|
5
|
+
effortlessly find and add missing keys. This is particularly helpful
|
6
|
+
when you decide to add keys to an established Rails app.
|
9
7
|
|
10
8
|
## Installation
|
11
9
|
|
@@ -15,6 +13,9 @@ Add the following to your Gemfile:
|
|
15
13
|
gem 'immigrant'
|
16
14
|
```
|
17
15
|
|
16
|
+
If you're using a version of Rails prior to 4.2, you'll also need the
|
17
|
+
[Foreigner](https://github.com/matthuhiggins/foreigner) gem.
|
18
|
+
|
18
19
|
## Usage
|
19
20
|
|
20
21
|
```bash
|
@@ -10,8 +10,7 @@ class ImmigrationGenerator < ActiveRecord::Generators::Base
|
|
10
10
|
$stderr.puts "NOTICE: #{key.options[:name]} has ON DELETE CASCADE. You should remove the :dependent option from the association to take advantage of this."
|
11
11
|
end
|
12
12
|
if @keys.present?
|
13
|
-
|
14
|
-
migration_template template, "db/migrate/#{file_name}.rb"
|
13
|
+
migration_template Immigrant::TEMPLATE, "db/migrate/#{file_name}.rb"
|
15
14
|
else
|
16
15
|
puts 'Nothing to do'
|
17
16
|
end
|
data/lib/immigrant.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
require 'active_support/all'
|
2
|
-
require 'foreigner'
|
3
2
|
|
4
3
|
module Immigrant
|
5
|
-
extend ActiveSupport::Autoload
|
6
|
-
autoload :Loader
|
7
|
-
autoload :ForeignKeyDefinition
|
8
|
-
|
9
4
|
class << self
|
10
5
|
def infer_keys(db_keys = current_foreign_keys, classes = model_classes)
|
11
6
|
database_keys = db_keys.inject({}) { |hash, foreign_key|
|
@@ -17,7 +12,7 @@ module Immigrant
|
|
17
12
|
model_keys.keys.each do |hash_key|
|
18
13
|
foreign_key = model_keys[hash_key]
|
19
14
|
# if the foreign key exists in the db, we call it good (even if
|
20
|
-
# the name is different or :
|
15
|
+
# the name is different or :on_delete doesn't match, etc.), though
|
21
16
|
# we do warn on clearly broken stuff
|
22
17
|
if current_key = database_keys[hash_key]
|
23
18
|
if current_key.to_table != foreign_key.to_table || current_key.options[:primary_key] != foreign_key.options[:primary_key]
|
@@ -111,26 +106,26 @@ module Immigrant
|
|
111
106
|
def infer_belongs_to_keys(klass, reflection)
|
112
107
|
return [] if reflection.name == :left_side # redundant and unusable reflection automagically created by HABTM
|
113
108
|
[
|
114
|
-
|
109
|
+
ForeignKeyDefinition.new(
|
115
110
|
klass.table_name,
|
116
111
|
reflection.klass.table_name,
|
117
|
-
:column => reflection.send(
|
112
|
+
:column => reflection.send(FOREIGN_KEY).to_s,
|
118
113
|
:primary_key => (reflection.options[:primary_key] || reflection.klass.primary_key).to_s,
|
119
114
|
# although belongs_to can specify :dependent, it doesn't make
|
120
115
|
# sense from a foreign key perspective
|
121
|
-
|
116
|
+
ON_DELETE => nil
|
122
117
|
)
|
123
118
|
]
|
124
119
|
end
|
125
120
|
|
126
121
|
def infer_has_n_keys(klass, reflection)
|
127
122
|
[
|
128
|
-
|
123
|
+
ForeignKeyDefinition.new(
|
129
124
|
reflection.klass.table_name,
|
130
125
|
klass.table_name,
|
131
|
-
:column => reflection.send(
|
126
|
+
:column => reflection.send(FOREIGN_KEY).to_s,
|
132
127
|
:primary_key => (reflection.options[:primary_key] || klass.primary_key).to_s,
|
133
|
-
|
128
|
+
ON_DELETE => [:delete, :delete_all].include?(reflection.options[:dependent]) && !qualified_reflection?(reflection, klass) ? :delete : nil
|
134
129
|
)
|
135
130
|
]
|
136
131
|
end
|
@@ -138,46 +133,22 @@ module Immigrant
|
|
138
133
|
def infer_habtm_keys(klass, reflection)
|
139
134
|
join_table = (reflection.respond_to?(:join_table) ? reflection.join_table : reflection.options[:join_table]).to_s
|
140
135
|
[
|
141
|
-
|
136
|
+
ForeignKeyDefinition.new(
|
142
137
|
join_table,
|
143
138
|
klass.table_name,
|
144
|
-
:column => reflection.send(
|
139
|
+
:column => reflection.send(FOREIGN_KEY).to_s,
|
145
140
|
:primary_key => klass.primary_key.to_s,
|
146
|
-
|
141
|
+
ON_DELETE => nil
|
147
142
|
),
|
148
|
-
|
143
|
+
ForeignKeyDefinition.new(
|
149
144
|
join_table,
|
150
145
|
reflection.klass.table_name,
|
151
146
|
:column => reflection.association_foreign_key.to_s,
|
152
147
|
:primary_key => reflection.klass.primary_key.to_s,
|
153
|
-
|
148
|
+
ON_DELETE => nil
|
154
149
|
)
|
155
150
|
]
|
156
151
|
end
|
157
|
-
|
158
|
-
def fk_method
|
159
|
-
ActiveRecord::VERSION::STRING < '3.1.' ? :primary_key_name : :foreign_key
|
160
|
-
end
|
161
|
-
|
162
|
-
def qualified_reflection?(reflection, klass)
|
163
|
-
if ActiveRecord::VERSION::STRING < '4.'
|
164
|
-
reflection.options[:conditions].present?
|
165
|
-
else
|
166
|
-
scope = reflection.scope
|
167
|
-
if scope.nil?
|
168
|
-
false
|
169
|
-
elsif scope.respond_to?(:options)
|
170
|
-
scope.options[:where].present?
|
171
|
-
else
|
172
|
-
klass.instance_exec(*([nil]*scope.arity), &scope).where_values.present?
|
173
|
-
end
|
174
|
-
end
|
175
|
-
rescue
|
176
|
-
# if there's an error evaluating the scope block or whatever, just
|
177
|
-
# err on the side of caution and assume there are conditions
|
178
|
-
true
|
179
|
-
end
|
180
|
-
|
181
152
|
end
|
182
153
|
end
|
183
154
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
version = ActiveRecord::VERSION::STRING
|
2
|
+
|
3
|
+
if version >= '4.2.'
|
4
|
+
require_relative 'compat/4.2'
|
5
|
+
else
|
6
|
+
begin
|
7
|
+
require 'foreigner'
|
8
|
+
rescue LoadError
|
9
|
+
$stderr.puts <<-ERR.strip_heredoc
|
10
|
+
|
11
|
+
ERROR: immigrant requires the foreigner gem (unless you are on rails 4.2+)
|
12
|
+
|
13
|
+
To fix this, add the following to your Gemfile:
|
14
|
+
|
15
|
+
gem "foreigner", "~> 1.2"
|
16
|
+
|
17
|
+
Or just upgrade rails ;) ... lol, "just"
|
18
|
+
|
19
|
+
ERR
|
20
|
+
exit 1
|
21
|
+
end
|
22
|
+
Foreigner.load
|
23
|
+
|
24
|
+
require_relative 'compat/foreigner_extensions'
|
25
|
+
|
26
|
+
if version >= '4.0'
|
27
|
+
require_relative 'compat/4.0'
|
28
|
+
elsif version >= '3.1'
|
29
|
+
require_relative 'compat/3.1'
|
30
|
+
else
|
31
|
+
require_relative 'compat/3.0'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# add some useful things for querying/comparing/dumping foreign keys
|
36
|
+
module Immigrant
|
37
|
+
module ForeignKeyExtensions
|
38
|
+
def initialize(from_table, to_table, options, *args)
|
39
|
+
options ||= {}
|
40
|
+
options[:name] ||= "#{from_table}_#{options[:column]}_fk"
|
41
|
+
super(from_table, to_table, options, *args)
|
42
|
+
end
|
43
|
+
|
44
|
+
def hash_key
|
45
|
+
[from_table, options[:column]]
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_ruby(action = :add)
|
49
|
+
if action == :add
|
50
|
+
dump_foreign_key(self)
|
51
|
+
else
|
52
|
+
"remove_foreign_key #{from_table.inspect}, " \
|
53
|
+
":name => #{options[:name].inspect}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Immigrant
|
2
|
+
ForeignKeyDefinition = ::Foreigner::ConnectionAdapters::ForeignKeyDefinition
|
3
|
+
|
4
|
+
TEMPLATE = 'immigration-pre-3.1.rb.erb'
|
5
|
+
FOREIGN_KEY = :primary_key_name
|
6
|
+
ON_DELETE = :dependent
|
7
|
+
|
8
|
+
def self.qualified_reflection?(reflection, klass)
|
9
|
+
reflection.options[:conditions].present?
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Immigrant
|
2
|
+
ForeignKeyDefinition = ::Foreigner::ConnectionAdapters::ForeignKeyDefinition
|
3
|
+
|
4
|
+
TEMPLATE = 'immigration.rb.erb'
|
5
|
+
FOREIGN_KEY = :foreign_key
|
6
|
+
ON_DELETE = :dependent
|
7
|
+
|
8
|
+
def self.qualified_reflection?(reflection, klass)
|
9
|
+
reflection.options[:conditions].present?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Immigrant
|
2
|
+
ForeignKeyDefinition = ::Foreigner::ConnectionAdapters::ForeignKeyDefinition
|
3
|
+
|
4
|
+
TEMPLATE = 'immigration.rb.erb'
|
5
|
+
FOREIGN_KEY = :foreign_key
|
6
|
+
ON_DELETE = :dependent
|
7
|
+
|
8
|
+
def self.qualified_reflection?(reflection, klass)
|
9
|
+
scope = reflection.scope
|
10
|
+
if scope.nil?
|
11
|
+
false
|
12
|
+
elsif scope.respond_to?(:options)
|
13
|
+
scope.options[:where].present?
|
14
|
+
else
|
15
|
+
klass.instance_exec(*([nil]*scope.arity), &scope).where_values.present?
|
16
|
+
end
|
17
|
+
rescue
|
18
|
+
# if there's an error evaluating the scope block or whatever, just
|
19
|
+
# err on the side of caution and assume there are conditions
|
20
|
+
true
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "active_record/connection_adapters/abstract/schema_definitions"
|
2
|
+
|
3
|
+
module Immigrant
|
4
|
+
ForeignKeyDefinition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition
|
5
|
+
|
6
|
+
TEMPLATE = 'immigration.rb.erb'
|
7
|
+
FOREIGN_KEY = :foreign_key
|
8
|
+
ON_DELETE = :on_delete
|
9
|
+
|
10
|
+
def self.qualified_reflection?(reflection, klass)
|
11
|
+
scope = reflection.scope
|
12
|
+
if scope.nil?
|
13
|
+
false
|
14
|
+
elsif scope.respond_to?(:options)
|
15
|
+
scope.options[:where].present?
|
16
|
+
else
|
17
|
+
klass.instance_exec(*([nil]*scope.arity), &scope).where_values.present?
|
18
|
+
end
|
19
|
+
rescue
|
20
|
+
# if there's an error evaluating the scope block or whatever, just
|
21
|
+
# err on the side of caution and assume there are conditions
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
module ForeignKeyExtensions
|
26
|
+
# DRY alert: copied from ActiveRecord::SchemaDumper#foreign_keys
|
27
|
+
def dump_foreign_key(foreign_key)
|
28
|
+
parts = [
|
29
|
+
"add_foreign_key #{remove_prefix_and_suffix(foreign_key.from_table).inspect}",
|
30
|
+
remove_prefix_and_suffix(foreign_key.to_table).inspect,
|
31
|
+
]
|
32
|
+
|
33
|
+
if foreign_key.column != foreign_key_column_for(foreign_key.to_table)
|
34
|
+
parts << "column: #{foreign_key.column.inspect}"
|
35
|
+
end
|
36
|
+
|
37
|
+
if foreign_key.custom_primary_key?
|
38
|
+
parts << "primary_key: #{foreign_key.primary_key.inspect}"
|
39
|
+
end
|
40
|
+
|
41
|
+
if foreign_key.name !~ /^fk_rails_[0-9a-f]{10}$/
|
42
|
+
parts << "name: #{foreign_key.name.inspect}"
|
43
|
+
end
|
44
|
+
|
45
|
+
parts << "on_update: #{foreign_key.on_update.inspect}" if foreign_key.on_update
|
46
|
+
parts << "on_delete: #{foreign_key.on_delete.inspect}" if foreign_key.on_delete
|
47
|
+
|
48
|
+
" #{parts.join(', ')}"
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def remove_prefix_and_suffix(table)
|
53
|
+
table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/, "\\2")
|
54
|
+
end
|
55
|
+
|
56
|
+
def foreign_key_column_for(table_name)
|
57
|
+
"#{table_name.to_s.singularize}_id"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/immigrant/loader.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Immigrant
|
2
2
|
def self.load
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
require "active_record"
|
4
|
+
require_relative "compat"
|
5
|
+
|
6
|
+
ForeignKeyDefinition.send :include, ForeignKeyExtensions
|
6
7
|
end
|
7
|
-
end
|
8
|
+
end
|
data/test/compat.rb
ADDED
data/test/helper.rb
CHANGED
@@ -3,20 +3,15 @@ require 'bundler/setup'
|
|
3
3
|
Bundler.require(:default)
|
4
4
|
|
5
5
|
require 'minitest/autorun'
|
6
|
-
require 'active_record'
|
7
6
|
|
8
|
-
require '
|
9
|
-
|
10
|
-
def self.configured_name; "dummy_adapter"; end
|
11
|
-
def self.load!; end
|
12
|
-
end
|
13
|
-
Foreigner.load
|
7
|
+
require 'active_record'
|
8
|
+
require_relative "compat"
|
14
9
|
|
15
10
|
require 'immigrant'
|
16
11
|
Immigrant.load
|
17
12
|
|
18
13
|
module TestMethods
|
19
14
|
def foreign_key_definition(*args)
|
20
|
-
|
15
|
+
Immigrant::ForeignKeyDefinition.new(*args)
|
21
16
|
end
|
22
17
|
end
|
data/test/immigrant_test.rb
CHANGED
@@ -75,7 +75,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
75
75
|
assert_equal(
|
76
76
|
[foreign_key_definition(
|
77
77
|
'books', 'authors',
|
78
|
-
:column => 'author_id', :primary_key => 'id',
|
78
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
79
79
|
)],
|
80
80
|
keys
|
81
81
|
)
|
@@ -94,13 +94,13 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
94
94
|
assert_equal(
|
95
95
|
[foreign_key_definition(
|
96
96
|
'books', 'authors',
|
97
|
-
:column => 'author_id', :primary_key => 'id',
|
97
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
98
98
|
)],
|
99
99
|
keys
|
100
100
|
)
|
101
101
|
end
|
102
102
|
|
103
|
-
test 'has_one :dependent => :delete should generate a foreign key with :
|
103
|
+
test 'has_one :dependent => :delete should generate a foreign key with :on_delete => :delete' do
|
104
104
|
given <<-CODE
|
105
105
|
class Author < MockModel
|
106
106
|
has_one :book, :order => "id DESC", :dependent => :delete
|
@@ -113,7 +113,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
113
113
|
assert_equal(
|
114
114
|
[foreign_key_definition(
|
115
115
|
'books', 'authors',
|
116
|
-
:column => 'author_id', :primary_key => 'id',
|
116
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => :delete
|
117
117
|
)],
|
118
118
|
keys
|
119
119
|
)
|
@@ -132,13 +132,13 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
132
132
|
assert_equal(
|
133
133
|
[foreign_key_definition(
|
134
134
|
'books', 'authors',
|
135
|
-
:column => 'author_id', :primary_key => 'id',
|
135
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
136
136
|
)],
|
137
137
|
keys
|
138
138
|
)
|
139
139
|
end
|
140
140
|
|
141
|
-
test 'has_many :dependent => :delete_all should generate a foreign key with :
|
141
|
+
test 'has_many :dependent => :delete_all should generate a foreign key with :on_delete => :delete' do
|
142
142
|
given <<-CODE
|
143
143
|
class Author < MockModel
|
144
144
|
has_many :books, :dependent => :delete_all
|
@@ -151,7 +151,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
151
151
|
assert_equal(
|
152
152
|
[foreign_key_definition(
|
153
153
|
'books', 'authors',
|
154
|
-
:column => 'author_id', :primary_key => 'id',
|
154
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => :delete
|
155
155
|
)],
|
156
156
|
keys
|
157
157
|
)
|
@@ -170,11 +170,11 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
170
170
|
assert_equal(
|
171
171
|
[foreign_key_definition(
|
172
172
|
'authors_fans', 'authors',
|
173
|
-
:column => 'author_id', :primary_key => 'id',
|
173
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
174
174
|
),
|
175
175
|
foreign_key_definition(
|
176
176
|
'authors_fans', 'fans',
|
177
|
-
:column => 'fan_id', :primary_key => 'id',
|
177
|
+
:column => 'fan_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
178
178
|
)],
|
179
179
|
keys
|
180
180
|
)
|
@@ -193,11 +193,11 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
193
193
|
assert_equal(
|
194
194
|
[foreign_key_definition(
|
195
195
|
'lols_wuts', 'authors',
|
196
|
-
:column => 'author_id', :primary_key => 'id',
|
196
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
197
197
|
),
|
198
198
|
foreign_key_definition(
|
199
199
|
'lols_wuts', 'fans',
|
200
|
-
:column => 'fan_id', :primary_key => 'id',
|
200
|
+
:column => 'fan_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
201
201
|
)],
|
202
202
|
keys
|
203
203
|
)
|
@@ -219,11 +219,11 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
219
219
|
assert_equal(
|
220
220
|
[foreign_key_definition(
|
221
221
|
'articles', 'authors',
|
222
|
-
:column => 'author_id', :primary_key => 'id',
|
222
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
223
223
|
),
|
224
224
|
foreign_key_definition(
|
225
225
|
'books', 'authors',
|
226
|
-
:column => 'author_id', :primary_key => 'id',
|
226
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
227
227
|
)],
|
228
228
|
keys
|
229
229
|
)
|
@@ -245,7 +245,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
245
245
|
assert_equal(
|
246
246
|
[foreign_key_definition(
|
247
247
|
'emails', 'users',
|
248
|
-
:column => 'to', :primary_key => 'email',
|
248
|
+
:column => 'to', :primary_key => 'email', Immigrant::ON_DELETE => nil
|
249
249
|
)],
|
250
250
|
keys
|
251
251
|
)
|
@@ -268,7 +268,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
268
268
|
assert_equal(
|
269
269
|
[foreign_key_definition(
|
270
270
|
'employees', 'companies',
|
271
|
-
:column => 'company_id', :primary_key => 'id',
|
271
|
+
:column => 'company_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
272
272
|
)],
|
273
273
|
keys
|
274
274
|
)
|
@@ -289,7 +289,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
289
289
|
assert_equal(
|
290
290
|
[foreign_key_definition(
|
291
291
|
'books', 'authors',
|
292
|
-
:column => 'author_id', :primary_key => 'id',
|
292
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
293
293
|
)],
|
294
294
|
keys
|
295
295
|
)
|
@@ -310,7 +310,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
310
310
|
assert_equal(
|
311
311
|
[foreign_key_definition(
|
312
312
|
'books', 'authors',
|
313
|
-
:column => 'author_id', :primary_key => 'id',
|
313
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
314
314
|
)],
|
315
315
|
keys
|
316
316
|
)
|
@@ -319,16 +319,16 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
319
319
|
|
320
320
|
# skipped associations
|
321
321
|
|
322
|
-
test 'associations should not generate foreign keys if they already exist, even if :
|
322
|
+
test 'associations should not generate foreign keys if they already exist, even if :on_delete/name are different' do
|
323
323
|
database_keys = [
|
324
324
|
foreign_key_definition(
|
325
325
|
'articles', 'authors',
|
326
|
-
:column => 'author_id', :primary_key => 'id',
|
326
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil,
|
327
327
|
:name => "doesn't_matter"
|
328
328
|
),
|
329
329
|
foreign_key_definition(
|
330
330
|
'books', 'authors', :column => 'author_id', :primary_key => 'id',
|
331
|
-
|
331
|
+
Immigrant::ON_DELETE => :delete
|
332
332
|
)
|
333
333
|
]
|
334
334
|
|
@@ -403,11 +403,11 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
403
403
|
assert_equal(
|
404
404
|
[foreign_key_definition(
|
405
405
|
'authors_fans', 'authors',
|
406
|
-
:column => 'author_id', :primary_key => 'id',
|
406
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
407
407
|
),
|
408
408
|
foreign_key_definition(
|
409
409
|
'authors_fans', 'fans',
|
410
|
-
:column => 'fan_id', :primary_key => 'id',
|
410
|
+
:column => 'fan_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
411
411
|
)],
|
412
412
|
keys
|
413
413
|
)
|
@@ -427,7 +427,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
427
427
|
assert_equal(
|
428
428
|
[foreign_key_definition(
|
429
429
|
'books', 'authors',
|
430
|
-
:column => 'author_id', :primary_key => 'id',
|
430
|
+
:column => 'author_id', :primary_key => 'id', Immigrant::ON_DELETE => nil
|
431
431
|
)],
|
432
432
|
keys
|
433
433
|
)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immigrant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -27,22 +27,6 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '3.0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: foreigner
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: 1.2.1
|
38
|
-
type: :runtime
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: 1.2.1
|
46
30
|
description: Adds a generator for creating a foreign key migration based on your current
|
47
31
|
model associations
|
48
32
|
email: jenseng@gmail.com
|
@@ -57,10 +41,16 @@ files:
|
|
57
41
|
- lib/generators/immigration_generator.rb
|
58
42
|
- lib/generators/templates/immigration-pre-3.1.rb.erb
|
59
43
|
- lib/generators/templates/immigration.rb.erb
|
60
|
-
- lib/immigrant/
|
44
|
+
- lib/immigrant/compat/3.0.rb
|
45
|
+
- lib/immigrant/compat/3.1.rb
|
46
|
+
- lib/immigrant/compat/4.0.rb
|
47
|
+
- lib/immigrant/compat/4.2.rb
|
48
|
+
- lib/immigrant/compat/foreigner_extensions.rb
|
49
|
+
- lib/immigrant/compat.rb
|
61
50
|
- lib/immigrant/loader.rb
|
62
51
|
- lib/immigrant/railtie.rb
|
63
52
|
- lib/immigrant.rb
|
53
|
+
- test/compat.rb
|
64
54
|
- test/helper.rb
|
65
55
|
- test/immigrant_test.rb
|
66
56
|
homepage: http://github.com/jenseng/immigrant
|
@@ -83,9 +73,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
73
|
version: 1.3.5
|
84
74
|
requirements: []
|
85
75
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.8.23
|
76
|
+
rubygems_version: 1.8.23.2
|
87
77
|
signing_key:
|
88
78
|
specification_version: 3
|
89
|
-
summary:
|
79
|
+
summary: Foreign key migration generator for Rails
|
90
80
|
test_files: []
|
91
81
|
has_rdoc:
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Immigrant
|
2
|
-
# add some useful stuff to foreigner's ForeignKeyDefinition
|
3
|
-
# TODO: get more of this into foreigner so we don't need to monkey patch
|
4
|
-
module ForeignKeyDefinition
|
5
|
-
include Foreigner::SchemaDumper::ClassMethods
|
6
|
-
|
7
|
-
def initialize(from_table, to_table, options, *args)
|
8
|
-
options ||= {}
|
9
|
-
options[:name] ||= "#{from_table}_#{options[:column]}_fk"
|
10
|
-
super(from_table, to_table, options, *args)
|
11
|
-
end
|
12
|
-
|
13
|
-
def hash_key
|
14
|
-
[from_table, options[:column]]
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_ruby(action = :add)
|
18
|
-
if action == :add
|
19
|
-
dump_foreign_key(self)
|
20
|
-
else
|
21
|
-
"remove_foreign_key #{from_table.inspect}, " \
|
22
|
-
":name => #{options[:name].inspect}"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|