activerecord-referential_integrity 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +1 -0
- data/activerecord-referential_integrity.gemspec +24 -0
- data/lib/activerecord-referential_integrity.rb +9 -0
- data/lib/activerecord/referential_integrity/active_record/connection_adapters/abstract_adapter.rb +9 -0
- data/lib/activerecord/referential_integrity/active_record/connection_adapters/postgresql_adapter.rb +19 -0
- data/lib/activerecord/referential_integrity/active_record/migration/command_recorder.rb +11 -0
- data/lib/activerecord/referential_integrity/version.rb +5 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d6ac2cbc77863e65e24222f60379683214e8e914
|
4
|
+
data.tar.gz: d6c9043d8d080abd16499431d0c3b7b2dbcfed14
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d20175adab2ba64911e5613c4d185a3182ec0c5f5207efc1c7dacb4d1e7633f30bf9bb3eff55617efb11c9fe495ceb2f2de068fb5c7c86705dc89c05be46592
|
7
|
+
data.tar.gz: aac8adca5abed2d1fda0dec2a73938e8a4f1fc5de7ffa159eb1b5b3e2ff3cacee0e908f4a6e8abdbc418041a2d60dabb219a63fa94bc7a0603a66913f0e8bd17
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Orcas Net, Inc.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Referential Integrity for ActiveRecord
|
2
|
+
|
3
|
+
Adds as reversible `add_foreign_key` migration to Rails. `add_foreign_key`
|
4
|
+
creates a database level foreign key constraint, this ensures that records
|
5
|
+
cannot reference foreign keys in other tables that do not exist.
|
6
|
+
|
7
|
+
See [PostgreSQL foreign key constraint documentation](http://www.postgresql.org/docs/9.3/static/ddl-constraints.html#DDL-CONSTRAINTS-FK)
|
8
|
+
for more.
|
9
|
+
|
10
|
+
## Limitations
|
11
|
+
|
12
|
+
Currently supports only PostgreSQL.
|
13
|
+
Currently is written against Rails/ActiveRecord 3.2.16.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'activerecord-referential_integrity'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install activerecord-referential_integrity
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Inside your migrations, call `add_foreign_key`, passing in the table and a hash of column => table pairs.
|
32
|
+
|
33
|
+
For example:
|
34
|
+
```ruby
|
35
|
+
class CreateUserSettings < ActiveRecord::Migration
|
36
|
+
def change
|
37
|
+
create_table :user_settings do |t|
|
38
|
+
t.references :user, null: false
|
39
|
+
# or t.integer :user_id, null: false
|
40
|
+
[...]
|
41
|
+
end
|
42
|
+
|
43
|
+
add_foreign_key :somethings, :user_id => :users
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
TODO: Write usage instructions here
|
49
|
+
|
50
|
+
## Versioning
|
51
|
+
Uses [Semantic Versioning](http://semver.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it (http://github.com/orcasnet/activerecord-referential_integrity/fork)
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord/referential_integrity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'activerecord-referential_integrity'
|
8
|
+
spec.version = ActiveRecord::ReferentialIntegrity::VERSION
|
9
|
+
spec.authors = ['Martin Tithonium', 'John Wulff']
|
10
|
+
spec.email = ['marty@orcasnet.com', 'johnw@orcasnet.com']
|
11
|
+
spec.summary = %q{Adds add_foreign_key to Rails migrations with relevant databases}
|
12
|
+
spec.description = %q{Adds as reversible add_foreign_key migration to Rails. Currently supports only PostgreSQL}
|
13
|
+
spec.homepage = 'https://github.com/orcasnet/activerecord-referential_integrity'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'activerecord', '~> 3.2.16'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'activerecord/referential_integrity/version'
|
2
|
+
require 'activerecord/referential_integrity/active_record/connection_adapters/abstract_adapter'
|
3
|
+
require 'activerecord/referential_integrity/active_record/connection_adapters/postgresql_adapter'
|
4
|
+
require 'activerecord/referential_integrity/active_record/migration/command_recorder'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module ReferentialIntegrity
|
8
|
+
end
|
9
|
+
end
|
data/lib/activerecord/referential_integrity/active_record/connection_adapters/abstract_adapter.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
class ActiveRecord::ConnectionAdapters::AbstractAdapter
|
2
|
+
def add_foreign_key(table, keypairs)
|
3
|
+
raise NotImplementedError, 'add_foreign_key is not implemented'
|
4
|
+
end
|
5
|
+
|
6
|
+
def drop_foreign_key(table, keys)
|
7
|
+
raise NotImplementedError, 'drop_foreign_key is not implemented'
|
8
|
+
end
|
9
|
+
end
|
data/lib/activerecord/referential_integrity/active_record/connection_adapters/postgresql_adapter.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
|
2
|
+
def add_foreign_key(table, keypairs)
|
3
|
+
keypairs.each_pair do |foreign_key, foreign_table|
|
4
|
+
sql = "ALTER TABLE #{table} "
|
5
|
+
sql << "ADD CONSTRAINT #{table}_fk_#{foreign_key} "
|
6
|
+
sql << "FOREIGN KEY (#{foreign_key}) "
|
7
|
+
sql << "REFERENCES #{foreign_table} (id)"
|
8
|
+
execute sql
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def drop_foreign_key(table, keys)
|
13
|
+
keys.each do |foreign_key|
|
14
|
+
sql = "ALTER TABLE #{table} "
|
15
|
+
sql << "DROP CONSTRAINT IF EXISTS #{table}_fk_#{foreign_key} "
|
16
|
+
execute sql
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-referential_integrity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Tithonium
|
8
|
+
- John Wulff
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 3.2.16
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 3.2.16
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.5'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.5'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Adds as reversible add_foreign_key migration to Rails. Currently supports
|
57
|
+
only PostgreSQL
|
58
|
+
email:
|
59
|
+
- marty@orcasnet.com
|
60
|
+
- johnw@orcasnet.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- activerecord-referential_integrity.gemspec
|
71
|
+
- lib/activerecord-referential_integrity.rb
|
72
|
+
- lib/activerecord/referential_integrity/active_record/connection_adapters/abstract_adapter.rb
|
73
|
+
- lib/activerecord/referential_integrity/active_record/connection_adapters/postgresql_adapter.rb
|
74
|
+
- lib/activerecord/referential_integrity/active_record/migration/command_recorder.rb
|
75
|
+
- lib/activerecord/referential_integrity/version.rb
|
76
|
+
homepage: https://github.com/orcasnet/activerecord-referential_integrity
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.0.rc.1
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Adds add_foreign_key to Rails migrations with relevant databases
|
100
|
+
test_files: []
|