@vrplatform/kysely 1.3.45-37798929841 → 1.3.45-37952226697
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,92 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
// Owner statement attach and detach only move the statement columns. They hold no
|
|
5
|
+
// payment evidence, so deployments without the payments writer may change them.
|
|
6
|
+
// txn_code is generated and still null in NEW inside a BEFORE trigger.
|
|
7
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
8
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
9
|
+
await sql`set local statement_timeout = '60s'`.execute(db);
|
|
10
|
+
await sql`
|
|
11
|
+
create or replace function accounting.require_payment_writer() returns trigger
|
|
12
|
+
language plpgsql as $$
|
|
13
|
+
declare
|
|
14
|
+
target record;
|
|
15
|
+
has_payments boolean;
|
|
16
|
+
begin
|
|
17
|
+
if current_setting('accounting.payment_writer_version', true) = '1'
|
|
18
|
+
or to_regclass('accounting.payment') is null then
|
|
19
|
+
if tg_op = 'DELETE' then return old; else return new; end if;
|
|
20
|
+
end if;
|
|
21
|
+
if tg_op = 'UPDATE' and tg_table_name = 'journal_entry'
|
|
22
|
+
and to_jsonb(new) - array['attached_to_owner_statement_id', 'listing_ownership_period_id',
|
|
23
|
+
'updated_at', 'txn_code']
|
|
24
|
+
= to_jsonb(old) - array['attached_to_owner_statement_id', 'listing_ownership_period_id',
|
|
25
|
+
'updated_at', 'txn_code'] then
|
|
26
|
+
return new;
|
|
27
|
+
end if;
|
|
28
|
+
for target in
|
|
29
|
+
select distinct (value ->> 'tenant_id')::uuid as tenant_id,
|
|
30
|
+
(case when tg_table_name = 'transaction' then value ->> 'id'
|
|
31
|
+
else value ->> 'transaction_id' end)::uuid as transaction_id
|
|
32
|
+
from jsonb_array_elements(jsonb_build_array(to_jsonb(old), to_jsonb(new)))
|
|
33
|
+
where value is not null and value <> 'null'::jsonb
|
|
34
|
+
order by tenant_id, transaction_id
|
|
35
|
+
loop
|
|
36
|
+
if target.transaction_id is null then continue; end if;
|
|
37
|
+
-- Payment creation locks this same row before inserting its evidence.
|
|
38
|
+
perform id from accounting.transaction
|
|
39
|
+
where id = target.transaction_id and tenant_id = target.tenant_id for update;
|
|
40
|
+
execute 'select exists (select 1 from accounting.payment
|
|
41
|
+
where tenant_id = $1 and transaction_id = $2)'
|
|
42
|
+
into has_payments using target.tenant_id, target.transaction_id;
|
|
43
|
+
if has_payments then
|
|
44
|
+
raise exception using errcode = '23514', constraint = 'payment_writer_compatibility',
|
|
45
|
+
message = 'This transaction requires a deployment with payments support',
|
|
46
|
+
detail = jsonb_build_object('tenantId', target.tenant_id,
|
|
47
|
+
'transactionId', target.transaction_id)::text;
|
|
48
|
+
end if;
|
|
49
|
+
end loop;
|
|
50
|
+
if tg_op = 'DELETE' then return old; else return new; end if;
|
|
51
|
+
end $$
|
|
52
|
+
`.execute(db);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
56
|
+
await sql`
|
|
57
|
+
create or replace function accounting.require_payment_writer() returns trigger
|
|
58
|
+
language plpgsql as $$
|
|
59
|
+
declare
|
|
60
|
+
target record;
|
|
61
|
+
has_payments boolean;
|
|
62
|
+
begin
|
|
63
|
+
if current_setting('accounting.payment_writer_version', true) = '1'
|
|
64
|
+
or to_regclass('accounting.payment') is null then
|
|
65
|
+
if tg_op = 'DELETE' then return old; else return new; end if;
|
|
66
|
+
end if;
|
|
67
|
+
for target in
|
|
68
|
+
select distinct (value ->> 'tenant_id')::uuid as tenant_id,
|
|
69
|
+
(case when tg_table_name = 'transaction' then value ->> 'id'
|
|
70
|
+
else value ->> 'transaction_id' end)::uuid as transaction_id
|
|
71
|
+
from jsonb_array_elements(jsonb_build_array(to_jsonb(old), to_jsonb(new)))
|
|
72
|
+
where value is not null and value <> 'null'::jsonb
|
|
73
|
+
order by tenant_id, transaction_id
|
|
74
|
+
loop
|
|
75
|
+
if target.transaction_id is null then continue; end if;
|
|
76
|
+
-- Payment creation locks this same row before inserting its evidence.
|
|
77
|
+
perform id from accounting.transaction
|
|
78
|
+
where id = target.transaction_id and tenant_id = target.tenant_id for update;
|
|
79
|
+
execute 'select exists (select 1 from accounting.payment
|
|
80
|
+
where tenant_id = $1 and transaction_id = $2)'
|
|
81
|
+
into has_payments using target.tenant_id, target.transaction_id;
|
|
82
|
+
if has_payments then
|
|
83
|
+
raise exception using errcode = '23514', constraint = 'payment_writer_compatibility',
|
|
84
|
+
message = 'This transaction requires a deployment with payments support',
|
|
85
|
+
detail = jsonb_build_object('tenantId', target.tenant_id,
|
|
86
|
+
'transactionId', target.transaction_id)::text;
|
|
87
|
+
end if;
|
|
88
|
+
end loop;
|
|
89
|
+
if tg_op = 'DELETE' then return old; else return new; end if;
|
|
90
|
+
end $$
|
|
91
|
+
`.execute(db);
|
|
92
|
+
}
|