foreigner 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +6 -11
- data/lib/foreigner.rb +4 -1
- data/lib/foreigner/adapter.rb +1 -1
- data/lib/foreigner/connection_adapters/abstract/schema_definitions.rb +1 -1
- data/lib/foreigner/connection_adapters/mysql2_adapter.rb +6 -0
- data/lib/foreigner/connection_adapters/mysql_adapter.rb +1 -0
- data/lib/foreigner/connection_adapters/postgresql_adapter.rb +8 -1
- data/lib/foreigner/connection_adapters/sql2003.rb +1 -1
- data/lib/foreigner/migration/command_recorder.rb +2 -4
- data/test/helper.rb +9 -4
- data/test/unit/mysql2_adapter_test.rb +20 -3
- data/test/unit/mysql_adapter_test.rb +10 -0
- data/test/unit/postgresql_adapter_test.rb +17 -0
- metadata +39 -34
data/Rakefile
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
require 'rake'
|
2
|
-
|
3
|
-
require '
|
2
|
+
# begin
|
3
|
+
# require 'bundler/setup'
|
4
|
+
# rescue LoadError
|
5
|
+
# puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
# end
|
4
7
|
|
5
8
|
desc 'Default: run unit tests.'
|
6
9
|
task :default => :test
|
7
10
|
|
11
|
+
require 'rake/testtask'
|
8
12
|
desc 'Test the foreigner plugin.'
|
9
13
|
Rake::TestTask.new(:test) do |t|
|
10
14
|
t.libs << 'lib'
|
@@ -12,12 +16,3 @@ Rake::TestTask.new(:test) do |t|
|
|
12
16
|
t.pattern = 'test/**/*_test.rb'
|
13
17
|
t.verbose = true
|
14
18
|
end
|
15
|
-
|
16
|
-
desc 'Generate documentation for the foreigner plugin.'
|
17
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
-
rdoc.rdoc_dir = 'rdoc'
|
19
|
-
rdoc.title = 'Foreigner'
|
20
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
-
rdoc.rdoc_files.include('README')
|
22
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
-
end
|
data/lib/foreigner.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
1
3
|
module Foreigner
|
2
4
|
extend ActiveSupport::Autoload
|
3
5
|
autoload :Adapter
|
@@ -18,7 +20,8 @@ module Foreigner
|
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
23
|
+
Foreigner::Adapter.register 'mysql', 'foreigner/connection_adapters/mysql_adapter'
|
21
24
|
Foreigner::Adapter.register 'mysql2', 'foreigner/connection_adapters/mysql2_adapter'
|
22
25
|
Foreigner::Adapter.register 'postgresql', 'foreigner/connection_adapters/postgresql_adapter'
|
23
26
|
|
24
|
-
require 'foreigner/railtie'
|
27
|
+
require 'foreigner/railtie' if defined?(Rails)
|
data/lib/foreigner/adapter.rb
CHANGED
@@ -2,6 +2,12 @@ module Foreigner
|
|
2
2
|
module ConnectionAdapters
|
3
3
|
module Mysql2Adapter
|
4
4
|
include Foreigner::ConnectionAdapters::Sql2003
|
5
|
+
|
6
|
+
def drop_table(table_name, options = {})
|
7
|
+
execute "SET FOREIGN_KEY_CHECKS=0"
|
8
|
+
execute "DROP TABLE #{quote_table_name(table_name)}"
|
9
|
+
execute "SET FOREIGN_KEY_CHECKS=1"
|
10
|
+
end
|
5
11
|
|
6
12
|
def remove_foreign_key_sql(table, options)
|
7
13
|
if Hash === options
|
@@ -0,0 +1 @@
|
|
1
|
+
p "WARNING: Please upgrade to mysql2. The old mysql adapter is not supported by foreigner."
|
@@ -3,6 +3,10 @@ module Foreigner
|
|
3
3
|
module PostgreSQLAdapter
|
4
4
|
include Foreigner::ConnectionAdapters::Sql2003
|
5
5
|
|
6
|
+
def drop_table(table_name, options = {})
|
7
|
+
execute "DROP TABLE #{quote_table_name(table_name)} CASCADE"
|
8
|
+
end
|
9
|
+
|
6
10
|
def foreign_keys(table_name)
|
7
11
|
fk_info = select_all %{
|
8
12
|
SELECT t2.relname AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confdeltype AS dependency
|
@@ -11,7 +15,10 @@ module Foreigner
|
|
11
15
|
JOIN pg_class t2 ON c.confrelid = t2.oid
|
12
16
|
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
|
13
17
|
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
|
14
|
-
|
18
|
+
JOIN pg_namespace t3 ON c.connamespace = t3.oid
|
19
|
+
WHERE c.contype = 'f'
|
20
|
+
AND t1.relname = '#{table_name}'
|
21
|
+
AND t3.nspname = ANY (current_schemas(false))
|
15
22
|
ORDER BY c.conname
|
16
23
|
}
|
17
24
|
|
@@ -9,7 +9,7 @@ module Foreigner
|
|
9
9
|
record(:remove_foreign_key, args)
|
10
10
|
end
|
11
11
|
|
12
|
-
def invert_add_foreign_key(args)
|
12
|
+
def invert_add_foreign_key(args)
|
13
13
|
from_table, to_table, add_options = *args
|
14
14
|
add_options ||= {}
|
15
15
|
|
@@ -25,6 +25,4 @@ module Foreigner
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
|
-
end
|
29
|
-
|
30
|
-
|
28
|
+
end
|
data/test/helper.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
|
-
require '
|
3
|
+
require 'active_record'
|
4
4
|
|
5
5
|
require File.expand_path('../../lib/foreigner', __FILE__)
|
6
6
|
|
7
|
-
Foreigner::Adapter.registered.values.each do |file_name|
|
8
|
-
|
9
|
-
end
|
7
|
+
# Foreigner::Adapter.registered.values.each do |file_name|
|
8
|
+
# require file_name
|
9
|
+
# end
|
10
10
|
|
11
11
|
module Foreigner
|
12
12
|
class UnitTest < ActiveSupport::TestCase
|
13
13
|
private
|
14
14
|
def execute(sql, name = nil)
|
15
|
+
sql_statements << sql
|
15
16
|
sql
|
16
17
|
end
|
17
18
|
|
@@ -22,6 +23,10 @@ module Foreigner
|
|
22
23
|
def quote_table_name(name)
|
23
24
|
quote_column_name(name).gsub('.', '`.`')
|
24
25
|
end
|
26
|
+
|
27
|
+
def sql_statements
|
28
|
+
@sql_statements ||= []
|
29
|
+
end
|
25
30
|
end
|
26
31
|
|
27
32
|
class IntegrationTest < ActiveSupport::TestCase
|
@@ -1,23 +1,40 @@
|
|
1
1
|
require 'helper'
|
2
|
+
require 'foreigner/connection_adapters/mysql2_adapter'
|
2
3
|
|
3
4
|
class Foreigner::Mysql2AdapterTest < Foreigner::UnitTest
|
4
5
|
include Foreigner::ConnectionAdapters::Mysql2Adapter
|
6
|
+
|
7
|
+
test 'drop_table' do
|
8
|
+
drop_table :widgets
|
9
|
+
assert_equal(
|
10
|
+
[
|
11
|
+
"SET FOREIGN_KEY_CHECKS=0",
|
12
|
+
"DROP TABLE `widgets`",
|
13
|
+
"SET FOREIGN_KEY_CHECKS=1"
|
14
|
+
],
|
15
|
+
sql_statements
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'drop_table with stupid options hash' do
|
20
|
+
drop_table :widgets, {}
|
21
|
+
end
|
5
22
|
|
6
|
-
test '
|
23
|
+
test 'remove_foreign_key_sql by table' do
|
7
24
|
assert_equal(
|
8
25
|
"DROP FOREIGN KEY `suppliers_company_id_fk`",
|
9
26
|
remove_foreign_key_sql(:suppliers, :companies)
|
10
27
|
)
|
11
28
|
end
|
12
29
|
|
13
|
-
test '
|
30
|
+
test 'remove_foreign_key_sql by name' do
|
14
31
|
assert_equal(
|
15
32
|
"DROP FOREIGN KEY `belongs_to_supplier`",
|
16
33
|
remove_foreign_key_sql(:suppliers, :name => "belongs_to_supplier")
|
17
34
|
)
|
18
35
|
end
|
19
36
|
|
20
|
-
test '
|
37
|
+
test 'remove_foreign_key_sql by column' do
|
21
38
|
assert_equal(
|
22
39
|
"DROP FOREIGN KEY `suppliers_ship_to_id_fk`",
|
23
40
|
remove_foreign_key_sql(:suppliers, :column => "ship_to_id")
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Foreigner::MysqlAdapterTest < Foreigner::UnitTest
|
4
|
+
test 'warning' do
|
5
|
+
output = capture(:stdout) do
|
6
|
+
require 'foreigner/connection_adapters/mysql_adapter'
|
7
|
+
end
|
8
|
+
assert_match /WARNING: Please upgrade to mysql2. The old mysql adapter is not supported by foreigner./, output
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'foreigner/connection_adapters/postgresql_adapter'
|
3
|
+
|
4
|
+
class Foreigner::PostgreSQLAdapterTest < Foreigner::UnitTest
|
5
|
+
include Foreigner::ConnectionAdapters::PostgreSQLAdapter
|
6
|
+
|
7
|
+
test 'drop_table' do
|
8
|
+
assert_equal(
|
9
|
+
"DROP TABLE `widgets` CASCADE",
|
10
|
+
drop_table(:widgets)
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'drop_table with stupid options hash' do
|
15
|
+
drop_table :widgets, {}
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,38 +1,45 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreigner
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
4
5
|
prerelease:
|
5
|
-
version: 1.0.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Matthew Higgins
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-07-29 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: activerecord
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2162120540 !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
24
21
|
version: 3.0.0
|
25
22
|
type: :runtime
|
26
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2162120540
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &2162120060 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.1.0.rc5
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2162120060
|
27
36
|
description: Adds helpers to migrations and dumps foreign keys to schema.rb
|
28
37
|
email: developer@matthewhiggins.com
|
29
38
|
executables: []
|
30
|
-
|
31
39
|
extensions: []
|
32
|
-
|
33
|
-
extra_rdoc_files:
|
40
|
+
extra_rdoc_files:
|
34
41
|
- README.rdoc
|
35
|
-
files:
|
42
|
+
files:
|
36
43
|
- MIT-LICENSE
|
37
44
|
- Rakefile
|
38
45
|
- README.rdoc
|
@@ -40,6 +47,7 @@ files:
|
|
40
47
|
- lib/foreigner/connection_adapters/abstract/schema_definitions.rb
|
41
48
|
- lib/foreigner/connection_adapters/abstract/schema_statements.rb
|
42
49
|
- lib/foreigner/connection_adapters/mysql2_adapter.rb
|
50
|
+
- lib/foreigner/connection_adapters/mysql_adapter.rb
|
43
51
|
- lib/foreigner/connection_adapters/postgresql_adapter.rb
|
44
52
|
- lib/foreigner/connection_adapters/sql2003.rb
|
45
53
|
- lib/foreigner/migration/command_recorder.rb
|
@@ -49,34 +57,31 @@ files:
|
|
49
57
|
- test/helper.rb
|
50
58
|
- test/unit/command_recorder_test.rb
|
51
59
|
- test/unit/mysql2_adapter_test.rb
|
60
|
+
- test/unit/mysql_adapter_test.rb
|
61
|
+
- test/unit/postgresql_adapter_test.rb
|
52
62
|
- test/unit/sql2003_test.rb
|
53
|
-
has_rdoc: true
|
54
63
|
homepage: http://github.com/matthuhiggins/foreigner
|
55
64
|
licenses: []
|
56
|
-
|
57
65
|
post_install_message:
|
58
66
|
rdoc_options: []
|
59
|
-
|
60
|
-
require_paths:
|
67
|
+
require_paths:
|
61
68
|
- lib
|
62
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
70
|
none: false
|
64
|
-
requirements:
|
65
|
-
- -
|
66
|
-
- !ruby/object:Gem::Version
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
67
74
|
version: 1.8.7
|
68
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
76
|
none: false
|
70
|
-
requirements:
|
71
|
-
- -
|
72
|
-
- !ruby/object:Gem::Version
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
73
80
|
version: 1.3.5
|
74
81
|
requirements: []
|
75
|
-
|
76
82
|
rubyforge_project: foreigner
|
77
|
-
rubygems_version: 1.
|
83
|
+
rubygems_version: 1.8.5
|
78
84
|
signing_key:
|
79
85
|
specification_version: 3
|
80
86
|
summary: Foreign Keys for Rails
|
81
87
|
test_files: []
|
82
|
-
|