pg_spec_helper 1.5.0 → 1.6.0

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: '0448eb4ca85825f030ce96b4138d8bbf1ecce2dd51e6767a936591582034bd99'
4
- data.tar.gz: bd24c7db5a5ad3c267dd4b26d7b753aaa547db79e662235d96bb027e5cc8e5e7
3
+ metadata.gz: 5c885ddde22232c3907a08da5411a55e09b1105a87afbac5aac0b2b443c63ee1
4
+ data.tar.gz: a8220ca48f1dea784d20e45ecc74dfd0f464c8524a55240715bb7154dc6f8879
5
5
  SHA512:
6
- metadata.gz: 8ec24b586e7b1550c638b9c1387ef3ac60b244ab35c857f95ab75e661f725f2c2eefeb65326ca946c2f0c97593db15bb8e3e156831cbbe72631d3afc46f29215
7
- data.tar.gz: b4b3982f19db7d7fc2ee038167eb8fea189a368d1ac6cb98916da60008635ba2293998216a057e451523b45855b6c0d8d8992ecda80b86c030f929fa306d12f1
6
+ metadata.gz: '08770ab6431e7b5d2f21b0443d88282400ea597d78b946fe5c68b4c4ff562ae7bd14e0ddb5db4c5353ec8954ee0472ce5e5a2858259186504469f0a6783d66df'
7
+ data.tar.gz: f811831ee0dc0381a3252556d28c1ac85f476707ab3ac6fd3c951c0c2ecb985ea7d18a1daf663d27ce533c06cab94e91d0963a200a88590bae5004a14d64b0d7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.6.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.5.0...v1.6.0) (2023-08-06)
4
+
5
+
6
+ ### Features
7
+
8
+ * added support for triggers and functions ([f89bf5e](https://github.com/craigulliott/pg_spec_helper/commit/f89bf5e3afa6fc411e9d1f16cb62db74fc8dc987))
9
+
3
10
  ## [1.5.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.4.0...v1.5.0) (2023-08-01)
4
11
 
5
12
 
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PGSpecHelper
4
+ module Functions
5
+ # create a function
6
+ def create_function schema_name, function_name, function_definition
7
+ connection.exec <<-SQL
8
+ CREATE FUNCTION #{schema_name}.#{function_name}() returns trigger language plpgsql AS $$
9
+ BEGIN
10
+ #{function_definition};
11
+ RETURN NEW;
12
+ END $$;
13
+ SQL
14
+ end
15
+
16
+ # return a list of function names for the provided schema
17
+ def get_function_names schema_name
18
+ # get the function names
19
+ rows = connection.exec(<<-SQL, [schema_name.to_s])
20
+ SELECT
21
+ routine_name
22
+ FROM
23
+ information_schema.routines
24
+ WHERE
25
+ routine_schema = $1
26
+ SQL
27
+ rows.map { |r| r["routine_name"].to_sym }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PGSpecHelper
4
+ module Triggers
5
+ # create a trigger
6
+ class UnexpectedEventManipulationError < StandardError
7
+ end
8
+
9
+ class UnexpectedActionOrderError < StandardError
10
+ end
11
+
12
+ class UnexpectedActionStatementError < StandardError
13
+ end
14
+
15
+ class UnexpectedActionOrientationError < StandardError
16
+ end
17
+
18
+ class UnexpectedActionTimingError < StandardError
19
+ end
20
+
21
+ # create a postgres trigger
22
+ 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
+ unless [:insert, :delete, :update].include? event_manipulation
24
+ raise UnexpectedEventManipulationError, event_manipulation
25
+ end
26
+
27
+ unless action_condition.nil? || action_condition.is_a?(String)
28
+ raise ExpectedStringError, action_condition
29
+ end
30
+
31
+ unless [:row, :statement].include? action_orientation
32
+ raise UnexpectedActionOrientationError, action_orientation
33
+ end
34
+
35
+ unless [:before, :after].include? action_timing
36
+ raise UnexpectedActionTimingError, action_timing
37
+ end
38
+
39
+ # BEFORE INSERT / AFTER INSERT / BEFORE UPDATE / AFTER UPDATE / BEFORE DELETE / AFTER DELETE
40
+ timing_sql = "#{action_timing} #{event_manipulation}".upcase
41
+
42
+ condition_sql = action_condition.nil? ? "" : "WHEN (#{action_condition})"
43
+
44
+ temp_tables = []
45
+ unless action_reference_old_table.nil?
46
+ temp_tables << "OLD TABLE AS #{action_reference_old_table}"
47
+ end
48
+ unless action_reference_new_table.nil?
49
+ temp_tables << "NEW TABLE AS #{action_reference_new_table}"
50
+ end
51
+ temp_tables_sql = temp_tables.any? ? "REFERENCING #{temp_tables.join(" ")}" : ""
52
+
53
+ connection.exec <<-SQL
54
+ -- trigger names only need to be unique for this table
55
+ CREATE TRIGGER #{name}
56
+ #{timing_sql} ON #{schema_name}.#{table_name} #{temp_tables_sql}
57
+ FOR EACH #{action_orientation}
58
+ #{condition_sql}
59
+ EXECUTE PROCEDURE #{routine_schema}.#{routine_name}();
60
+ SQL
61
+ end
62
+
63
+ # return a list of trigger names for the provided table
64
+ def get_trigger_names schema_name, table_name
65
+ # get the trigger names
66
+ rows = connection.exec(<<-SQL, [schema_name.to_s, table_name.to_s])
67
+ SELECT
68
+ trigger_name
69
+ FROM
70
+ information_schema.triggers
71
+ WHERE
72
+ event_object_schema = $1
73
+ AND event_object_table = $2
74
+ SQL
75
+ rows.map { |r| r["trigger_name"].to_sym }
76
+ end
77
+ end
78
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class PGSpecHelper
4
- VERSION = "1.5.0"
4
+ VERSION = "1.6.0"
5
5
  end
@@ -16,6 +16,8 @@ require "pg_spec_helper/foreign_keys"
16
16
  require "pg_spec_helper/unique_constraints"
17
17
  require "pg_spec_helper/primary_keys"
18
18
  require "pg_spec_helper/indexes"
19
+ require "pg_spec_helper/triggers"
20
+ require "pg_spec_helper/functions"
19
21
  require "pg_spec_helper/models"
20
22
  require "pg_spec_helper/materialized_views"
21
23
  require "pg_spec_helper/reset"
@@ -39,6 +41,8 @@ class PGSpecHelper
39
41
  include UniqueConstraints
40
42
  include PrimaryKeys
41
43
  include Indexes
44
+ include Triggers
45
+ include Functions
42
46
  include Models
43
47
  include MaterializedViews
44
48
  include Reset
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_spec_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Ulliott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-01 00:00:00.000000000 Z
11
+ date: 2023-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -55,6 +55,7 @@ files:
55
55
  - lib/pg_spec_helper/connection.rb
56
56
  - lib/pg_spec_helper/empty_database.rb
57
57
  - lib/pg_spec_helper/foreign_keys.rb
58
+ - lib/pg_spec_helper/functions.rb
58
59
  - lib/pg_spec_helper/ignored_schemas.rb
59
60
  - lib/pg_spec_helper/indexes.rb
60
61
  - lib/pg_spec_helper/materialized_views.rb
@@ -66,6 +67,7 @@ files:
66
67
  - lib/pg_spec_helper/table_executer.rb
67
68
  - lib/pg_spec_helper/tables.rb
68
69
  - lib/pg_spec_helper/track_changes.rb
70
+ - lib/pg_spec_helper/triggers.rb
69
71
  - lib/pg_spec_helper/unique_constraints.rb
70
72
  - lib/pg_spec_helper/validations.rb
71
73
  - lib/pg_spec_helper/version.rb