@vrplatform/kysely 1.3.41 → 1.3.45-1

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.
Files changed (159) hide show
  1. package/build/.data.tar +0 -0
  2. package/build/main/camel-case/index.js +1 -1
  3. package/build/main/camel-case/index.js.map +1 -1
  4. package/build/main/control-plane/index.d.ts +368 -0
  5. package/build/main/control-plane/index.js +64 -0
  6. package/build/main/control-plane/index.js.map +1 -0
  7. package/build/main/domain.d.ts +4 -0
  8. package/build/main/domain.js +3 -0
  9. package/build/main/domain.js.map +1 -0
  10. package/build/main/error.d.ts +2 -1
  11. package/build/main/error.js +2 -1
  12. package/build/main/error.js.map +1 -1
  13. package/build/main/index.d.ts +13 -16
  14. package/build/main/index.js +145 -65
  15. package/build/main/index.js.map +1 -1
  16. package/build/main/isomorphic.d.ts +5 -5
  17. package/build/main/isomorphic.js +4 -10
  18. package/build/main/isomorphic.js.map +1 -1
  19. package/build/main/local/digest.d.ts +3 -3
  20. package/build/main/local/digest.js.map +1 -1
  21. package/build/main/local/generate.js +10 -10
  22. package/build/main/local/generate.js.map +1 -1
  23. package/build/main/local/index.d.ts +5 -1
  24. package/build/main/local/index.js +215 -6
  25. package/build/main/local/index.js.map +1 -1
  26. package/build/main/local/prune-pulled-schema.js +4 -1
  27. package/build/main/local/prune-pulled-schema.js.map +1 -1
  28. package/build/main/migration.d.ts +7 -0
  29. package/build/main/migration.js +11 -0
  30. package/build/main/migration.js.map +1 -0
  31. package/build/main/pganalyze-comment-plugin/comment-transformer.js +0 -1
  32. package/build/main/pganalyze-comment-plugin/comment-transformer.js.map +1 -1
  33. package/build/main/pganalyze-comment-plugin/index.js +16 -3
  34. package/build/main/pganalyze-comment-plugin/index.js.map +1 -1
  35. package/build/main/plugins.d.ts +2 -2
  36. package/build/main/plugins.js.map +1 -1
  37. package/build/main/postgres-error.d.ts +8 -0
  38. package/build/main/postgres-error.js +21 -0
  39. package/build/main/postgres-error.js.map +1 -0
  40. package/build/main/query/index.d.ts +1 -0
  41. package/build/main/query/index.js +1 -0
  42. package/build/main/query/index.js.map +1 -1
  43. package/build/main/query/listing/status.d.ts +2 -12
  44. package/build/main/query/listing/status.js +31 -19
  45. package/build/main/query/listing/status.js.map +1 -1
  46. package/build/main/query/tenant/billing-partner.d.ts +4 -0
  47. package/build/main/query/tenant/billing-partner.js +8 -0
  48. package/build/main/query/tenant/billing-partner.js.map +1 -0
  49. package/build/main/query/tenant/index.d.ts +2 -0
  50. package/build/main/query/tenant/index.js +19 -0
  51. package/build/main/query/tenant/index.js.map +1 -0
  52. package/build/main/query/tenant/status.d.ts +14 -0
  53. package/build/main/query/tenant/status.js +40 -0
  54. package/build/main/query/tenant/status.js.map +1 -0
  55. package/build/main/regional.d.ts +60 -0
  56. package/build/main/regional.js +205 -0
  57. package/build/main/regional.js.map +1 -0
  58. package/build/main/test/index.d.ts +3 -14
  59. package/build/main/test/index.js +0 -23
  60. package/build/main/test/index.js.map +1 -1
  61. package/build/main/transaction.d.ts +3 -3
  62. package/build/main/transaction.js.map +1 -1
  63. package/build/main/v1.generated.d.ts +593 -23
  64. package/build/main/v1.generated.js.map +1 -1
  65. package/build/migrations-v2/0000000000000-baseline.ts +5 -0
  66. package/build/migrations-v2/0000000000001-public-audit-events-v2.ts +137 -0
  67. package/build/migrations-v2/0000000000002-audit-recovery-watermarks.ts +30 -0
  68. package/build/migrations-v2/0000000000003-audit-effect-recovery-watermark.ts +13 -0
  69. package/build/migrations-v2/0000000000004-audit-event-projected-at.ts +15 -0
  70. package/build/migrations-v2/0000000000005-audit-recovery-indexes.ts +63 -0
  71. package/build/migrations-v2/0000000000006-audit-action-public-event-projected-at.ts +22 -0
  72. package/build/migrations-v2/0000000000007-audit-action-public-event-projected-at-backfill.ts +35 -0
  73. package/build/migrations-v2/0000000000008-global-user-attribution.ts +33 -0
  74. package/build/migrations-v2/0000000000008-team-issue-rule-state.ts +52 -0
  75. package/build/migrations-v2/0000000000009-reservation-issue-projections.ts +45 -0
  76. package/build/migrations-v2/0000000000010-bank-rule-application-bank-record-delete-cascade.ts +26 -0
  77. package/build/migrations-v2/0000000000011-guest-balance-journal-entry-lookup-index.ts +25 -0
  78. package/build/migrations-v2/0000000000012-trial-free-analytics-billing-status.ts +72 -0
  79. package/build/migrations-v2/0000000000013-ui-permission-bundles.ts +45 -0
  80. package/build/migrations-v2/0000000000014-ui-permission-bundle-top-up.ts +32 -0
  81. package/build/migrations-v2/0000000000016-managed-team-access.ts +34 -0
  82. package/build/migrations-v2/0000000000017-drop-trial-until.ts +42 -0
  83. package/build/migrations-v2/0000000000018-accounting-integrity-monitoring.ts +276 -0
  84. package/build/migrations-v2/0000000000019-accounting-integrity-run-shards.ts +26 -0
  85. package/build/migrations-v2/0000000000020-accounting-integrity-case-classification.ts +52 -0
  86. package/build/migrations-v2/0000000000021-payment-line-classification-monitor-indexes.ts +31 -0
  87. package/build/migrations-v2/0000000000022-accounting-integrity-controlled-repairs.ts +281 -0
  88. package/build/migrations-v2/0000000000023-line-classification-usage.ts +325 -0
  89. package/build/migrations-v2/0000000000024-tenant-region-migration.ts +108 -0
  90. package/build/migrations-v2/0000000000025-partner-ach-denial-default.ts +30 -0
  91. package/build/migrations-v2/0000000000026-line-classification-usage-capture.ts +399 -0
  92. package/build/migrations-v2/0000000000027-payout-replacement-chain.ts +27 -0
  93. package/build/migrations-v2/0000000000028-accounting-integrity-operator-workflow.ts +108 -0
  94. package/build/migrations-v2/0000000000029-accounting-integrity-definition-artifacts.ts +40 -0
  95. package/build/migrations-v2/0000000000030-accounting-integrity-review-metadata-compatibility.ts +49 -0
  96. package/build/module/camel-case/index.js +1 -1
  97. package/build/module/camel-case/index.js.map +1 -1
  98. package/build/module/control-plane/index.d.ts +368 -0
  99. package/build/module/control-plane/index.js +50 -0
  100. package/build/module/control-plane/index.js.map +1 -0
  101. package/build/module/domain.d.ts +4 -0
  102. package/build/module/domain.js +2 -0
  103. package/build/module/domain.js.map +1 -0
  104. package/build/module/error.d.ts +2 -1
  105. package/build/module/error.js +3 -1
  106. package/build/module/error.js.map +1 -1
  107. package/build/module/index.d.ts +13 -16
  108. package/build/module/index.js +146 -63
  109. package/build/module/index.js.map +1 -1
  110. package/build/module/isomorphic.d.ts +5 -5
  111. package/build/module/isomorphic.js +1 -6
  112. package/build/module/isomorphic.js.map +1 -1
  113. package/build/module/local/digest.d.ts +3 -3
  114. package/build/module/local/digest.js.map +1 -1
  115. package/build/module/local/generate.js +10 -10
  116. package/build/module/local/generate.js.map +1 -1
  117. package/build/module/local/index.d.ts +5 -1
  118. package/build/module/local/index.js +216 -7
  119. package/build/module/local/index.js.map +1 -1
  120. package/build/module/local/prune-pulled-schema.js +4 -1
  121. package/build/module/local/prune-pulled-schema.js.map +1 -1
  122. package/build/module/migration.d.ts +7 -0
  123. package/build/module/migration.js +8 -0
  124. package/build/module/migration.js.map +1 -0
  125. package/build/module/pganalyze-comment-plugin/comment-transformer.js.map +1 -1
  126. package/build/module/pganalyze-comment-plugin/index.js.map +1 -1
  127. package/build/module/plugins.d.ts +2 -2
  128. package/build/module/plugins.js.map +1 -1
  129. package/build/module/postgres-error.d.ts +8 -0
  130. package/build/module/postgres-error.js +18 -0
  131. package/build/module/postgres-error.js.map +1 -0
  132. package/build/module/query/index.d.ts +1 -0
  133. package/build/module/query/index.js +1 -0
  134. package/build/module/query/index.js.map +1 -1
  135. package/build/module/query/listing/status.d.ts +2 -12
  136. package/build/module/query/listing/status.js +31 -17
  137. package/build/module/query/listing/status.js.map +1 -1
  138. package/build/module/query/tenant/billing-partner.d.ts +4 -0
  139. package/build/module/query/tenant/billing-partner.js +5 -0
  140. package/build/module/query/tenant/billing-partner.js.map +1 -0
  141. package/build/module/query/tenant/index.d.ts +2 -0
  142. package/build/module/query/tenant/index.js +3 -0
  143. package/build/module/query/tenant/index.js.map +1 -0
  144. package/build/module/query/tenant/status.d.ts +14 -0
  145. package/build/module/query/tenant/status.js +35 -0
  146. package/build/module/query/tenant/status.js.map +1 -0
  147. package/build/module/regional.d.ts +60 -0
  148. package/build/module/regional.js +162 -0
  149. package/build/module/regional.js.map +1 -0
  150. package/build/module/test/index.d.ts +3 -14
  151. package/build/module/test/index.js +1 -23
  152. package/build/module/test/index.js.map +1 -1
  153. package/build/module/transaction.d.ts +3 -3
  154. package/build/module/transaction.js.map +1 -1
  155. package/build/module/v1.generated.d.ts +593 -23
  156. package/build/module/v1.generated.js.map +1 -1
  157. package/package.json +18 -9
  158. package/build/main/tsconfig.main.tsbuildinfo +0 -1
  159. package/build/module/tsconfig.esm.tsbuildinfo +0 -1
