rein 4.0.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +30 -13
- data/lib/rein/constraint/exclusion.rb +10 -1
- data/lib/rein/version.rb +1 -1
- metadata +6 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12dbfe85b21d4fe3a16982a86d784701a74ed611046b9eaa0dcddea86afb8bd3
|
4
|
+
data.tar.gz: be0b34a4ecaa0098217e6bb8327fbc1cf09adf329a3ceb30d26afb4be3fb071b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9bce885142492aa55ef15346ef19d1d631a1f85cd9ba8bc7c60ce977bd481a3d2962c3507bccde0ed9c5306f8a8f6d76b30e8e83a721f030f04df999a03ac47
|
7
|
+
data.tar.gz: 9e8c8ef912098fc12d944c1198783bf2b0308fa3a241ca937ae03d4ee9532c9acd391da045bb778a002c2cf528c81112008309614254aaf01a1f1863e9385add
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rein
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.
|
3
|
+
[![Build Status](https://travis-ci.com/nullobject/rein.svg?branch=master)](https://travis-ci.com/nullobject/rein)
|
4
4
|
|
5
5
|
[Data integrity](http://en.wikipedia.org/wiki/Data_integrity) is a good thing.
|
6
6
|
Constraining the values allowed by your application at the database-level,
|
@@ -164,13 +164,15 @@ add_unique_constraint :authors, :name, deferrable: false
|
|
164
164
|
### Exclusion Constraints
|
165
165
|
|
166
166
|
An exclusion constraint is a lot like a unique constraint, but more general.
|
167
|
-
Whereas a unique constraint forbids two rows from having all constrained
|
168
|
-
an exclusion constraint forbids two rows from having all
|
169
|
-
where the relationship is up to you
|
170
|
-
For instance you can prevent two
|
171
|
-
You can read more in
|
172
|
-
|
173
|
-
|
167
|
+
Whereas a unique constraint forbids two rows from having all constrained
|
168
|
+
columns be *equal*, an exclusion constraint forbids two rows from having all
|
169
|
+
constrained columns be *some relationship*, where the relationship is up to you
|
170
|
+
(and can be different for each column). For instance you can prevent two
|
171
|
+
ranges from overlapping with the `&&` operator. You can read more in [the
|
172
|
+
Postgres
|
173
|
+
docs](https://www.postgresql.org/docs/9.0/static/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION)
|
174
|
+
or [a slideshow by the author, Jeff
|
175
|
+
Davis](https://www.slideshare.net/pgconf/not-just-unique-exclusion-constraints).
|
174
176
|
|
175
177
|
For example, no two people should own copyright to a book at the same time:
|
176
178
|
|
@@ -178,14 +180,14 @@ For example, no two people should own copyright to a book at the same time:
|
|
178
180
|
add_exclusion_constraint :book_owners, [[:book_id, '='], [:owned_during, '&&']], using: :gist
|
179
181
|
```
|
180
182
|
|
181
|
-
By default, the database checks exclusion constraints immediately (i.e. as soon
|
182
|
-
a record is created or updated). If a record with an excluded value exists,
|
183
|
+
By default, the database checks exclusion constraints immediately (i.e. as soon
|
184
|
+
as a record is created or updated). If a record with an excluded value exists,
|
183
185
|
then the database will raise an error.
|
184
186
|
|
185
187
|
Sometimes it is necessary to wait until the end of a transaction to do the
|
186
|
-
checking (e.g. maybe you want to move the date a copyright changed hands). To
|
187
|
-
need to tell the database to *defer* checking the constraint until
|
188
|
-
the current transaction:
|
188
|
+
checking (e.g. maybe you want to move the date a copyright changed hands). To
|
189
|
+
do so, you need to tell the database to *defer* checking the constraint until
|
190
|
+
the end of the current transaction:
|
189
191
|
|
190
192
|
```sql
|
191
193
|
BEGIN;
|
@@ -213,6 +215,21 @@ a transaction, then you can set the `deferrable` option to `false`:
|
|
213
215
|
add_exclusion_constraint :book_owners, [[:book_id, '='], [:owned_during, '&&']], using: :gist, deferrable: false
|
214
216
|
```
|
215
217
|
|
218
|
+
If you want to specify something like a operator class for a attribute specific
|
219
|
+
attribute, you can add it to the attribute specification between attribute name
|
220
|
+
and the operator:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
add_exclusion_constraint :book_owners, [[:book_id, :gist_int8_ops, '='], [:owned_during, '&&']], using: :gist
|
224
|
+
```
|
225
|
+
|
226
|
+
If you want to set the constraint to a subset of the table, you can use a `where`
|
227
|
+
option to set the filter condition:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
add_exclusion_constraint :books, [[:isbn, '=']], using: :gist, where: "state='active'"
|
231
|
+
```
|
232
|
+
|
216
233
|
### Inclusion Constraints
|
217
234
|
|
218
235
|
An inclusion constraint specifies the possible values that a column value can
|
@@ -42,15 +42,24 @@ module Rein
|
|
42
42
|
name = Util.constraint_name(table, attributes.map { |att| att[0] }.join('_'), 'exclude', options)
|
43
43
|
table = Util.wrap_identifier(table)
|
44
44
|
attributes = attributes.map do |attribute|
|
45
|
-
|
45
|
+
_get_attribute_expression(attribute)
|
46
46
|
end
|
47
47
|
initially = options[:deferred] ? 'DEFERRED' : 'IMMEDIATE'
|
48
48
|
using = options[:using] ? " USING #{options[:using]}" : ''
|
49
49
|
sql = "ALTER TABLE #{table} ADD CONSTRAINT #{name} EXCLUDE#{using} (#{attributes.join(', ')})"
|
50
|
+
sql << " WHERE (#{options[:where]})" if options[:where].present?
|
50
51
|
sql << " DEFERRABLE INITIALLY #{initially}" unless options[:deferrable] == false
|
51
52
|
execute(sql)
|
52
53
|
end
|
53
54
|
|
55
|
+
def _get_attribute_expression(attribute)
|
56
|
+
if attribute.size > 2
|
57
|
+
"#{Util.wrap_identifier(attribute[0])} #{attribute[1, attribute.size - 2].join(' ')} WITH #{attribute.last}"
|
58
|
+
else
|
59
|
+
"#{Util.wrap_identifier(attribute[0])} WITH #{attribute[1]}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
54
63
|
def _remove_exclusion_constraint(table, attributes, options = {})
|
55
64
|
name = Util.constraint_name(table, attributes.map { |att| att[0] }.join('_'), 'exclude', options)
|
56
65
|
table = Util.wrap_identifier(table)
|
data/lib/rein/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rein
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Bassett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -17,9 +17,6 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 4.0.0
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '6'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +24,6 @@ dependencies:
|
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 4.0.0
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '6'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: activesupport
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -37,9 +31,6 @@ dependencies:
|
|
37
31
|
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
33
|
version: 4.0.0
|
40
|
-
- - "<"
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version: '6'
|
43
34
|
type: :runtime
|
44
35
|
prerelease: false
|
45
36
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -47,9 +38,6 @@ dependencies:
|
|
47
38
|
- - ">="
|
48
39
|
- !ruby/object:Gem::Version
|
49
40
|
version: 4.0.0
|
50
|
-
- - "<"
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version: '6'
|
53
41
|
- !ruby/object:Gem::Dependency
|
54
42
|
name: appraisal
|
55
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,14 +114,14 @@ dependencies:
|
|
126
114
|
requirements:
|
127
115
|
- - "~>"
|
128
116
|
- !ruby/object:Gem::Version
|
129
|
-
version: 0.
|
117
|
+
version: 0.67.2
|
130
118
|
type: :development
|
131
119
|
prerelease: false
|
132
120
|
version_requirements: !ruby/object:Gem::Requirement
|
133
121
|
requirements:
|
134
122
|
- - "~>"
|
135
123
|
- !ruby/object:Gem::Version
|
136
|
-
version: 0.
|
124
|
+
version: 0.67.2
|
137
125
|
description: Rein adds bunch of methods to your ActiveRecord migrations so you can
|
138
126
|
easily tame your database.
|
139
127
|
email: josh.bassett@gmail.com
|
@@ -180,7 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
168
|
- !ruby/object:Gem::Version
|
181
169
|
version: '0'
|
182
170
|
requirements: []
|
183
|
-
|
171
|
+
rubyforge_project:
|
172
|
+
rubygems_version: 2.7.6.2
|
184
173
|
signing_key:
|
185
174
|
specification_version: 4
|
186
175
|
summary: Database constraints made easy for ActiveRecord.
|