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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1048872dde6eb9e2fe0bdbce34da53c019ecc62
4
- data.tar.gz: 223f215b01978b726f72d21146cb3c5dcca3ccb8
3
+ metadata.gz: 3f3dc30a6f85cf5072a81f21125ec4bba9a527b9
4
+ data.tar.gz: 819358b14d23000061fff2a17e4531bd74efe768
5
5
  SHA512:
6
- metadata.gz: e09208d765f0717700ca88e88da3d3b5ea17c477e511fccd053ecaecd0d0d77e60c30130c4e3e0f8efe8c173e6d8d518f47a1d94ed8c128b6fa44a133dd679e2
7
- data.tar.gz: dce6d419be2203db356b60ae728378c868ecd9eaac878a493fef165645ec25360073c6bc2ebf9b01ad2e0f002ef3868fca2d67c0aa33df497db4604db5950ccc
6
+ metadata.gz: 845c9557db6a072a53c2046f2f08dd0b9c1bedfc46468aa386395a9d4d58753ba662efaba39a8742697d11e9f62ae218a02e91cf9d26818379a57e807f7370ca
7
+ data.tar.gz: e44349720d60291552b1e96ad39b6a545690746133178c7ab3cb7dec170ed1a7c8bec847936c85c094f17445b78f42b4043d1a452b546f14df6ecf5675cedd9c
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Alexei Mikhailov
1
+ Copyright (c) 2015 Alexei Mikhailov
2
2
 
3
3
  MIT License
4
4
 
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(table_name, condition, :name => constraint_name)`
35
- * `remove_check(table_name, :name => constraint_name)`
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, :numericality => {:greater_than => 0}
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', :name => 'products_price_check'
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, :name => 'products_price_check'
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, :null => false
72
- t.check 'price > 0', :name => 'products_price_check'
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 :name => 'products_price_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/[my-github-username]/postgresql-check/fork )
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
@@ -1,2 +1 @@
1
1
  require "bundler/gem_tasks"
2
-
@@ -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 > "4.2"
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(table, stream)
12
- check_constraints(table, stream)
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(check)
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
- ' add_check ' + remove_prefix_and_suffix(check.table_name).inspect + ', '+
30
- check.condition.inspect + ', name: ' + check.name.inspect
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', :name => 'products_price_chk'
15
+ # add_check :products, 'price > 0', name: 'products_price_check'
16
16
  #
17
17
  # # Generates:
18
- # # ALTER TABLE products ADD CONSTRAINT products_price_chk CHECK (price > 0)
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
- sql = "ALTER TABLE #{quote_table_name(table_name)} " +
30
- "ADD CONSTRAINT #{quote_column_name(name)} " +
31
- "CHECK (#{condition})"
32
-
33
- execute(sql)
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
- sql = "ALTER TABLE #{quote_table_name(table_name)} " +
52
- "DROP CONSTRAINT #{quote_column_name(name)}"
53
-
54
- execute(sql)
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, &block)
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
- block.call(td) unless block.nil?
63
+ yield td if block_given?
64
64
  end
65
65
 
66
66
  definition.checks.each do |condition, options|
67
- add_check(table_name, condition, options)
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 ON c.conrelid = t.oid
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(table_name, row['conname'], row['consrc'])
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(check_table_name, condition, options)
5
+ @base.add_check check_table_name, condition, options
6
6
  end
7
7
 
8
8
  def remove_check(options)
9
- @base.remove_check(check_table_name, options)
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 > "4.2"
15
+ if ActiveRecord::VERSION::STRING > '4.2'
16
16
  @name
17
17
  else
18
18
  @table_name
@@ -6,7 +6,7 @@ module Postgresql
6
6
  # Example:
7
7
  # create_table :goods do |t|
8
8
  # t.float :price
9
- # t.check 'price > 0', :name => 'price_gt_0'
9
+ # t.check 'price > 0', :name => 'goods_price_gt_0_check'
10
10
  # end
11
11
  def check(condition, options)
12
12
  checks << [condition, options]
@@ -1,5 +1,5 @@
1
1
  module Postgresql
2
2
  module Check
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
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.0
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-19 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord