rafaelp-postgresql_migrations 0.1.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/MIT-LICENSE +20 -0
- data/README.mkdn +65 -0
- data/Rakefile +22 -0
- data/config/requirements.rb +14 -0
- data/postgresql_migrations.gemspec +27 -0
- data/postgresql_migrations.rb +49 -0
- data/test/postgresql_migrations_add_foreign_key_test.rb +54 -0
- data/test/postgresql_migrations_remove_foreign_key_test.rb +30 -0
- data/test/test_helper.rb +5 -0
- metadata +77 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Rafael Lima (http://rafael.adm.br)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.mkdn
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Postgresql Migrations
|
2
|
+
|
3
|
+
## DESCRIPTION
|
4
|
+
|
5
|
+
This gem implements "add\_foreign\_key" and "remove\_foreign\_key" for PostgreSQLAdapter.
|
6
|
+
|
7
|
+
## REQUIREMENTS
|
8
|
+
|
9
|
+
* activerecord
|
10
|
+
* postgres
|
11
|
+
|
12
|
+
## INSTALLATION
|
13
|
+
|
14
|
+
$ sudo gem sources -a http://gems.github.com (you only have to do this once)
|
15
|
+
$ sudo gem install rafaelp-postgresql_migrations
|
16
|
+
|
17
|
+
## USAGE
|
18
|
+
|
19
|
+
### Example 1
|
20
|
+
|
21
|
+
require 'postgresql_migrations'
|
22
|
+
class UserBelongsToAccount < ActiveRecord::Migration
|
23
|
+
def self.up
|
24
|
+
add_foreign_key :users, :account_id, :accounts, :id
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.down
|
28
|
+
remove_foreign_key :users, :account_id, :accounts, :id
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
### Example 2
|
33
|
+
|
34
|
+
require 'postgresql_migrations'
|
35
|
+
class UserBelongsToAccount < ActiveRecord::Migration
|
36
|
+
def self.up
|
37
|
+
add_foreign_key :users, :account_id, :accounts, :id, :match => 'simple', :on_delete => 'restrict', :on_update => 'cascade'
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.down
|
41
|
+
remove_foreign_key :users, :account_id
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
## LICENSE
|
46
|
+
|
47
|
+
Postgresql Migrations is released under the MIT License.
|
48
|
+
|
49
|
+
## AUTHOR
|
50
|
+
|
51
|
+
### **Rafael Lima**
|
52
|
+
|
53
|
+
Working at [Myfreecomm](http://myfreecomm.com.br)
|
54
|
+
|
55
|
+
Blog: [http://rafael.adm.br](http://rafael.adm.br)
|
56
|
+
|
57
|
+
Podcast: [http://rafael.adm.br/voltandopracasa](http://rafael.adm.br/voltandopracasa)
|
58
|
+
|
59
|
+
Github: [http://github.com/rafaelp](http://github.com/rafaelp)
|
60
|
+
|
61
|
+
Twitter: [http://twitter.com/rafaelp](http://twitter.com/rafaelp)
|
62
|
+
|
63
|
+
### Did you like?
|
64
|
+
|
65
|
+
[Recommend me at Working With Rails](http://workingwithrails.com/recommendation/new/person/14248-rafael-lima)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the dbdesigner_migration_generator plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the dbdesigner_migration_generator plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'DbdesignerMigrationGenerator'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
%w[activerecord pg].each do |req_gem|
|
3
|
+
begin
|
4
|
+
require req_gem
|
5
|
+
rescue LoadError
|
6
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
7
|
+
puts "Installation: sudo gem install #{req_gem} -y"
|
8
|
+
exit
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
13
|
+
|
14
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
@@ -0,0 +1,27 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = "postgresql_migrations"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.date = "2009-09-18"
|
5
|
+
s.author = "Rafael Lima"
|
6
|
+
s.email = "contato@rafael.adm.br"
|
7
|
+
s.homepage = "http://rafael.adm.br"
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.summary = "This gem implements add_foreign_key and remove_foreign_key for PostgreSQLAdapter."
|
10
|
+
s.files = [
|
11
|
+
"MIT-LICENSE",
|
12
|
+
"Rakefile",
|
13
|
+
"README.mkdn",
|
14
|
+
"postgresql_migrations.gemspec",
|
15
|
+
"config/requirements.rb",
|
16
|
+
"postgresql_migrations.rb"]
|
17
|
+
s.test_files = [
|
18
|
+
"test/test_helper.rb",
|
19
|
+
"test/postgresql_migrations_add_foreign_key_test.rb",
|
20
|
+
"test/postgresql_migrations_remove_foreign_key_test.rb",
|
21
|
+
]
|
22
|
+
s.require_path = "."
|
23
|
+
s.has_rdoc = true
|
24
|
+
s.extra_rdoc_files = ["README.mkdn"]
|
25
|
+
s.add_dependency("activerecord", ["> 0.0.0"])
|
26
|
+
s.add_dependency("pg", ["> 0.0.0"])
|
27
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# PostgresqlMigrations
|
2
|
+
|
3
|
+
module PostgresqlMigrations
|
4
|
+
def add_foreign_key(from_table, from_columns, to_table, to_columns, options = {})
|
5
|
+
from_columns = [from_columns] unless from_columns.is_a? Array
|
6
|
+
to_columns = [to_columns] unless to_columns.is_a? Array
|
7
|
+
|
8
|
+
constraint_name = "fk_#{from_table}_#{from_columns.join('_')}"
|
9
|
+
|
10
|
+
sql = "ALTER TABLE #{from_table} ADD CONSTRAINT #{constraint_name} FOREIGN KEY (#{from_columns.join(',')}) REFERENCES #{to_table}(#{to_columns.join(',')})"
|
11
|
+
|
12
|
+
# MATCH
|
13
|
+
|
14
|
+
# FULL
|
15
|
+
# will not allow one column of a multicolumn foreign key to be null unless all foreign key columns are null
|
16
|
+
# PARTIAL
|
17
|
+
# is not yet implemented
|
18
|
+
# SIMPLE
|
19
|
+
# allows some foreign key columns to be null while other parts of the foreign key are not null
|
20
|
+
sql << " MATCH #{options[:match]}" unless options[:match].nil?
|
21
|
+
|
22
|
+
# ACTIONS
|
23
|
+
|
24
|
+
# NO ACTION
|
25
|
+
# Produce an error indicating that the deletion or update would create a foreign key constraint violation. If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows. This is the default action.
|
26
|
+
# RESTRICT
|
27
|
+
# Produce an error indicating that constraint_name = "fk_#{from_table}_#{from_columns.join('_')}"the deletion or update would create a foreign key constraint violation. This is the same as NO ACTION except that the check is not deferrable.
|
28
|
+
# CASCADE
|
29
|
+
# Delete any rows referencing the deleted row, or update the value of the referencing column to the new value of the referenced column, respectively.
|
30
|
+
# SET NULL
|
31
|
+
# Set the referencing column(s) to null.
|
32
|
+
# SET DEFAULT
|
33
|
+
# Set the referencing column(s) to their default values.
|
34
|
+
sql << " ON DELETE #{options[:on_delete]}" unless options[:on_delete].nil?
|
35
|
+
sql << " ON UPDATE #{options[:on_update]}" unless options[:on_update].nil?
|
36
|
+
|
37
|
+
execute sql
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove_foreign_key(from_table, from_columns)
|
41
|
+
from_columns = [from_columns] unless from_columns.is_a? Array
|
42
|
+
constraint_name = "fk_#{from_table}_#{from_columns.join('_')}"
|
43
|
+
|
44
|
+
sql = "ALTER TABLE #{from_table} DROP CONSTRAINT #{constraint_name}"
|
45
|
+
execute sql
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgresqlMigrations
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestPostgresqlMigrationsAddForeignKey < Test::Unit::TestCase
|
4
|
+
include PostgresqlMigrations
|
5
|
+
|
6
|
+
def test_simple_call_with_strings
|
7
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id FOREIGN KEY (account_id) REFERENCES account(id)"
|
8
|
+
expects(:execute).with(sql).returns(true)
|
9
|
+
add_foreign_key('users', 'account_id', 'account', 'id')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_simple_call_with_symbols
|
13
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id FOREIGN KEY (account_id) REFERENCES account(id)"
|
14
|
+
expects(:execute).with(sql).returns(true)
|
15
|
+
add_foreign_key(:users, :account_id, :account, :id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_multiple_columns_with_strings
|
19
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id_account_version FOREIGN KEY (account_id,account_version) REFERENCES account(id,version)"
|
20
|
+
expects(:execute).with(sql).returns(true)
|
21
|
+
add_foreign_key('users', ['account_id','account_version'], 'account', ['id','version'])
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_multiple_columns_with_symbols
|
25
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id_account_version FOREIGN KEY (account_id,account_version) REFERENCES account(id,version)"
|
26
|
+
expects(:execute).with(sql).returns(true)
|
27
|
+
add_foreign_key(:users, [:account_id,:account_version], :account, [:id,:version])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_call_with_match
|
31
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id FOREIGN KEY (account_id) REFERENCES account(id) MATCH simple"
|
32
|
+
expects(:execute).with(sql).returns(true)
|
33
|
+
add_foreign_key('users', 'account_id', 'account', 'id', :match => 'simple')
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_call_with_on_delete
|
37
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE cascade"
|
38
|
+
expects(:execute).with(sql).returns(true)
|
39
|
+
add_foreign_key('users', 'account_id', 'account', 'id', :on_delete => 'cascade')
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_call_with_on_update
|
43
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id FOREIGN KEY (account_id) REFERENCES account(id) ON UPDATE cascade"
|
44
|
+
expects(:execute).with(sql).returns(true)
|
45
|
+
add_foreign_key('users', 'account_id', 'account', 'id', :on_update => 'cascade')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_call_with_match_on_delete_and_on_update
|
49
|
+
sql = "ALTER TABLE users ADD CONSTRAINT fk_users_account_id FOREIGN KEY (account_id) REFERENCES account(id) MATCH full ON DELETE restrict ON UPDATE restrict"
|
50
|
+
expects(:execute).with(sql).returns(true)
|
51
|
+
add_foreign_key('users', 'account_id', 'account', 'id', :match => 'full', :on_delete => 'restrict', :on_update => 'restrict')
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestPostgresqlMigrationsRemoveForeignKey < Test::Unit::TestCase
|
4
|
+
include PostgresqlMigrations
|
5
|
+
|
6
|
+
def test_simple_call_with_strings
|
7
|
+
sql = "ALTER TABLE users DROP CONSTRAINT fk_users_account_id"
|
8
|
+
expects(:execute).with(sql).returns(true)
|
9
|
+
remove_foreign_key('users', 'account_id')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_simple_call_with_symbols
|
13
|
+
sql = "ALTER TABLE users DROP CONSTRAINT fk_users_account_id"
|
14
|
+
expects(:execute).with(sql).returns(true)
|
15
|
+
remove_foreign_key(:users, :account_id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_multiple_columns_with_strings
|
19
|
+
sql = "ALTER TABLE users DROP CONSTRAINT fk_users_account_id_parent_id"
|
20
|
+
expects(:execute).with(sql).returns(true)
|
21
|
+
remove_foreign_key('users', ['account_id','parent_id'])
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_multiple_columns_with_symbols
|
25
|
+
sql = "ALTER TABLE users DROP CONSTRAINT fk_users_account_id_parent_id"
|
26
|
+
expects(:execute).with(sql).returns(true)
|
27
|
+
remove_foreign_key(:users, [:account_id,:parent_id])
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rafaelp-postgresql_migrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Lima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-18 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.0.0
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: pg
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">"
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 0.0.0
|
32
|
+
version:
|
33
|
+
description:
|
34
|
+
email: contato@rafael.adm.br
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- README.mkdn
|
41
|
+
files:
|
42
|
+
- MIT-LICENSE
|
43
|
+
- Rakefile
|
44
|
+
- README.mkdn
|
45
|
+
- postgresql_migrations.gemspec
|
46
|
+
- config/requirements.rb
|
47
|
+
- postgresql_migrations.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://rafael.adm.br
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- .
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: This gem implements add_foreign_key and remove_foreign_key for PostgreSQLAdapter.
|
74
|
+
test_files:
|
75
|
+
- test/test_helper.rb
|
76
|
+
- test/postgresql_migrations_add_foreign_key_test.rb
|
77
|
+
- test/postgresql_migrations_remove_foreign_key_test.rb
|