fides 0.0.2
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +12 -0
- data/fides.gemspec +27 -0
- data/lib/fides/version.rb +3 -0
- data/lib/fides.rb +120 -0
- data/test/lib/fides/version_test.rb +9 -0
- data/test/test_helper.rb +3 -0
- metadata +123 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Marty Kraft
|
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,63 @@
|
|
1
|
+
# Fides
|
2
|
+
|
3
|
+
This gem adds helper methods for use in Rails migrations that creates SQL triggers to enforce the
|
4
|
+
integrity of polymorphic associations at the database level.
|
5
|
+
|
6
|
+
When an attempt is made to create or update a record in a polymorphic table the SQL trigger checks for
|
7
|
+
the existance of the referred-to model of the specified type in the other table. If it doesn't exist in
|
8
|
+
it throws a descriptive SQL exception.
|
9
|
+
|
10
|
+
Similarly, when an attempt is made to delete a record from a table that could be pointed to from the
|
11
|
+
polymorphic table, a SQL trigger checks that there are no references to it in the polymorphic table. If
|
12
|
+
there are it throws a SQL exception suggesting changing the delete order.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'fides'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install fides
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Fides has the following public methods:
|
31
|
+
|
32
|
+
- `add_polymorphic_triggers`
|
33
|
+
- `remove_polymorphic_triggers`
|
34
|
+
|
35
|
+
Following the [Rails Polymorphic Associations example](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations),
|
36
|
+
you would do the following in a migration:
|
37
|
+
|
38
|
+
class AddReferentialIntegrityToImageable < ActiveRecord::Migration
|
39
|
+
|
40
|
+
def self.up
|
41
|
+
add_polymorphic_triggers(:polymorphic_model => "Picture", :has_many_models => ["Employee", "Product"])
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.down
|
45
|
+
remove_polymorphic_triggers(:polymorphic_model => "Picture")
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
Fides assumes the use of Rails conventions, so if there's something that needs overriding just open a
|
51
|
+
bug or shoot me a pull request.
|
52
|
+
|
53
|
+
## Caveats
|
54
|
+
|
55
|
+
Fides currently only functions with PostgreSQL. Please feel free to contribute other adapters.
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/fides.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fides/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fides"
|
8
|
+
spec.version = Fides::VERSION
|
9
|
+
spec.authors = ["Martin Kraft"]
|
10
|
+
spec.email = ["martin.kraft@gmail.com"]
|
11
|
+
spec.description = %q{Maintains referential integrity of Rails polymorphic associations.}
|
12
|
+
spec.summary = %q{Creates SQL triggers from Rails migrations to enforce the integrity of
|
13
|
+
polymorphic associations at the database level.}
|
14
|
+
spec.homepage = "https://github.com/mkraft/fides"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activesupport"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
end
|
data/lib/fides.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
require "fides/version"
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Fides
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def add_polymorphic_triggers(opts)
|
11
|
+
has_many_models = opts[:has_many_models]
|
12
|
+
polymorphic_model = opts[:polymorphic_model]
|
13
|
+
interface_name = opts.has_key?(:interface_name) ? opts[:interface_name] : interface_name(polymorphic_model)
|
14
|
+
|
15
|
+
sql = get_create_function_sql(interface_name, has_many_models, polymorphic_model)
|
16
|
+
sql << get_delete_function_sql(interface_name, has_many_models, polymorphic_model)
|
17
|
+
|
18
|
+
execute sql
|
19
|
+
end
|
20
|
+
|
21
|
+
def remove_polymorphic_triggers(opts)
|
22
|
+
polymorphic_model = opts[:polymorphic_model]
|
23
|
+
interface_name = opts.has_key?(:interface_name) ? opts[:interface_name] : interface_name(polymorphic_model)
|
24
|
+
|
25
|
+
execute %{
|
26
|
+
DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;
|
27
|
+
DROP FUNCTION IF EXISTS check_#{interface_name}_delete_integrity() CASCADE;
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# TODO: Is it safe to just grab the first polymorphic association?
|
34
|
+
def interface_name(model_name)
|
35
|
+
model_name.constantize.reflect_on_all_associations.select { |r| r if r.options[:polymorphic] }.first.name
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_create_function_sql(interface_name, models, polymorphic_model)
|
39
|
+
sql = "DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;"
|
40
|
+
|
41
|
+
sql << %{
|
42
|
+
CREATE FUNCTION check_#{interface_name}_create_integrity() RETURNS TRIGGER AS '
|
43
|
+
BEGIN
|
44
|
+
IF NEW.#{interface_name}_type = ''#{models[0]}'' AND EXISTS (
|
45
|
+
SELECT id FROM #{models[0].constantize.table_name} WHERE id = NEW.#{interface_name}_id) THEN
|
46
|
+
RETURN NEW;
|
47
|
+
}
|
48
|
+
|
49
|
+
models[1..-1].each do |model|
|
50
|
+
sql << %{
|
51
|
+
ELSEIF NEW.#{interface_name}_type = ''#{model}'' AND EXISTS (
|
52
|
+
SELECT id FROM #{model.constantize.table_name} WHERE id = NEW.#{interface_name}_id) THEN
|
53
|
+
RETURN NEW;
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
sql << %{
|
58
|
+
ELSE
|
59
|
+
RAISE EXCEPTION ''No % model with id %.'', NEW.#{interface_name}_type, NEW.#{interface_name}_id;
|
60
|
+
RETURN NULL;
|
61
|
+
END IF;
|
62
|
+
END'
|
63
|
+
LANGUAGE plpgsql;
|
64
|
+
|
65
|
+
CREATE TRIGGER check_#{interface_name}_create_integrity_trigger
|
66
|
+
BEFORE INSERT OR UPDATE ON #{polymorphic_model.constantize.table_name}
|
67
|
+
FOR EACH ROW EXECUTE PROCEDURE check_#{interface_name}_create_integrity();
|
68
|
+
}
|
69
|
+
|
70
|
+
return sql
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_delete_function_sql(interface_name, models, polymorphic_model)
|
74
|
+
polymorphic_model_table_name = polymorphic_model.constantize.table_name
|
75
|
+
|
76
|
+
sql = ""
|
77
|
+
sql << %{
|
78
|
+
CREATE FUNCTION check_#{interface_name}_delete_integrity() RETURNS TRIGGER AS '
|
79
|
+
BEGIN
|
80
|
+
IF TG_TABLE_NAME = ''#{models[0].constantize.table_name}'' AND EXISTS (
|
81
|
+
SELECT id FROM #{polymorphic_model_table_name}
|
82
|
+
WHERE #{interface_name}_type = ''#{models[0]}'' AND #{interface_name}_id = OLD.id) THEN
|
83
|
+
RAISE EXCEPTION ''There are records in #{polymorphic_model_table_name} that refer to %. You must delete those records first.'', OLD;
|
84
|
+
}
|
85
|
+
|
86
|
+
models[1..-1].each do |model|
|
87
|
+
sql << %{
|
88
|
+
ELSEIF TG_TABLE_NAME = ''#{model.constantize.table_name}'' AND EXISTS (
|
89
|
+
SELECT id FROM #{polymorphic_model_table_name}
|
90
|
+
WHERE #{interface_name}_type = ''#{model}'' AND #{interface_name}_id = OLD.id) THEN
|
91
|
+
RAISE EXCEPTION ''There are records in #{polymorphic_model_table_name} that refer to %. You must delete those records first.'', OLD;
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
sql << %{
|
96
|
+
ELSE
|
97
|
+
RETURN NULL;
|
98
|
+
END IF;
|
99
|
+
END'
|
100
|
+
LANGUAGE plpgsql;
|
101
|
+
}
|
102
|
+
|
103
|
+
models.each do |model|
|
104
|
+
table_name = model.constantize.table_name
|
105
|
+
|
106
|
+
sql << %{
|
107
|
+
CREATE TRIGGER check_#{table_name}_delete_integrity_trigger
|
108
|
+
BEFORE DELETE ON #{table_name}
|
109
|
+
FOR EACH ROW EXECUTE PROCEDURE check_#{interface_name}_delete_integrity();
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
return sql
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
ActiveRecord::Migration.send(:include, Fides)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fides
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martin Kraft
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Maintains referential integrity of Rails polymorphic associations.
|
79
|
+
email:
|
80
|
+
- martin.kraft@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- fides.gemspec
|
91
|
+
- lib/fides.rb
|
92
|
+
- lib/fides/version.rb
|
93
|
+
- test/lib/fides/version_test.rb
|
94
|
+
- test/test_helper.rb
|
95
|
+
homepage: https://github.com/mkraft/fides
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.25
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Creates SQL triggers from Rails migrations to enforce the integrity of polymorphic
|
120
|
+
associations at the database level.
|
121
|
+
test_files:
|
122
|
+
- test/lib/fides/version_test.rb
|
123
|
+
- test/test_helper.rb
|