polymorpheus 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/polymorpheus/mysql_adapter.rb +71 -28
- data/lib/polymorpheus/version.rb +1 -1
- data/polymorpheus.gemspec +3 -1
- data/spec/mysql2_adapter_spec.rb +33 -0
- metadata +30 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b4fa428b1261c05072970feddd72eca1d4e0d70
|
4
|
+
data.tar.gz: 189801e622b209426bb6e39b86a32b7c34bbc954
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1624b4a33ce2a7875acda4688ebad9662e2f6201e01d329196cee018acfcbec0bafe86f3d07c732c383525e13b96e3fda65b510f29201615aa7637803300b32c
|
7
|
+
data.tar.gz: f207481aef2d8e20c22c7efb2d86691c2d9ed6ba241356e7eb977a27c3d7c90c1f9aceab5f3b4972956f730ca0baa850d6c5b3e0a38866f54a478e6b8daaeb29
|
data/README.md
CHANGED
@@ -198,6 +198,6 @@ pic.polymorpheus.query_condition
|
|
198
198
|
* It uses the [Foreigner gem](https://github.com/matthuhiggins/foreigner) under
|
199
199
|
the hood for a few things
|
200
200
|
|
201
|
-
polymorpheus is Copyright © 2011-
|
201
|
+
polymorpheus is Copyright © 2011-2015 Barun Singh and [WegoWise](
|
202
202
|
http://wegowise.com). It is free software, and may be redistributed under the
|
203
203
|
terms specified in the LICENSE file.
|
@@ -5,37 +5,59 @@ module Polymorpheus
|
|
5
5
|
INSERT = 'INSERT'
|
6
6
|
UPDATE = 'UPDATE'
|
7
7
|
|
8
|
-
# See the README for
|
8
|
+
# See the README for more information about the use of these methods.
|
9
9
|
#
|
10
10
|
# table: a string equal to the name of the db table
|
11
11
|
#
|
12
12
|
# columns: a hash, with keys equal to the column names in the table we
|
13
13
|
# are operating on, and values indicating the foreign key
|
14
|
-
# association through the form "table.column".
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
14
|
+
# association through the form "table.column".
|
15
|
+
#
|
16
|
+
# For example:
|
17
|
+
#
|
18
|
+
# {
|
19
|
+
# 'employee_id' => 'employees.ssn',
|
20
|
+
# 'product_id' => 'products.id'
|
21
|
+
# }
|
22
|
+
#
|
23
|
+
# This indicates that the `employee_id` column in `table`
|
24
|
+
# should have a foreign key constraint connecting it to the
|
25
|
+
# `ssn` column in the `employees` table, and the `product_id`
|
26
|
+
# column should have a foreign key constraint with the `id`
|
27
|
+
# column in the `products` table.
|
28
|
+
#
|
29
|
+
# options: a hash, accepting the following options
|
30
|
+
#
|
31
|
+
# :unique
|
32
|
+
#
|
33
|
+
# If the columns hash was specified as above, and :unique is true:
|
34
|
+
#
|
35
|
+
# { :unique => true }
|
36
|
+
#
|
37
|
+
# Then this creates a uniqueness constraint in the database that
|
38
|
+
# will ensure that any given employee_id can only be in the table
|
39
|
+
# once, and that any given product_id can only be in the table
|
40
|
+
# once.
|
41
|
+
#
|
42
|
+
# Alternatively, you can supply a column name or array of column
|
43
|
+
# names to the :unique option:
|
44
|
+
#
|
45
|
+
# { :unique => 'picture_url' }
|
46
|
+
#
|
47
|
+
# This will allow an employee_id (or product_id) to appear multiple
|
48
|
+
# times in the table, but no two employee IDs would be able to have
|
49
|
+
# the same picture_url.
|
50
|
+
#
|
51
|
+
# :on_delete
|
52
|
+
#
|
53
|
+
# Action that happens ON DELETE. Valid values are :nullify,
|
54
|
+
# :cascade and :restrict.
|
55
|
+
#
|
56
|
+
# :on_update
|
57
|
+
#
|
58
|
+
# Action that happens ON UPDATE. Valid values are :nullify,
|
59
|
+
# :cascade and :restrict.
|
60
|
+
|
39
61
|
|
40
62
|
def add_polymorphic_constraints(table, columns, options={})
|
41
63
|
column_names = columns.keys.sort
|
@@ -48,7 +70,8 @@ module Polymorpheus
|
|
48
70
|
ref_table, ref_col = columns[col_name].to_s.split('.')
|
49
71
|
add_foreign_key table, ref_table,
|
50
72
|
:column => col_name,
|
51
|
-
:primary_key => (ref_col || 'id')
|
73
|
+
:primary_key => (ref_col || 'id'),
|
74
|
+
:options => generate_constraints(options)
|
52
75
|
end
|
53
76
|
end
|
54
77
|
|
@@ -167,6 +190,27 @@ module Polymorpheus
|
|
167
190
|
columns.map { |c| c.to_s.gsub('_','').first(col_length-1) }.join('_')
|
168
191
|
end
|
169
192
|
|
193
|
+
def generate_constraints(options)
|
194
|
+
constraints = []
|
195
|
+
|
196
|
+
['delete', 'update'].each do |event|
|
197
|
+
option = "on_#{event}".to_sym
|
198
|
+
next unless options.has_key?(option) &&
|
199
|
+
options[option].respond_to?(:to_sym)
|
200
|
+
|
201
|
+
action = case options[option].to_sym
|
202
|
+
when :nullify then 'SET NULL'
|
203
|
+
when :cascade then 'CASCADE'
|
204
|
+
when :restrict then 'RESTRICT'
|
205
|
+
end
|
206
|
+
next unless action
|
207
|
+
|
208
|
+
constraints << "ON #{event.upcase} #{action}"
|
209
|
+
end
|
210
|
+
|
211
|
+
constraints.join(' ')
|
212
|
+
end
|
213
|
+
|
170
214
|
end
|
171
215
|
end
|
172
216
|
end
|
@@ -179,4 +223,3 @@ end
|
|
179
223
|
rescue
|
180
224
|
end
|
181
225
|
end
|
182
|
-
|
data/lib/polymorpheus/version.rb
CHANGED
data/polymorpheus.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
|
20
20
|
s.add_dependency('foreigner')
|
21
21
|
s.add_dependency('activerecord', '>= 3.2', '< 4.2')
|
22
|
+
|
23
|
+
s.add_development_dependency('rake', '~> 10.4.2')
|
22
24
|
s.add_development_dependency('rspec-rails', '~> 2.14.0')
|
23
|
-
s.add_development_dependency('mysql2', '~> 0.3')
|
25
|
+
s.add_development_dependency('mysql2', '~> 0.3.10')
|
24
26
|
end
|
data/spec/mysql2_adapter_spec.rb
CHANGED
@@ -99,6 +99,39 @@ describe Polymorpheus::ConnectionAdapters::MysqlAdapter do
|
|
99
99
|
it_behaves_like "mysql2 migration statements"
|
100
100
|
end
|
101
101
|
|
102
|
+
context "specifying an on update constraint" do
|
103
|
+
include_context "columns with short names"
|
104
|
+
let(:options) { { :on_update => :cascade } }
|
105
|
+
let(:fkey_sql) do
|
106
|
+
%{ ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id) ON UPDATE CASCADE
|
107
|
+
ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) ON UPDATE CASCADE }
|
108
|
+
end
|
109
|
+
|
110
|
+
it_behaves_like "mysql2 migration statements"
|
111
|
+
end
|
112
|
+
|
113
|
+
context "specifying on delete and on update constraints" do
|
114
|
+
include_context "columns with short names"
|
115
|
+
let(:options) { { :on_update => :cascade, :on_delete => :restrict } }
|
116
|
+
let(:fkey_sql) do
|
117
|
+
%{ ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id) ON DELETE RESTRICT ON UPDATE CASCADE
|
118
|
+
ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) ON DELETE RESTRICT ON UPDATE CASCADE }
|
119
|
+
end
|
120
|
+
|
121
|
+
it_behaves_like "mysql2 migration statements"
|
122
|
+
end
|
123
|
+
|
124
|
+
context "when on_delete and on_update have invalid arguments" do
|
125
|
+
include_context "columns with short names"
|
126
|
+
let(:options) { { :on_update => :invalid, :on_delete => nil } }
|
127
|
+
let(:fkey_sql) do
|
128
|
+
%{ ALTER TABLE `pets` ADD CONSTRAINT `pets_dog_id_fk` FOREIGN KEY (`dog_id`) REFERENCES `dogs`(id)
|
129
|
+
ALTER TABLE `pets` ADD CONSTRAINT `pets_kitty_id_fk` FOREIGN KEY (`kitty_id`) REFERENCES `cats`(name) }
|
130
|
+
end
|
131
|
+
|
132
|
+
it_behaves_like "mysql2 migration statements"
|
133
|
+
end
|
134
|
+
|
102
135
|
context "when table and column names combined are very long" do
|
103
136
|
include_context "columns with long names"
|
104
137
|
|
metadata
CHANGED
@@ -1,77 +1,91 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorpheus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barun Singh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: foreigner
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '3.2'
|
34
|
-
- - <
|
34
|
+
- - "<"
|
35
35
|
- !ruby/object:Gem::Version
|
36
36
|
version: '4.2'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '3.2'
|
44
|
-
- - <
|
44
|
+
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '4.2'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 10.4.2
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 10.4.2
|
47
61
|
- !ruby/object:Gem::Dependency
|
48
62
|
name: rspec-rails
|
49
63
|
requirement: !ruby/object:Gem::Requirement
|
50
64
|
requirements:
|
51
|
-
- - ~>
|
65
|
+
- - "~>"
|
52
66
|
- !ruby/object:Gem::Version
|
53
67
|
version: 2.14.0
|
54
68
|
type: :development
|
55
69
|
prerelease: false
|
56
70
|
version_requirements: !ruby/object:Gem::Requirement
|
57
71
|
requirements:
|
58
|
-
- - ~>
|
72
|
+
- - "~>"
|
59
73
|
- !ruby/object:Gem::Version
|
60
74
|
version: 2.14.0
|
61
75
|
- !ruby/object:Gem::Dependency
|
62
76
|
name: mysql2
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
64
78
|
requirements:
|
65
|
-
- - ~>
|
79
|
+
- - "~>"
|
66
80
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
81
|
+
version: 0.3.10
|
68
82
|
type: :development
|
69
83
|
prerelease: false
|
70
84
|
version_requirements: !ruby/object:Gem::Requirement
|
71
85
|
requirements:
|
72
|
-
- - ~>
|
86
|
+
- - "~>"
|
73
87
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
88
|
+
version: 0.3.10
|
75
89
|
description: Provides a database-friendly method for polymorphic relationships
|
76
90
|
email: bsingh@wegowise.com
|
77
91
|
executables: []
|
@@ -120,12 +134,12 @@ require_paths:
|
|
120
134
|
- lib
|
121
135
|
required_ruby_version: !ruby/object:Gem::Requirement
|
122
136
|
requirements:
|
123
|
-
- -
|
137
|
+
- - ">="
|
124
138
|
- !ruby/object:Gem::Version
|
125
139
|
version: '0'
|
126
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
141
|
requirements:
|
128
|
-
- -
|
142
|
+
- - ">="
|
129
143
|
- !ruby/object:Gem::Version
|
130
144
|
version: 1.3.6
|
131
145
|
requirements: []
|