postgresql-check 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +9 -9
- data/Rakefile +0 -1
- data/lib/postgresql/check/schema_definitions.rb +1 -1
- data/lib/postgresql/check/schema_dumper.rb +8 -8
- data/lib/postgresql/check/schema_statements.rb +19 -18
- data/lib/postgresql/check/table.rb +3 -3
- data/lib/postgresql/check/table_definition.rb +1 -1
- data/lib/postgresql/check/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f3dc30a6f85cf5072a81f21125ec4bba9a527b9
|
4
|
+
data.tar.gz: 819358b14d23000061fff2a17e4531bd74efe768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 845c9557db6a072a53c2046f2f08dd0b9c1bedfc46468aa386395a9d4d58753ba662efaba39a8742697d11e9f62ae218a02e91cf9d26818379a57e807f7370ca
|
7
|
+
data.tar.gz: e44349720d60291552b1e96ad39b6a545690746133178c7ab3cb7dec170ed1a7c8bec847936c85c094f17445b78f42b4043d1a452b546f14df6ecf5675cedd9c
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -31,21 +31,21 @@ Or install it yourself as:
|
|
31
31
|
|
32
32
|
Two new methods are introduced to migrations:
|
33
33
|
|
34
|
-
* `add_check
|
35
|
-
* `remove_check
|
34
|
+
* `add_check table_name, condition, name: constraint_name`
|
35
|
+
* `remove_check table_name, name: constraint_name`
|
36
36
|
|
37
37
|
Given the following model:
|
38
38
|
|
39
39
|
```ruby
|
40
40
|
class Product < ActiveRecord::Base
|
41
|
-
validates :price, :
|
41
|
+
validates :price, numericality: { greater_than: 0 }
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
45
|
You can add a check constraint in your migration:
|
46
46
|
|
47
47
|
```ruby
|
48
|
-
add_check :products, 'price > 0', :
|
48
|
+
add_check :products, 'price > 0', name: 'products_price_check'
|
49
49
|
```
|
50
50
|
|
51
51
|
The code above generates following SQL:
|
@@ -59,7 +59,7 @@ ALTER TABLE "products" ADD CONSTRAINT "products_price_check" (price > 0)
|
|
59
59
|
To remove constraint use `remove_check` method:
|
60
60
|
|
61
61
|
```ruby
|
62
|
-
remove_check :products, :
|
62
|
+
remove_check :products, name: 'products_price_check'
|
63
63
|
```
|
64
64
|
|
65
65
|
## Change Table methods
|
@@ -68,8 +68,8 @@ This gem adds extra methods to `create_table` and `change_table`:
|
|
68
68
|
|
69
69
|
```ruby
|
70
70
|
create_table :products do |t|
|
71
|
-
t.decimal :price, :
|
72
|
-
t.check 'price > 0', :
|
71
|
+
t.decimal :price, null: false
|
72
|
+
t.check 'price > 0', name: 'products_price_check'
|
73
73
|
end
|
74
74
|
```
|
75
75
|
|
@@ -77,7 +77,7 @@ Remove a check constraint:
|
|
77
77
|
|
78
78
|
```ruby
|
79
79
|
change_table :products do |t|
|
80
|
-
t.remove_check :
|
80
|
+
t.remove_check name: 'products_price_check'
|
81
81
|
end
|
82
82
|
```
|
83
83
|
|
@@ -89,7 +89,7 @@ end
|
|
89
89
|
|
90
90
|
## Contributing
|
91
91
|
|
92
|
-
1. Fork it ( https://github.com/
|
92
|
+
1. Fork it ( https://github.com/take-five/postgresql-check/fork )
|
93
93
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
94
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
95
95
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ module Postgresql
|
|
2
2
|
module Check
|
3
3
|
module SchemaDefinitions
|
4
4
|
def self.included(base)
|
5
|
-
if ActiveRecord::VERSION::STRING >
|
5
|
+
if ActiveRecord::VERSION::STRING > '4.2'
|
6
6
|
ActiveRecord::ConnectionAdapters::PostgreSQL::Table.class_eval do
|
7
7
|
include Postgresql::Check::Table
|
8
8
|
end
|
@@ -8,27 +8,27 @@ module Postgresql
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def table_with_checks(table, stream)
|
11
|
-
table_without_checks
|
12
|
-
check_constraints
|
11
|
+
table_without_checks table, stream
|
12
|
+
check_constraints table, stream
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
16
16
|
def check_constraints(table, stream)
|
17
17
|
if (checks = @connection.checks(table)).any?
|
18
18
|
definitions = checks.map do |check|
|
19
|
-
dump_check_constraint
|
19
|
+
dump_check_constraint check
|
20
20
|
end
|
21
21
|
|
22
|
-
stream.puts
|
23
22
|
stream.puts definitions.join("\n")
|
24
|
-
stream.puts
|
23
|
+
stream.puts "\n"
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
27
|
def dump_check_constraint(check)
|
29
|
-
|
30
|
-
|
28
|
+
<<-RUBY.chomp
|
29
|
+
add_check "#{remove_prefix_and_suffix(check.table_name)}", "#{check.condition}", name: "#{check.name}"
|
30
|
+
RUBY
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
end
|
34
|
+
end
|
@@ -12,10 +12,10 @@ module Postgresql
|
|
12
12
|
# by +:name+ options in +options+ argument.
|
13
13
|
#
|
14
14
|
# @example
|
15
|
-
# add_check :products, 'price > 0', :
|
15
|
+
# add_check :products, 'price > 0', name: 'products_price_check'
|
16
16
|
#
|
17
17
|
# # Generates:
|
18
|
-
# # ALTER TABLE products ADD CONSTRAINT
|
18
|
+
# # ALTER TABLE products ADD CONSTRAINT products_price_check CHECK (price > 0)
|
19
19
|
#
|
20
20
|
# @note +:name+ option is mandatory.
|
21
21
|
#
|
@@ -26,11 +26,11 @@ module Postgresql
|
|
26
26
|
def add_check(table_name, condition, options)
|
27
27
|
name = options.fetch(:name) { raise 'add_check, :name option required' }
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
execute <<-SQL
|
30
|
+
ALTER TABLE #{quote_table_name(table_name)}
|
31
|
+
ADD CONSTRAINT #{quote_column_name(name)}
|
32
|
+
CHECK (#{condition})
|
33
|
+
SQL
|
34
34
|
end
|
35
35
|
|
36
36
|
# Remove constraint with given name from table. Constraint name
|
@@ -48,38 +48,39 @@ module Postgresql
|
|
48
48
|
def remove_check(table_name, options)
|
49
49
|
name = options.fetch(:name) { raise 'remove_check, :name option required' }
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
execute <<-SQL
|
52
|
+
ALTER TABLE #{quote_table_name(table_name)}
|
53
|
+
DROP CONSTRAINT #{quote_column_name(name)}
|
54
|
+
SQL
|
55
55
|
end
|
56
56
|
|
57
57
|
# @api private
|
58
|
-
def create_table_with_checks(table_name, *args
|
58
|
+
def create_table_with_checks(table_name, *args)
|
59
59
|
definition = nil
|
60
60
|
|
61
61
|
create_table_without_checks(table_name, *args) do |td|
|
62
62
|
definition = td # trick to get the definition
|
63
|
-
|
63
|
+
yield td if block_given?
|
64
64
|
end
|
65
65
|
|
66
66
|
definition.checks.each do |condition, options|
|
67
|
-
add_check
|
67
|
+
add_check table_name, condition, options
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
71
|
# @api private
|
72
72
|
def checks(table_name)
|
73
|
-
checks_info = select_all
|
73
|
+
checks_info = select_all <<-SQL
|
74
74
|
SELECT c.conname, c.consrc
|
75
75
|
FROM pg_constraint c
|
76
|
-
JOIN pg_class t
|
76
|
+
INNER JOIN pg_class t
|
77
|
+
ON c.conrelid = t.oid
|
77
78
|
WHERE c.contype = 'c'
|
78
79
|
AND t.relname = '#{table_name}'
|
79
|
-
|
80
|
+
SQL
|
80
81
|
|
81
82
|
checks_info.map do |row|
|
82
|
-
Constraint.new
|
83
|
+
Constraint.new table_name, row['conname'], row['consrc']
|
83
84
|
end
|
84
85
|
end
|
85
86
|
end
|
@@ -2,17 +2,17 @@ module Postgresql
|
|
2
2
|
module Check
|
3
3
|
module Table
|
4
4
|
def check(condition, options)
|
5
|
-
@base.add_check
|
5
|
+
@base.add_check check_table_name, condition, options
|
6
6
|
end
|
7
7
|
|
8
8
|
def remove_check(options)
|
9
|
-
@base.remove_check
|
9
|
+
@base.remove_check check_table_name, options
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
13
|
# @api private
|
14
14
|
def check_table_name
|
15
|
-
if ActiveRecord::VERSION::STRING >
|
15
|
+
if ActiveRecord::VERSION::STRING > '4.2'
|
16
16
|
@name
|
17
17
|
else
|
18
18
|
@table_name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postgresql-check
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexei Mikhailov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|