foreigner 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/foreigner.rb CHANGED
@@ -22,6 +22,7 @@ end
22
22
 
23
23
  Foreigner::Adapter.register 'mysql', 'foreigner/connection_adapters/mysql_adapter'
24
24
  Foreigner::Adapter.register 'mysql2', 'foreigner/connection_adapters/mysql2_adapter'
25
+ Foreigner::Adapter.register 'jdbcmysql', 'foreigner/connection_adapters/mysql2_adapter'
25
26
  Foreigner::Adapter.register 'postgresql', 'foreigner/connection_adapters/postgresql_adapter'
26
27
 
27
- require 'foreigner/railtie' if defined?(Rails)
28
+ require 'foreigner/railtie' if defined?(Rails)
@@ -6,7 +6,12 @@ module Foreigner
6
6
  end
7
7
 
8
8
  def drop_table(*args)
9
- disable_referential_integrity { super }
9
+ options = args.extract_options!
10
+ if options[:force]
11
+ disable_referential_integrity { super }
12
+ else
13
+ super
14
+ end
10
15
  end
11
16
 
12
17
  def add_foreign_key(from_table, to_table, options = {})
@@ -5,14 +5,22 @@ module Foreigner
5
5
  included do
6
6
  alias_method_chain :tables, :foreign_keys
7
7
  end
8
-
8
+
9
9
  def tables_with_foreign_keys(stream)
10
10
  tables_without_foreign_keys(stream)
11
11
  @connection.tables.sort.each do |table|
12
+ next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
13
+ case ignored
14
+ when String; table == ignored
15
+ when Regexp; table =~ ignored
16
+ else
17
+ raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
18
+ end
19
+ end
12
20
  foreign_keys(table, stream)
13
21
  end
14
22
  end
15
-
23
+
16
24
  private
17
25
  def foreign_keys(table_name, stream)
18
26
  if (foreign_keys = @connection.foreign_keys(table_name)).any?
@@ -20,7 +28,7 @@ module Foreigner
20
28
  statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
21
29
  statement_parts << foreign_key.to_table.inspect
22
30
  statement_parts << (':name => ' + foreign_key.options[:name].inspect)
23
-
31
+
24
32
  if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
25
33
  statement_parts << (':column => ' + foreign_key.options[:column].inspect)
26
34
  end
@@ -39,4 +47,4 @@ module Foreigner
39
47
  end
40
48
  end
41
49
  end
42
- end
50
+ end
data/test/helper.rb CHANGED
@@ -1,32 +1,48 @@
1
- require 'rubygems'
1
+ require 'bundler/setup'
2
+ Bundler.require(:default)
3
+
2
4
  require 'test/unit'
3
5
  require 'active_record'
4
6
 
5
- require File.expand_path('../../lib/foreigner', __FILE__)
6
-
7
7
  # Foreigner::Adapter.registered.values.each do |file_name|
8
8
  # require file_name
9
9
  # end
10
10
 
11
- module Foreigner
12
- class UnitTest < ActiveSupport::TestCase
13
- private
14
- def execute(sql, name = nil)
15
- sql_statements << sql
16
- sql
17
- end
11
+ module TestAdapterMethods
12
+ def execute(sql, name = nil)
13
+ sql_statements << sql
14
+ sql
15
+ end
16
+
17
+ def quote_table_name(name)
18
+ quote_column_name(name).gsub('.', '`.`')
19
+ end
18
20
 
19
- def quote_column_name(name)
20
- "`#{name}`"
21
- end
21
+ def quote_column_name(name)
22
+ "`#{name}`"
23
+ end
22
24
 
23
- def quote_table_name(name)
24
- quote_column_name(name).gsub('.', '`.`')
25
- end
25
+ def sql_statements
26
+ @sql_statements ||= []
27
+ end
26
28
 
27
- def sql_statements
28
- @sql_statements ||= []
29
- end
29
+ def drop_table(name, options = {})
30
+ end
31
+
32
+ def disable_referential_integrity
33
+ @disable_referential_integrity = true
34
+ yield
35
+ end
36
+
37
+ private
38
+ def execute(sql, name = nil)
39
+ sql_statements << sql
40
+ sql
41
+ end
42
+ end
43
+
44
+ module Foreigner
45
+ class UnitTest < ActiveSupport::TestCase
30
46
  end
31
47
 
32
48
  class IntegrationTest < ActiveSupport::TestCase
@@ -2,26 +2,33 @@ require 'helper'
2
2
  require 'foreigner/connection_adapters/mysql2_adapter'
3
3
 
4
4
  class Foreigner::Mysql2AdapterTest < Foreigner::UnitTest
