@vrplatform/kysely 1.3.45-391 → 1.3.45-39120281816
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.
- package/build/.data.tar +2 -2
- package/build/main/camel-case/index.d.ts +4 -4
- package/build/main/camel-case/index.js +2 -1
- package/build/main/camel-case/index.js.map +1 -1
- package/build/main/connect-handoff.d.ts +12 -0
- package/build/main/connect-handoff.js +73 -0
- package/build/main/connect-handoff.js.map +1 -0
- package/build/main/control-plane/index.d.ts +19 -1
- package/build/main/control-plane/index.js +10 -0
- package/build/main/control-plane/index.js.map +1 -1
- package/build/main/index.d.ts +38 -2
- package/build/main/index.js +323 -58
- package/build/main/index.js.map +1 -1
- package/build/main/isomorphic.d.ts +1 -1
- package/build/main/isomorphic.js +2 -1
- package/build/main/isomorphic.js.map +1 -1
- package/build/main/local/generate.js +4 -3
- package/build/main/local/generate.js.map +1 -1
- package/build/main/local/index.js +3 -0
- package/build/main/local/index.js.map +1 -1
- package/build/main/migration-source.d.ts +4 -0
- package/build/main/migration-source.js +45 -0
- package/build/main/migration-source.js.map +1 -0
- package/build/main/pganalyze-comment-plugin/comment-transformer.js +3 -1
- package/build/main/pganalyze-comment-plugin/comment-transformer.js.map +1 -1
- package/build/main/query/listing/status.js +6 -2
- package/build/main/query/listing/status.js.map +1 -1
- package/build/main/query/tenant/status.d.ts +1 -6
- package/build/main/query/tenant/status.js +4 -12
- package/build/main/query/tenant/status.js.map +1 -1
- package/build/main/regional.d.ts +1 -0
- package/build/main/regional.js +1 -0
- package/build/main/regional.js.map +1 -1
- package/build/main/test/index.js +4 -3
- package/build/main/test/index.js.map +1 -1
- package/build/main/v1.generated.d.ts +255 -19
- package/build/main/v1.generated.js.map +1 -1
- package/build/migrations-v2/0000000000033-plaid-connect-session-telemetry.ts +31 -0
- package/build/migrations-v2/0000000000049-queued-journal-operation-failures.ts +60 -0
- package/build/migrations-v2/0000000000050-connection-oauth-state.ts +43 -0
- package/build/migrations-v2/0000000000051-owner-statement-email-delivery.ts +134 -0
- package/build/migrations-v2/0000000000052-fee-journal-lock-skip-operations.ts +59 -0
- package/build/migrations-v2/0000000000053-fee-journal-lock-skip-null-attempt.ts +44 -0
- package/build/migrations-v2/0000000000054-journal-refresh-issue-codes.ts +16 -0
- package/build/migrations-v2/0000000000055-tenant-billing-subscription.ts +8 -0
- package/build/migrations-v2/0000000000056-journal-operation-failure-precedence.ts +306 -0
- package/build/migrations-v2/0000000000057-journal-invalidation-failure-code.ts +28 -0
- package/build/migrations-v2/0000000000058-journal-invalidation-locked-failure.ts +16 -0
- package/build/migrations-v2/0000000000059-billing-active-listing-cross-region-partner.ts +135 -0
- package/build/migrations-v2/0000000000060-accounting-integrity-change-driven.ts +99 -0
- package/build/migrations-v2/0000000000061-tenant-pending-onboarding-status.ts +37 -0
- package/build/migrations-v2/0000000000062-statement-payout-operations.ts +16 -0
- package/build/migrations-v2/0000000000063-missing-account-assignments-issue-code.ts +13 -0
- package/build/migrations-v2/0000000000064-journal-invalidation-source-lookup-index.ts +26 -0
- package/build/migrations-v2/0000000000065-hasura-connection-event-credentials.ts +42 -0
- package/build/migrations-v2/0000000000066-recurring-fee-listing-subscription-match-by.ts +51 -0
- package/build/migrations-v2/0000000000067-connection-credential-metadata.ts +60 -0
- package/build/migrations-v2/0000000000068-connection-credential-version.ts +34 -0
- package/build/migrations-v2/0000000000069-owner-statement-contact-email-delivery.ts +84 -0
- package/build/migrations-v2/0000000000070-payment-writer-compatibility.ts +69 -0
- package/build/migrations-v2/0000000000071-payments.ts +532 -0
- package/build/migrations-v2/0000000000072-journal-entry-payment-index.ts +21 -0
- package/build/migrations-v2/0000000000073-batch-invalidation-operation-registration.ts +74 -0
- package/build/migrations-v2/0000000000074-owner-statement-email-history.ts +34 -0
- package/build/migrations-v2/0000000000075-tenant-deletion-lookup-indexes.ts +46 -0
- package/build/migrations-v2/0000000000076-tenant-onboarding-not-null.ts +23 -0
- package/build/migrations-v2/0000000000077-payment-writer-statement-attachment.ts +92 -0
- package/build/migrations-v2/0000000000078-trigger-timeout-indexes.ts +44 -0
- package/build/migrations-v2/0000000000079-core-change-tenant-statistics.ts +26 -0
- package/build/migrations-v2/0000000000080-drop-legacy-tracking-event.ts +60 -0
- package/build/migrations-v2/0000000000081-recurring-fee-planner-operation-status.ts +35 -0
- package/build/migrations-v2/0000000000082-reservation-search-trigram-indexes.ts +54 -0
- package/build/migrations-v2/0000000000083-journal-entry-deposit-lookup-index.ts +31 -0
- package/build/migrations-v2/0000000000084-journal-entry-reservation-revenue-indexes.ts +39 -0
- package/build/migrations-v2/0000000000085-owner-statement-link-email-template.ts +23 -0
- package/build/migrations-v2/0000000000086-contact-payment-setup.ts +72 -0
- package/build/migrations-v2/0000000000087-address-delete-fk-indexes.ts +30 -0
- package/build/migrations-v2/0000000000088-owner-statement-email-switch.ts +76 -0
- package/build/module/camel-case/index.d.ts +4 -4
- package/build/module/camel-case/index.js +3 -2
- package/build/module/camel-case/index.js.map +1 -1
- package/build/module/connect-handoff.d.ts +12 -0
- package/build/module/connect-handoff.js +70 -0
- package/build/module/connect-handoff.js.map +1 -0
- package/build/module/control-plane/index.d.ts +19 -1
- package/build/module/control-plane/index.js +9 -0
- package/build/module/control-plane/index.js.map +1 -1
- package/build/module/index.d.ts +38 -2
- package/build/module/index.js +322 -58
- package/build/module/index.js.map +1 -1
- package/build/module/isomorphic.d.ts +1 -1
- package/build/module/isomorphic.js +1 -1
- package/build/module/isomorphic.js.map +1 -1
- package/build/module/local/generate.js +4 -3
- package/build/module/local/generate.js.map +1 -1
- package/build/module/local/index.js +3 -0
- package/build/module/local/index.js.map +1 -1
- package/build/module/migration-source.d.ts +4 -0
- package/build/module/migration-source.js +42 -0
- package/build/module/migration-source.js.map +1 -0
- package/build/module/pganalyze-comment-plugin/comment-transformer.js +3 -1
- package/build/module/pganalyze-comment-plugin/comment-transformer.js.map +1 -1
- package/build/module/query/listing/status.js +6 -2
- package/build/module/query/listing/status.js.map +1 -1
- package/build/module/query/tenant/status.d.ts +1 -6
- package/build/module/query/tenant/status.js +4 -11
- package/build/module/query/tenant/status.js.map +1 -1
- package/build/module/regional.d.ts +1 -0
- package/build/module/regional.js +1 -0
- package/build/module/regional.js.map +1 -1
- package/build/module/test/index.js +4 -3
- package/build/module/test/index.js.map +1 -1
- package/build/module/v1.generated.d.ts +255 -19
- package/build/module/v1.generated.js.map +1 -1
- package/package.json +8 -6
|
@@ -0,0 +1,43 @@
|
|
|
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 = '60s'`.execute(db);
|
|
7
|
+
await sql`
|
|
8
|
+
create table public.connection_oauth_state (
|
|
9
|
+
state_hash text primary key,
|
|
10
|
+
tenant_id uuid not null references public.tenant (id) on delete cascade,
|
|
11
|
+
user_id uuid not null,
|
|
12
|
+
app_id text not null,
|
|
13
|
+
connection_id uuid,
|
|
14
|
+
params jsonb not null,
|
|
15
|
+
redirect_uri text not null,
|
|
16
|
+
expires_at timestamptz not null,
|
|
17
|
+
constraint connection_oauth_state_hash_shape check (state_hash ~ '^[0-9a-f]{64}$'),
|
|
18
|
+
constraint connection_oauth_state_params_object check (jsonb_typeof(params) = 'object')
|
|
19
|
+
)
|
|
20
|
+
`.execute(db);
|
|
21
|
+
await sql`create index connection_oauth_state_expiry_idx on public.connection_oauth_state (expires_at)`.execute(
|
|
22
|
+
db
|
|
23
|
+
);
|
|
24
|
+
await sql`create index connection_oauth_state_tenant_idx on public.connection_oauth_state (tenant_id)`.execute(
|
|
25
|
+
db
|
|
26
|
+
);
|
|
27
|
+
await sql`
|
|
28
|
+
do $$
|
|
29
|
+
begin
|
|
30
|
+
if exists (select 1 from pg_roles where rolname = 'application') then
|
|
31
|
+
grant select, insert, delete on public.connection_oauth_state to application;
|
|
32
|
+
-- SELECT FOR UPDATE SKIP LOCKED needs UPDATE on at least one column.
|
|
33
|
+
-- State bindings, parameters, and expiry remain non-updatable at runtime.
|
|
34
|
+
grant update (state_hash) on public.connection_oauth_state to application;
|
|
35
|
+
end if;
|
|
36
|
+
end;
|
|
37
|
+
$$
|
|
38
|
+
`.execute(db);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
42
|
+
await sql`drop table public.connection_oauth_state`.execute(db);
|
|
43
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
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 = '60s'`.execute(db);
|
|
7
|
+
|
|
8
|
+
await sql`
|
|
9
|
+
create type core.email_notification_status as enum (
|
|
10
|
+
'queued', 'processing', 'completed', 'failed', 'canceled'
|
|
11
|
+
)
|
|
12
|
+
`.execute(db);
|
|
13
|
+
await sql`
|
|
14
|
+
create type core.email_delivery_status as enum (
|
|
15
|
+
'queued', 'sending', 'retrying', 'accepted', 'rejected', 'failed',
|
|
16
|
+
'canceled'
|
|
17
|
+
)
|
|
18
|
+
`.execute(db);
|
|
19
|
+
await sql`
|
|
20
|
+
create type core.email_delivery_failure_code as enum (
|
|
21
|
+
'inactive_recipient',
|
|
22
|
+
'invalid_provider_response',
|
|
23
|
+
'postmark_not_configured',
|
|
24
|
+
'provider_rejected',
|
|
25
|
+
'task_failed',
|
|
26
|
+
'transport_failed'
|
|
27
|
+
)
|
|
28
|
+
`.execute(db);
|
|
29
|
+
|
|
30
|
+
await sql`
|
|
31
|
+
create table core.email_notification (
|
|
32
|
+
id uuid primary key references audit.effect(id) on delete cascade,
|
|
33
|
+
tenant_id uuid not null references public.tenant(id) on delete cascade,
|
|
34
|
+
owner_statement_id uuid not null,
|
|
35
|
+
status core.email_notification_status not null default 'queued',
|
|
36
|
+
trigger_run_id text,
|
|
37
|
+
recipients_resolved_at timestamptz,
|
|
38
|
+
started_at timestamptz,
|
|
39
|
+
finished_at timestamptz,
|
|
40
|
+
created_at timestamptz not null default now(),
|
|
41
|
+
updated_at timestamptz not null default now(),
|
|
42
|
+
constraint email_notification_terminal_check check (
|
|
43
|
+
(status in ('completed', 'failed', 'canceled') and finished_at is not null)
|
|
44
|
+
or (status in ('queued', 'processing') and finished_at is null)
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
`.execute(db);
|
|
48
|
+
await sql`
|
|
49
|
+
create index email_notification_statement_history_idx
|
|
50
|
+
on core.email_notification (
|
|
51
|
+
tenant_id, owner_statement_id, created_at desc, id desc
|
|
52
|
+
)
|
|
53
|
+
`.execute(db);
|
|
54
|
+
|
|
55
|
+
await sql`
|
|
56
|
+
create table core.email_delivery (
|
|
57
|
+
id uuid primary key default gen_random_uuid(),
|
|
58
|
+
notification_id uuid not null
|
|
59
|
+
references core.email_notification(id) on delete cascade,
|
|
60
|
+
tenant_id uuid not null references public.tenant(id) on delete cascade,
|
|
61
|
+
user_id uuid not null,
|
|
62
|
+
status core.email_delivery_status not null default 'queued',
|
|
63
|
+
attempt_count integer not null default 0,
|
|
64
|
+
trigger_run_id text,
|
|
65
|
+
postmark_message_id text,
|
|
66
|
+
postmark_error_code integer,
|
|
67
|
+
http_status integer,
|
|
68
|
+
failure_code core.email_delivery_failure_code,
|
|
69
|
+
submitted_at timestamptz,
|
|
70
|
+
last_attempt_at timestamptz,
|
|
71
|
+
finished_at timestamptz,
|
|
72
|
+
created_at timestamptz not null default now(),
|
|
73
|
+
updated_at timestamptz not null default now(),
|
|
74
|
+
constraint email_delivery_notification_user_key
|
|
75
|
+
unique (notification_id, user_id),
|
|
76
|
+
constraint email_delivery_attempt_count_check check (attempt_count >= 0),
|
|
77
|
+
constraint email_delivery_postmark_error_code_check check (
|
|
78
|
+
postmark_error_code is null or postmark_error_code >= 0
|
|
79
|
+
),
|
|
80
|
+
constraint email_delivery_http_status_check check (
|
|
81
|
+
http_status is null or http_status between 100 and 599
|
|
82
|
+
),
|
|
83
|
+
constraint email_delivery_terminal_check check (
|
|
84
|
+
(status in ('accepted', 'rejected', 'failed', 'canceled')
|
|
85
|
+
and finished_at is not null)
|
|
86
|
+
or (status in ('queued', 'sending', 'retrying') and finished_at is null)
|
|
87
|
+
),
|
|
88
|
+
constraint email_delivery_accepted_check check (
|
|
89
|
+
status != 'accepted'
|
|
90
|
+
or (
|
|
91
|
+
postmark_message_id is not null
|
|
92
|
+
and submitted_at is not null
|
|
93
|
+
and failure_code is null
|
|
94
|
+
)
|
|
95
|
+
),
|
|
96
|
+
constraint email_delivery_failure_check check (
|
|
97
|
+
status not in ('rejected', 'failed') or failure_code is not null
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
`.execute(db);
|
|
101
|
+
await sql`
|
|
102
|
+
create index email_delivery_notification_status_idx
|
|
103
|
+
on core.email_delivery (notification_id, status, id)
|
|
104
|
+
`.execute(db);
|
|
105
|
+
await sql`
|
|
106
|
+
create index email_delivery_tenant_created_idx
|
|
107
|
+
on core.email_delivery (tenant_id, created_at desc, id desc)
|
|
108
|
+
`.execute(db);
|
|
109
|
+
await sql`
|
|
110
|
+
create unique index email_delivery_postmark_message_idx
|
|
111
|
+
on core.email_delivery (postmark_message_id)
|
|
112
|
+
where postmark_message_id is not null
|
|
113
|
+
`.execute(db);
|
|
114
|
+
|
|
115
|
+
await sql`
|
|
116
|
+
do $$
|
|
117
|
+
begin
|
|
118
|
+
if exists (select 1 from pg_roles where rolname = 'application') then
|
|
119
|
+
grant select, insert, update
|
|
120
|
+
on core.email_notification, core.email_delivery
|
|
121
|
+
to application;
|
|
122
|
+
end if;
|
|
123
|
+
end;
|
|
124
|
+
$$
|
|
125
|
+
`.execute(db);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
129
|
+
await sql`drop table core.email_delivery`.execute(db);
|
|
130
|
+
await sql`drop table core.email_notification`.execute(db);
|
|
131
|
+
await sql`drop type core.email_delivery_failure_code`.execute(db);
|
|
132
|
+
await sql`drop type core.email_delivery_status`.execute(db);
|
|
133
|
+
await sql`drop type core.email_notification_status`.execute(db);
|
|
134
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Kysely, sql } from 'kysely';
|
|
2
|
+
|
|
3
|
+
const original = `invalidation.id is null
|
|
4
|
+
and effect.status = 'completed'`;
|
|
5
|
+
const corrected = `(invalidation.id is null or (
|
|
6
|
+
exists (
|
|
7
|
+
select 1 from audit.action as action
|
|
8
|
+
where action.id = effect.action_id
|
|
9
|
+
and action.tenant_id = effect.tenant_id
|
|
10
|
+
and action.root_entity_type = 'recurringFee'
|
|
11
|
+
)
|
|
12
|
+
and (
|
|
13
|
+
select attempt.status from audit.effect_attempt as attempt
|
|
14
|
+
where attempt.effect_id = effect.id
|
|
15
|
+
and attempt.tenant_id = effect.tenant_id
|
|
16
|
+
order by attempt.attempt_num desc limit 1
|
|
17
|
+
) = 'skipped_locked'
|
|
18
|
+
))
|
|
19
|
+
and effect.status = 'completed'`;
|
|
20
|
+
|
|
21
|
+
async function replaceFeeLockSkipProjection<Database>(
|
|
22
|
+
db: Kysely<Database>,
|
|
23
|
+
before: string,
|
|
24
|
+
after: string
|
|
25
|
+
) {
|
|
26
|
+
const { rows } = await sql<{ definition: string }>`
|
|
27
|
+
select pg_get_functiondef('core.refresh_audit_action_source(uuid)'::regprocedure)
|
|
28
|
+
as definition
|
|
29
|
+
`.execute(db);
|
|
30
|
+
const definition = rows[0]?.definition;
|
|
31
|
+
if (!definition || definition.split(before).length !== 2) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
'Journal operation source projection has an unexpected definition'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
await sql.raw(definition.replace(before, after)).execute(db);
|
|
37
|
+
// Reproject derived operation state only. Preserve original attempts and journal invalidations.
|
|
38
|
+
await sql`
|
|
39
|
+
select core.refresh_audit_action_source(action.id)
|
|
40
|
+
from audit.action as action
|
|
41
|
+
where action.root_entity_type = 'recurringFee'
|
|
42
|
+
and exists (
|
|
43
|
+
select 1 from core.operation_source as source
|
|
44
|
+
inner join core.operation as operation on operation.id = source.operation_id
|
|
45
|
+
where source.source_type = 'audit-action'
|
|
46
|
+
and source.source_id = action.id
|
|
47
|
+
and operation.tenant_id = action.tenant_id
|
|
48
|
+
and operation.type = 'reservation-journal-refresh'
|
|
49
|
+
)
|
|
50
|
+
`.execute(db);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function up<Database>(db: Kysely<Database>): Promise<void> {
|
|
54
|
+
await replaceFeeLockSkipProjection(db, original, corrected);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function down<Database>(db: Kysely<Database>): Promise<void> {
|
|
58
|
+
await replaceFeeLockSkipProjection(db, corrected, original);
|
|
59
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Kysely, sql } from 'kysely';
|
|
2
|
+
|
|
3
|
+
async function replaceNullableLockSkip<Database>(
|
|
4
|
+
db: Kysely<Database>,
|
|
5
|
+
before: string,
|
|
6
|
+
after: string
|
|
7
|
+
) {
|
|
8
|
+
const { rows } = await sql<{ definition: string }>`
|
|
9
|
+
select pg_get_functiondef('core.refresh_audit_action_source(uuid)'::regprocedure) as definition
|
|
10
|
+
`.execute(db);
|
|
11
|
+
const definition = rows[0]?.definition;
|
|
12
|
+
if (!definition || definition.split(before).length !== 2) {
|
|
13
|
+
throw new Error('Fee lock skip projection has an unexpected definition');
|
|
14
|
+
}
|
|
15
|
+
await sql.raw(definition.replace(before, after)).execute(db);
|
|
16
|
+
await sql`
|
|
17
|
+
select core.refresh_audit_action_source(action.id)
|
|
18
|
+
from audit.action as action
|
|
19
|
+
where action.root_entity_type = 'recurringFee'
|
|
20
|
+
and exists (
|
|
21
|
+
select 1 from core.operation_source as source
|
|
22
|
+
inner join core.operation as operation on operation.id = source.operation_id
|
|
23
|
+
where source.source_type = 'audit-action' and source.source_id = action.id
|
|
24
|
+
and operation.tenant_id = action.tenant_id
|
|
25
|
+
and operation.type = 'reservation-journal-refresh'
|
|
26
|
+
)
|
|
27
|
+
`.execute(db);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function up<Database>(db: Kysely<Database>): Promise<void> {
|
|
31
|
+
await replaceNullableLockSkip(
|
|
32
|
+
db,
|
|
33
|
+
") = 'skipped_locked'",
|
|
34
|
+
") is not distinct from 'skipped_locked'"
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function down<Database>(db: Kysely<Database>): Promise<void> {
|
|
39
|
+
await replaceNullableLockSkip(
|
|
40
|
+
db,
|
|
41
|
+
") is not distinct from 'skipped_locked'",
|
|
42
|
+
") = 'skipped_locked'"
|
|
43
|
+
);
|
|
44
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
5
|
+
// Expand storage only. New codes must be readable on every channel before
|
|
6
|
+
// issue collectors begin writing them to shared regional databases.
|
|
7
|
+
await sql`alter type health.tenant_issue_code add value if not exists 'journalLocked'`.execute(
|
|
8
|
+
db
|
|
9
|
+
);
|
|
10
|
+
await sql`alter type health.tenant_issue_code add value if not exists 'journalConfigurationInvalid'`.execute(
|
|
11
|
+
db
|
|
12
|
+
);
|
|
13
|
+
await sql`alter type health.tenant_issue_code add value if not exists 'journalUnbalanced'`.execute(
|
|
14
|
+
db
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { type Kysely, sql } from 'kysely';
|
|
2
|
+
|
|
3
|
+
// The served `core.operation.failure_code` projection must agree with the
|
|
4
|
+
// TypeScript classifier in `packages/repository/src/journal-freshness/load.ts`:
|
|
5
|
+
// a governing accounting lock wins over a retained failure (CUS-2936), and a
|
|
6
|
+
// retained unbalanced attempt is recognized by its message prefix as well as by
|
|
7
|
+
// the typed result code (CUS-2894).
|
|
8
|
+
const failureProjection = (ordering: 'lock-first' | 'failure-first') => sql`
|
|
9
|
+
create or replace function core.refresh_operation_state(
|
|
10
|
+
target_operation_id uuid
|
|
11
|
+
) returns void
|
|
12
|
+
language plpgsql
|
|
13
|
+
as $$
|
|
14
|
+
declare
|
|
15
|
+
next_status core.operation_status;
|
|
16
|
+
next_started_at timestamptz;
|
|
17
|
+
next_finished_at timestamptz;
|
|
18
|
+
next_failure_code text;
|
|
19
|
+
next_failure_message text;
|
|
20
|
+
target_operation_type core.operation_type;
|
|
21
|
+
target_public_id_format core.operation_public_id_format;
|
|
22
|
+
target_action_id uuid;
|
|
23
|
+
begin
|
|
24
|
+
select
|
|
25
|
+
operation.type,
|
|
26
|
+
operation.public_id_format,
|
|
27
|
+
source.source_id
|
|
28
|
+
into
|
|
29
|
+
target_operation_type,
|
|
30
|
+
target_public_id_format,
|
|
31
|
+
target_action_id
|
|
32
|
+
from core.operation as operation
|
|
33
|
+
left join core.operation_source as source
|
|
34
|
+
on source.operation_id = operation.id
|
|
35
|
+
and source.source_type = 'audit-action'
|
|
36
|
+
where operation.id = target_operation_id;
|
|
37
|
+
|
|
38
|
+
select
|
|
39
|
+
case
|
|
40
|
+
when count(*) filter (where required) = 0 then 'queued'
|
|
41
|
+
when bool_and(status = 'completed') filter (where required)
|
|
42
|
+
then 'completed'
|
|
43
|
+
when bool_and(status in ('completed', 'failed')) filter (where required)
|
|
44
|
+
then 'failed'
|
|
45
|
+
when bool_or(status = 'running') filter (where required)
|
|
46
|
+
or bool_or(status in ('completed', 'failed')) filter (where required)
|
|
47
|
+
then 'running'
|
|
48
|
+
else 'queued'
|
|
49
|
+
end::core.operation_status,
|
|
50
|
+
min(started_at) filter (where required),
|
|
51
|
+
max(finished_at) filter (where required)
|
|
52
|
+
into next_status, next_started_at, next_finished_at
|
|
53
|
+
from core.operation_source
|
|
54
|
+
where operation_id = target_operation_id;
|
|
55
|
+
|
|
56
|
+
if next_status = 'failed' then
|
|
57
|
+
if target_operation_type = 'reservation-journal-refresh'
|
|
58
|
+
and target_action_id is not null then
|
|
59
|
+
select
|
|
60
|
+
case
|
|
61
|
+
${sql.raw(
|
|
62
|
+
ordering === 'lock-first'
|
|
63
|
+
? `when core.journal_refresh_all_outstanding_locked(
|
|
64
|
+
bool_and(
|
|
65
|
+
case
|
|
66
|
+
when invalidation.id is not null
|
|
67
|
+
and invalidation.applied_at is null
|
|
68
|
+
then effect.status = 'completed'
|
|
69
|
+
and attempt.status is not distinct from 'skipped_locked'
|
|
70
|
+
end
|
|
71
|
+
),
|
|
72
|
+
bool_and(
|
|
73
|
+
case
|
|
74
|
+
when invalidation.id is not null
|
|
75
|
+
and invalidation.applied_at is null
|
|
76
|
+
then core.journal_reservation_refresh_locked(
|
|
77
|
+
invalidation.tenant_id,
|
|
78
|
+
invalidation.reservation_id
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
)
|
|
82
|
+
) then 'journalLocked'
|
|
83
|
+
when bool_or(
|
|
84
|
+
invalidation.applied_at is null
|
|
85
|
+
and attempt.result_code = 'journal_configuration_invalid'
|
|
86
|
+
) then 'journalConfigurationInvalid'
|
|
87
|
+
when bool_or(
|
|
88
|
+
invalidation.applied_at is null
|
|
89
|
+
and (
|
|
90
|
+
attempt.result_code = 'journal_unbalanced'
|
|
91
|
+
or attempt.error_message like 'Journal refresh produced unbalanced%'
|
|
92
|
+
)
|
|
93
|
+
) then 'journalUnbalanced'`
|
|
94
|
+
: `when bool_or(
|
|
95
|
+
invalidation.applied_at is null
|
|
96
|
+
and attempt.result_code = 'journal_unbalanced'
|
|
97
|
+
) then 'journalUnbalanced'
|
|
98
|
+
when bool_or(
|
|
99
|
+
invalidation.applied_at is null
|
|
100
|
+
and attempt.result_code = 'journal_configuration_invalid'
|
|
101
|
+
) then 'journalConfigurationInvalid'
|
|
102
|
+
when bool_or(
|
|
103
|
+
invalidation.applied_at is null
|
|
104
|
+
and attempt.result_code in (
|
|
105
|
+
'statement_period_locked',
|
|
106
|
+
'skipped_locked'
|
|
107
|
+
)
|
|
108
|
+
) then 'journalLocked'`
|
|
109
|
+
)}
|
|
110
|
+
else 'journalRefreshFailed'
|
|
111
|
+
end,
|
|
112
|
+
case
|
|
113
|
+
${sql.raw(
|
|
114
|
+
ordering === 'lock-first'
|
|
115
|
+
? `when core.journal_refresh_all_outstanding_locked(
|
|
116
|
+
bool_and(
|
|
117
|
+
case
|
|
118
|
+
when invalidation.id is not null
|
|
119
|
+
and invalidation.applied_at is null
|
|
120
|
+
then effect.status = 'completed'
|
|
121
|
+
and attempt.status is not distinct from 'skipped_locked'
|
|
122
|
+
end
|
|
123
|
+
),
|
|
124
|
+
bool_and(
|
|
125
|
+
case
|
|
126
|
+
when invalidation.id is not null
|
|
127
|
+
and invalidation.applied_at is null
|
|
128
|
+
then core.journal_reservation_refresh_locked(
|
|
129
|
+
invalidation.tenant_id,
|
|
130
|
+
invalidation.reservation_id
|
|
131
|
+
)
|
|
132
|
+
end
|
|
133
|
+
)
|
|
134
|
+
) then 'Journal refresh is blocked by accounting locks'
|
|
135
|
+
when bool_or(
|
|
136
|
+
invalidation.applied_at is null
|
|
137
|
+
and attempt.result_code = 'journal_configuration_invalid'
|
|
138
|
+
) then 'Journal configuration is invalid'
|
|
139
|
+
when bool_or(
|
|
140
|
+
invalidation.applied_at is null
|
|
141
|
+
and (
|
|
142
|
+
attempt.result_code = 'journal_unbalanced'
|
|
143
|
+
or attempt.error_message like 'Journal refresh produced unbalanced%'
|
|
144
|
+
)
|
|
145
|
+
) then 'Journal refresh produced unbalanced entries'`
|
|
146
|
+
: `when bool_or(
|
|
147
|
+
invalidation.applied_at is null
|
|
148
|
+
and attempt.result_code = 'journal_unbalanced'
|
|
149
|
+
) then 'Journal refresh produced unbalanced entries'
|
|
150
|
+
when bool_or(
|
|
151
|
+
invalidation.applied_at is null
|
|
152
|
+
and attempt.result_code = 'journal_configuration_invalid'
|
|
153
|
+
) then 'Journal configuration is invalid'
|
|
154
|
+
when bool_or(
|
|
155
|
+
invalidation.applied_at is null
|
|
156
|
+
and attempt.result_code in (
|
|
157
|
+
'statement_period_locked',
|
|
158
|
+
'skipped_locked'
|
|
159
|
+
)
|
|
160
|
+
) then 'Journal refresh is blocked by accounting locks'`
|
|
161
|
+
)}
|
|
162
|
+
else 'Journal refresh failed'
|
|
163
|
+
end
|
|
164
|
+
into next_failure_code, next_failure_message
|
|
165
|
+
from audit.effect as effect
|
|
166
|
+
left join accounting.reservation_journal_invalidation as invalidation
|
|
167
|
+
on invalidation.tenant_id = effect.tenant_id
|
|
168
|
+
and invalidation.effect_id = effect.id
|
|
169
|
+
left join lateral (
|
|
170
|
+
select
|
|
171
|
+
effect_attempt.status,
|
|
172
|
+
effect_attempt.result_code,
|
|
173
|
+
effect_attempt.error_message
|
|
174
|
+
from audit.effect_attempt as effect_attempt
|
|
175
|
+
where effect_attempt.tenant_id = effect.tenant_id
|
|
176
|
+
and effect_attempt.effect_id = effect.id
|
|
177
|
+
order by effect_attempt.attempt_num desc
|
|
178
|
+
limit 1
|
|
179
|
+
) as attempt on true
|
|
180
|
+
where effect.action_id = target_action_id
|
|
181
|
+
and effect.effect_type = 'REFRESH_RESERVATION_JOURNAL'
|
|
182
|
+
and effect.entity_type = 'reservation';
|
|
183
|
+
else
|
|
184
|
+
next_failure_code := 'operation_failed';
|
|
185
|
+
next_failure_message := 'Operation failed';
|
|
186
|
+
end if;
|
|
187
|
+
else
|
|
188
|
+
next_failure_code := null;
|
|
189
|
+
next_failure_message := null;
|
|
190
|
+
end if;
|
|
191
|
+
|
|
192
|
+
update core.operation
|
|
193
|
+
set
|
|
194
|
+
status = next_status,
|
|
195
|
+
started_at = coalesce(core.operation.started_at, next_started_at),
|
|
196
|
+
finished_at = case
|
|
197
|
+
when next_status in ('completed', 'failed')
|
|
198
|
+
then coalesce(next_finished_at, now())
|
|
199
|
+
else null
|
|
200
|
+
end,
|
|
201
|
+
failure_code = next_failure_code,
|
|
202
|
+
failure_message = next_failure_message,
|
|
203
|
+
updated_at = now(),
|
|
204
|
+
source_version = source_version + 1
|
|
205
|
+
where id = target_operation_id
|
|
206
|
+
and (
|
|
207
|
+
status is distinct from next_status
|
|
208
|
+
or started_at is distinct from coalesce(started_at, next_started_at)
|
|
209
|
+
or finished_at is distinct from case
|
|
210
|
+
when next_status in ('completed', 'failed')
|
|
211
|
+
then coalesce(next_finished_at, finished_at, now())
|
|
212
|
+
else null
|
|
213
|
+
end
|
|
214
|
+
or failure_code is distinct from next_failure_code
|
|
215
|
+
or failure_message is distinct from next_failure_message
|
|
216
|
+
);
|
|
217
|
+
end;
|
|
218
|
+
$$
|
|
219
|
+
`;
|
|
220
|
+
|
|
221
|
+
// The reservation lock driver from `isReservationRefreshLocked`: the refresh is
|
|
222
|
+
// locked when every active reservation entry sits in a published statement or
|
|
223
|
+
// in closed books.
|
|
224
|
+
const reservationRefreshLocked = sql`
|
|
225
|
+
create or replace function core.journal_reservation_refresh_locked(
|
|
226
|
+
target_tenant_id uuid,
|
|
227
|
+
target_reservation_id uuid
|
|
228
|
+
) returns boolean
|
|
229
|
+
language sql
|
|
230
|
+
stable
|
|
231
|
+
as $$
|
|
232
|
+
select exists (
|
|
233
|
+
select 1
|
|
234
|
+
from accounting.journal_entry as entry
|
|
235
|
+
inner join public.tenant as tenant on tenant.id = entry.tenant_id
|
|
236
|
+
where entry.tenant_id = target_tenant_id
|
|
237
|
+
and entry.reservation_id = target_reservation_id
|
|
238
|
+
and entry.status = 'active'
|
|
239
|
+
and (entry.entity_type is null or entry.entity_type = 'reservation')
|
|
240
|
+
and (
|
|
241
|
+
entry.attached_to_owner_statement_id is not null
|
|
242
|
+
or entry.transfer_owner_statement_id is not null
|
|
243
|
+
or entry.txn_at < tenant.books_closed_at
|
|
244
|
+
)
|
|
245
|
+
)
|
|
246
|
+
$$
|
|
247
|
+
`;
|
|
248
|
+
|
|
249
|
+
// `bool_and` over no outstanding invalidation returns null; the classifier
|
|
250
|
+
// treats that as "not locked".
|
|
251
|
+
const allOutstandingLocked = sql`
|
|
252
|
+
create or replace function core.journal_refresh_all_outstanding_locked(
|
|
253
|
+
all_lock_skipped boolean,
|
|
254
|
+
all_reservations_locked boolean
|
|
255
|
+
) returns boolean
|
|
256
|
+
language sql
|
|
257
|
+
immutable
|
|
258
|
+
as $$
|
|
259
|
+
select coalesce(all_lock_skipped, false) or coalesce(all_reservations_locked, false)
|
|
260
|
+
$$
|
|
261
|
+
`;
|
|
262
|
+
|
|
263
|
+
async function recomputeFailedJournalOperations(db: Kysely<unknown>) {
|
|
264
|
+
// Derived operation state only. Attempts, invalidations and journals are untouched.
|
|
265
|
+
await sql`
|
|
266
|
+
select core.refresh_operation_state(operation.id)
|
|
267
|
+
from core.operation as operation
|
|
268
|
+
where operation.type = 'reservation-journal-refresh'
|
|
269
|
+
and operation.status = 'failed'
|
|
270
|
+
and exists (
|
|
271
|
+
select 1 from core.operation_source as source
|
|
272
|
+
where source.operation_id = operation.id
|
|
273
|
+
and source.source_type = 'audit-action'
|
|
274
|
+
)
|
|
275
|
+
`.execute(db);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
279
|
+
await sql`
|
|
280
|
+
do $$
|
|
281
|
+
begin
|
|
282
|
+
if position(
|
|
283
|
+
'and target_public_id_format = ''action'''
|
|
284
|
+
in pg_get_functiondef('core.refresh_operation_state(uuid)'::regprocedure)
|
|
285
|
+
) > 0 then
|
|
286
|
+
raise exception 'Journal operation failure projection has an unexpected definition';
|
|
287
|
+
end if;
|
|
288
|
+
end;
|
|
289
|
+
$$
|
|
290
|
+
`.execute(db);
|
|
291
|
+
await reservationRefreshLocked.execute(db);
|
|
292
|
+
await allOutstandingLocked.execute(db);
|
|
293
|
+
await failureProjection('lock-first').execute(db);
|
|
294
|
+
await recomputeFailedJournalOperations(db);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
298
|
+
await failureProjection('failure-first').execute(db);
|
|
299
|
+
await recomputeFailedJournalOperations(db);
|
|
300
|
+
await sql`drop function if exists core.journal_refresh_all_outstanding_locked(boolean, boolean)`.execute(
|
|
301
|
+
db
|
|
302
|
+
);
|
|
303
|
+
await sql`drop function if exists core.journal_reservation_refresh_locked(uuid, uuid)`.execute(
|
|
304
|
+
db
|
|
305
|
+
);
|
|
306
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
// An inline reservation journal refresh has no effect row, so a refresh that
|
|
5
|
+
// completes without writing the reservation's postings had nowhere to record
|
|
6
|
+
// why. Storage only: nullable column, no default, no backfill, no rewrite.
|
|
7
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
8
|
+
await sql`
|
|
9
|
+
create type accounting.reservation_journal_invalidation_failure as enum (
|
|
10
|
+
'journal_configuration_invalid'
|
|
11
|
+
)
|
|
12
|
+
`.execute(db);
|
|
13
|
+
await sql`
|
|
14
|
+
alter table accounting.reservation_journal_invalidation
|
|
15
|
+
add column failure_code
|
|
16
|
+
accounting.reservation_journal_invalidation_failure
|
|
17
|
+
`.execute(db);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
21
|
+
await sql`
|
|
22
|
+
alter table accounting.reservation_journal_invalidation
|
|
23
|
+
drop column if exists failure_code
|
|
24
|
+
`.execute(db);
|
|
25
|
+
await sql`
|
|
26
|
+
drop type if exists accounting.reservation_journal_invalidation_failure
|
|
27
|
+
`.execute(db);
|
|
28
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
// An inline reservation journal refresh refused by an accounting lock records
|
|
5
|
+
// why on its invalidation, like a discarded build does. Storage only: the
|
|
6
|
+
// value must be readable on every channel before the writer starts using it.
|
|
7
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
8
|
+
await sql`
|
|
9
|
+
alter type accounting.reservation_journal_invalidation_failure
|
|
10
|
+
add value if not exists 'journal_locked'
|
|
11
|
+
`.execute(db);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function down(_db: Kysely<unknown>): Promise<void> {
|
|
15
|
+
// PostgreSQL enum values cannot be removed safely without rebuilding the type.
|
|
16
|
+
}
|