sparkfly-foreigner 0.5.4
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/.gitignore +3 -0
- data/MIT-LICENSE +21 -0
- data/README.textile +105 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/lib/foreigner.rb +58 -0
- data/lib/foreigner/connection_adapters/abstract/schema_definitions.rb +154 -0
- data/lib/foreigner/connection_adapters/abstract/schema_statements.rb +75 -0
- data/lib/foreigner/connection_adapters/mysql_adapter.rb +48 -0
- data/lib/foreigner/connection_adapters/postgresql_adapter.rb +47 -0
- data/lib/foreigner/connection_adapters/sqlite3_adapter.rb +46 -0
- data/lib/foreigner/schema_dumper.rb +47 -0
- data/lib/foreigner/semantics/sql_2003.rb +78 -0
- data/spec/adapter_helper.rb +100 -0
- data/spec/factory_helper.rb +70 -0
- data/spec/mysql/schema_extractor_spec.rb +143 -0
- data/spec/mysql/schema_spec.rb +87 -0
- data/spec/mysql/semantics_spec.rb +75 -0
- data/spec/postgresql/schema_extractor_spec.rb +143 -0
- data/spec/postgresql/schema_spec.rb +152 -0
- data/spec/postgresql/semantics_spec.rb +75 -0
- data/spec/schema_dumper_spec.rb +96 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/sqlite3/schema_spec.rb +86 -0
- data/tasks/foreigner_tasks.rake +4 -0
- metadata +96 -0
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.expand_path('../spec_helper.rb', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Foreigner::ConnectionAdapters::PostgreSQLAdapter do
|
4
|
+
include MigrationFactory
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@adapter = AdapterHelper::PostgreSQLTestAdapter.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'when adding foreign keys' do
|
11
|
+
it 'should add foreign key without options' do
|
12
|
+
@adapter.add_foreign_key(:employees, :companies).should eql(
|
13
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_company_id` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)"
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should add foreign key with a name' do
|
18
|
+
@name = 'favorite_company_fk'
|
19
|
+
@adapter.add_foreign_key(:employees, :companies, :name => @name).should eql(
|
20
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `#{@name}` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)"
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should add foreign key with a column' do
|
25
|
+
@column = 'last_employer_id'
|
26
|
+
@adapter.add_foreign_key(:employees, :companies, :column => @column).should eql(
|
27
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_last_employer_id` FOREIGN KEY (`#{@column}`) REFERENCES `companies`(id)"
|
28
|
+
)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should add foreign key with column and a name' do
|
33
|
+
@name = 'favorite_company_fk'
|
34
|
+
@column = 'last_employer_id'
|
35
|
+
@adapter.add_foreign_key(:employees, :companies, :column => @column, :name => @name).should eql(
|
36
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `#{@name}` FOREIGN KEY (`#{@column}`) REFERENCES `companies`(id)"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should add foreign key with :dependent => :delete' do
|
41
|
+
@adapter.add_foreign_key(:employees, :companies, :dependent => :delete).should eql(
|
42
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_company_id` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
43
|
+
"ON DELETE CASCADE")
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should add foreign key with :dependent => :nullify' do
|
47
|
+
@adapter.add_foreign_key(:employees, :companies, :dependent => :nullify).should eql(
|
48
|
+
"ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_company_id` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
|
49
|
+
"ON DELETE SET NULL"
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when dropping foreign keys' do
|
55
|
+
it 'should drop foreign key' do
|
56
|
+
@adapter.remove_foreign_key(:suppliers, :companies).should eql(
|
57
|
+
"ALTER TABLE `suppliers` DROP CONSTRAINT `fk_suppliers_company_id`"
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should drop foreign key by name' do
|
62
|
+
@adapter.remove_foreign_key(:suppliers, :name => "belongs_to_supplier").should eql(
|
63
|
+
"ALTER TABLE `suppliers` DROP CONSTRAINT `belongs_to_supplier`"
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should drop foreign key by column' do
|
68
|
+
@adapter.remove_foreign_key(:suppliers, :column => "ship_to_id").should eql(
|
69
|
+
"ALTER TABLE `suppliers` DROP CONSTRAINT `fk_suppliers_ship_to_id`"
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require File.expand_path('spec_helper.rb', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class SchemaDumperTester
|
4
|
+
|
5
|
+
def tables(stream)
|
6
|
+
stream
|
7
|
+
end
|
8
|
+
|
9
|
+
include Foreigner::SchemaDumper
|
10
|
+
include ForeignKeyDefinitionFactory
|
11
|
+
|
12
|
+
def valid_foreign_key_definition(opt = {})
|
13
|
+
options = {
|
14
|
+
:from_table => 'items',
|
15
|
+
:to_table => 'collections'
|
16
|
+
}.merge(opt)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Helper when we want to test a single generated statement
|
20
|
+
def generate_schema_statement(foreign_key_definition)
|
21
|
+
fkd = new_foreign_key(foreign_key_definition)
|
22
|
+
generate_foreign_keys_statements([fkd]).first
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe Foreigner::SchemaDumper do
|
27
|
+
|
28
|
+
before(:each) do
|
29
|
+
@dumper = SchemaDumperTester.new
|
30
|
+
@fk_definition = {
|
31
|
+
:from_table => 'items',
|
32
|
+
:to_table => 'collections'
|
33
|
+
}
|
34
|
+
|
35
|
+
# Sanity Check
|
36
|
+
fkd = @dumper.new_foreign_key(@fk_definition)
|
37
|
+
fkd.from_table.should eql('items')
|
38
|
+
fkd.to_table.should eql('collections')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should generate an add_foreign_key' do
|
42
|
+
@dumper.generate_schema_statement(@fk_definition).should match(/\s*add_foreign_key\s+:items,\s:collections/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should generate with a custom foreign key name' do
|
46
|
+
@foreign_key_name = 'fk_foreign_key_name'
|
47
|
+
@dumper.generate_schema_statement(:name => @foreign_key_name).should match(
|
48
|
+
/\s*add_foreign_key\s+:items,\s+:collections,\s+:name\s+=>\s+\"#{@foreign_key_name}\"/
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should generate with a custom column id' do
|
53
|
+
@column = 'acctno'
|
54
|
+
@dumper.generate_schema_statement(:column => @column).should match(
|
55
|
+
/\s*add_foreign_key\s+:items,\s+:collections,\s+:column\s+=>\s+\"#{@column}\"/
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should ignore custom column id conforming to Rails convention' do
|
60
|
+
@to_table = 'collections'
|
61
|
+
@column = @to_table.to_s.singularize + '_id'
|
62
|
+
@dumper.generate_schema_statement(:to_table => @to_table, :column => @column).should match(
|
63
|
+
/\s*add_foreign_key\s+:items,\s+#{@to_table.to_sym.inspect}/
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should generate with a custom primary key' do
|
68
|
+
@primary_key = 'upc'
|
69
|
+
@dumper.generate_schema_statement(:primary_key => @primary_key).should match(
|
70
|
+
/\s*add_foreign_key\s+:items,\s+:collections,\s+:primary_key\s+=>\s+\"#{@primary_key}\"/
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should ignore custom primary key conforming to Rails convention' do
|
75
|
+
@primary_key = 'id'
|
76
|
+
@dumper.generate_schema_statement(:primary_key => @primary_key).should match(
|
77
|
+
/\s*add_foreign_key\s+:items,\s+:collections/
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should generate with a :dependent => :nullify' do
|
82
|
+
@dependent = :nullify
|
83
|
+
@dumper.generate_schema_statement(:dependent => @dependent).should match(
|
84
|
+
/\s*add_foreign_key\s+:items,\s+:collections,\s+:dependent\s+=>\s+#{@dependent.inspect}/
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should generate with a :dependent => :delete' do
|
89
|
+
@dependent = :delete
|
90
|
+
@dumper.generate_schema_statement(:dependent => @dependent).should match(
|
91
|
+
/\s*add_foreign_key\s+:items,\s+:collections,\s+:dependent\s+=>\s+#{@dependent.inspect}/
|
92
|
+
)
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
7
|
+
require 'active_record/connection_adapters/sqlite3_adapter'
|
8
|
+
require 'active_record/connection_adapters/mysql_adapter'
|
9
|
+
require 'foreigner'
|
10
|
+
require "foreigner/connection_adapters/postgresql_adapter"
|
11
|
+
require "foreigner/connection_adapters/mysql_adapter"
|
12
|
+
require "foreigner/connection_adapters/sqlite3_adapter"
|
13
|
+
|
14
|
+
require File.expand_path('factory_helper.rb', File.dirname(__FILE__))
|
15
|
+
require File.expand_path('adapter_helper.rb', File.dirname(__FILE__))
|
16
|
+
|
17
|
+
CONFIGURATIONS = {
|
18
|
+
:postgresql => {
|
19
|
+
:adapter => "postgresql",
|
20
|
+
:username => "root",
|
21
|
+
:password => "",
|
22
|
+
:database => "test_foreigner_gem",
|
23
|
+
:min_messages => "ERROR"
|
24
|
+
},
|
25
|
+
:postgresql_admin => {
|
26
|
+
:adapter => "postgresql",
|
27
|
+
:username => "root",
|
28
|
+
:password => "",
|
29
|
+
:database => "test",
|
30
|
+
:min_messages => "ERROR"
|
31
|
+
}, # :postgresql_admin is used to connect in; :postgresql is used to actually test the migrations
|
32
|
+
:mysql => {
|
33
|
+
:adapter => 'mysql',
|
34
|
+
:host => 'localhost',
|
35
|
+
:username => 'root',
|
36
|
+
:database => 'foreigner_test'
|
37
|
+
|
38
|
+
},
|
39
|
+
:sqlite3 => {
|
40
|
+
:adapter => "sqlite3",
|
41
|
+
:database => ":memory:"
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
# Turn this on for debugging
|
46
|
+
ActiveRecord::Migration.verbose = false
|
47
|
+
|
48
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require File.expand_path('../spec_helper.rb', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Foreigner::ConnectionAdapters::SQLite3Adapter do
|
4
|
+
include MigrationFactory
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@adapter = AdapterHelper::SQLite3TestAdapter.new
|
8
|
+
@adapter.recreate_test_environment
|
9
|
+
@adapter.schema(:items).should be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'when creating tables with t.foreign_key' do
|
13
|
+
it 'should understand t.foreign_key' do
|
14
|
+
create_table :items do |t|
|
15
|
+
t.string :name
|
16
|
+
t.references :collection, :null => false
|
17
|
+
t.foreign_key :collection
|
18
|
+
end
|
19
|
+
|
20
|
+
@adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\)/)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should accept a :column parameter' do
|
24
|
+
@column = :item_collection_id
|
25
|
+
|
26
|
+
create_table :items do |t|
|
27
|
+
t.string :name
|
28
|
+
t.integer @column
|
29
|
+
t.foreign_key :collection, :column => @column
|
30
|
+
end
|
31
|
+
|
32
|
+
@adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"#{@column}\"\) REFERENCES \"collections\"\s*\(id\)/)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should accept :dependent => :nullify' do
|
36
|
+
create_table :items do |t|
|
37
|
+
t.string :name
|
38
|
+
t.references :collection
|
39
|
+
t.foreign_key :collection, :dependent => :nullify
|
40
|
+
end
|
41
|
+
|
42
|
+
@adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE SET NULL/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should accept :dependent => :delete' do
|
46
|
+
create_table :items do |t|
|
47
|
+
t.string :name
|
48
|
+
t.references :collection
|
49
|
+
t.foreign_key :collection, :dependent => :delete
|
50
|
+
end
|
51
|
+
|
52
|
+
@adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE CASCADE/)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'when creating tables with t.reference' do
|
57
|
+
it 'should accept a t.references constraint' do
|
58
|
+
create_table :items do |t|
|
59
|
+
t.string :name
|
60
|
+
t.references :collection, :foreign_key => true
|
61
|
+
end
|
62
|
+
|
63
|
+
@adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\)/)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should accept :foreign_key => { :dependent => :nullify }' do
|
67
|
+
create_table :items do |t|
|
68
|
+
t.string :name
|
69
|
+
t.references :collection, :foreign_key => {:dependent => :nullify}
|
70
|
+
end
|
71
|
+
|
72
|
+
@adapter.schema(:items).match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE SET NULL/)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should accept :foreign_key => { :dependent => :delete }' do
|
76
|
+
create_table :items do |t|
|
77
|
+
t.string :name
|
78
|
+
t.references :collection, :foreign_key => {:dependent => :delete}
|
79
|
+
end
|
80
|
+
|
81
|
+
@adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE CASCADE/)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sparkfly-foreigner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 5
|
8
|
+
- 4
|
9
|
+
version: 0.5.4
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ho-Sheng Hsiao
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-07 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Allows you to add foreign keys to your migrations and enforce them
|
22
|
+
email: hosh@sparkfly.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.textile
|
29
|
+
files:
|
30
|
+
- .gitignore
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.textile
|
33
|
+
- Rakefile
|
34
|
+
- VERSION
|
35
|
+
- lib/foreigner.rb
|
36
|
+
- lib/foreigner/connection_adapters/abstract/schema_definitions.rb
|
37
|
+
- lib/foreigner/connection_adapters/abstract/schema_statements.rb
|
38
|
+
- lib/foreigner/connection_adapters/mysql_adapter.rb
|
39
|
+
- lib/foreigner/connection_adapters/postgresql_adapter.rb
|
40
|
+
- lib/foreigner/connection_adapters/sqlite3_adapter.rb
|
41
|
+
- lib/foreigner/schema_dumper.rb
|
42
|
+
- lib/foreigner/semantics/sql_2003.rb
|
43
|
+
- spec/adapter_helper.rb
|
44
|
+
- spec/factory_helper.rb
|
45
|
+
- spec/mysql/schema_extractor_spec.rb
|
46
|
+
- spec/mysql/schema_spec.rb
|
47
|
+
- spec/mysql/semantics_spec.rb
|
48
|
+
- spec/postgresql/schema_extractor_spec.rb
|
49
|
+
- spec/postgresql/schema_spec.rb
|
50
|
+
- spec/postgresql/semantics_spec.rb
|
51
|
+
- spec/schema_dumper_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/sqlite3/schema_spec.rb
|
54
|
+
- tasks/foreigner_tasks.rake
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/hosh/foreigner
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.6
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Foreign keys for Rails migrations for PostgreSQL, MySQL and Sqlite3. Based on dwilkie-foreigner
|
85
|
+
test_files:
|
86
|
+
- spec/schema_dumper_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/sqlite3/schema_spec.rb
|
89
|
+
- spec/postgresql/schema_extractor_spec.rb
|
90
|
+
- spec/postgresql/schema_spec.rb
|
91
|
+
- spec/postgresql/semantics_spec.rb
|
92
|
+
- spec/adapter_helper.rb
|
93
|
+
- spec/factory_helper.rb
|
94
|
+
- spec/mysql/schema_extractor_spec.rb
|
95
|
+
- spec/mysql/schema_spec.rb
|
96
|
+
- spec/mysql/semantics_spec.rb
|