@@ -0,0 +1,276 @@
1
+ import { type Kysely, sql } from 'kysely';
2
+
3
+ export async function up(db: Kysely<unknown>): Promise<void> {
4
+ await sql`
5
+ create type health.accounting_integrity_run_status as enum (
6
+ 'running',
7
+ 'completed',
8
+ 'failed',
9
+ 'skipped'
10
+ )
11
+ `.execute(db);
12
+ await sql`
13
+ create type health.accounting_integrity_case_status as enum (
14
+ 'open',
15
+ 'resolved'
16
+ )
17
+ `.execute(db);
18
+ await sql`
19
+ create type health.accounting_integrity_review_status as enum (
20
+ 'unreviewed',
21
+ 'confirmed_issue',
22
+ 'expected',
23
+ 'false_positive'
24
+ )
25
+ `.execute(db);
26
+ await sql`
27
+ create type health.accounting_integrity_actionability as enum (
28
+ 'automatically_repairable',
29
+ 'manual_repair',
30
+ 'blocked',
31
+ 'code_fix_required',
32
+ 'unknown'
33
+ )
34
+ `.execute(db);
35
+ await sql`
36
+ create type health.accounting_integrity_resolution_classification as enum (
37
+ 'code_fix',
38
+ 'configuration',
39
+ 'data_repair',
40
+ 'expected',
41
+ 'false_positive',
42
+ 'monitor_fix',
43
+ 'unknown'
44
+ )
45
+ `.execute(db);
46
+ await sql`
47
+ create type health.accounting_integrity_severity as enum (
48
+ 'info',
49
+ 'warning',
50
+ 'error',
51
+ 'critical'
52
+ )
53
+ `.execute(db);
54
+ await sql`
55
+ create type health.accounting_integrity_transition_type as enum (
56
+ 'opened',
57
+ 'changed',
58
+ 'reopened',
59
+ 'resolved',
60
+ 'reviewed'
61
+ )
62
+ `.execute(db);
63
+ await sql`
64
+ create type health.accounting_integrity_delivery_channel as enum (
65
+ 'sentry',
66
+ 'slack'
67
+ )
68
+ `.execute(db);
69
+ await sql`
70
+ create type health.accounting_integrity_delivery_status as enum (
71
+ 'suppressed',
72
+ 'pending',
73
+ 'processing',
74
+ 'delivered',
75
+ 'failed',
76
+ 'dead_letter'
77
+ )
78
+ `.execute(db);
79
+
80
+ await sql`
81
+ create table health.accounting_integrity_run (
82
+ id uuid primary key default gen_random_uuid(),
83
+ evaluation_id uuid not null,
84
+ code text not null,
85
+ definition_version integer not null,
86
+ git_revision text not null,
87
+ environment text not null,
88
+ data_region text not null,
89
+ status health.accounting_integrity_run_status not null,
90
+ started_at timestamptz not null,
91
+ completed_at timestamptz,
92
+ rows_scanned bigint not null default 0,
93
+ finding_row_count bigint not null default 0,
94
+ case_count bigint not null default 0,
95
+ opened_case_count bigint not null default 0,
96
+ changed_case_count bigint not null default 0,
97
+ reopened_case_count bigint not null default 0,
98
+ resolved_case_count bigint not null default 0,
99
+ unchanged_case_count bigint not null default 0,
100
+ error jsonb,
101
+ created_at timestamptz not null default now(),
102
+ updated_at timestamptz not null default now(),
103
+ constraint accounting_integrity_run_evaluation_region_code_key
104
+ unique (evaluation_id, data_region, code)
105
+ );
106
+ `.execute(db);
107
+
108
+ await sql`
109
+ create index accounting_integrity_run_metrics_idx
110
+ on health.accounting_integrity_run (
111
+ environment,
112
+ started_at desc
113
+ );
114
+ `.execute(db);
115
+
116
+ await sql`
117
+ create table health.accounting_integrity_case (
118
+ id uuid primary key default gen_random_uuid(),
119
+ data_region text not null,
120
+ code text not null,
121
+ tenant_id uuid not null references public.tenant(id) on delete cascade,
122
+ case_key text not null,
123
+ version bigint not null default 1,
124
+ status health.accounting_integrity_case_status not null,
125
+ review_status health.accounting_integrity_review_status
126
+ not null default 'unreviewed',
127
+ actionability health.accounting_integrity_actionability not null,
128
+ resolution_classification
129
+ health.accounting_integrity_resolution_classification not null,
130
+ severity health.accounting_integrity_severity not null,
131
+ title text not null,
132
+ root_cause text not null,
133
+ expected jsonb not null,
134
+ actual jsonb not null,
135
+ impact jsonb not null,
136
+ lock_assessment jsonb not null,
137
+ evidence jsonb not null,
138
+ evidence_complete boolean not null,
139
+ portal_urls jsonb not null,
140
+ historical_issue_refs jsonb not null,
141
+ verification jsonb not null,
142
+ repair jsonb not null,
143
+ content_hash text not null,
144
+ run_id uuid not null
145
+ references health.accounting_integrity_run(id) on delete restrict,
146
+ definition_version integer not null,
147
+ git_revision text not null,
148
+ first_seen_at timestamptz not null,
149
+ last_seen_at timestamptz not null,
150
+ last_changed_at timestamptz not null,
151
+ resolved_at timestamptz,
152
+ created_at timestamptz not null default now(),
153
+ updated_at timestamptz not null default now(),
154
+ constraint accounting_integrity_case_identity_key
155
+ unique (data_region, code, tenant_id, case_key),
156
+ constraint accounting_integrity_case_version_check check (version > 0)
157
+ );
158
+ `.execute(db);
159
+
160
+ await sql`
161
+ create index accounting_integrity_case_tenant_idx
162
+ on health.accounting_integrity_case (tenant_id)
163
+ `.execute(db);
164
+ await sql`
165
+ create index accounting_integrity_case_run_idx
166
+ on health.accounting_integrity_case (run_id)
167
+ `.execute(db);
168
+ await sql`
169
+ create index accounting_integrity_case_open_idx
170
+ on health.accounting_integrity_case (
171
+ data_region,
172
+ code,
173
+ severity,
174
+ tenant_id,
175
+ last_changed_at desc
176
+ )
177
+ where status = 'open';
178
+ `.execute(db);
179
+
180
+ await sql`
181
+ create table health.accounting_integrity_transition (
182
+ id uuid primary key default gen_random_uuid(),
183
+ run_id uuid not null
184
+ references health.accounting_integrity_run(id) on delete cascade,
185
+ case_id uuid not null
186
+ references health.accounting_integrity_case(id) on delete cascade,
187
+ case_version bigint not null,
188
+ type health.accounting_integrity_transition_type not null,
189
+ payload jsonb not null,
190
+ created_at timestamptz not null default now(),
191
+ constraint accounting_integrity_transition_case_version_type_key
192
+ unique (case_id, case_version, type)
193
+ );
194
+ `.execute(db);
195
+
196
+ await sql`
197
+ create index accounting_integrity_transition_run_idx
198
+ on health.accounting_integrity_transition (run_id)
199
+ `.execute(db);
200
+ await sql`
201
+ create table health.accounting_integrity_delivery (
202
+ id uuid primary key default gen_random_uuid(),
203
+ run_id uuid not null
204
+ references health.accounting_integrity_run(id) on delete cascade,
205
+ case_id uuid
206
+ references health.accounting_integrity_case(id) on delete cascade,
207
+ channel health.accounting_integrity_delivery_channel not null,
208
+ event_type text not null,
209
+ dedupe_key text not null unique,
210
+ payload jsonb not null,
211
+ status health.accounting_integrity_delivery_status
212
+ not null default 'pending',
213
+ attempts integer not null default 0,
214
+ claim_id uuid,
215
+ claimed_at timestamptz,
216
+ next_attempt_at timestamptz not null default now(),
217
+ delivered_at timestamptz,
218
+ result jsonb,
219
+ last_error jsonb,
220
+ created_at timestamptz not null default now(),
221
+ updated_at timestamptz not null default now()
222
+ );
223
+ `.execute(db);
224
+
225
+ await sql`
226
+ create index accounting_integrity_delivery_run_idx
227
+ on health.accounting_integrity_delivery (run_id)
228
+ `.execute(db);
229
+ await sql`
230
+ create index accounting_integrity_delivery_case_idx
231
+ on health.accounting_integrity_delivery (case_id)
232
+ `.execute(db);
233
+ await sql`
234
+ create index accounting_integrity_delivery_pending_idx
235
+ on health.accounting_integrity_delivery (next_attempt_at, created_at, id)
236
+ where status in ('pending', 'processing', 'failed');
237
+ `.execute(db);
238
+ }
239
+
240
+ export async function down(db: Kysely<unknown>): Promise<void> {
241
+ await sql`drop table if exists health.accounting_integrity_delivery;`.execute(
242
+ db
243
+ );
244
+ await sql`drop table if exists health.accounting_integrity_transition;`.execute(
245
+ db
246
+ );
247
+ await sql`drop table if exists health.accounting_integrity_case;`.execute(db);
248
+ await sql`drop table if exists health.accounting_integrity_run;`.execute(db);
249
+ await sql`drop type if exists health.accounting_integrity_delivery_status`.execute(
250
+ db
251
+ );
252
+ await sql`drop type if exists health.accounting_integrity_delivery_channel`.execute(
253
+ db
254
+ );
255
+ await sql`drop type if exists health.accounting_integrity_transition_type`.execute(
256
+ db
257
+ );
258
+ await sql`drop type if exists health.accounting_integrity_severity`.execute(
259
+ db
260
+ );
261
+ await sql`drop type if exists health.accounting_integrity_resolution_classification`.execute(
262
+ db
263
+ );
264
+ await sql`drop type if exists health.accounting_integrity_actionability`.execute(
265
+ db
266
+ );
267
+ await sql`drop type if exists health.accounting_integrity_review_status`.execute(
268
+ db
269
+ );
270
+ await sql`drop type if exists health.accounting_integrity_case_status`.execute(
271
+ db
272
+ );
273
+ await sql`drop type if exists health.accounting_integrity_run_status`.execute(
274
+ db
275
+ );
276
+ }
@@ -0,0 +1,26 @@
1
+ import { type Kysely, sql } from 'kysely';
2
+
3
+ export async function up(db: Kysely<unknown>): Promise<void> {
4
+ await sql`
5
+ alter table health.accounting_integrity_run
6
+ add column shard_key text not null default 'regional'
7
+ `.execute(db);
8
+
9
+ await sql`
10
+ alter table health.accounting_integrity_run
11
+ drop constraint accounting_integrity_run_evaluation_region_code_key,
12
+ add constraint accounting_integrity_run_evaluation_region_code_shard_key
13
+ unique (evaluation_id, data_region, code, shard_key)
14
+ `.execute(db);
15
+ }
16
+
17
+ export async function down(db: Kysely<unknown>): Promise<void> {
18
+ await sql`
19
+ alter table health.accounting_integrity_run
20
+ drop constraint
21
+ accounting_integrity_run_evaluation_region_code_shard_key,
22
+ drop column shard_key,
23
+ add constraint accounting_integrity_run_evaluation_region_code_key
24
+ unique (evaluation_id, data_region, code)
25
+ `.execute(db);
26
+ }
@@ -0,0 +1,52 @@
1
+ import { type Kysely, sql } from 'kysely';
2
+
3
+ export async function up(db: Kysely<unknown>): Promise<void> {
4
+ await sql`
5
+ create type health.accounting_integrity_urgency as enum (
6
+ 'immediate',
7
+ 'next_triage',
8
+ 'backlog',
9
+ 'none'
10
+ )
11
+ `.execute(db);
12
+ await sql`
13
+ create type health.accounting_integrity_confidence as enum (
14
+ 'proven',
15
+ 'probable',
16
+ 'unknown'
17
+ )
18
+ `.execute(db);
19
+ await sql`
20
+ alter table health.accounting_integrity_case
21
+ add column urgency health.accounting_integrity_urgency,
22
+ add column confidence health.accounting_integrity_confidence
23
+ `.execute(db);
24
+ await sql`
25
+ update health.accounting_integrity_case
26
+ set
27
+ urgency = case
28
+ when severity = 'info'
29
+ then 'none'::health.accounting_integrity_urgency
30
+ when nullif(impact -> 'modifiedAt' ->> 'latest', '')::timestamptz >=
31
+ now() - interval '28 days'
32
+ then 'next_triage'::health.accounting_integrity_urgency
33
+ else 'backlog'::health.accounting_integrity_urgency
34
+ end,
35
+ confidence = 'probable'::health.accounting_integrity_confidence
36
+ `.execute(db);
37
+ await sql`
38
+ alter table health.accounting_integrity_case
39
+ alter column urgency set not null,
40
+ alter column confidence set not null
41
+ `.execute(db);
42
+ }
43
+
44
+ export async function down(db: Kysely<unknown>): Promise<void> {
45
+ await sql`
46
+ alter table health.accounting_integrity_case
47
+ drop column confidence,
48
+ drop column urgency
49
+ `.execute(db);
50
+ await sql`drop type health.accounting_integrity_confidence`.execute(db);
51
+ await sql`drop type health.accounting_integrity_urgency`.execute(db);
52
+ }
@@ -0,0 +1,31 @@
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`
6
+ create index if not exists payment_line_recent_classification_idx
7
+ on public.payment_line (updated_at, type2)
8
+ include (payment_id, reservation_id)
9
+ where type2 is not null and type2 != 'deviation'
10
+ `.execute(db);
11
+
12
+ await sql`
13
+ create index if not exists transaction_line_recent_classification_idx
14
+ on accounting.transaction_line (
15
+ updated_at,
16
+ match_line_type_classification
17
+ )
18
+ where match_line_type_classification is not null
19
+ `.execute(db);
20
+ }
21
+
22
+ export async function down(db: Kysely<unknown>): Promise<void> {
23
+ await sql`
24
+ drop index if exists
25
+ accounting.transaction_line_recent_classification_idx
26
+ `.execute(db);
27
+
28
+ await sql`
29
+ drop index if exists public.payment_line_recent_classification_idx
30
+ `.execute(db);
31
+ }
@@ -0,0 +1,281 @@
1
+ import { type Kysely, sql } from 'kysely';
2
+
3
+ export async function up(db: Kysely<unknown>): Promise<void> {
4
+ await sql`
5
+ alter table health.accounting_integrity_run
6
+ add column scope_tenant_ids uuid[],
7
+ add column resolves_cases boolean not null default true,
8
+ add constraint accounting_integrity_run_scope_tenant_ids_check
9
+ check (
10
+ scope_tenant_ids is null or cardinality(scope_tenant_ids) > 0
11
+ )
12
+ `.execute(db);
13
+
14
+ await sql`
15
+ create type health.accounting_integrity_repair_plan_status as enum (
16
+ 'draft',
17
+ 'active',
18
+ 'paused',
19
+ 'failed',
20
+ 'completed',
21
+ 'canceled'
22
+ )
23
+ `.execute(db);
24
+ await sql`
25
+ create type health.accounting_integrity_repair_stage_status as enum (
26
+ 'planned',
27
+ 'dry_run_passed',
28
+ 'approved',
29
+ 'applying',
30
+ 'verifying',
31
+ 'verified',
32
+ 'paused',
33
+ 'failed',
34
+ 'canceled'
35
+ )
36
+ `.execute(db);
37
+ await sql`
38
+ create type health.accounting_integrity_repair_scope_type as enum (
39
+ 'case',
40
+ 'tenant'
41
+ )
42
+ `.execute(db);
43
+ await sql`
44
+ create type health.accounting_integrity_repair_target_status as enum (
45
+ 'pending',
46
+ 'applying',
47
+ 'applied',
48
+ 'verifying',
49
+ 'verified',
50
+ 'paused',
51
+ 'failed',
52
+ 'canceled'
53
+ )
54
+ `.execute(db);
55
+ await sql`
56
+ create type health.accounting_integrity_repair_transition_type as enum (
57
+ 'plan_created',
58
+ 'plan_activated',
59
+ 'dry_run_passed',
60
+ 'approval_granted',
61
+ 'approval_invalidated',
62
+ 'stage_applying',
63
+ 'target_applying',
64
+ 'target_applied',
65
+ 'target_verifying',
66
+ 'target_verified',
67
+ 'stage_verified',
68
+ 'paused',
69
+ 'failed',
70
+ 'canceled',
71
+ 'completed'
72
+ )
73
+ `.execute(db);
74
+
75
+ await sql`
76
+ create table health.accounting_integrity_repair_plan (
77
+ id uuid primary key default gen_random_uuid(),
78
+ data_region text not null,
79
+ environment text not null,
80
+ code text not null,
81
+ recipe_version integer not null,
82
+ git_revision text not null,
83
+ status health.accounting_integrity_repair_plan_status
84
+ not null default 'draft',
85
+ title text not null,
86
+ linear_ref text,
87
+ created_by text not null,
88
+ created_at timestamptz not null default now(),
89
+ updated_at timestamptz not null default now(),
90
+ completed_at timestamptz,
91
+ canceled_at timestamptz,
92
+ constraint accounting_integrity_repair_plan_recipe_version_check
93
+ check (recipe_version > 0),
94
+ constraint accounting_integrity_repair_plan_completion_check
95
+ check ((status = 'completed') = (completed_at is not null)),
96
+ constraint accounting_integrity_repair_plan_cancellation_check
97
+ check ((status = 'canceled') = (canceled_at is not null))
98
+ )
99
+ `.execute(db);
100
+
101
+ await sql`
102
+ create index accounting_integrity_repair_plan_status_idx
103
+ on health.accounting_integrity_repair_plan (
104
+ data_region,
105
+ environment,
106
+ status,
107
+ updated_at desc
108
+ )
109
+ `.execute(db);
110
+
111
+ await sql`
112
+ create table health.accounting_integrity_repair_stage (
113
+ id uuid primary key default gen_random_uuid(),
114
+ plan_id uuid not null
115
+ references health.accounting_integrity_repair_plan(id)
116
+ on delete cascade,
117
+ ordinal integer not null,
118
+ scope_type health.accounting_integrity_repair_scope_type not null,
119
+ tenant_id uuid not null,
120
+ status health.accounting_integrity_repair_stage_status
121
+ not null default 'planned',
122
+ manifest jsonb not null,
123
+ manifest_hash text not null,
124
+ limits jsonb not null,
125
+ dry_run_result jsonb,
126
+ approved_by text,
127
+ approved_at timestamptz,
128
+ approval_ref text,
129
+ apply_result jsonb,
130
+ verification_result jsonb,
131
+ started_at timestamptz,
132
+ completed_at timestamptz,
133
+ created_at timestamptz not null default now(),
134
+ updated_at timestamptz not null default now(),
135
+ constraint accounting_integrity_repair_stage_plan_ordinal_key
136
+ unique (plan_id, ordinal),
137
+ constraint accounting_integrity_repair_stage_ordinal_check
138
+ check (ordinal > 0),
139
+ constraint accounting_integrity_repair_stage_approval_check check (
140
+ (approved_by is null and approved_at is null and approval_ref is null)
141
+ or
142
+ (approved_by is not null and approved_at is not null and
143
+ approval_ref is not null)
144
+ ),
145
+ constraint accounting_integrity_repair_stage_completion_check check (
146
+ (status = 'verified') = (completed_at is not null)
147
+ )
148
+ )
149
+ `.execute(db);
150
+
151
+ await sql`
152
+ create index accounting_integrity_repair_stage_plan_status_idx
153
+ on health.accounting_integrity_repair_stage (
154
+ plan_id,
155
+ status,
156
+ ordinal
157
+ )
158
+ `.execute(db);
159
+
160
+ await sql`
161
+ create table health.accounting_integrity_repair_target (
162
+ id uuid primary key default gen_random_uuid(),
163
+ stage_id uuid not null
164
+ references health.accounting_integrity_repair_stage(id)
165
+ on delete cascade,
166
+ ordinal integer not null,
167
+ case_id uuid not null,
168
+ case_version bigint not null,
169
+ case_content_hash text not null,
170
+ tenant_id uuid not null,
171
+ target_key text not null,
172
+ action jsonb not null,
173
+ before_state jsonb not null,
174
+ before_hash text not null,
175
+ expected_result jsonb not null,
176
+ lock_assessment jsonb not null,
177
+ effect_plan jsonb not null,
178
+ status health.accounting_integrity_repair_target_status
179
+ not null default 'pending',
180
+ idempotency_key text not null unique,
181
+ audit_action_id uuid,
182
+ apply_result jsonb,
183
+ verification_result jsonb,
184
+ started_at timestamptz,
185
+ completed_at timestamptz,
186
+ created_at timestamptz not null default now(),
187
+ updated_at timestamptz not null default now(),
188
+ constraint accounting_integrity_repair_target_stage_key
189
+ unique (stage_id, target_key),
190
+ constraint accounting_integrity_repair_target_stage_ordinal_key
191
+ unique (stage_id, ordinal),
192
+ constraint accounting_integrity_repair_target_ordinal_check
193
+ check (ordinal > 0),
194
+ constraint accounting_integrity_repair_target_case_version_check
195
+ check (case_version > 0),
196
+ constraint accounting_integrity_repair_target_completion_check check (
197
+ (status = 'verified') = (completed_at is not null)
198
+ )
199
+ )
200
+ `.execute(db);
201
+
202
+ await sql`
203
+ create index accounting_integrity_repair_target_stage_status_idx
204
+ on health.accounting_integrity_repair_target (
205
+ stage_id,
206
+ status,
207
+ ordinal
208
+ )
209
+ `.execute(db);
210
+ await sql`
211
+ create index accounting_integrity_repair_target_case_idx
212
+ on health.accounting_integrity_repair_target (case_id)
213
+ `.execute(db);
214
+
215
+ await sql`
216
+ create table health.accounting_integrity_repair_transition (
217
+ id uuid primary key default gen_random_uuid(),
218
+ plan_id uuid not null
219
+ references health.accounting_integrity_repair_plan(id)
220
+ on delete cascade,
221
+ stage_id uuid
222
+ references health.accounting_integrity_repair_stage(id)
223
+ on delete cascade,
224
+ target_id uuid
225
+ references health.accounting_integrity_repair_target(id)
226
+ on delete cascade,
227
+ type health.accounting_integrity_repair_transition_type not null,
228
+ actor text not null,
229
+ payload jsonb not null,
230
+ created_at timestamptz not null default now(),
231
+ constraint accounting_integrity_repair_transition_target_stage_check
232
+ check (target_id is null or stage_id is not null)
233
+ )
234
+ `.execute(db);
235
+
236
+ await sql`
237
+ create index accounting_integrity_repair_transition_plan_idx
238
+ on health.accounting_integrity_repair_transition (
239
+ plan_id,
240
+ created_at,
241
+ id
242
+ )
243
+ `.execute(db);
244
+ }
245
+
246
+ export async function down(db: Kysely<unknown>): Promise<void> {
247
+ await sql`
248
+ drop table if exists health.accounting_integrity_repair_transition
249
+ `.execute(db);
250
+ await sql`
251
+ drop table if exists health.accounting_integrity_repair_target
252
+ `.execute(db);
253
+ await sql`
254
+ drop table if exists health.accounting_integrity_repair_stage
255
+ `.execute(db);
256
+ await sql`
257
+ drop table if exists health.accounting_integrity_repair_plan
258
+ `.execute(db);
259
+ await sql`
260
+ drop type if exists
261
+ health.accounting_integrity_repair_transition_type
262
+ `.execute(db);
263
+ await sql`
264
+ drop type if exists health.accounting_integrity_repair_target_status
265
+ `.execute(db);
266
+ await sql`
267
+ drop type if exists health.accounting_integrity_repair_scope_type
268
+ `.execute(db);
269
+ await sql`
270
+ drop type if exists health.accounting_integrity_repair_stage_status
271
+ `.execute(db);
272
+ await sql`
273
+ drop type if exists health.accounting_integrity_repair_plan_status
274
+ `.execute(db);
275
+ await sql`
276
+ alter table health.accounting_integrity_run
277
+ drop constraint accounting_integrity_run_scope_tenant_ids_check,
278
+ drop column resolves_cases,
279
+ drop column scope_tenant_ids
280
+ `.execute(db);
281
+ }