rails-pg_trigger 0.1.0 → 0.1.2

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: 4d272414038e590f0a426449fa3ed1072a6f8d7706603e745c08bad04c7222a2
4
- data.tar.gz: 364c89e5ecc7736df8997a74a324f8d784a7a692b958bf9ad799407aa588f0a8
3
+ metadata.gz: dd4fadc7023d22fd1843ebab5974e7212fdc620de62059ae102657b6c24a1191
4
+ data.tar.gz: f8bfc0acf6c0647167ccbc3803dc6826fb1c96d56fdf7faf292dca6c08a8721b
5
5
  SHA512:
6
- metadata.gz: 2454620e450678dbe9e63efc27735a7f632d33ef9c527edbe9da90b53667a2df12a578bd3c49ddfc489a88f92cd4954f593e37ec89abca2033794a5cb1262154
7
- data.tar.gz: 53f38eb069859f13b5d43cf79dbf1ed32e3391d0e1d346ae5e6f8961b24f142e831e919ae5755ddb763b60acb8d1f3c09aabcb2025a229b1491394c14a68001d
6
+ metadata.gz: b1df9ba8c9d32fb33106e6939e183aa0917a150c22f3f77b39bda995ff1cdbee2ddd646a4cd6efbbb8a8a43c02a3b2375765f9d9ff96cb5ed04d26afb462ae1f
7
+ data.tar.gz: bad3ffd878a86fe85577da0ceec414ce17bab77cd5834abc5628536966c3b66bcc0643669c75703c2c64253703c150f6a67709b7b7887942adf6ed9a91fc3779
data/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
- ## [Unreleased]
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
+
8
+ ## [0.1.1]
9
+
10
+ - Relax pg version dependency
2
11
 
3
12
  ## [0.1.0] - 2024-01-04
4
13
 
@@ -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.0"
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.0
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-15 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
@@ -54,16 +54,22 @@ dependencies:
54
54
  name: pg
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - '='
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.2.0
60
+ - - "<"
58
61
  - !ruby/object:Gem::Version
59
- version: 1.2.3
62
+ version: '2'
60
63
  type: :runtime
61
64
  prerelease: false
62
65
  version_requirements: !ruby/object:Gem::Requirement
63
66
  requirements:
64
- - - '='
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.2.0
70
+ - - "<"
65
71
  - !ruby/object:Gem::Version
66
- version: 1.2.3
72
+ version: '2'
67
73
  description: Write your Postgres triggers directly in your models.
68
74
  email:
69
75
  - cocchi.c@gmail.com