5
- include Foreigner::ConnectionAdapters::Mysql2Adapter
5
+ class Mysql2Adapter
6
+ include TestAdapterMethods
7
+ include Foreigner::ConnectionAdapters::Mysql2Adapter
8
+ end
9
+
10
+ setup do
11
+ @adapter = Mysql2Adapter.new
12
+ end
6
13
 
7
14
  test 'remove_foreign_key_sql by table' do
8
15
  assert_equal(
9
16
  "DROP FOREIGN KEY `suppliers_company_id_fk`",
10
- remove_foreign_key_sql(:suppliers, :companies)
17
+ @adapter.remove_foreign_key_sql(:suppliers, :companies)
11
18
  )
12
19
  end
13
20
 
14
21
  test 'remove_foreign_key_sql by name' do
15
22
  assert_equal(
16
23
  "DROP FOREIGN KEY `belongs_to_supplier`",
17
- remove_foreign_key_sql(:suppliers, :name => "belongs_to_supplier")
24
+ @adapter.remove_foreign_key_sql(:suppliers, :name => "belongs_to_supplier")
18
25
  )
19
26
  end
20
27
 
21
28
  test 'remove_foreign_key_sql by column' do
22
29
  assert_equal(
23
30
  "DROP FOREIGN KEY `suppliers_ship_to_id_fk`",
24
- remove_foreign_key_sql(:suppliers, :column => "ship_to_id")
31
+ @adapter.remove_foreign_key_sql(:suppliers, :column => "ship_to_id")
25
32
  )
26
33
  end
27
34
  end
@@ -0,0 +1,46 @@
1
+ require 'helper'
2
+
3
+ class Foreigner::SchemaDumperTest < Foreigner::UnitTest
4
+
5
+ class MockConnection
6
+ def tables
7
+ [ 'foo', 'bar' ]
8
+ end
9
+ end
10
+
11
+ class MockSchemaDumper
12
+ cattr_accessor :ignore_tables, :processed_tables
13
+ @@ignore_table = []
14
+ @@processed_tables = []
15
+
16
+ @connection = MockConnection.new
17
+
18
+ # need this here so ActiveRecord::Concern has something to redefine
19
+ def tables
20
+ end
21
+
22
+ include Foreigner::SchemaDumper
23
+
24
+ # override this method so we don't have to mock up
25
+ # all of the necessary scafolding for things to work
26
+ def foreign_keys(table, stream)
27
+ processed_tables << table
28
+ end
29
+
30
+ def tables(ignore_list)
31
+ ignore_tables = ignore_list
32
+ processed_table = nil
33
+ end
34
+ end
35
+
36
+ test 'all tables' do
37
+ MockSchemaDumper.ignore_tables = []
38
+ assert MockSchemaDumper.new.processed_tables.sort.to_s, "['bar', 'foo']"
39
+ end
40
+
41
+ test 'ignores tables' do
42
+ MockSchemaDumper.ignore_tables = ['foo']
43
+ assert MockSchemaDumper.new.processed_tables.to_s, "['bar']"
44
+ end
45
+ end
46
+
@@ -1,33 +1,50 @@
1
1
  require 'helper'
2
2
 
3
3
  class Foreigner::Sql2003Test < Foreigner::UnitTest
4
- include Foreigner::ConnectionAdapters::Sql2003
4
+ class TestAdapter
5
+ include TestAdapterMethods
6
+ include Foreigner::ConnectionAdapters::Sql2003
7
+ end
8
+
9
+ setup do
10
+ @adapter = TestAdapter.new
11
+ end
12
+
13
+ test 'drop_table without force' do
14
+ @adapter.drop_table 'shoes'
15
+ assert !@adapter.instance_variable_get(:@disable_referential_integrity)
16
+ end
17
+
18
+ test 'drop_table with force' do
19
+ @adapter.drop_table 'shoes', force: true
20
+ assert @adapter.instance_variable_get(:@disable_referential_integrity)
21
+ end
5
22
 
6
23
  test 'add_without_options' do
7
24
  assert_equal(
8
25
  "ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
9
- add_foreign_key(:employees, :companies)
26
+ @adapter.add_foreign_key(:employees, :companies)
10
27
  )
11
28
  end
12
29
 
13
30
  test 'add_with_name' do
14
31
  assert_equal(
15
32
  "ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
16
- add_foreign_key(:employees, :companies, :name => 'favorite_company_fk')
33
+ @adapter.add_foreign_key(:employees, :companies, :name => 'favorite_company_fk')
17
34
  )
18
35
  end
19
36
 
20
37
  test 'add_with_column' do
21
38
  assert_equal(
22
39
  "ALTER TABLE `employees` ADD CONSTRAINT `employees_last_employer_id_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
23
- add_foreign_key(:employees, :companies, :column => 'last_employer_id')
40
+ @adapter.add_foreign_key(:employees, :companies, :column => 'last_employer_id')
24
41
  )
25
42
  end
26
43
 
27
44
  test 'add_with_column_and_name' do
28
45
  assert_equal(
29
46
  "ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
30
- add_foreign_key(:employees, :companies, :column => 'last_employer_id', :name => 'favorite_company_fk')
47
+ @adapter.add_foreign_key(:employees, :companies, :column => 'last_employer_id', :name => 'favorite_company_fk')
31
48
  )
32
49
  end
33
50
 
@@ -35,7 +52,7 @@ class Foreigner::Sql2003Test < Foreigner::UnitTest
35
52
  assert_equal(
36
53
  "ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
37
54
  "ON DELETE CASCADE",
38
- add_foreign_key(:employees, :companies, :dependent => :delete)
55
+ @adapter.add_foreign_key(:employees, :companies, :dependent => :delete)
39
56
  )
40
57
  end
41
58
 
@@ -43,7 +60,7 @@ class Foreigner::Sql2003Test < Foreigner::UnitTest
43
60
  assert_equal(
44
61
  "ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
45
62
  "ON DELETE SET NULL",
46
- add_foreign_key(:employees, :companies, :dependent => :nullify)
63
+ @adapter.add_foreign_key(:employees, :companies, :dependent => :nullify)
47
64
  )
48
65
  end
49
66
 
@@ -51,7 +68,7 @@ class Foreigner::Sql2003Test < Foreigner::UnitTest
51
68
  assert_equal(
52
69
  "ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
53
70
  "ON DELETE RESTRICT",
54
- add_foreign_key(:employees, :companies, :dependent => :restrict)
71
+ @adapter.add_foreign_key(:employees, :companies, :dependent => :restrict)
55
72
  )
56
73
  end
57
74
 
@@ -59,28 +76,28 @@ class Foreigner::Sql2003Test < Foreigner::UnitTest
59
76
  assert_equal(
60
77
  "ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
61
78
  "on delete foo",
62
- add_foreign_key(:employees, :companies, :options => 'on delete foo')
79
+ @adapter.add_foreign_key(:employees, :companies, :options => 'on delete foo')
63
80
  )
64
81
  end
65
82
 
66
83
  test 'remove_by_table' do
67
84
  assert_equal(
68
85
  "ALTER TABLE `suppliers` DROP CONSTRAINT `suppliers_company_id_fk`",
69
- remove_foreign_key(:suppliers, :companies)
86
+ @adapter.remove_foreign_key(:suppliers, :companies)
70
87
  )
71
88
  end
72
89
 
73
90
  test 'remove_by_name' do
74
91
  assert_equal(
75
92
  "ALTER TABLE `suppliers` DROP CONSTRAINT `belongs_to_supplier`",
76
- remove_foreign_key(:suppliers, :name => "belongs_to_supplier")
93
+ @adapter.remove_foreign_key(:suppliers, :name => "belongs_to_supplier")
77
94
  )
78
95
  end
79
96
 
80
97
  test 'remove_by_column' do
81
98
  assert_equal(
82
99
  "ALTER TABLE `suppliers` DROP CONSTRAINT `suppliers_ship_to_id_fk`",
83
- remove_foreign_key(:suppliers, :column => "ship_to_id")
100
+ @adapter.remove_foreign_key(:suppliers, :column => "ship_to_id")
84
101
  )
85
102
  end
86
103
  end
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.1
4
+ version: 1.1.2
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: 2011-08-09 00:00:00.000000000Z
12
+ date: 2012-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2161584820 !ruby/object:Gem::Requirement
16
+ requirement: &70097178167620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,18 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2161584820
24
+ version_requirements: *70097178167620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &2161584240 !ruby/object:Gem::Requirement
27
+ requirement: &70097178167160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
- version: 3.1.0.rc5
32
+ version: 3.1.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2161584240
35
+ version_requirements: *70097178167160
36
36
  description: Adds helpers to migrations and dumps foreign keys to schema.rb
37
37
  email: developer@matthewhiggins.com
38
38
  executables: []
@@ -59,6 +59,7 @@ files:
59
59
  - test/unit/mysql2_adapter_test.rb
60
60
  - test/unit/mysql_adapter_test.rb
61
61
  - test/unit/postgresql_adapter_test.rb
62
+ - test/unit/schema_dumper_test.rb
62
63
  - test/unit/sql2003_test.rb
63
64
  homepage: http://github.com/matthuhiggins/foreigner
64
65
  licenses: []
@@ -80,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  version: 1.3.5
81
82
  requirements: []
82
83
  rubyforge_project: foreigner
83
- rubygems_version: 1.8.5
84
+ rubygems_version: 1.8.10
84
85
  signing_key:
85
86
  specification_version: 3
86
87
  summary: Foreign Keys for Rails