pg_spec_helper 1.6.0 → 1.7.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
  SHA256:
3
- metadata.gz: 5c885ddde22232c3907a08da5411a55e09b1105a87afbac5aac0b2b443c63ee1
4
- data.tar.gz: a8220ca48f1dea784d20e45ecc74dfd0f464c8524a55240715bb7154dc6f8879
3
+ metadata.gz: 17fc16c3791eb6ee55c8e8e80d2ac6c4c2e79fd8b126d114463c50d38bb38bd2
4
+ data.tar.gz: 1982015231eec0884fc3bd7f5eeeae18b401f02140fa9aaafaa874a78bd602fb
5
5
  SHA512:
6
- metadata.gz: '08770ab6431e7b5d2f21b0443d88282400ea597d78b946fe5c68b4c4ff562ae7bd14e0ddb5db4c5353ec8954ee0472ce5e5a2858259186504469f0a6783d66df'
7
- data.tar.gz: f811831ee0dc0381a3252556d28c1ac85f476707ab3ac6fd3c951c0c2ecb985ea7d18a1daf663d27ce533c06cab94e91d0963a200a88590bae5004a14d64b0d7
6
+ metadata.gz: 28de3b1fe55f219949ffb1703c24b20d5de0157a8c44ff4bb749e176053e0d01cdd7ae5f2e75996bd457bb8121c0dd61db0f8ed4a69d1962758b8e6ed376c162
7
+ data.tar.gz: 17b7496d0ce6bad41fdeeb7fed9edbc1c2ec9469a31788f3709ef902711a59e1dd7a6a07578b5af76c93262ff310ae6ef05f9741745f1db26d8b536d70e56f93
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.7.1](https://github.com/craigulliott/pg_spec_helper/compare/v1.7.0...v1.7.1) (2023-08-06)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * adding missing error class ([1810743](https://github.com/craigulliott/pg_spec_helper/commit/181074326fbf63eb27760486b4f18e1ea11c86b9))
9
+
10
+ ## [1.7.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.6.0...v1.7.0) (2023-08-06)
11
+
12
+
13
+ ### Features
14
+
15
+ * adding support for "instead of" in triggers ([488b4ab](https://github.com/craigulliott/pg_spec_helper/commit/488b4ab5fb458db2c7e2f60395b12dcfa9559459))
16
+
3
17
  ## [1.6.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.5.0...v1.6.0) (2023-08-06)
4
18
 
5
19
 
@@ -8,7 +8,7 @@ class PGSpecHelper
8
8
  # create a column for the provided table
9
9
  def create_column schema_name, table_name, column_name, type, null = true
10
10
  # note the `type` is safe from sql_injection due to the validation above
11
- connection.exec(<<-SQL)
11
+ connection.exec(<<~SQL)
12
12
  ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
13
13
  ADD COLUMN #{connection.quote_ident column_name.to_s} #{sanitize_name type} #{null ? "" : "NOT NULL"}
14
14
  SQL
@@ -16,7 +16,7 @@ class PGSpecHelper
16
16
 
17
17
  # return an array of column names for the provided table
18
18
  def get_column_names schema_name, table_name
19
- rows = connection.exec_params(<<-SQL, [schema_name.to_s, table_name.to_s])
19
+ rows = connection.exec_params(<<~SQL, [schema_name.to_s, table_name.to_s])
20
20
  SELECT column_name
21
21
  FROM information_schema.columns
22
22
  WHERE table_schema = $1
@@ -28,7 +28,7 @@ class PGSpecHelper
28
28
 
29
29
  # return an array of column names for the provided table
30
30
  def is_column_nullable schema_name, table_name, column_name
31
- rows = connection.exec_params(<<-SQL, [schema_name.to_s, table_name.to_s, column_name.to_s])
31
+ rows = connection.exec_params(<<~SQL, [schema_name.to_s, table_name.to_s, column_name.to_s])
32
32
  SELECT is_nullable
33
33
  FROM information_schema.columns
34
34
  WHERE table_schema = $1
@@ -7,7 +7,7 @@ class PGSpecHelper
7
7
  column_names_sql = column_names.map { |n| sanitize_name n }.join(", ")
8
8
  foreign_column_names_sql = foreign_column_names.map { |n| sanitize_name n }.join(", ")
9
9
  # add the foreign key
10
- connection.exec(<<-SQL)
10
+ connection.exec(<<~SQL)
11
11
  ALTER TABLE #{sanitize_name schema_name}.#{sanitize_name table_name}
12
12
  ADD CONSTRAINT #{sanitize_name foreign_key_name}
13
13
  FOREIGN KEY (#{column_names_sql})
@@ -17,7 +17,7 @@ class PGSpecHelper
17
17
 
18
18
  # returns a list of foreign keys for the provided table
19
19
  def get_foreign_key_names schema_name, table_name
20
- rows = connection.exec_params(<<-SQL, [schema_name.to_s, table_name.to_s])
20
+ rows = connection.exec_params(<<~SQL, [schema_name.to_s, table_name.to_s])
21
21
  SELECT constraint_name
22
22
  FROM information_schema.table_constraints
23
23
  WHERE table_schema = $1
@@ -4,7 +4,7 @@ class PGSpecHelper
4
4
  module Functions
5
5
  # create a function
6
6
  def create_function schema_name, function_name, function_definition
7
- connection.exec <<-SQL
7
+ connection.exec <<~SQL
8
8
  CREATE FUNCTION #{schema_name}.#{function_name}() returns trigger language plpgsql AS $$
9
9
  BEGIN
10
10
  #{function_definition};
@@ -16,7 +16,7 @@ class PGSpecHelper
16
16
  # return a list of function names for the provided schema
17
17
  def get_function_names schema_name
18
18
  # get the function names
19
- rows = connection.exec(<<-SQL, [schema_name.to_s])
19
+ rows = connection.exec(<<~SQL, [schema_name.to_s])
20
20
  SELECT
21
21
  routine_name
22
22
  FROM
@@ -5,7 +5,7 @@ class PGSpecHelper
5
5
  # Create an index
6
6
  def create_index schema_name, table_name, column_names, index_name
7
7
  column_names_sql = column_names.map { |n| sanitize_name n }.join(", ")
8
- connection.exec(<<-SQL)
8
+ connection.exec(<<~SQL)
9
9
  CREATE INDEX #{connection.quote_ident index_name.to_s}
10
10
  ON #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s} (#{column_names_sql})
11
11
  SQL
@@ -13,7 +13,7 @@ class PGSpecHelper
13
13
 
14
14
  # get a list of index names for the provided table
15
15
  def get_index_names schema_name, table_name
16
- rows = connection.exec_params(<<-SQL, [schema_name.to_s, table_name.to_s])
16
+ rows = connection.exec_params(<<~SQL, [schema_name.to_s, table_name.to_s])
17
17
  SELECT indexname
18
18
  FROM pg_indexes
19
19
  WHERE schemaname = $1
@@ -6,7 +6,7 @@ class PGSpecHelper
6
6
  def create_primary_key schema_name, table_name, column_names, primary_key_name
7
7
  column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
8
8
  # add the primary_key
9
- connection.exec(<<-SQL)
9
+ connection.exec(<<~SQL)
10
10
  ALTER TABLE #{sanitize_name schema_name.to_s}.#{sanitize_name table_name.to_s}
11
11
  ADD CONSTRAINT #{sanitize_name primary_key_name}
12
12
  PRIMARY KEY (#{column_names_sql})
@@ -16,7 +16,7 @@ class PGSpecHelper
16
16
  # get the primary_key name for the provided table
17
17
  def get_primary_key_name schema_name, table_name
18
18
  # get the primary_key name
19
- rows = connection.exec(<<-SQL, [schema_name.to_s, table_name.to_s])
19
+ rows = connection.exec(<<~SQL, [schema_name.to_s, table_name.to_s])
20
20
  SELECT
21
21
  constraint_name
22
22
  FROM
@@ -4,7 +4,7 @@ class PGSpecHelper
4
4
  module Schemas
5
5
  # create a new schema in the database
6
6
  def create_schema schema_name
7
- connection.exec(<<-SQL)
7
+ connection.exec(<<~SQL)
8
8
  CREATE SCHEMA #{connection.quote_ident schema_name.to_s};
9
9
  SQL
10
10
  end
@@ -13,7 +13,7 @@ class PGSpecHelper
13
13
  def get_schema_names
14
14
  ignored_schemas_sql = ignored_schemas.map { |n| sanitize_name n }.join("', '")
15
15
  # return a list of the schema names from the database
16
- results = connection.exec(<<-SQL)
16
+ results = connection.exec(<<~SQL)
17
17
  SELECT schema_name
18
18
  FROM information_schema.schemata
19
19
  WHERE
@@ -28,7 +28,7 @@ class PGSpecHelper
28
28
  def delete_all_schemas
29
29
  # delete all schemas except public
30
30
  get_schema_names.reject { |schema_name| schema_name == :public }.each do |schema_name|
31
- connection.exec(<<-SQL)
31
+ connection.exec(<<~SQL)
32
32
  -- temporarily set the client_min_messages to WARNING to
33
33
  -- suppress the NOTICE messages about cascading deletes
34
34
  SET client_min_messages TO WARNING;
@@ -4,7 +4,7 @@ class PGSpecHelper
4
4
  module Tables
5
5
  # create a new table in the provided schema
6
6
  def create_table schema_name, table_name
7
- connection.exec(<<-SQL)
7
+ connection.exec(<<~SQL)
8
8
  CREATE TABLE #{sanitize_name schema_name.to_s}.#{sanitize_name table_name.to_s}(
9
9
  -- tables are created empty, and have columns added to them later
10
10
  );
@@ -13,7 +13,7 @@ class PGSpecHelper
13
13
 
14
14
  # return an array of table names for the provided schema
15
15
  def get_table_names schema_name
16
- rows = connection.exec_params(<<-SQL, [schema_name.to_s])
16
+ rows = connection.exec_params(<<~SQL, [schema_name.to_s])
17
17
  SELECT table_name FROM information_schema.tables
18
18
  WHERE
19
19
  table_schema = $1
@@ -26,7 +26,7 @@ class PGSpecHelper
26
26
  # delete all tables in the provided schema
27
27
  def delete_tables schema_name
28
28
  get_table_names(schema_name).each do |table_name|
29
- connection.exec(<<-SQL)
29
+ connection.exec(<<~SQL)
30
30
  -- temporarily set the client_min_messages to WARNING to
31
31
  -- suppress the NOTICE messages about cascading deletes
32
32
  SET client_min_messages TO WARNING;
@@ -6,18 +6,15 @@ class PGSpecHelper
6
6
  class UnexpectedEventManipulationError < StandardError
7
7
  end
8
8
 
9
- class UnexpectedActionOrderError < StandardError
10
- end
11
-
12
- class UnexpectedActionStatementError < StandardError
13
- end
14
-
15
9
  class UnexpectedActionOrientationError < StandardError
16
10
  end
17
11
 
18
12
  class UnexpectedActionTimingError < StandardError
19
13
  end
20
14
 
15
+ class UnexpectedConditionsError < StandardError
16
+ end
17
+
21
18
  # create a postgres trigger
22
19
  def create_trigger schema_name, table_name, name, action_timing:, event_manipulation:, action_orientation:, routine_schema:, routine_name:, action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil
23
20
  unless [:insert, :delete, :update].include? event_manipulation
@@ -25,19 +22,19 @@ class PGSpecHelper
25
22
  end
26
23
 
27
24
  unless action_condition.nil? || action_condition.is_a?(String)
28
- raise ExpectedStringError, action_condition
25
+ raise UnexpectedConditionsError, "expected String but got `#{action_condition}`"
29
26
  end
30
27
 
31
28
  unless [:row, :statement].include? action_orientation
32
29
  raise UnexpectedActionOrientationError, action_orientation
33
30
  end
34
31
 
35
- unless [:before, :after].include? action_timing
32
+ unless [:before, :after, :instead_of].include? action_timing
36
33
  raise UnexpectedActionTimingError, action_timing
37
34
  end
38
35
 
39
- # BEFORE INSERT / AFTER INSERT / BEFORE UPDATE / AFTER UPDATE / BEFORE DELETE / AFTER DELETE
40
- timing_sql = "#{action_timing} #{event_manipulation}".upcase
36
+ # "INSTEAD OF/BEFORE/AFTER" "INSERT/UPDATE/DELETE"
37
+ timing_sql = "#{action_timing.to_s.sub("_", " ")} #{event_manipulation}".upcase
41
38
 
42
39
  condition_sql = action_condition.nil? ? "" : "WHEN (#{action_condition})"
43
40
 
@@ -50,7 +47,7 @@ class PGSpecHelper
50
47
  end
51
48
  temp_tables_sql = temp_tables.any? ? "REFERENCING #{temp_tables.join(" ")}" : ""
52
49
 
53
- connection.exec <<-SQL
50
+ connection.exec <<~SQL
54
51
  -- trigger names only need to be unique for this table
55
52
  CREATE TRIGGER #{name}
56
53
  #{timing_sql} ON #{schema_name}.#{table_name} #{temp_tables_sql}
@@ -63,7 +60,7 @@ class PGSpecHelper
63
60
  # return a list of trigger names for the provided table
64
61
  def get_trigger_names schema_name, table_name
65
62
  # get the trigger names
66
- rows = connection.exec(<<-SQL, [schema_name.to_s, table_name.to_s])
63
+ rows = connection.exec(<<~SQL, [schema_name.to_s, table_name.to_s])
67
64
  SELECT
68
65
  trigger_name
69
66
  FROM
@@ -6,7 +6,7 @@ class PGSpecHelper
6
6
  def create_unique_constraint schema_name, table_name, column_names, constraint_key_name
7
7
  column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
8
8
  # add the constraint key
9
- connection.exec(<<-SQL)
9
+ connection.exec(<<~SQL)
10
10
  ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
11
11
  ADD CONSTRAINT #{connection.quote_ident constraint_key_name.to_s}
12
12
  UNIQUE (#{column_names_sql})
@@ -16,7 +16,7 @@ class PGSpecHelper
16
16
  # get a list of unique constraints for the provided table
17
17
  def get_unique_constraint_names schema_name, table_name
18
18
  # get the unique constraint names
19
- rows = connection.exec(<<-SQL, [schema_name.to_s, table_name.to_s])
19
+ rows = connection.exec(<<~SQL, [schema_name.to_s, table_name.to_s])
20
20
  SELECT
21
21
  constraint_name
22
22
  FROM
@@ -6,7 +6,7 @@ class PGSpecHelper
6
6
  def create_validation schema_name, table_name, validation_name, check_clause
7
7
  # todo the check_clause is vulnerable to sql injection (although this is very low risk because
8
8
  # it is only ever provided by the test suite, and is never provided by the user)
9
- connection.exec(<<-SQL)
9
+ connection.exec(<<~SQL)
10
10
  ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
11
11
  ADD CONSTRAINT #{connection.quote_ident validation_name.to_s} CHECK (#{check_clause})
12
12
  SQL
@@ -15,7 +15,7 @@ class PGSpecHelper
15
15
  # return a list of validation names for the provided table
16
16
  def get_validation_names schema_name, table_name
17
17
  # get the validation names
18
- rows = connection.exec(<<-SQL, [schema_name.to_s, table_name.to_s])
18
+ rows = connection.exec(<<~SQL, [schema_name.to_s, table_name.to_s])
19
19
  SELECT
20
20
  constraint_name
21
21
  FROM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class PGSpecHelper
4
- VERSION = "1.6.0"
4
+ VERSION = "1.7.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_spec_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Ulliott