rails-pg_trigger 0.1.1 → 0.1.2

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: f20870cd133b627bbdab5bfe175faf4ea8a4e203c7258c4e6926a3ecd0b5cc07
4
- data.tar.gz: 27955f294a387c3bc40b88d68f4f363c240c819cb7a9f27c972fade8c2db89b4
3
+ metadata.gz: dd4fadc7023d22fd1843ebab5974e7212fdc620de62059ae102657b6c24a1191
4
+ data.tar.gz: f8bfc0acf6c0647167ccbc3803dc6826fb1c96d56fdf7faf292dca6c08a8721b
5
5
  SHA512:
6
- metadata.gz: 37ea780b68669610fb44af40a99e71cfef021471b9d663d3d033833f699e5de4aa299036da50eed1894df4aff175a00ba0316095242915b471b333c76cd0de8a
7
- data.tar.gz: a07f6751a7c43d8d31f44697b0918efed7013f2609e10dc2fc2521dc2b6f3f794640a6a707ceec1cbbbc508487af04b22f2205a3f1af56bd5480ed36fbfdbba7
6
+ metadata.gz: b1df9ba8c9d32fb33106e6939e183aa0917a150c22f3f77b39bda995ff1cdbee2ddd646a4cd6efbbb8a8a43c02a3b2375765f9d9ff96cb5ed04d26afb462ae1f
7
+ data.tar.gz: bad3ffd878a86fe85577da0ceec414ce17bab77cd5834abc5628536966c3b66bcc0643669c75703c2c64253703c150f6a67709b7b7887942adf6ed9a91fc3779
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.1.2]
2
+
3
+ - `where_clause` is now also used to compare triggers
4
+ - Add trigger's columns in generated SQL and name
5
+ - Correctly sequence creations and deletions in migration
6
+ - Ignore case when comparing where clauses
7
+
1
8
  ## [0.1.1]
2
9
 
3
10
  - Relax pg version dependency
@@ -43,9 +43,9 @@ module PgTrigger
43
43
  def generate_output
44
44
  @output = ""
45
45
  header
46
- up(:up)
46
+ add_direction(:up)
47
47
  @output << "\n"
48
- up(:down)
48
+ add_direction(:down)
49
49
  footer
50
50
  end
51
51
 
@@ -58,34 +58,21 @@ module PgTrigger
58
58
  @output << "class #{migration_name} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]\n"
59
59
  end
60
60
 
61
- def up(dir)
61
+ def add_direction(dir)
62
62
  res = IndentedString.new(size: 2, initial_indent: true)
63
63
  res << "def #{dir}\n"
64
64
  res.indent!
65
65
 
66
- create = ->(t) do
67
- add_command("create_trigger", t) do |s|
68
- if t.create_function?
69
- s += t.create_function_sql
70
- s.newline
71
- end
72
- s += t.create_trigger_sql
73
- end
74
- end
66
+ blocks = []
75
67
 
76
- drop = ->(t) do
77
- add_command("drop_trigger", t) do |s|
78
- s += t.drop_trigger_sql
79
- s.newline
80
- if t.create_function?
81
- s += t.drop_function_sql
82
- end
83
- end
68
+ if dir == :up
69
+ blocks.concat @plan.removed_triggers.map { |t| drop_trigger_command(t) }
70
+ blocks.concat @plan.new_triggers.map { |t| create_trigger_command(t) }
71
+ else
72
+ blocks.concat @plan.new_triggers.map { |t| drop_trigger_command(t) }
73
+ blocks.concat @plan.removed_triggers.map { |t| create_trigger_command(t) }
84
74
  end
85
75
 
86
- blocks = @plan.new_triggers.map { |t| (dir == :up ? create : drop).call(t) }
87
- blocks.concat @plan.removed_triggers.map { |t| (dir == :up ? drop : create).call(t) }
88
-
89
76
  res += blocks.join("\n")
90
77
  res.outdent!
91
78
  res << "end\n"
@@ -96,7 +83,27 @@ module PgTrigger
96
83
  @output << "end\n"
97
84
  end
98
85
 
99
- def add_command(cmd, t)
86
+ def create_trigger_command(t)
87
+ make_command("create_trigger", t) do |s|
88
+ if t.create_function?
89
+ s += t.create_function_sql
90
+ s.newline
91
+ end
92
+ s += t.create_trigger_sql
93
+ end
94
+ end
95
+
96
+ def drop_trigger_command(t)
97
+ make_command("drop_trigger", t) do |s|
98
+ s += t.drop_trigger_sql
99
+ s.newline
100
+ if t.create_function?
101
+ s += t.drop_function_sql
102
+ end
103
+ end
104
+ end
105
+
106
+ def make_command(cmd, t)
100
107
  res = IndentedString.new(size: 0)
101
108
  res << %{#{cmd} "#{t.name}", <<~SQL\n}
102
109
  res.indent!
@@ -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_content_as?(e)
23
+ plan.update_trigger(t) unless t.same?(e)
24
24
  else
25
25
  plan.add_trigger(t)
26
26
  end
@@ -108,8 +108,9 @@ module PgTrigger
108
108
  def where_clause = @where
109
109
 
110
110
  # Compare content without taking indentation into account
111
- def same_content_as?(other)
112
- content.gsub(/\s+/, " ") == other.content.gsub(/\s+/, " ")
111
+ def same?(other)
112
+ where_clause&.downcase == other&.where_clause&.downcase &&
113
+ content.gsub(/\s+/, " ") == other.content.gsub(/\s+/, " ")
113
114
  end
114
115
 
115
116
  FN_CONTENT_REGEX = /BEGIN\s+(?<content>.+;)\n\s+RETURN NULL;/m
@@ -140,14 +141,16 @@ module PgTrigger
140
141
  end
141
142
 
142
143
  def create_trigger_sql
143
- whr = @where.nil? ? "" : "\nWHEN (#@where)"
144
+ sql = "CREATE TRIGGER #{name}\n"
145
+ sql << @timing.to_s.upcase
146
+ sql << " #{events.map(&:upcase).join(" OR ")} "
147
+ sql << "OF #{columns.join(", ")} " if columns.any?
148
+ sql << "ON #{adapter.quote_table_name(@table)}\n"
149
+ sql << "FOR EACH ROW\n"
150
+ sql << "WHEN (#{@where})\n" unless @where.nil?
151
+ sql << "EXECUTE FUNCTION #{name}();"
144
152
 
145
- <<~SQL
146
- CREATE TRIGGER #{name}
147
- #{@timing.upcase} #{events.map(&:upcase).join(" OR ")} ON #{adapter.quote_table_name(@table)}
148
- FOR EACH ROW#{whr}
149
- EXECUTE FUNCTION #{name}();
150
- SQL
153
+ sql
151
154
  end
152
155
 
153
156
  def drop_function_sql
@@ -181,10 +184,18 @@ module PgTrigger
181
184
  end
182
185
 
183
186
  def inferred_name
184
- [@table,
185
- @timing,
186
- @events.join("_or_"),
187
- ].join("_").downcase.slice(0, 60) << "_tr"
187
+ base = [
188
+ @table,
189
+ @timing,
190
+ @events.join("_or_"),
191
+ ].join("_").downcase
192
+
193
+ if columns.any?
194
+ addendum = "_of_#{columns.join("_")}"
195
+ base << addendum if base.length + addendum.length <= 60
196
+ end
197
+
198
+ base.slice(0, 60) << "_tr"
188
199
  end
189
200
  end
190
201
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgTrigger
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
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.1
4
+ version: 0.1.2
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-16 00:00:00.000000000 Z
11
+ date: 2024-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord