@vrplatform/kysely 1.3.45-44053902920 → 1.3.45-44287025938
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.
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
// The effect runner writes 25 effects of one action per statement: the claim,
|
|
5
|
+
// the attempt insert, the invalidation apply, the completion and the attempt
|
|
6
|
+
// completion. Row triggers refreshed the action's operation source once per row,
|
|
7
|
+
// 25 identical refreshes after each statement, and each one scans the action's
|
|
8
|
+
// active effects (40,000 for the largest US actions). Statement triggers refresh
|
|
9
|
+
// each action of a statement once, in action order (PRO-20933).
|
|
10
|
+
//
|
|
11
|
+
// A trigger with transition tables fires on one event, so insert, update and
|
|
12
|
+
// delete each get a trigger. The update triggers keep their names; the functions
|
|
13
|
+
// keep their names for the region migration bypass (scripts/migrate-region).
|
|
14
|
+
export async function up<Database>(db: Kysely<Database>): Promise<void> {
|
|
15
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
16
|
+
|
|
17
|
+
await sql`drop trigger refresh_audit_action_operation on audit.effect`.execute(
|
|
18
|
+
db
|
|
19
|
+
);
|
|
20
|
+
await sql`
|
|
21
|
+
drop trigger refresh_audit_action_operation_attempt on audit.effect_attempt
|
|
22
|
+
`.execute(db);
|
|
23
|
+
await sql`
|
|
24
|
+
drop trigger refresh_reservation_journal_invalidation_operation
|
|
25
|
+
on accounting.reservation_journal_invalidation
|
|
26
|
+
`.execute(db);
|
|
27
|
+
|
|
28
|
+
// An update refreshes only when a column the old row trigger listed changed.
|
|
29
|
+
for (const [name, columns] of [
|
|
30
|
+
[
|
|
31
|
+
'refresh_audit_action_operation',
|
|
32
|
+
'status, retry_count, claimed_at, completed_at, last_attempt_at',
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
'refresh_audit_action_operation_attempt',
|
|
36
|
+
'status, result_code, finished_at',
|
|
37
|
+
],
|
|
38
|
+
] as const) {
|
|
39
|
+
const changed = sql.raw(
|
|
40
|
+
`(${columns
|
|
41
|
+
.split(', ')
|
|
42
|
+
.map((column) => `changed_rows.${column}`)
|
|
43
|
+
.join(', ')}) is distinct from (${columns
|
|
44
|
+
.split(', ')
|
|
45
|
+
.map((column) => `old_rows.${column}`)
|
|
46
|
+
.join(', ')})`
|
|
47
|
+
);
|
|
48
|
+
await sql`
|
|
49
|
+
create or replace function ${sql.raw(`core.${name}`)}()
|
|
50
|
+
returns trigger
|
|
51
|
+
language plpgsql
|
|
52
|
+
as $$
|
|
53
|
+
declare
|
|
54
|
+
target_action_id uuid;
|
|
55
|
+
begin
|
|
56
|
+
if tg_op = 'UPDATE' then
|
|
57
|
+
for target_action_id in
|
|
58
|
+
select distinct changed_rows.action_id
|
|
59
|
+
from changed_rows
|
|
60
|
+
inner join old_rows on old_rows.id = changed_rows.id
|
|
61
|
+
where changed_rows.action_id is not null and ${changed}
|
|
62
|
+
order by changed_rows.action_id
|
|
63
|
+
loop
|
|
64
|
+
perform core.refresh_audit_action_source(target_action_id);
|
|
65
|
+
end loop;
|
|
66
|
+
return null;
|
|
67
|
+
end if;
|
|
68
|
+
for target_action_id in
|
|
69
|
+
select distinct action_id from changed_rows
|
|
70
|
+
where action_id is not null
|
|
71
|
+
order by action_id
|
|
72
|
+
loop
|
|
73
|
+
perform core.refresh_audit_action_source(target_action_id);
|
|
74
|
+
end loop;
|
|
75
|
+
return null;
|
|
76
|
+
end;
|
|
77
|
+
$$
|
|
78
|
+
`.execute(db);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// New actions register, old actions of a moved row refresh. One loop in
|
|
82
|
+
// action order keeps the operation source locks in one order (PRO-19762).
|
|
83
|
+
await sql`
|
|
84
|
+
create or replace function core.refresh_reservation_journal_invalidation_operation()
|
|
85
|
+
returns trigger
|
|
86
|
+
language plpgsql
|
|
87
|
+
as $$
|
|
88
|
+
declare
|
|
89
|
+
target record;
|
|
90
|
+
begin
|
|
91
|
+
for target in
|
|
92
|
+
select action_id, bool_or(registers) as registers
|
|
93
|
+
from (
|
|
94
|
+
select changed_rows.action_id, true as registers
|
|
95
|
+
from changed_rows
|
|
96
|
+
inner join old_rows on old_rows.id = changed_rows.id
|
|
97
|
+
where changed_rows.action_id is not null
|
|
98
|
+
and (changed_rows.action_id, changed_rows.applied_at)
|
|
99
|
+
is distinct from (old_rows.action_id, old_rows.applied_at)
|
|
100
|
+
union all
|
|
101
|
+
select old_rows.action_id, false
|
|
102
|
+
from old_rows
|
|
103
|
+
inner join changed_rows on changed_rows.id = old_rows.id
|
|
104
|
+
where old_rows.action_id is not null
|
|
105
|
+
and old_rows.action_id is distinct from changed_rows.action_id
|
|
106
|
+
) as touched
|
|
107
|
+
group by action_id
|
|
108
|
+
order by action_id
|
|
109
|
+
loop
|
|
110
|
+
if target.registers then
|
|
111
|
+
perform core.register_reservation_journal_operation(target.action_id);
|
|
112
|
+
else
|
|
113
|
+
perform core.refresh_audit_action_source(target.action_id);
|
|
114
|
+
end if;
|
|
115
|
+
end loop;
|
|
116
|
+
return null;
|
|
117
|
+
end;
|
|
118
|
+
$$
|
|
119
|
+
`.execute(db);
|
|
120
|
+
|
|
121
|
+
for (const [table, name] of [
|
|
122
|
+
['audit.effect', 'refresh_audit_action_operation'],
|
|
123
|
+
['audit.effect_attempt', 'refresh_audit_action_operation_attempt'],
|
|
124
|
+
] as const) {
|
|
125
|
+
const target = sql.raw(table);
|
|
126
|
+
const fn = sql.raw(`core.${name}()`);
|
|
127
|
+
await sql`
|
|
128
|
+
create trigger ${sql.raw(`${name}_insert`)}
|
|
129
|
+
after insert on ${target}
|
|
130
|
+
referencing new table as changed_rows
|
|
131
|
+
for each statement execute function ${fn}
|
|
132
|
+
`.execute(db);
|
|
133
|
+
await sql`
|
|
134
|
+
create trigger ${sql.raw(name)}
|
|
135
|
+
after update on ${target}
|
|
136
|
+
referencing old table as old_rows new table as changed_rows
|
|
137
|
+
for each statement execute function ${fn}
|
|
138
|
+
`.execute(db);
|
|
139
|
+
await sql`
|
|
140
|
+
create trigger ${sql.raw(`${name}_delete`)}
|
|
141
|
+
after delete on ${target}
|
|
142
|
+
referencing old table as changed_rows
|
|
143
|
+
for each statement execute function ${fn}
|
|
144
|
+
`.execute(db);
|
|
145
|
+
}
|
|
146
|
+
await sql`
|
|
147
|
+
create trigger refresh_reservation_journal_invalidation_operation
|
|
148
|
+
after update on accounting.reservation_journal_invalidation
|
|
149
|
+
referencing old table as old_rows new table as changed_rows
|
|
150
|
+
for each statement
|
|
151
|
+
execute function core.refresh_reservation_journal_invalidation_operation()
|
|
152
|
+
`.execute(db);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export async function down<Database>(db: Kysely<Database>): Promise<void> {
|
|
156
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
157
|
+
|
|
158
|
+
for (const [table, name] of [
|
|
159
|
+
['audit.effect', 'refresh_audit_action_operation'],
|
|
160
|
+
['audit.effect_attempt', 'refresh_audit_action_operation_attempt'],
|
|
161
|
+
] as const) {
|
|
162
|
+
for (const trigger of [`${name}_insert`, name, `${name}_delete`]) {
|
|
163
|
+
await sql`drop trigger ${sql.raw(trigger)} on ${sql.raw(table)}`.execute(
|
|
164
|
+
db
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
await sql`
|
|
169
|
+
drop trigger refresh_reservation_journal_invalidation_operation
|
|
170
|
+
on accounting.reservation_journal_invalidation
|
|
171
|
+
`.execute(db);
|
|
172
|
+
|
|
173
|
+
for (const name of [
|
|
174
|
+
'refresh_audit_action_operation',
|
|
175
|
+
'refresh_audit_action_operation_attempt',
|
|
176
|
+
]) {
|
|
177
|
+
await sql`
|
|
178
|
+
create or replace function ${sql.raw(`core.${name}`)}()
|
|
179
|
+
returns trigger
|
|
180
|
+
language plpgsql
|
|
181
|
+
as $$
|
|
182
|
+
begin
|
|
183
|
+
perform core.refresh_audit_action_source(
|
|
184
|
+
coalesce(new.action_id, old.action_id)
|
|
185
|
+
);
|
|
186
|
+
return coalesce(new, old);
|
|
187
|
+
end;
|
|
188
|
+
$$
|
|
189
|
+
`.execute(db);
|
|
190
|
+
}
|
|
191
|
+
await sql`
|
|
192
|
+
create or replace function core.refresh_reservation_journal_invalidation_operation()
|
|
193
|
+
returns trigger
|
|
194
|
+
language plpgsql
|
|
195
|
+
as $$
|
|
196
|
+
begin
|
|
197
|
+
if new.action_id is not null then
|
|
198
|
+
perform core.register_reservation_journal_operation(new.action_id);
|
|
199
|
+
end if;
|
|
200
|
+
if tg_op = 'UPDATE'
|
|
201
|
+
and old.action_id is distinct from new.action_id
|
|
202
|
+
and old.action_id is not null then
|
|
203
|
+
perform core.refresh_audit_action_source(old.action_id);
|
|
204
|
+
end if;
|
|
205
|
+
return new;
|
|
206
|
+
end;
|
|
207
|
+
$$
|
|
208
|
+
`.execute(db);
|
|
209
|
+
|
|
210
|
+
await sql`
|
|
211
|
+
create trigger refresh_audit_action_operation
|
|
212
|
+
after insert or update of status, retry_count, claimed_at, completed_at, last_attempt_at
|
|
213
|
+
or delete on audit.effect
|
|
214
|
+
for each row execute function core.refresh_audit_action_operation()
|
|
215
|
+
`.execute(db);
|
|
216
|
+
await sql`
|
|
217
|
+
create trigger refresh_audit_action_operation_attempt
|
|
218
|
+
after insert or update of status, result_code, finished_at or delete
|
|
219
|
+
on audit.effect_attempt
|
|
220
|
+
for each row execute function core.refresh_audit_action_operation_attempt()
|
|
221
|
+
`.execute(db);
|
|
222
|
+
await sql`
|
|
223
|
+
create trigger refresh_reservation_journal_invalidation_operation
|
|
224
|
+
after update of action_id, applied_at
|
|
225
|
+
on accounting.reservation_journal_invalidation
|
|
226
|
+
for each row
|
|
227
|
+
execute function core.refresh_reservation_journal_invalidation_operation()
|
|
228
|
+
`.execute(db);
|
|
229
|
+
}
|