rails-pg_trigger 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/pg_trigger/generator.rb +31 -24
- data/lib/pg_trigger/plan.rb +1 -1
- data/lib/pg_trigger/trigger.rb +24 -13
- data/lib/pg_trigger/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd4fadc7023d22fd1843ebab5974e7212fdc620de62059ae102657b6c24a1191
|
4
|
+
data.tar.gz: f8bfc0acf6c0647167ccbc3803dc6826fb1c96d56fdf7faf292dca6c08a8721b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/pg_trigger/generator.rb
CHANGED
@@ -43,9 +43,9 @@ module PgTrigger
|
|
43
43
|
def generate_output
|
44
44
|
@output = ""
|
45
45
|
header
|
46
|
-
|
46
|
+
add_direction(:up)
|
47
47
|
@output << "\n"
|
48
|
-
|
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
|
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
|
-
|
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
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
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!
|
data/lib/pg_trigger/plan.rb
CHANGED
data/lib/pg_trigger/trigger.rb
CHANGED
@@ -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
|
112
|
-
|
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
|
-
|
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
|
-
|
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
|
-
[
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|
data/lib/pg_trigger/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2024-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|