rails-pg_trigger 0.1.2 → 0.1.4

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
  SHA256:
3
- metadata.gz: dd4fadc7023d22fd1843ebab5974e7212fdc620de62059ae102657b6c24a1191
4
- data.tar.gz: f8bfc0acf6c0647167ccbc3803dc6826fb1c96d56fdf7faf292dca6c08a8721b
3
+ metadata.gz: 2f40387dfea07ab95c4dca20f9b26c2af48fd1fda8dffcb360048fa409a08d32
4
+ data.tar.gz: 17b3356c9bc2ebb418921bab81e7d6722d03dc47021e4a47be6657f4014b804d
5
5
  SHA512:
6
- metadata.gz: b1df9ba8c9d32fb33106e6939e183aa0917a150c22f3f77b39bda995ff1cdbee2ddd646a4cd6efbbb8a8a43c02a3b2375765f9d9ff96cb5ed04d26afb462ae1f
7
- data.tar.gz: bad3ffd878a86fe85577da0ceec414ce17bab77cd5834abc5628536966c3b66bcc0643669c75703c2c64253703c150f6a67709b7b7887942adf6ed9a91fc3779
6
+ metadata.gz: 842f7827eaa749632f4f6e58fc8345f6bf3e6f084d8ed2e197703a548c6e373a837a0b626e2e262eaf91c9bb4395dc7ea0eb73277ce7172124d883a05843fa86
7
+ data.tar.gz: db8e8a003a9e4236683568c13686f228c8acb56a05948d7e795c8bd5364ebdaec7e388a110b21f428f074aa53c1fc8cc4bbde973a00011e921966c4b605c1a56
data/CHANGELOG.md CHANGED
@@ -1,4 +1,17 @@
1
- ## [0.1.2]
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.4] - 2024-03-25
4
+
5
+ - Implement `nowrap`
6
+ - Allow parenthesis-wrapped condition
7
+ - Force negative condition to be parenthesis-wrapped
8
+
9
+ ## [0.1.3] - 2024-02-11
10
+
11
+ - Correclty rollback updated triggers
12
+ - Fix noop in non dev/test environments
13
+
14
+ ## [0.1.2] - 2024-01-22
2
15
 
3
16
  - `where_clause` is now also used to compare triggers
4
17
  - Add trigger's columns in generated SQL and name
@@ -96,8 +96,8 @@ module PgTrigger
96
96
  def drop_trigger_command(t)
97
97
  make_command("drop_trigger", t) do |s|
98
98
  s += t.drop_trigger_sql
99
- s.newline
100
99
  if t.create_function?
100
+ s.newline
101
101
  s += t.drop_function_sql
102
102
  end
103
103
  end
@@ -3,8 +3,23 @@ module PgTrigger::Noop
3
3
  other.extend ClassMethods
4
4
  end
5
5
 
6
+ class Proxy
7
+ def self.chain(*methods)
8
+ methods.each do |m|
9
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
10
+ def #{m}(*)
11
+ self
12
+ end
13
+ RUBY
14
+ end
15
+ end
16
+
17
+ chain :on, :of, :after, :before, :named, :where, :nowrap
18
+ end
19
+
6
20
  module ClassMethods
7
21
  def trigger
22
+ Proxy.new
8
23
  end
9
24
  end
10
25
  end
@@ -20,7 +20,7 @@ module PgTrigger
20
20
  @expected.each do |t|
21
21
  e = @existing.find { |_t| _t.name == t.name }
22
22
  if e
23
- plan.update_trigger(t) unless t.same?(e)
23
+ plan.update_trigger(e, t) unless t.same?(e)
24
24
  else
25
25
  plan.add_trigger(t)
26
26
  end
@@ -81,12 +81,12 @@ module PgTrigger
81
81
  @actions[:to_remove] << t
82
82
  end
83
83
 
84
- def update_trigger(t)
84
+ def update_trigger(old_t, new_t)
85
85
  set_type :update
86
- set_table t.table
86
+ set_table new_t.table
87
87
 
88
- @actions[:to_remove] << t
89
- @actions[:to_add] << t
88
+ @actions[:to_remove] << old_t
89
+ @actions[:to_add] << new_t
90
90
  end
91
91
 
92
92
  private
@@ -26,7 +26,7 @@ module PgTrigger
26
26
  "(?<timing>AFTER|BEFORE)",
27
27
  "(?<events>(?:INSERT|UPDATE|DELETE)(?: OR (?:INSERT|UPDATE|DELETE))?)",
28
28
  "(?:OF(?<columns>(?:\\s[a-z0-9_]+,?)+)\\s)?ON (?:[\\w\"]+\\.)?(?<table>\\w+)",
29
- "FOR EACH ROW(?: WHEN \\((?<where>[^\\)]+)\\))?",
29
+ "FOR EACH ROW(?: WHEN \\((?<where>\\(?[^\\)]+\\)?)\\))?",
30
30
  "EXECUTE FUNCTION (?:\\w+\\.)?(?<fn>\\w+)",
31
31
  ].join("\\s")
32
32
  .yield_self { |str| Regexp.new(str) }
@@ -51,7 +51,10 @@ module PgTrigger
51
51
  trigger.where(where)
52
52
  end
53
53
 
54
- trigger.nowrap if match[:fn] != match[:name]
54
+ if match[:fn] != match[:name]
55
+ trigger.nowrap { "#{match[:fn]}()" }
56
+ end
57
+
55
58
  trigger
56
59
  end
57
60
  end
@@ -88,6 +91,9 @@ module PgTrigger
88
91
  end
89
92
 
90
93
  def where(condition)
94
+ if condition.start_with?("NOT", "not")
95
+ raise AmbiguousConditionError, "when condition starting with a NOT should be enclosed in parenthesis"
96
+ end
91
97
  @where = condition
92
98
  end
93
99
 
@@ -125,6 +131,10 @@ module PgTrigger
125
131
  !@options[:nowrap]
126
132
  end
127
133
 
134
+ def nowrap?
135
+ @options[:nowrap]
136
+ end
137
+
128
138
  def create_function_sql
129
139
  str = IndentedString.new(size: 4)
130
140
  str += @content
@@ -141,6 +151,12 @@ module PgTrigger
141
151
  end
142
152
 
143
153
  def create_trigger_sql
154
+ action = if create_function?
155
+ "#{name}();"
156
+ else
157
+ @content
158
+ end
159
+
144
160
  sql = "CREATE TRIGGER #{name}\n"
145
161
  sql << @timing.to_s.upcase
146
162
  sql << " #{events.map(&:upcase).join(" OR ")} "
@@ -148,7 +164,7 @@ module PgTrigger
148
164
  sql << "ON #{adapter.quote_table_name(@table)}\n"
149
165
  sql << "FOR EACH ROW\n"
150
166
  sql << "WHEN (#{@where})\n" unless @where.nil?
151
- sql << "EXECUTE FUNCTION #{name}();"
167
+ sql << "EXECUTE FUNCTION #{action}"
152
168
 
153
169
  sql
154
170
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgTrigger
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/pg_trigger.rb CHANGED
@@ -5,6 +5,7 @@ require_relative "pg_trigger/railtie"
5
5
 
6
6
  module PgTrigger
7
7
  class InvalidTriggerDefinition < StandardError; end
8
+ class AmbiguousConditionError < StandardError; end
8
9
 
9
10
  class << self
10
11
  attr_accessor :structure_file_path, :schema, :migrations_path, :raise_on_invalid_definition
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-pg_trigger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccocchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-22 00:00:00.000000000 Z
11
+ date: 2024-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord