rails-pg_trigger 0.1.1 → 0.1.3

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: f20870cd133b627bbdab5bfe175faf4ea8a4e203c7258c4e6926a3ecd0b5cc07
4
- data.tar.gz: 27955f294a387c3bc40b88d68f4f363c240c819cb7a9f27c972fade8c2db89b4
3
+ metadata.gz: 44aab12612f46126b08d12e199cb56b8011848c3f6d6fd1297d3dc82079640d0
4
+ data.tar.gz: 7cc00af7a6f44b0d3c773a70526a907656c7eeecbcd8e73857330011946a206b
5
5
  SHA512:
6
- metadata.gz: 37ea780b68669610fb44af40a99e71cfef021471b9d663d3d033833f699e5de4aa299036da50eed1894df4aff175a00ba0316095242915b471b333c76cd0de8a
7
- data.tar.gz: a07f6751a7c43d8d31f44697b0918efed7013f2609e10dc2fc2521dc2b6f3f794640a6a707ceec1cbbbc508487af04b22f2205a3f1af56bd5480ed36fbfdbba7
6
+ metadata.gz: b8c0b565a02f88efbf883c1bf76fcd9e9e18c49ddcdb90b40399521635f193a2bcded0421b297e3f553a8fea863a87d142cda075469d025bbb50d19253bb45af
7
+ data.tar.gz: 53fc370375dd2cac4ccc0699a260e35829fdf5d353097001ab1675be0267d18826a0fb9c2745fe97a9edc228eccd7ca3905b473471f7172f11798a28b6881e90
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.3] - 2024-02-11
4
+
5
+ - Correclty rollback updated triggers
6
+ - Fix noop in non dev/test environments
7
+
8
+ ## [0.1.2] - 2024-01-22
9
+
10
+ - `where_clause` is now also used to compare triggers
11
+ - Add trigger's columns in generated SQL and name
12
+ - Correctly sequence creations and deletions in migration
13
+ - Ignore case when comparing where clauses
14
+
1
15
  ## [0.1.1]
2
16
 
3
17
  - 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!
@@ -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_content_as?(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
@@ -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.3"
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccocchi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-16 00:00:00.000000000 Z
11
+ date: 2024-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -104,7 +104,7 @@ metadata:
104
104
  homepage_uri: https://github.com/ccocchi/rails-pg_trigger
105
105
  source_code_uri: https://github.com/ccocchi/rails-pg_trigger
106
106
  changelog_uri: https://github.com/ccocchi/rails-pg_trigger/blob/main/CHANGELOG.md
107
- post_install_message:
107
+ post_install_message:
108
108
  rdoc_options: []
109
109
  require_paths:
110
110
  - lib
@@ -119,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  requirements: []
122
- rubygems_version: 3.3.7
123
- signing_key:
122
+ rubygems_version: 3.0.3.1
123
+ signing_key:
124
124
  specification_version: 4
125
125
  summary: Postgres triggers for Rails
126
126
  test_files: []