foreigner 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 +1 -0
- data/lib/foreigner.rb +1 -0
- data/lib/foreigner/connection_adapters/abstract/foreign_key_definition.rb +6 -0
- data/lib/foreigner/connection_adapters/abstract/schema_definitions.rb +1 -4
- data/lib/foreigner/schema_dumper.rb +21 -15
- data/test/unit/schema_dumper_test.rb +26 -16
- metadata +7 -6
data/README.rdoc
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
= Foreigner
|
2
|
+
{<img src="https://secure.travis-ci.org/matthuhiggins/foreigner.png?rvm=1.9.3" />}[http://travis-ci.org/matthuhiggins/foreigner]
|
2
3
|
|
3
4
|
Foreigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to schema.rb.
|
4
5
|
|
data/lib/foreigner.rb
CHANGED
@@ -6,6 +6,26 @@ module Foreigner
|
|
6
6
|
alias_method_chain :tables, :foreign_keys
|
7
7
|
end
|
8
8
|
|
9
|
+
module ClassMethods
|
10
|
+
def dump_foreign_key(foreign_key)
|
11
|
+
statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
|
12
|
+
statement_parts << foreign_key.to_table.inspect
|
13
|
+
statement_parts << (':name => ' + foreign_key.options[:name].inspect)
|
14
|
+
|
15
|
+
if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
|
16
|
+
statement_parts << (':column => ' + foreign_key.options[:column].inspect)
|
17
|
+
end
|
18
|
+
if foreign_key.options[:primary_key] != 'id'
|
19
|
+
statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
|
20
|
+
end
|
21
|
+
if foreign_key.options[:dependent].present?
|
22
|
+
statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
|
23
|
+
end
|
24
|
+
|
25
|
+
statement_parts.join(', ')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
9
29
|
def tables_with_foreign_keys(stream)
|
10
30
|
tables_without_foreign_keys(stream)
|
11
31
|
@connection.tables.sort.each do |table|
|
@@ -25,21 +45,7 @@ module Foreigner
|
|
25
45
|
def foreign_keys(table_name, stream)
|
26
46
|
if (foreign_keys = @connection.foreign_keys(table_name)).any?
|
27
47
|
add_foreign_key_statements = foreign_keys.map do |foreign_key|
|
28
|
-
|
29
|
-
statement_parts << foreign_key.to_table.inspect
|
30
|
-
statement_parts << (':name => ' + foreign_key.options[:name].inspect)
|
31
|
-
|
32
|
-
if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
|
33
|
-
statement_parts << (':column => ' + foreign_key.options[:column].inspect)
|
34
|
-
end
|
35
|
-
if foreign_key.options[:primary_key] != 'id'
|
36
|
-
statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
|
37
|
-
end
|
38
|
-
if foreign_key.options[:dependent].present?
|
39
|
-
statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
|
40
|
-
end
|
41
|
-
|
42
|
-
' ' + statement_parts.join(', ')
|
48
|
+
' ' + self.class.dump_foreign_key(foreign_key)
|
43
49
|
end
|
44
50
|
|
45
51
|
stream.puts add_foreign_key_statements.sort.join("\n")
|
@@ -9,38 +9,48 @@ class Foreigner::SchemaDumperTest < Foreigner::UnitTest
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class MockSchemaDumper
|
12
|
-
cattr_accessor :ignore_tables
|
13
|
-
@@ignore_table = []
|
14
|
-
@@processed_tables = []
|
12
|
+
cattr_accessor :ignore_tables
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
attr_accessor :processed_tables
|
15
|
+
def initialize
|
16
|
+
@connection = MockConnection.new
|
17
|
+
@processed_tables = []
|
20
18
|
end
|
21
19
|
|
22
|
-
|
20
|
+
def tables(stream)
|
21
|
+
end
|
23
22
|
|
24
|
-
# override this method so we don't have to mock up
|
25
|
-
# all of the necessary scafolding for things to work
|
26
23
|
def foreign_keys(table, stream)
|
27
24
|
processed_tables << table
|
28
25
|
end
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
include Foreigner::SchemaDumper
|
28
|
+
end
|
29
|
+
|
30
|
+
test 'dump_foreign_key' do
|
31
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", :name => \"lulz\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'bar_id', primary_key: 'id', name: 'lulz')
|
32
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", :name => \"lulz\", :primary_key => \"uuid\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'bar_id', primary_key: 'uuid', name: 'lulz')
|
33
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", :name => \"lulz\", :dependent => :delete", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'bar_id', primary_key: 'id', name: 'lulz', dependent: :delete)
|
34
|
+
assert_dump "add_foreign_key \"foos\", \"bars\", :name => \"lulz\", :column => \"mamma_id\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('foos', 'bars', column: 'mamma_id', primary_key: 'id', name: 'lulz')
|
34
35
|
end
|
35
36
|
|
36
37
|
test 'all tables' do
|
37
38
|
MockSchemaDumper.ignore_tables = []
|
38
|
-
|
39
|
+
dumper = MockSchemaDumper.new
|
40
|
+
dumper.tables(StringIO.new)
|
41
|
+
assert_equal ['bar', 'foo'].to_set, dumper.processed_tables.to_set
|
39
42
|
end
|
40
43
|
|
41
44
|
test 'ignores tables' do
|
42
45
|
MockSchemaDumper.ignore_tables = ['foo']
|
43
|
-
|
46
|
+
dumper = MockSchemaDumper.new
|
47
|
+
dumper.tables(StringIO.new)
|
48
|
+
assert_equal ['bar'].to_set, dumper.processed_tables.to_set
|
44
49
|
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def assert_dump(expected, definition)
|
53
|
+
assert_equal expected, MockSchemaDumper.dump_foreign_key(definition)
|
54
|
+
end
|
45
55
|
end
|
46
56
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreigner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70302659050020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70302659050020
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
requirement: &
|
27
|
+
requirement: &70302659074460 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 3.1.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70302659074460
|
36
36
|
description: Adds helpers to migrations and dumps foreign keys to schema.rb
|
37
37
|
email: developer@matthewhiggins.com
|
38
38
|
executables: []
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- Rakefile
|
45
45
|
- README.rdoc
|
46
46
|
- lib/foreigner/adapter.rb
|
47
|
+
- lib/foreigner/connection_adapters/abstract/foreign_key_definition.rb
|
47
48
|
- lib/foreigner/connection_adapters/abstract/schema_definitions.rb
|
48
49
|
- lib/foreigner/connection_adapters/abstract/schema_statements.rb
|
49
50
|
- lib/foreigner/connection_adapters/mysql2_adapter.rb
|