rein 3.3.0 → 3.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c092acce07a6896465dbff427aa22bbac84a4b9
4
- data.tar.gz: 7cdc3ad273b86b172930826e82264d29c938326a
3
+ metadata.gz: aaa40d32186ff9860ca6bec250fbd45b907d4806
4
+ data.tar.gz: a665283cac4d00701e507f057e4f61d7153c2041
5
5
  SHA512:
6
- metadata.gz: 591a05bf15e0857934e00ec98cc91644fed9d5ba5d793dfd8216e9cf51b5ecf797e16f7e78cd43aaef887299b079b7d0c2702c4210f742b38e385c604083b378
7
- data.tar.gz: cd84ac0a68c41f37969debd3c719e502750f002725287f72a2e829bcce58a998239935d503144553d7d5ab48444c2675a335913e72472b36754aa95532f2f7bb
6
+ metadata.gz: b3c07e416e2e6d633b962317e277242feee7f8c7c2b2570734e695a37a090109e507eabd3b438e406e12ffeb2d4e795e770b9b03633df6ba8b438e40436fb7ee
7
+ data.tar.gz: 156fcb4153daf846be853980248472d2bdb6d0e2db83581acd3fedc81bc8cdcd5ab26b6bae0b14b7bd971caa38e36e90c455689978805ec8e53af40382c1b578
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.4.0 / 2018-03-23
4
+
5
+ * Wrap column and table names in foreign key constraints
6
+
3
7
  ## 3.3.0
4
8
  - Add unique constraint.
5
9
 
data/README.md CHANGED
@@ -32,6 +32,7 @@ advantage of reversible Rails migrations.
32
32
  * [Views](#views)
33
33
  * [Schemas](#schemas)
34
34
  * [Examples](#examples)
35
+ * [Contribute](#contribute)
35
36
  * [License](#license)
36
37
 
37
38
  ## Getting Started
@@ -498,6 +499,19 @@ class CreateArchivedBooksTable < ActiveRecord::Migration
498
499
  end
499
500
  ```
500
501
 
502
+ ## Contribute
503
+
504
+ PRs are always welcome! :heart: To work with rein, there is a
505
+ [Makefile](https://en.wikipedia.org/wiki/Makefile) to keep things simple.
506
+
507
+ Before you do anything, you'll need to bootstrap your environment:
508
+
509
+ make config
510
+
511
+ Make sure you run the tests before submitting a PR:
512
+
513
+ make test
514
+
501
515
  ## License
502
516
 
503
517
  Rein is licensed under the [MIT License](/LICENSE.md).
@@ -25,10 +25,10 @@ module Rein
25
25
  referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym
26
26
  referenced_attribute = (options[:referenced] || 'id').to_sym
27
27
  name = Util.constraint_name(referencing_table, referencing_attribute, 'fk', options)
28
- sql = "ALTER TABLE #{referencing_table}"
28
+ sql = "ALTER TABLE #{Util.wrap_identifier(referencing_table)}"
29
29
  sql << " ADD CONSTRAINT #{name}"
30
- sql << " FOREIGN KEY (#{referencing_attribute})"
31
- sql << " REFERENCES #{referenced_table} (#{referenced_attribute})"
30
+ sql << " FOREIGN KEY (#{Util.wrap_identifier(referencing_attribute)})"
31
+ sql << " REFERENCES #{referenced_table} (#{Util.wrap_identifier(referenced_attribute)})"
32
32
  sql << " ON DELETE #{referential_action(options[:on_delete])}" if options[:on_delete].present?
33
33
  sql << " ON UPDATE #{referential_action(options[:on_update])}" if options[:on_update].present?
34
34
  execute(sql)
@@ -38,7 +38,7 @@ module Rein
38
38
  def _remove_foreign_key_constraint(referencing_table, referenced_table, options = {})
39
39
  referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym
40
40
  name = Util.constraint_name(referencing_table, referencing_attribute, 'fk', options)
41
- execute("ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}")
41
+ execute("ALTER TABLE #{Util.wrap_identifier(referencing_table)} DROP CONSTRAINT #{name}")
42
42
  remove_index(referencing_table, referencing_attribute) if options[:index] == true
43
43
  end
44
44
 
@@ -24,13 +24,16 @@ module Rein
24
24
 
25
25
  def _add_inclusion_constraint(table, attribute, options = {})
26
26
  name = Util.constraint_name(table, attribute, 'inclusion', options)
27
+ table = Util.wrap_identifier(table)
27
28
  values = options[:in].map { |value| quote(value) }.join(', ')
29
+ attribute = Util.wrap_identifier(attribute)
28
30
  conditions = Util.conditions_with_if("#{attribute} IN (#{values})", options)
29
31
  execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
30
32
  end
31
33
 
32
34
  def _remove_inclusion_constraint(table, attribute, options = {})
33
35
  name = Util.constraint_name(table, attribute, 'inclusion', options)
36
+ table = Util.wrap_identifier(table)
34
37
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
35
38
  end
36
39
  end
@@ -31,6 +31,8 @@ module Rein
31
31
 
32
32
  def _add_length_constraint(table, attribute, options = {})
33
33
  name = Util.constraint_name(table, attribute, 'length', options)
34
+ table = Util.wrap_identifier(table)
35
+ attribute = Util.wrap_identifier(attribute)
34
36
  attribute_length = "length(#{attribute})"
35
37
  conditions = OPERATORS.slice(*options.keys).map { |key, operator|
36
38
  value = options[key]
@@ -42,6 +44,7 @@ module Rein
42
44
 
43
45
  def _remove_length_constraint(table, attribute, options = {})
44
46
  name = Util.constraint_name(table, attribute, 'length', options)
47
+ table = Util.wrap_identifier(table)
45
48
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
46
49
  end
47
50
  end
@@ -29,6 +29,8 @@ module Rein
29
29
 
30
30
  def _add_match_constraint(table, attribute, options = {})
31
31
  name = Util.constraint_name(table, attribute, 'match', options)
32
+ table = Util.wrap_identifier(table)
33
+ attribute = Util.wrap_identifier(attribute)
32
34
  conditions = OPERATORS.slice(*options.keys).map { |key, operator|
33
35
  value = options[key]
34
36
  [attribute, operator, "'#{value}'"].join(' ')
@@ -39,6 +41,7 @@ module Rein
39
41
 
40
42
  def _remove_match_constraint(table, attribute, options = {})
41
43
  name = Util.constraint_name(table, attribute, 'match', options)
44
+ table = Util.wrap_identifier(table)
42
45
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
43
46
  end
44
47
  end
@@ -24,12 +24,15 @@ module Rein
24
24
 
25
25
  def _add_null_constraint(table, attribute, options = {})
26
26
  name = Util.constraint_name(table, attribute, 'null', options)
27
+ table = Util.wrap_identifier(table)
28
+ attribute = Util.wrap_identifier(attribute)
27
29
  conditions = Util.conditions_with_if("#{attribute} IS NOT NULL", options)
28
30
  execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
29
31
  end
30
32
 
31
33
  def _remove_null_constraint(table, attribute, options = {})
32
34
  name = Util.constraint_name(table, attribute, 'null', options)
35
+ table = Util.wrap_identifier(table)
33
36
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
34
37
  end
35
38
  end
@@ -31,6 +31,8 @@ module Rein
31
31
 
32
32
  def _add_numericality_constraint(table, attribute, options = {})
33
33
  name = Util.constraint_name(table, attribute, 'numericality', options)
34
+ table = Util.wrap_identifier(table)
35
+ attribute = Util.wrap_identifier(attribute)
34
36
  conditions = OPERATORS.slice(*options.keys).map { |key, operator|
35
37
  value = options[key]
36
38
  [attribute, operator, value].join(' ')
@@ -41,6 +43,7 @@ module Rein
41
43
 
42
44
  def _remove_numericality_constraint(table, attribute, options = {})
43
45
  name = Util.constraint_name(table, attribute, 'numericality', options)
46
+ table = Util.wrap_identifier(table)
44
47
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
45
48
  end
46
49
  end
@@ -24,6 +24,8 @@ module Rein
24
24
 
25
25
  def _add_presence_constraint(table, attribute, options = {})
26
26
  name = Util.constraint_name(table, attribute, 'presence', options)
27
+ table = Util.wrap_identifier(table)
28
+ attribute = Util.wrap_identifier(attribute)
27
29
  conditions = Util.conditions_with_if(
28
30
  "(#{attribute} IS NOT NULL) AND (#{attribute} !~ '^\\s*$')",
29
31
  options
@@ -33,6 +35,7 @@ module Rein
33
35
 
34
36
  def _remove_presence_constraint(table, attribute, options = {})
35
37
  name = Util.constraint_name(table, attribute, 'presence', options)
38
+ table = Util.wrap_identifier(table)
36
39
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
37
40
  end
38
41
  end
@@ -19,11 +19,13 @@ module Rein
19
19
  private
20
20
 
21
21
  def _add_primary_key(table, options = {})
22
+ table = Util.wrap_identifier(table)
22
23
  attribute = (options[:column] || 'id').to_sym
23
24
  execute("ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})")
24
25
  end
25
26
 
26
27
  def _remove_primary_key(table, options = {})
28
+ table = Util.wrap_identifier(table)
27
29
  attribute = (options[:column] || 'id').to_sym
28
30
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{attribute}_pkey")
29
31
  end
@@ -25,6 +25,8 @@ module Rein
25
25
  def _add_unique_constraint(table, attributes, options = {})
26
26
  attributes = [attributes].flatten
27
27
  name = Util.constraint_name(table, attributes.join('_'), 'unique', options)
28
+ table = Util.wrap_identifier(table)
29
+ attributes = attributes.map { |attribute| Util.wrap_identifier(attribute) }
28
30
  initially = options[:deferred] ? 'DEFERRED' : 'IMMEDIATE'
29
31
  sql = "ALTER TABLE #{table} ADD CONSTRAINT #{name} UNIQUE (#{attributes.join(', ')})"
30
32
  sql << " DEFERRABLE INITIALLY #{initially}" unless options[:deferrable] == false
@@ -34,6 +36,7 @@ module Rein
34
36
  def _remove_unique_constraint(table, attributes, options = {})
35
37
  attributes = [attributes].flatten
36
38
  name = Util.constraint_name(table, attributes.join('_'), 'unique', options)
39
+ table = Util.wrap_identifier(table)
37
40
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
38
41
  end
39
42
  end
@@ -1,17 +1,23 @@
1
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
2
+ # The {Util} module provides utility methods for handling options.
3
+ module Util
4
+ def self.conditions_with_if(conditions, options = {})
5
+ if options[:if].present?
6
+ "NOT (#{options[:if]}) OR (#{conditions})"
7
+ else
8
+ conditions
11
9
  end
10
+ end
11
+
12
+ def self.constraint_name(table, attribute, suffix, options = {})
13
+ options[:name].presence || "#{table}_#{attribute}_#{suffix}"
14
+ end
12
15
 
13
- def self.constraint_name(table, attribute, suffix, options = {})
14
- options[:name].presence || "#{table}_#{attribute}_#{suffix}"
16
+ def self.wrap_identifier(attribute)
17
+ if /^".*"$/.match?(attribute)
18
+ attribute
19
+ else
20
+ "\"#{attribute}\""
15
21
  end
16
22
  end
17
23
  end
@@ -1,3 +1,3 @@
1
1
  module Rein
2
- VERSION = '3.3.0'.freeze
2
+ VERSION = '3.4.0'.freeze
3
3
  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: 3.3.0
4
+ version: 3.4.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-12-22 00:00:00.000000000 Z
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord