@vrplatform/kysely 1.3.45-37798929841 → 1.3.45-37968626009
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
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
5
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
6
|
+
// These indexes scan existing multi-million-row tables.
|
|
7
|
+
await sql`set local statement_timeout = '10min'`.execute(db);
|
|
8
|
+
|
|
9
|
+
// recoverPublicAuditEvents (PRO-19064) looks for events projected with an
|
|
10
|
+
// older version. Without this index it probes every audit action since the
|
|
11
|
+
// cutover on each run.
|
|
12
|
+
await sql`
|
|
13
|
+
create index if not exists public_event_projection_version_idx
|
|
14
|
+
on audit.public_event (projection_version)
|
|
15
|
+
`.execute(db);
|
|
16
|
+
// The payment linker (PRO-18060) reads pending lines newest first, one
|
|
17
|
+
// keyset page at a time. match_status_tenant_id_idx cannot order them, so
|
|
18
|
+
// every page read all pending lines of the tenant.
|
|
19
|
+
await sql`
|
|
20
|
+
create index if not exists payment_line_pending_tenant_created_idx
|
|
21
|
+
on public.payment_line (tenant_id, created_at desc, id desc)
|
|
22
|
+
where match_status = 'pending'
|
|
23
|
+
`.execute(db);
|
|
24
|
+
await sql`
|
|
25
|
+
create index if not exists transaction_line_pending_tenant_created_idx
|
|
26
|
+
on accounting.transaction_line (tenant_id, created_at desc, id desc)
|
|
27
|
+
where match_status = 'pending'
|
|
28
|
+
`.execute(db);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
32
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
33
|
+
await sql`set local statement_timeout = '60s'`.execute(db);
|
|
34
|
+
|
|
35
|
+
await sql`
|
|
36
|
+
drop index if exists accounting.transaction_line_pending_tenant_created_idx
|
|
37
|
+
`.execute(db);
|
|
38
|
+
await sql`
|
|
39
|
+
drop index if exists public.payment_line_pending_tenant_created_idx
|
|
40
|
+
`.execute(db);
|
|
41
|
+
await sql`
|
|
42
|
+
drop index if exists audit.public_event_projection_version_idx
|
|
43
|
+
`.execute(db);
|
|
44
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
5
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
6
|
+
await sql`set local statement_timeout = '10min'`.execute(db);
|
|
7
|
+
|
|
8
|
+
// With 100 most-common values, the planner assumed an emptied tenant still
|
|
9
|
+
// owned about 545000 core.change rows, chose a skip scan over
|
|
10
|
+
// (sync_id, tenant_id) for the foreign-key check, and timed out deleting the
|
|
11
|
+
// tenant row (PRO-18138). A running vacuum holds the lock this needs; apply
|
|
12
|
+
// it when the table is idle.
|
|
13
|
+
await sql`
|
|
14
|
+
alter table core.change alter column tenant_id set statistics 1000
|
|
15
|
+
`.execute(db);
|
|
16
|
+
await sql`analyze core.change (tenant_id)`.execute(db);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
20
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
21
|
+
await sql`set local statement_timeout = '60s'`.execute(db);
|
|
22
|
+
|
|
23
|
+
await sql`
|
|
24
|
+
alter table core.change alter column tenant_id set statistics -1
|
|
25
|
+
`.execute(db);
|
|
26
|
+
}
|