rein 2.1.0 → 3.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +29 -14
- data/lib/rein.rb +12 -29
- data/lib/rein/constraint/foreign_key.rb +25 -30
- data/lib/rein/constraint/inclusion.rb +22 -7
- data/lib/rein/constraint/null.rb +23 -6
- data/lib/rein/constraint/numericality.rb +22 -9
- data/lib/rein/constraint/presence.rb +23 -6
- data/lib/rein/constraint/primary_key.rb +23 -3
- data/lib/rein/schema.rb +20 -6
- data/lib/rein/type/enum.rb +21 -5
- data/lib/rein/util.rb +18 -0
- data/lib/rein/version.rb +1 -1
- data/lib/rein/view.rb +20 -6
- metadata +3 -3
- data/lib/rein/constraint/options.rb +0 -18
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9f1047d5663ba27fedf399e87223e024fe7a6fa
|
|
4
|
+
data.tar.gz: fe37532d4ee714cd1e1fa281611f4ae61cd5561a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b3c700dd2bc15a2edf547ac390ec78e4972bc1bb3c7d4f534a495969d4e2e8b014121f9c11a230e415ba892c0e22a16d62bf391f21c69cf5d88bc7eef3d43f02
|
|
7
|
+
data.tar.gz: 4153810c938897f244d72088e9c0095e99f7f8476c09069f647a4b974d7ad180860d019f6962b3192cf28fa70c2f8a0e549a32f9c6b3cd366d03ca9577bb8d42
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -12,6 +12,9 @@ database integrity without resorting to hand-crafted SQL. Rein (pronounced
|
|
|
12
12
|
"rain") adds a handful of methods to your ActiveRecord migrations so that you
|
|
13
13
|
can easily tame the data in your database.
|
|
14
14
|
|
|
15
|
+
All methods in the DSL are automatically *reversible*, so you can take
|
|
16
|
+
advantage of reversible Rails migrations.
|
|
17
|
+
|
|
15
18
|
## Table of contents
|
|
16
19
|
|
|
17
20
|
* [Rein](#rein)
|
|
@@ -28,14 +31,30 @@ can easily tame the data in your database.
|
|
|
28
31
|
* [Views](#views)
|
|
29
32
|
* [Schemas](#schemas)
|
|
30
33
|
* [Example](#example)
|
|
31
|
-
* [Contributing](#contributing)
|
|
32
34
|
* [License](#license)
|
|
33
35
|
|
|
34
36
|
## Getting started
|
|
35
37
|
|
|
36
38
|
Install the gem:
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
```
|
|
41
|
+
> gem install rein
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Add a constraint to your migrations:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
class CreateAuthorsTable < ActiveRecord::Migration
|
|
48
|
+
def change
|
|
49
|
+
create_table :authors do |t|
|
|
50
|
+
t.string :name, null: false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# An author must have a name.
|
|
54
|
+
add_presence_constraint :authors, :name
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
```
|
|
39
58
|
|
|
40
59
|
## Constraint types
|
|
41
60
|
|
|
@@ -51,6 +70,14 @@ For example, let's say that we want to constrain the `author_id` column in the
|
|
|
51
70
|
add_foreign_key_constraint :books, :authors
|
|
52
71
|
```
|
|
53
72
|
|
|
73
|
+
Adding a foreign key doesn't automatically create an index on the referenced
|
|
74
|
+
column. Having an index will generally speed up any joins you perform on the
|
|
75
|
+
foreign key. To create an index you can specify the `index` option:
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
add_foreign_key_constraint :books, :authors, index: true
|
|
79
|
+
```
|
|
80
|
+
|
|
54
81
|
Rein will automatically infer the column names for the tables, but if we need
|
|
55
82
|
to be explicit we can using the `referenced` and `referencing` options:
|
|
56
83
|
|
|
@@ -86,8 +113,6 @@ remove_foreign_key_constraint :books, :authors
|
|
|
86
113
|
|
|
87
114
|
### Inclusion constraints
|
|
88
115
|
|
|
89
|
-
*(PostgreSQL only)*
|
|
90
|
-
|
|
91
116
|
An inclusion constraint specifies the possible values that a column value can
|
|
92
117
|
take.
|
|
93
118
|
|
|
@@ -123,8 +148,6 @@ add_inclusion_constraint :books, :state,
|
|
|
123
148
|
|
|
124
149
|
### Numericality constraints
|
|
125
150
|
|
|
126
|
-
*(PostgreSQL only)*
|
|
127
|
-
|
|
128
151
|
A numericality constraint specifies the range of values that a numeric column
|
|
129
152
|
value can take.
|
|
130
153
|
|
|
@@ -173,8 +196,6 @@ remove_numericality_constraint :books, :publication_month
|
|
|
173
196
|
|
|
174
197
|
### Presence constraints
|
|
175
198
|
|
|
176
|
-
*(PostgreSQL only)*
|
|
177
|
-
|
|
178
199
|
A presence constraint ensures that a string column value is non-empty.
|
|
179
200
|
|
|
180
201
|
A `NOT NULL` constraint will be satisfied by an empty string, but sometimes may
|
|
@@ -205,8 +226,6 @@ remove_presence_constraint :books, :title
|
|
|
205
226
|
|
|
206
227
|
### Null constraints
|
|
207
228
|
|
|
208
|
-
*(PostgreSQL only)*
|
|
209
|
-
|
|
210
229
|
A null constraint ensures that a column does *not* contain a null value. This
|
|
211
230
|
is the same as adding `NOT NULL` to a column, the difference being that it can
|
|
212
231
|
be _applied conditionally_.
|
|
@@ -228,8 +247,6 @@ remove_null_constraint :books, :due_date
|
|
|
228
247
|
|
|
229
248
|
### Enumerated types
|
|
230
249
|
|
|
231
|
-
*(PostgreSQL only)*
|
|
232
|
-
|
|
233
250
|
An enum is a data type that represents a static, ordered set of values.
|
|
234
251
|
|
|
235
252
|
```ruby
|
|
@@ -262,8 +279,6 @@ drop_view :available_books
|
|
|
262
279
|
|
|
263
280
|
## Schemas
|
|
264
281
|
|
|
265
|
-
*(PostgreSQL only)*
|
|
266
|
-
|
|
267
282
|
A database can contain one or more named schemas, which in turn contain tables.
|
|
268
283
|
Sometimes it might be helpful to split your database into multiple schemas to
|
|
269
284
|
logically group tables together.
|
data/lib/rein.rb
CHANGED
|
@@ -1,41 +1,24 @@
|
|
|
1
1
|
require "active_record"
|
|
2
|
-
require "active_record/connection_adapters/abstract_mysql_adapter"
|
|
3
|
-
|
|
4
|
-
require "rein/constraint/options"
|
|
5
|
-
require "rein/constraint/primary_key"
|
|
6
2
|
require "rein/constraint/foreign_key"
|
|
7
3
|
require "rein/constraint/inclusion"
|
|
8
4
|
require "rein/constraint/null"
|
|
9
5
|
require "rein/constraint/numericality"
|
|
10
6
|
require "rein/constraint/presence"
|
|
7
|
+
require "rein/constraint/primary_key"
|
|
8
|
+
require "rein/schema"
|
|
11
9
|
require "rein/type/enum"
|
|
12
10
|
require "rein/view"
|
|
13
|
-
require "rein/schema"
|
|
14
11
|
|
|
15
12
|
module ActiveRecord
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
include Rein::View
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
class PostgreSQLAdapter < AbstractAdapter # :nodoc:
|
|
30
|
-
include Rein::Constraint::PrimaryKey
|
|
31
|
-
include Rein::Constraint::ForeignKey
|
|
32
|
-
include Rein::Constraint::Inclusion
|
|
33
|
-
include Rein::Constraint::Null
|
|
34
|
-
include Rein::Constraint::Numericality
|
|
35
|
-
include Rein::Constraint::Presence
|
|
36
|
-
include Rein::Type::Enum
|
|
37
|
-
include Rein::View
|
|
38
|
-
include Rein::Schema
|
|
39
|
-
end
|
|
13
|
+
class Migration # :nodoc:
|
|
14
|
+
include Rein::Constraint::ForeignKey
|
|
15
|
+
include Rein::Constraint::Inclusion
|
|
16
|
+
include Rein::Constraint::Null
|
|
17
|
+
include Rein::Constraint::Numericality
|
|
18
|
+
include Rein::Constraint::Presence
|
|
19
|
+
include Rein::Constraint::PrimaryKey
|
|
20
|
+
include Rein::Schema
|
|
21
|
+
include Rein::Type::Enum
|
|
22
|
+
include Rein::View
|
|
40
23
|
end
|
|
41
24
|
end
|
|
@@ -1,46 +1,46 @@
|
|
|
1
1
|
require "active_support/inflector"
|
|
2
|
+
require "rein/util"
|
|
2
3
|
|
|
3
4
|
module Rein
|
|
4
5
|
module Constraint
|
|
5
6
|
# This module contains methods for defining foreign key constraints.
|
|
6
7
|
module ForeignKey
|
|
7
|
-
def add_foreign_key_constraint(
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
def add_foreign_key_constraint(*args)
|
|
9
|
+
reversible do |dir|
|
|
10
|
+
dir.up { _add_foreign_key_constraint(*args) }
|
|
11
|
+
dir.down { _remove_foreign_key_constraint(*args) }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def remove_foreign_key_constraint(*args)
|
|
16
|
+
reversible do |dir|
|
|
17
|
+
dir.up { _remove_foreign_key_constraint(*args) }
|
|
18
|
+
dir.down { _add_foreign_key_constraint(*args) }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
10
21
|
|
|
11
|
-
|
|
22
|
+
private
|
|
12
23
|
|
|
24
|
+
def _add_foreign_key_constraint(referencing_table, referenced_table, options = {})
|
|
25
|
+
referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym
|
|
26
|
+
referenced_attribute = (options[:referenced] || "id").to_sym
|
|
27
|
+
name = Util.constraint_name(referencing_table, referencing_attribute, "fk", options)
|
|
13
28
|
sql = "ALTER TABLE #{referencing_table}"
|
|
14
29
|
sql << " ADD CONSTRAINT #{name}"
|
|
15
30
|
sql << " FOREIGN KEY (#{referencing_attribute})"
|
|
16
31
|
sql << " REFERENCES #{referenced_table} (#{referenced_attribute})"
|
|
17
32
|
sql << " ON DELETE #{referential_action(options[:on_delete])}" if options[:on_delete].present?
|
|
18
33
|
sql << " ON UPDATE #{referential_action(options[:on_update])}" if options[:on_update].present?
|
|
19
|
-
|
|
20
34
|
execute(sql)
|
|
21
|
-
|
|
22
|
-
# A foreign key constraint doesn't have an implicit index.
|
|
23
|
-
add_index(referencing_table, referencing_attribute) if options[:add_index] == true
|
|
35
|
+
add_index(referencing_table, referencing_attribute) if options[:index] == true
|
|
24
36
|
end
|
|
25
|
-
alias add_foreign_key add_foreign_key_constraint
|
|
26
37
|
|
|
27
|
-
def
|
|
38
|
+
def _remove_foreign_key_constraint(referencing_table, referenced_table, options = {})
|
|
28
39
|
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if mysql_adapter?
|
|
33
|
-
execute "ALTER TABLE #{referencing_table} DROP FOREIGN KEY #{name}"
|
|
34
|
-
else
|
|
35
|
-
execute "ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}"
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# A foreign key constraint doesn't have an implicit index.
|
|
39
|
-
remove_index(referencing_table, referencing_attribute) if options[:remove_index] == true
|
|
40
|
+
name = Util.constraint_name(referencing_table, referencing_attribute, "fk", options)
|
|
41
|
+
execute("ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}")
|
|
42
|
+
remove_index(referencing_table, referencing_attribute) if options[:index] == true
|
|
40
43
|
end
|
|
41
|
-
alias remove_foreign_key remove_foreign_key_constraint
|
|
42
|
-
|
|
43
|
-
private
|
|
44
44
|
|
|
45
45
|
def referential_action(action)
|
|
46
46
|
case action.to_sym
|
|
@@ -49,15 +49,10 @@ module Rein
|
|
|
49
49
|
when :restrict then "RESTRICT"
|
|
50
50
|
when :set_null, :nullify then "SET NULL"
|
|
51
51
|
when :set_default, :default then "SET DEFAULT"
|
|
52
|
-
else
|
|
53
|
-
raise "Unknown referential action '#{action}'"
|
|
52
|
+
else raise "Unknown referential action '#{action}'"
|
|
54
53
|
end
|
|
55
54
|
end
|
|
56
55
|
|
|
57
|
-
def mysql_adapter?
|
|
58
|
-
self.class.to_s =~ /Mysql[2]?Adapter/
|
|
59
|
-
end
|
|
60
|
-
|
|
61
56
|
def default_constraint_name(referencing_table, referencing_attribute)
|
|
62
57
|
"#{referencing_table}_#{referencing_attribute}_fk".to_sym
|
|
63
58
|
end
|
|
@@ -1,21 +1,36 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require "rein/util"
|
|
2
2
|
|
|
3
3
|
module Rein
|
|
4
4
|
module Constraint
|
|
5
5
|
# This module contains methods for defining inclusion constraints.
|
|
6
6
|
module Inclusion
|
|
7
7
|
include ActiveRecord::ConnectionAdapters::Quoting
|
|
8
|
-
include Rein::Constraint::Options
|
|
9
8
|
|
|
10
|
-
def add_inclusion_constraint(
|
|
11
|
-
|
|
9
|
+
def add_inclusion_constraint(*args)
|
|
10
|
+
reversible do |dir|
|
|
11
|
+
dir.up { _add_inclusion_constraint(*args) }
|
|
12
|
+
dir.down { _remove_inclusion_constraint(*args) }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def remove_inclusion_constraint(*args)
|
|
17
|
+
reversible do |dir|
|
|
18
|
+
dir.up { _remove_inclusion_constraint(*args) }
|
|
19
|
+
dir.down { _add_inclusion_constraint(*args) }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def _add_inclusion_constraint(table, attribute, options = {})
|
|
26
|
+
name = Util.constraint_name(table, attribute, "inclusion", options)
|
|
12
27
|
values = options[:in].map { |value| quote(value) }.join(", ")
|
|
13
|
-
conditions = conditions_with_if("#{attribute} IN (#{values})", options)
|
|
28
|
+
conditions = Util.conditions_with_if("#{attribute} IN (#{values})", options)
|
|
14
29
|
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
|
|
15
30
|
end
|
|
16
31
|
|
|
17
|
-
def
|
|
18
|
-
name = constraint_name(table, attribute, options)
|
|
32
|
+
def _remove_inclusion_constraint(table, attribute, options = {})
|
|
33
|
+
name = Util.constraint_name(table, attribute, "inclusion", options)
|
|
19
34
|
execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
|
|
20
35
|
end
|
|
21
36
|
end
|
data/lib/rein/constraint/null.rb
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
|
+
require "rein/util"
|
|
2
|
+
|
|
1
3
|
module Rein
|
|
2
4
|
module Constraint
|
|
3
5
|
# This module contains methods for defining null constraints.
|
|
4
6
|
module Null
|
|
5
7
|
include ActiveRecord::ConnectionAdapters::Quoting
|
|
6
|
-
include Rein::Constraint::Options
|
|
7
8
|
|
|
8
|
-
def add_null_constraint(
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
def add_null_constraint(*args)
|
|
10
|
+
reversible do |dir|
|
|
11
|
+
dir.up { _add_null_constraint(*args) }
|
|
12
|
+
dir.down { _remove_null_constraint(*args) }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def remove_null_constraint(*args)
|
|
17
|
+
reversible do |dir|
|
|
18
|
+
dir.up { _remove_null_constraint(*args) }
|
|
19
|
+
dir.down { _add_null_constraint(*args) }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def _add_null_constraint(table, attribute, options = {})
|
|
26
|
+
name = Util.constraint_name(table, attribute, "null", options)
|
|
27
|
+
conditions = Util.conditions_with_if("#{attribute} IS NOT NULL", options)
|
|
11
28
|
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
|
|
12
29
|
end
|
|
13
30
|
|
|
14
|
-
def
|
|
15
|
-
name = constraint_name(table, attribute, options)
|
|
31
|
+
def _remove_null_constraint(table, attribute, options = {})
|
|
32
|
+
name = Util.constraint_name(table, attribute, "null", options)
|
|
16
33
|
execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
|
|
17
34
|
end
|
|
18
35
|
end
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
require "rein/util"
|
|
2
|
+
|
|
1
3
|
module Rein
|
|
2
4
|
module Constraint
|
|
3
5
|
# This module contains methods for defining numericality constraints.
|
|
4
6
|
module Numericality
|
|
5
|
-
include Rein::Constraint::Options
|
|
6
|
-
|
|
7
7
|
OPERATORS = {
|
|
8
8
|
greater_than: :>,
|
|
9
9
|
greater_than_or_equal_to: :>=,
|
|
@@ -13,21 +13,34 @@ module Rein
|
|
|
13
13
|
less_than_or_equal_to: :<=
|
|
14
14
|
}.freeze
|
|
15
15
|
|
|
16
|
-
def add_numericality_constraint(
|
|
17
|
-
|
|
16
|
+
def add_numericality_constraint(*args)
|
|
17
|
+
reversible do |dir|
|
|
18
|
+
dir.up { _add_numericality_constraint(*args) }
|
|
19
|
+
dir.down { _remove_numericality_constraint(*args) }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def remove_numericality_constraint(*args)
|
|
24
|
+
reversible do |dir|
|
|
25
|
+
dir.up { _remove_numericality_constraint(*args) }
|
|
26
|
+
dir.down { _add_numericality_constraint(*args) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
18
31
|
|
|
32
|
+
def _add_numericality_constraint(table, attribute, options = {})
|
|
33
|
+
name = Util.constraint_name(table, attribute, "numericality", options)
|
|
19
34
|
conditions = OPERATORS.slice(*options.keys).map do |key, operator|
|
|
20
35
|
value = options[key]
|
|
21
36
|
[attribute, operator, value].join(" ")
|
|
22
37
|
end.join(" AND ")
|
|
23
|
-
|
|
24
|
-
conditions = conditions_with_if(conditions, options)
|
|
25
|
-
|
|
38
|
+
conditions = Util.conditions_with_if(conditions, options)
|
|
26
39
|
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
|
|
27
40
|
end
|
|
28
41
|
|
|
29
|
-
def
|
|
30
|
-
name = constraint_name(table, attribute, options)
|
|
42
|
+
def _remove_numericality_constraint(table, attribute, options = {})
|
|
43
|
+
name = Util.constraint_name(table, attribute, "numericality", options)
|
|
31
44
|
execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
|
|
32
45
|
end
|
|
33
46
|
end
|
|
@@ -1,21 +1,38 @@
|
|
|
1
|
+
require "rein/util"
|
|
2
|
+
|
|
1
3
|
module Rein
|
|
2
4
|
module Constraint
|
|
3
5
|
# This module contains methods for defining presence constraints.
|
|
4
6
|
module Presence
|
|
5
7
|
include ActiveRecord::ConnectionAdapters::Quoting
|
|
6
|
-
include Rein::Constraint::Options
|
|
7
8
|
|
|
8
|
-
def add_presence_constraint(
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
def add_presence_constraint(*args)
|
|
10
|
+
reversible do |dir|
|
|
11
|
+
dir.up { _add_presence_constraint(*args) }
|
|
12
|
+
dir.down { _remove_presence_constraint(*args) }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def remove_presence_constraint(*args)
|
|
17
|
+
reversible do |dir|
|
|
18
|
+
dir.up { _remove_presence_constraint(*args) }
|
|
19
|
+
dir.down { _add_presence_constraint(*args) }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def _add_presence_constraint(table, attribute, options = {})
|
|
26
|
+
name = Util.constraint_name(table, attribute, "presence", options)
|
|
27
|
+
conditions = Util.conditions_with_if(
|
|
11
28
|
"(#{attribute} IS NOT NULL) AND (#{attribute} !~ '^\\s*$')",
|
|
12
29
|
options
|
|
13
30
|
)
|
|
14
31
|
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
|
|
15
32
|
end
|
|
16
33
|
|
|
17
|
-
def
|
|
18
|
-
name = constraint_name(table, attribute, options)
|
|
34
|
+
def _remove_presence_constraint(table, attribute, options = {})
|
|
35
|
+
name = Util.constraint_name(table, attribute, "presence", options)
|
|
19
36
|
execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
|
|
20
37
|
end
|
|
21
38
|
end
|
|
@@ -2,10 +2,30 @@ module Rein
|
|
|
2
2
|
module Constraint
|
|
3
3
|
# This module contains methods for defining primary key constraints.
|
|
4
4
|
module PrimaryKey
|
|
5
|
-
def add_primary_key(
|
|
5
|
+
def add_primary_key(*args)
|
|
6
|
+
reversible do |dir|
|
|
7
|
+
dir.up { _add_primary_key(*args) }
|
|
8
|
+
dir.down { _remove_primary_key(*args) }
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def remove_primary_key(*args)
|
|
13
|
+
reversible do |dir|
|
|
14
|
+
dir.up { _remove_primary_key(*args) }
|
|
15
|
+
dir.down { _add_primary_key(*args) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def _add_primary_key(table, options = {})
|
|
22
|
+
attribute = (options[:column] || "id").to_sym
|
|
23
|
+
execute("ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def _remove_primary_key(table, options = {})
|
|
6
27
|
attribute = (options[:column] || "id").to_sym
|
|
7
|
-
|
|
8
|
-
execute(sql)
|
|
28
|
+
execute("ALTER TABLE #{table} DROP CONSTRAINT #{attribute}_pkey")
|
|
9
29
|
end
|
|
10
30
|
end
|
|
11
31
|
end
|
data/lib/rein/schema.rb
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
module Rein
|
|
2
2
|
# This module contains methods for creating/dropping schemas.
|
|
3
3
|
module Schema
|
|
4
|
-
def create_schema(
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
def create_schema(*args)
|
|
5
|
+
reversible do |dir|
|
|
6
|
+
dir.up { _create_schema(*args) }
|
|
7
|
+
dir.down { _drop_schema(*args) }
|
|
8
|
+
end
|
|
7
9
|
end
|
|
8
10
|
|
|
9
|
-
def drop_schema(
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
def drop_schema(*args)
|
|
12
|
+
reversible do |dir|
|
|
13
|
+
dir.up { _drop_schema(*args) }
|
|
14
|
+
dir.down { _create_schema(*args) }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def _create_schema(name)
|
|
21
|
+
execute("CREATE SCHEMA #{name}")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def _drop_schema(name)
|
|
25
|
+
execute("DROP SCHEMA #{name}")
|
|
12
26
|
end
|
|
13
27
|
end
|
|
14
28
|
end
|
data/lib/rein/type/enum.rb
CHANGED
|
@@ -6,18 +6,34 @@ module Rein
|
|
|
6
6
|
module Enum
|
|
7
7
|
include ActiveRecord::ConnectionAdapters::Quoting
|
|
8
8
|
|
|
9
|
-
def create_enum_type(
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
def create_enum_type(*args)
|
|
10
|
+
reversible do |dir|
|
|
11
|
+
dir.up { _create_enum_type(*args) }
|
|
12
|
+
dir.down { _drop_enum_type(*args) }
|
|
13
|
+
end
|
|
12
14
|
end
|
|
13
15
|
|
|
14
|
-
def drop_enum_type(
|
|
15
|
-
|
|
16
|
+
def drop_enum_type(*args)
|
|
17
|
+
reversible do |dir|
|
|
18
|
+
dir.up { _drop_enum_type(*args) }
|
|
19
|
+
dir.down { _create_enum_type(*args) }
|
|
20
|
+
end
|
|
16
21
|
end
|
|
17
22
|
|
|
18
23
|
def add_enum_value(enum_name, new_value)
|
|
19
24
|
execute("ALTER TYPE #{enum_name} ADD VALUE #{quote(new_value)}")
|
|
20
25
|
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def _create_enum_type(enum_name, enum_values = [])
|
|
30
|
+
enum_values = enum_values.map { |value| quote(value) }.join(", ")
|
|
31
|
+
execute("CREATE TYPE #{enum_name} AS ENUM (#{enum_values})")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def _drop_enum_type(enum_name)
|
|
35
|
+
execute("DROP TYPE #{enum_name}")
|
|
36
|
+
end
|
|
21
37
|
end
|
|
22
38
|
end
|
|
23
39
|
end
|
data/lib/rein/util.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Rein
|
|
2
|
+
module Constraint
|
|
3
|
+
# The {Util} module provides utility methods for handling options.
|
|
4
|
+
module Util
|
|
5
|
+
def self.conditions_with_if(conditions, options = {})
|
|
6
|
+
if options[:if].present?
|
|
7
|
+
"NOT (#{options[:if]}) OR (#{conditions})"
|
|
8
|
+
else
|
|
9
|
+
conditions
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.constraint_name(table, attribute, suffix, options = {})
|
|
14
|
+
options[:name].presence || "#{table}_#{attribute}_#{suffix}"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/rein/version.rb
CHANGED
data/lib/rein/view.rb
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
module Rein
|
|
2
2
|
# This module contains methods for creating/dropping views.
|
|
3
3
|
module View
|
|
4
|
-
def create_view(
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
def create_view(*args)
|
|
5
|
+
reversible do |dir|
|
|
6
|
+
dir.up { _create_view(*args) }
|
|
7
|
+
dir.down { _drop_view(*args) }
|
|
8
|
+
end
|
|
7
9
|
end
|
|
8
10
|
|
|
9
|
-
def drop_view(
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
def drop_view(*args)
|
|
12
|
+
reversible do |dir|
|
|
13
|
+
dir.up { _drop_view(*args) }
|
|
14
|
+
dir.down { _create_view(*args) }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def _create_view(view_name, sql)
|
|
21
|
+
execute("CREATE VIEW #{view_name} AS #{sql}")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def _drop_view(view_name)
|
|
25
|
+
execute("DROP VIEW #{view_name}")
|
|
12
26
|
end
|
|
13
27
|
end
|
|
14
28
|
end
|
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: 3.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: 2017-04-
|
|
11
|
+
date: 2017-04-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -149,11 +149,11 @@ files:
|
|
|
149
149
|
- lib/rein/constraint/inclusion.rb
|
|
150
150
|
- lib/rein/constraint/null.rb
|
|
151
151
|
- lib/rein/constraint/numericality.rb
|
|
152
|
-
- lib/rein/constraint/options.rb
|
|
153
152
|
- lib/rein/constraint/presence.rb
|
|
154
153
|
- lib/rein/constraint/primary_key.rb
|
|
155
154
|
- lib/rein/schema.rb
|
|
156
155
|
- lib/rein/type/enum.rb
|
|
156
|
+
- lib/rein/util.rb
|
|
157
157
|
- lib/rein/version.rb
|
|
158
158
|
- lib/rein/view.rb
|
|
159
159
|
homepage: http://github.com/nullobject/rein
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
module Rein
|
|
2
|
-
module Constraint
|
|
3
|
-
# This module defines methods for handling options command to several constraints.
|
|
4
|
-
module Options
|
|
5
|
-
def conditions_with_if(conditions, options = {})
|
|
6
|
-
if options[:if].present?
|
|
7
|
-
"NOT (#{options[:if]}) OR (#{conditions})"
|
|
8
|
-
else
|
|
9
|
-
conditions
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def constraint_name(table, attribute, options = {})
|
|
14
|
-
options[:name].presence || "#{table}_#{attribute}"
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|