@vrplatform/kysely 1.3.45-stage.4897 → 1.3.45-stage.490

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 (219) 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 +344 -16
  5. package/build/main/control-plane/index.js +61 -0
  6. package/build/main/control-plane/index.js.map +1 -1
  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 +19 -14
  14. package/build/main/index.js +235 -59
  15. package/build/main/index.js.map +1 -1
  16. package/build/main/isomorphic.d.ts +5 -5
  17. package/build/main/isomorphic.js +2 -8
  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 -8
  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 +321 -10
  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/plugins.d.ts +2 -2
  32. package/build/main/plugins.js.map +1 -1
  33. package/build/main/postgres-error.d.ts +3 -3
  34. package/build/main/postgres-error.js +4 -3
  35. package/build/main/postgres-error.js.map +1 -1
  36. package/build/main/query/listing/status.d.ts +0 -10
  37. package/build/main/query/listing/status.js +2 -14
  38. package/build/main/query/listing/status.js.map +1 -1
  39. package/build/main/query/tenant/status.d.ts +5 -8
  40. package/build/main/query/tenant/status.js +18 -21
  41. package/build/main/query/tenant/status.js.map +1 -1
  42. package/build/main/regional.d.ts +7 -1
  43. package/build/main/regional.js +30 -17
  44. package/build/main/regional.js.map +1 -1
  45. package/build/main/test/index.d.ts +23 -11
  46. package/build/main/test/index.js +31 -23
  47. package/build/main/test/index.js.map +1 -1
  48. package/build/main/transaction.d.ts +3 -3
  49. package/build/main/transaction.js.map +1 -1
  50. package/build/main/v1.generated.d.ts +771 -139
  51. package/build/main/v1.generated.js.map +1 -1
  52. package/build/migrations-v2/0000000000000-baseline.ts +5 -0
  53. package/build/migrations-v2/0000000000001-public-audit-events-v2.ts +137 -0
  54. package/build/migrations-v2/0000000000002-audit-recovery-watermarks.ts +30 -0
  55. package/build/migrations-v2/0000000000003-audit-effect-recovery-watermark.ts +13 -0
  56. package/build/migrations-v2/0000000000004-audit-event-projected-at.ts +15 -0
  57. package/build/migrations-v2/0000000000005-audit-recovery-indexes.ts +63 -0
  58. package/build/migrations-v2/0000000000006-audit-action-public-event-projected-at.ts +22 -0
  59. package/build/migrations-v2/0000000000007-audit-action-public-event-projected-at-backfill.ts +35 -0
  60. package/build/migrations-v2/0000000000008-global-user-attribution.ts +33 -0
  61. package/build/migrations-v2/0000000000008-team-issue-rule-state.ts +52 -0
  62. package/build/migrations-v2/0000000000009-reservation-issue-projections.ts +45 -0
  63. package/build/migrations-v2/0000000000010-bank-rule-application-bank-record-delete-cascade.ts +26 -0
  64. package/build/migrations-v2/0000000000011-guest-balance-journal-entry-lookup-index.ts +25 -0
  65. package/build/migrations-v2/0000000000012-trial-free-analytics-billing-status.ts +72 -0
  66. package/build/migrations-v2/0000000000013-ui-permission-bundles.ts +45 -0
  67. package/build/migrations-v2/0000000000014-ui-permission-bundle-top-up.ts +32 -0
  68. package/build/migrations-v2/0000000000015-durable-flow-events.ts +163 -0
  69. package/build/migrations-v2/0000000000016-billing-listing-facts.ts +539 -0
  70. package/build/migrations-v2/0000000000016-managed-team-access.ts +34 -0
  71. package/build/migrations-v2/0000000000017-drop-trial-until.ts +42 -0
  72. package/build/migrations-v2/0000000000017-webhook-secret-reencryption-failures.ts +18 -0
  73. package/build/migrations-v2/0000000000018-accounting-integrity-monitoring.ts +276 -0
  74. package/build/migrations-v2/0000000000018-durable-flow-event-sensitive-input.ts +28 -0
  75. package/build/migrations-v2/0000000000019-accounting-integrity-run-shards.ts +26 -0
  76. package/build/migrations-v2/0000000000019-asynchronous-operations.ts +736 -0
  77. package/build/migrations-v2/0000000000020-accounting-integrity-case-classification.ts +52 -0
  78. package/build/migrations-v2/0000000000021-payment-line-classification-monitor-indexes.ts +31 -0
  79. package/build/migrations-v2/0000000000022-accounting-integrity-controlled-repairs.ts +281 -0
  80. package/build/migrations-v2/0000000000023-line-classification-usage.ts +325 -0
  81. package/build/migrations-v2/0000000000024-tenant-region-migration.ts +108 -0
  82. package/build/migrations-v2/0000000000025-partner-ach-denial-default.ts +30 -0
  83. package/build/migrations-v2/0000000000026-line-classification-usage-capture.ts +399 -0
  84. package/build/migrations-v2/0000000000027-payout-replacement-chain.ts +27 -0
  85. package/build/migrations-v2/0000000000028-accounting-integrity-operator-workflow.ts +108 -0
  86. package/build/migrations-v2/0000000000029-accounting-integrity-definition-artifacts.ts +40 -0
  87. package/build/migrations-v2/0000000000030-accounting-integrity-review-metadata-compatibility.ts +49 -0
  88. package/build/migrations-v2/0000000000031-ui-permission-role-simplification.ts +70 -0
  89. package/build/migrations-v2/0000000000032-tenant-delete-ownership-cascades.ts +44 -0
  90. package/build/migrations-v2/0000000000033-reservation-issue-seed-bucket-index.ts +19 -0
  91. package/build/migrations-v2/0000000000034-retire-recent-classification-indexes.ts +29 -0
  92. package/build/migrations-v2/0000000000035-core-change-autovacuum.ts +24 -0
  93. package/build/migrations-v2/0000000000036-journal-entry-inactive-reason-constraint.ts +33 -0
  94. package/build/migrations-v2/0000000000037-journal-entry-inactive-reasons.ts +19 -0
  95. package/build/{migrations/0000000000023-journal-entry-currency-mismatch-reason.ts → migrations-v2/0000000000038-journal-entry-group-inactive-reason.ts} +4 -4
  96. package/build/migrations-v2/0000000000039-reservation-guest-breakdown.ts +19 -0
  97. package/build/migrations-v2/0000000000040-csv-import-source-identity-index.ts +23 -0
  98. package/build/migrations-v2/0000000000041-payment-returns.ts +38 -0
  99. package/build/migrations-v2/0000000000042-operation-resource-types.ts +609 -0
  100. package/build/migrations-v2/0000000000043-reservation-journal-freshness.ts +149 -0
  101. package/build/migrations-v2/0000000000044-reservation-journal-freshness-backfill.ts +254 -0
  102. package/build/migrations-v2/0000000000045-reservation-journal-freshness-revision-repair.ts +83 -0
  103. package/build/migrations-v2/0000000000046-materialized-reservation-journal-operations.ts +709 -0
  104. package/build/migrations-v2/0000000000047-public-audit-completion.ts +77 -0
  105. package/build/migrations-v2/0000000000048-reservation-journal-full-refresh-repair.ts +136 -0
  106. package/build/migrations-v2/0000000000049-queued-journal-operation-failures.ts +60 -0
  107. package/build/migrations-v2/0000000000050-connection-oauth-state.ts +43 -0
  108. package/build/module/camel-case/index.js +1 -1
  109. package/build/module/camel-case/index.js.map +1 -1
  110. package/build/module/control-plane/index.d.ts +344 -16
  111. package/build/module/control-plane/index.js +49 -1
  112. package/build/module/control-plane/index.js.map +1 -1
  113. package/build/module/domain.d.ts +4 -0
  114. package/build/module/domain.js +2 -0
  115. package/build/module/domain.js.map +1 -0
  116. package/build/module/error.d.ts +2 -1
  117. package/build/module/error.js +3 -1
  118. package/build/module/error.js.map +1 -1
  119. package/build/module/index.d.ts +19 -14
  120. package/build/module/index.js +236 -57
  121. package/build/module/index.js.map +1 -1
  122. package/build/module/isomorphic.d.ts +5 -5
  123. package/build/module/isomorphic.js +1 -6
  124. package/build/module/isomorphic.js.map +1 -1
  125. package/build/module/local/digest.d.ts +3 -3
  126. package/build/module/local/digest.js.map +1 -1
  127. package/build/module/local/generate.js +10 -8
  128. package/build/module/local/generate.js.map +1 -1
  129. package/build/module/local/index.d.ts +5 -1
  130. package/build/module/local/index.js +322 -11
  131. package/build/module/local/index.js.map +1 -1
  132. package/build/module/local/prune-pulled-schema.js +4 -1
  133. package/build/module/local/prune-pulled-schema.js.map +1 -1
  134. package/build/module/migration.d.ts +7 -0
  135. package/build/module/migration.js +8 -0
  136. package/build/module/migration.js.map +1 -0
  137. package/build/module/plugins.d.ts +2 -2
  138. package/build/module/plugins.js.map +1 -1
  139. package/build/module/postgres-error.d.ts +3 -3
  140. package/build/module/postgres-error.js +4 -3
  141. package/build/module/postgres-error.js.map +1 -1
  142. package/build/module/query/listing/status.d.ts +0 -10
  143. package/build/module/query/listing/status.js +2 -12
  144. package/build/module/query/listing/status.js.map +1 -1
  145. package/build/module/query/tenant/status.d.ts +5 -8
  146. package/build/module/query/tenant/status.js +18 -21
  147. package/build/module/query/tenant/status.js.map +1 -1
  148. package/build/module/regional.d.ts +7 -1
  149. package/build/module/regional.js +30 -17
  150. package/build/module/regional.js.map +1 -1
  151. package/build/module/test/index.d.ts +23 -11
  152. package/build/module/test/index.js +31 -23
  153. package/build/module/test/index.js.map +1 -1
  154. package/build/module/transaction.d.ts +3 -3
  155. package/build/module/transaction.js.map +1 -1
  156. package/build/module/v1.generated.d.ts +771 -139
  157. package/build/module/v1.generated.js.map +1 -1
  158. package/package.json +5 -4
  159. package/build/migrations/0000000000001-init.ts +0 -5
  160. package/build/migrations/0000000000009-drop-store-schema.ts +0 -14
  161. package/build/migrations/0000000000010-bank-rules.ts +0 -190
  162. package/build/migrations/0000000000011-bank-rule-line-adjustments.ts +0 -65
  163. package/build/migrations/0000000000012-bank-rule-conditions.ts +0 -45
  164. package/build/migrations/0000000000013-bank-rule-name-unique.ts +0 -15
  165. package/build/migrations/0000000000014-bank-rule-drop-legacy-conditions.ts +0 -41
  166. package/build/migrations/0000000000015-booking-channel-logo-dev-provider.ts +0 -70
  167. package/build/migrations/0000000000016-owner-statement-draft-status.ts +0 -22
  168. package/build/migrations/0000000000017-tenant-migration-markers.ts +0 -18
  169. package/build/migrations/0000000000018-security-deposits-payable-category.ts +0 -63
  170. package/build/migrations/0000000000019-tenant-app-settings.ts +0 -76
  171. package/build/migrations/0000000000020-operating-opening-trial-balance.ts +0 -36
  172. package/build/migrations/0000000000021-audit-foreign-key-indexes.ts +0 -77
  173. package/build/migrations/0000000000022-opening-trial-balance-combined-columns.ts +0 -30
  174. package/build/migrations/0000000000024-listing-parent.ts +0 -37
  175. package/build/migrations/0000000000025-journal-entry-parent-listing.ts +0 -26
  176. package/build/migrations/0000000000026-booking-channel-logo-slots.ts +0 -89
  177. package/build/migrations/0000000000027-list-browse-performance-indexes.ts +0 -52
  178. package/build/migrations/0000000000028-flow-setting-status-jsonb.ts +0 -20
  179. package/build/migrations/0000000000029-tenant-slug.ts +0 -328
  180. package/build/migrations/0000000000030-pms-accounting-windows.ts +0 -50
  181. package/build/migrations/0000000000031-owner-statement-projection-listing-revision.ts +0 -20
  182. package/build/migrations/0000000000032-flow-event-listening.ts +0 -28
  183. package/build/migrations/0000000000033-opening-trial-balance-transaction.ts +0 -272
  184. package/build/migrations/0000000000034-historic-general-ledger.ts +0 -24
  185. package/build/migrations/0000000000035-tenant-regional-placement.ts +0 -37
  186. package/build/migrations/0000000000036-control-plane-projection-events.ts +0 -43
  187. package/build/migrations/0000000000037-tenant-guide-onboarding.ts +0 -21
  188. package/build/migrations/0000000000038-tracking-event-analytics-sink.ts +0 -31
  189. package/build/migrations/0000000000039-crunchy-region.ts +0 -86
  190. package/build/migrations/0000000000040-hostaway-region.ts +0 -72
  191. package/build/migrations/0000000000041-audit-tenant-fk.ts +0 -66
  192. package/build/migrations/0000000000042-ramp-staging-connect-params.ts +0 -35
  193. package/build/migrations/0000000000043-gl-pms-accounting-window-guard.ts +0 -125
  194. package/build/migrations/0000000000044-tenant-cross-region-fks.ts +0 -45
  195. package/build/migrations/0000000000045-cloudflare-region-env.ts +0 -100
  196. package/build/migrations/0000000000046-control-plane-outbox-alias.ts +0 -46
  197. package/build/migrations/0000000000047-ramp-contact-last-4-digits.ts +0 -16
  198. package/build/migrations/0000000000047-tenant-status-backfill.ts +0 -40
  199. package/build/migrations/0000000000048-tenant-status-constraint.ts +0 -24
  200. package/build/migrations/0000000000049-health-tenant-issues.ts +0 -69
  201. package/build/migrations/0000000000050-partner-webhooks.ts +0 -135
  202. package/build/migrations/0000000000051-health-webhook-runtime-grants.ts +0 -42
  203. package/build/migrations/0000000000052-feature-approval-team-enablement.ts +0 -13
  204. package/build/migrations/0000000000053-health-webhook-enums.ts +0 -296
  205. package/build/migrations/0000000000054-user-secondary-emails-jsonb.ts +0 -25
  206. package/build/migrations/0000000000055-outbound-webhooks-v1.ts +0 -221
  207. package/build/migrations/0000000000056-connection-webhook-version.ts +0 -18
  208. package/build/migrations/0000000000057-sync-webhook-version.ts +0 -18
  209. package/build/migrations/0000000000058-reservation-webhook-version.ts +0 -18
  210. package/build/migrations/0000000000059-listing-webhook-version.ts +0 -18
  211. package/build/migrations/0000000000060-owner-statement-webhook-version.ts +0 -19
  212. package/build/migrations/0000000000061-transaction-webhook-version.ts +0 -18
  213. package/build/migrations/0000000000062-plaid-connect-session.ts +0 -102
  214. package/build/migrations/0000000000063-pgcrypto.ts +0 -27
  215. package/build/migrations/0000000000064-partner-api-foundation.ts +0 -36
  216. package/build/migrations/0000000000065-canonical-jsonb-shapes.ts +0 -231
  217. package/build/migrations/0000000000066-source-delete-fk-indexes.ts +0 -20
  218. package/build/migrations/0000000000067-remove-opening-trial-balance-account-columns.ts +0 -34
  219. package/build/migrations/0000000000068-bank-record-account-delete-cascade.ts +0 -26
@@ -0,0 +1,15 @@
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
+ alter table audit.public_event
7
+ add column projected_at timestamptz not null default now();
8
+ `.execute(db);
9
+ }
10
+
11
+ export async function down(db: Kysely<unknown>): Promise<void> {
12
+ await sql`
13
+ alter table audit.public_event drop column projected_at;
14
+ `.execute(db);
15
+ }
@@ -0,0 +1,63 @@
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 audit_action_created_at_idx
7
+ on audit.action (created_at);
8
+ `.execute(db);
9
+ await sql`
10
+ create index if not exists audit_mutation_created_at_idx
11
+ on audit.mutation (created_at)
12
+ where capture_order is not null;
13
+ `.execute(db);
14
+ await sql`
15
+ create index if not exists audit_effect_created_at_idx
16
+ on audit.effect (created_at);
17
+ `.execute(db);
18
+ await sql`
19
+ create index if not exists audit_effect_completed_at_idx
20
+ on audit.effect (completed_at)
21
+ where completed_at is not null;
22
+ `.execute(db);
23
+ await sql`
24
+ create index if not exists audit_effect_last_attempt_at_idx
25
+ on audit.effect (last_attempt_at)
26
+ where last_attempt_at is not null;
27
+ `.execute(db);
28
+ await sql`
29
+ create index if not exists audit_journal_delta_changed_at_idx
30
+ on audit.journal_delta (changed_at);
31
+ `.execute(db);
32
+
33
+ const indexes = await sql<{ count: number }>`
34
+ select count(*)::int as count
35
+ from pg_index i
36
+ inner join pg_class c on c.oid = i.indexrelid
37
+ inner join pg_namespace n on n.oid = c.relnamespace
38
+ where n.nspname = 'audit'
39
+ and c.relname in (
40
+ 'audit_action_created_at_idx',
41
+ 'audit_mutation_created_at_idx',
42
+ 'audit_effect_created_at_idx',
43
+ 'audit_effect_completed_at_idx',
44
+ 'audit_effect_last_attempt_at_idx',
45
+ 'audit_journal_delta_changed_at_idx'
46
+ )
47
+ and i.indisvalid
48
+ and i.indisready
49
+ and i.indislive
50
+ `.execute(db);
51
+ if (Number(indexes.rows[0]?.count) !== 6) {
52
+ throw new Error('Audit recovery indexes are unavailable');
53
+ }
54
+ }
55
+
56
+ export async function down(db: Kysely<unknown>): Promise<void> {
57
+ await sql`drop index audit.audit_journal_delta_changed_at_idx;`.execute(db);
58
+ await sql`drop index audit.audit_effect_last_attempt_at_idx;`.execute(db);
59
+ await sql`drop index audit.audit_effect_completed_at_idx;`.execute(db);
60
+ await sql`drop index audit.audit_effect_created_at_idx;`.execute(db);
61
+ await sql`drop index audit.audit_mutation_created_at_idx;`.execute(db);
62
+ await sql`drop index audit.audit_action_created_at_idx;`.execute(db);
63
+ }
@@ -0,0 +1,22 @@
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 = '180s';`.execute(db);
6
+ await sql`set local statement_timeout = '240s';`.execute(db);
7
+ await sql`
8
+ select pg_advisory_xact_lock(
9
+ hashtextextended('public_audit_event_recovery', 0)
10
+ );
11
+ `.execute(db);
12
+ await sql`
13
+ alter table audit.action
14
+ add column public_event_projected_at timestamptz default null;
15
+ `.execute(db);
16
+ }
17
+
18
+ export async function down(db: Kysely<unknown>): Promise<void> {
19
+ await sql`
20
+ alter table audit.action drop column public_event_projected_at;
21
+ `.execute(db);
22
+ }
@@ -0,0 +1,35 @@
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 = '180s';`.execute(db);
6
+ await sql`set local statement_timeout = '240s';`.execute(db);
7
+ await sql`
8
+ select pg_advisory_xact_lock(
9
+ hashtextextended('public_audit_event_recovery', 0)
10
+ );
11
+ `.execute(db);
12
+ await sql`
13
+ update audit.action as action
14
+ set public_event_projected_at = event.projected_at
15
+ from (
16
+ select tenant_id, action_id, max(projected_at) as projected_at
17
+ from audit.public_event
18
+ group by tenant_id, action_id
19
+ ) as event
20
+ where action.tenant_id = event.tenant_id
21
+ and action.id = event.action_id
22
+ and action.public_event_projected_at is null
23
+ and action.created_at >= (
24
+ select cutover_at
25
+ from audit.public_event_projection_state
26
+ where singleton = true
27
+ );
28
+ `.execute(db);
29
+ }
30
+
31
+ export async function down(db: Kysely<unknown>): Promise<void> {
32
+ await sql`
33
+ update audit.action set public_event_projected_at = null;
34
+ `.execute(db);
35
+ }
@@ -0,0 +1,33 @@
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`
7
+ alter table core.sync
8
+ drop constraint if exists sync_user_id_fkey
9
+ `.execute(db);
10
+ await sql`
11
+ alter table public.audit_log
12
+ drop constraint if exists audit_log_actor_user_id_fkey
13
+ `.execute(db);
14
+ }
15
+
16
+ export async function down(db: Kysely<unknown>): Promise<void> {
17
+ await sql`
18
+ alter table core.sync
19
+ add constraint sync_user_id_fkey
20
+ foreign key (user_id)
21
+ references public."user"(id)
22
+ on update restrict
23
+ on delete set null
24
+ `.execute(db);
25
+ await sql`
26
+ alter table public.audit_log
27
+ add constraint audit_log_actor_user_id_fkey
28
+ foreign key (actor_user_id)
29
+ references public."user"(id)
30
+ on update restrict
31
+ on delete set null
32
+ `.execute(db);
33
+ }
@@ -0,0 +1,52 @@
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 table health.tenant_issue_rule_state (
7
+ tenant_id uuid not null
8
+ references public.tenant(id) on delete cascade,
9
+ code health.tenant_issue_code not null,
10
+ dirty_at timestamptz not null,
11
+ computed_at timestamptz,
12
+ dirty_version bigint not null default 1,
13
+ computed_version bigint not null default 0,
14
+ created_at timestamptz not null default now(),
15
+ updated_at timestamptz not null default now(),
16
+ primary key (tenant_id, code)
17
+ );
18
+ `.execute(db);
19
+ await sql`
20
+ create index tenant_issue_rule_state_dirty_idx
21
+ on health.tenant_issue_rule_state (dirty_at, tenant_id, code)
22
+ where dirty_version > computed_version;
23
+ `.execute(db);
24
+ await sql`
25
+ insert into health.tenant_issue_rule_state (
26
+ tenant_id,
27
+ code,
28
+ dirty_at,
29
+ computed_at,
30
+ computed_version,
31
+ created_at,
32
+ updated_at
33
+ )
34
+ select
35
+ snapshot.tenant_id,
36
+ code,
37
+ snapshot.dirty_at,
38
+ snapshot.computed_at,
39
+ case
40
+ when snapshot.computed_at >= snapshot.dirty_at then 1
41
+ else 0
42
+ end,
43
+ snapshot.created_at,
44
+ snapshot.updated_at
45
+ from health.tenant_issue_snapshot as snapshot
46
+ cross join unnest(enum_range(null::health.tenant_issue_code)) as code;
47
+ `.execute(db);
48
+ }
49
+
50
+ export async function down(db: Kysely<unknown>): Promise<void> {
51
+ await sql`drop table health.tenant_issue_rule_state;`.execute(db);
52
+ }
@@ -0,0 +1,45 @@
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 table health.reservation_issue_snapshot (
7
+ reservation_id uuid primary key
8
+ references public.reservation(id) on delete cascade,
9
+ tenant_id uuid not null references public.tenant(id) on delete cascade,
10
+ version bigint not null default 0,
11
+ issues jsonb not null default '[]'::jsonb,
12
+ dirty_version bigint not null default 1,
13
+ computed_version bigint not null default 0,
14
+ dirty_at timestamptz not null default now(),
15
+ computed_at timestamptz,
16
+ created_at timestamptz not null default now(),
17
+ updated_at timestamptz not null default now(),
18
+ constraint reservation_issue_snapshot_version_check check (version >= 0),
19
+ constraint reservation_issue_snapshot_dirty_version_check
20
+ check (dirty_version >= 0),
21
+ constraint reservation_issue_snapshot_computed_version_check
22
+ check (computed_version >= 0)
23
+ );
24
+ `.execute(db);
25
+
26
+ await sql`
27
+ create index reservation_issue_snapshot_tenant_idx
28
+ on health.reservation_issue_snapshot (tenant_id, reservation_id);
29
+ `.execute(db);
30
+
31
+ await sql`
32
+ create index reservation_issue_snapshot_reconciliation_idx
33
+ on health.reservation_issue_snapshot (
34
+ ((dirty_version > computed_version)::int) desc,
35
+ computed_at,
36
+ reservation_id
37
+ );
38
+ `.execute(db);
39
+ }
40
+
41
+ export async function down(db: Kysely<unknown>): Promise<void> {
42
+ await sql`drop table if exists health.reservation_issue_snapshot;`.execute(
43
+ db
44
+ );
45
+ }
@@ -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`
6
+ alter table accounting.bank_rule_application
7
+ drop constraint bank_rule_application_bank_record_id_fkey,
8
+ add constraint bank_rule_application_bank_record_id_fkey
9
+ foreign key (bank_record_id)
10
+ references accounting.bank_record(id)
11
+ on update restrict
12
+ on delete cascade
13
+ `.execute(db);
14
+ }
15
+
16
+ export async function down(db: Kysely<unknown>): Promise<void> {
17
+ await sql`
18
+ alter table accounting.bank_rule_application
19
+ drop constraint bank_rule_application_bank_record_id_fkey,
20
+ add constraint bank_rule_application_bank_record_id_fkey
21
+ foreign key (bank_record_id)
22
+ references accounting.bank_record(id)
23
+ on update restrict
24
+ on delete restrict
25
+ `.execute(db);
26
+ }
@@ -0,0 +1,25 @@
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 index if not exists
10
+ journal_entry_transaction_reservation_tenant_idx
11
+ on accounting.journal_entry (
12
+ transaction_id,
13
+ reservation_id,
14
+ tenant_id
15
+ )
16
+ where transaction_id is not null
17
+ `.execute(db);
18
+ }
19
+
20
+ export async function down(db: Kysely<unknown>): Promise<void> {
21
+ await sql`
22
+ drop index if exists
23
+ accounting.journal_entry_transaction_reservation_tenant_idx
24
+ `.execute(db);
25
+ }
@@ -0,0 +1,72 @@
1
+ import { type Kysely, sql } from 'kysely';
2
+
3
+ export async function up(db: Kysely<unknown>): Promise<void> {
4
+ await sql`
5
+ create or replace function public.tenant_calculated_analytics_billing_status(
6
+ row_t public.tenant
7
+ ) returns text
8
+ language sql stable
9
+ as $$
10
+ with norm as (
11
+ select
12
+ lower(nullif(row_t.billing_subscription_status, '')) as sub_status,
13
+ nullif(trim(row_t.billing_payment_method_type), '') as pm_type,
14
+ coalesce(row_t.is_onboarding, false) as is_onboarding
15
+ )
16
+ select
17
+ case
18
+ when sub_status in ('cancelled', 'voided') then 'cancelled'
19
+ when sub_status = 'errored' then 'errored'
20
+ when is_onboarding then 'onboarding'
21
+ when sub_status is null then 'partner'
22
+ when sub_status = 'draft' then 'pending'
23
+ when sub_status = 'active' and pm_type is null then 'pending'
24
+ when sub_status = 'active' and pm_type is not null then 'active'
25
+ else 'unknown'
26
+ end
27
+ from norm;
28
+ $$;
29
+ `.execute(db);
30
+ }
31
+
32
+ export async function down(db: Kysely<unknown>): Promise<void> {
33
+ await sql`
34
+ create or replace function public.tenant_calculated_analytics_billing_status(
35
+ row_t public.tenant
36
+ ) returns text
37
+ language sql stable
38
+ as $$
39
+ with norm as (
40
+ select
41
+ lower(nullif(row_t.billing_subscription_status, '')) as sub_status,
42
+ nullif(trim(row_t.billing_payment_method_type), '') as pm_type,
43
+ row_t.trial_until as trial_until_date,
44
+ coalesce(row_t.is_onboarding, false) as is_onboarding
45
+ ),
46
+ flags as (
47
+ select
48
+ trial_until_date is not null
49
+ and trial_until_date > current_date as trial_active,
50
+ sub_status,
51
+ pm_type,
52
+ is_onboarding
53
+ from norm
54
+ )
55
+ select
56
+ case
57
+ when sub_status in ('cancelled', 'voided') then 'cancelled'
58
+ when sub_status = 'errored' then 'errored'
59
+ when is_onboarding then 'onboarding'
60
+ when sub_status is null then 'partner'
61
+ when sub_status = 'draft' then 'pending'
62
+ when sub_status = 'active' and trial_active then 'trial'
63
+ when sub_status = 'active'
64
+ and not trial_active
65
+ and pm_type is null then 'pending'
66
+ when sub_status = 'active' and pm_type is not null then 'active'
67
+ else 'unknown'
68
+ end
69
+ from flags;
70
+ $$;
71
+ `.execute(db);
72
+ }
@@ -0,0 +1,45 @@
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`
7
+ alter table public.tenant
8
+ add column partner_denied_permissions text[] not null default '{}'
9
+ `.execute(db);
10
+ await sql`
11
+ alter table public.tenant_user
12
+ add column permission_bundles text[] not null default '{}'
13
+ `.execute(db);
14
+ await sql`
15
+ alter table public.tenant_user
16
+ disable trigger set_public_tenant_user_updated_at
17
+ `.execute(db);
18
+ await sql`
19
+ update public.tenant_user as tenant_user
20
+ set permission_bundles = case
21
+ when tenant_user.role = 'admin' and tenant.type = 'partner'
22
+ then array['ui:team-admin:v1', 'ui:partner-admin:v1']::text[]
23
+ when tenant_user.role = 'admin'
24
+ then array['ui:team-admin:v1']::text[]
25
+ when tenant_user.role = 'user'
26
+ then array['ui:accounting:v1']::text[]
27
+ else array[]::text[]
28
+ end
29
+ from public.tenant as tenant
30
+ where tenant.id = tenant_user.tenant_id
31
+ `.execute(db);
32
+ await sql`
33
+ alter table public.tenant_user
34
+ enable trigger set_public_tenant_user_updated_at
35
+ `.execute(db);
36
+ }
37
+
38
+ export async function down(db: Kysely<unknown>): Promise<void> {
39
+ await sql`
40
+ alter table public.tenant_user drop column permission_bundles
41
+ `.execute(db);
42
+ await sql`
43
+ alter table public.tenant drop column partner_denied_permissions
44
+ `.execute(db);
45
+ }
@@ -0,0 +1,32 @@
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`
7
+ alter table public.tenant_user
8
+ disable trigger set_public_tenant_user_updated_at
9
+ `.execute(db);
10
+ await sql`
11
+ update public.tenant_user as tenant_user
12
+ set permission_bundles = case
13
+ when tenant_user.role = 'admin' and tenant.type = 'partner'
14
+ then array['ui:team-admin:v1', 'ui:partner-admin:v1']::text[]
15
+ when tenant_user.role = 'admin'
16
+ then array['ui:team-admin:v1']::text[]
17
+ when tenant_user.role = 'user'
18
+ then array['ui:accounting:v1']::text[]
19
+ else tenant_user.permission_bundles
20
+ end
21
+ from public.tenant as tenant
22
+ where tenant.id = tenant_user.tenant_id
23
+ and cardinality(tenant_user.permission_bundles) = 0
24
+ and tenant_user.role in ('admin', 'user')
25
+ `.execute(db);
26
+ await sql`
27
+ alter table public.tenant_user
28
+ enable trigger set_public_tenant_user_updated_at
29
+ `.execute(db);
30
+ }
31
+
32
+ export async function down(_db: Kysely<unknown>): Promise<void> {}
@@ -0,0 +1,163 @@
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 type core.flow_event_status as enum (
7
+ 'pending', 'processing', 'succeeded', 'dead', 'canceled'
8
+ )
9
+ `.execute(db);
10
+ await sql`
11
+ create type core.flow_event_operation_action as enum (
12
+ 'replay', 'abandon'
13
+ )
14
+ `.execute(db);
15
+
16
+ await sql`
17
+ create table core.flow_event (
18
+ id uuid primary key default gen_random_uuid(),
19
+ tenant_id uuid not null,
20
+ event_type text not null,
21
+ entity_type text not null,
22
+ entity_id uuid not null,
23
+ data jsonb not null default '{}'::jsonb,
24
+ sync_id uuid,
25
+ target_connection_id uuid,
26
+ target_flow_id uuid,
27
+ status core.flow_event_status not null default 'pending',
28
+ available_at timestamptz not null default now(),
29
+ claimed_at timestamptz,
30
+ claimed_by text,
31
+ attempts integer not null default 0,
32
+ max_attempts integer not null default 10,
33
+ completed_at timestamptz,
34
+ last_error_code text,
35
+ last_error_details jsonb,
36
+ created_at timestamptz not null default now(),
37
+ updated_at timestamptz not null default now(),
38
+ constraint flow_event_attempts_check check (attempts >= 0),
39
+ constraint flow_event_max_attempts_check check (max_attempts > 0),
40
+ constraint flow_event_target_check check (
41
+ target_connection_id is not null
42
+ or target_flow_id is null
43
+ )
44
+ )
45
+ `.execute(db);
46
+
47
+ await sql`
48
+ create table core.flow_event_delivery (
49
+ id uuid primary key default gen_random_uuid(),
50
+ event_id uuid not null
51
+ references core.flow_event(id) on delete cascade,
52
+ tenant_id uuid not null,
53
+ connection_id uuid not null,
54
+ flow_id uuid not null,
55
+ status core.flow_event_status not null default 'pending',
56
+ available_at timestamptz not null default now(),
57
+ claimed_at timestamptz,
58
+ claimed_by text,
59
+ attempts integer not null default 0,
60
+ max_attempts integer not null default 10,
61
+ trigger_run_id text,
62
+ completed_at timestamptz,
63
+ last_error_code text,
64
+ last_error_details jsonb,
65
+ created_at timestamptz not null default now(),
66
+ updated_at timestamptz not null default now(),
67
+ constraint flow_event_delivery_attempts_check check (attempts >= 0),
68
+ constraint flow_event_delivery_max_attempts_check
69
+ check (max_attempts > 0),
70
+ constraint flow_event_delivery_listener_key
71
+ unique (event_id, connection_id, flow_id)
72
+ )
73
+ `.execute(db);
74
+
75
+ await sql`
76
+ create table core.flow_event_secret (
77
+ event_id uuid primary key
78
+ references core.flow_event(id) on delete cascade,
79
+ encryption_key_id text not null,
80
+ encrypted_payload text not null,
81
+ expires_at timestamptz not null,
82
+ created_at timestamptz not null default now()
83
+ )
84
+ `.execute(db);
85
+ await sql`
86
+ create table core.flow_event_operation (
87
+ id uuid primary key default gen_random_uuid(),
88
+ event_id uuid not null references core.flow_event(id) on delete cascade,
89
+ action core.flow_event_operation_action not null,
90
+ actor text not null,
91
+ reason_code text not null,
92
+ previous_status core.flow_event_status not null,
93
+ created_at timestamptz not null default now()
94
+ )
95
+ `.execute(db);
96
+
97
+ await sql`
98
+ create index flow_event_pending_idx
99
+ on core.flow_event (available_at, created_at, id)
100
+ where status = 'pending'
101
+ `.execute(db);
102
+ await sql`
103
+ create index flow_event_stale_claim_idx
104
+ on core.flow_event (claimed_at, id)
105
+ where status = 'processing'
106
+ `.execute(db);
107
+ await sql`
108
+ create index flow_event_tenant_history_idx
109
+ on core.flow_event (tenant_id, created_at desc, id desc)
110
+ `.execute(db);
111
+ await sql`
112
+ create index flow_event_sync_idx
113
+ on core.flow_event (tenant_id, sync_id)
114
+ where sync_id is not null
115
+ `.execute(db);
116
+ await sql`
117
+ create index flow_event_delivery_pending_idx
118
+ on core.flow_event_delivery (available_at, created_at, id)
119
+ where status = 'pending'
120
+ `.execute(db);
121
+ await sql`
122
+ create index flow_event_delivery_stale_claim_idx
123
+ on core.flow_event_delivery (claimed_at, id)
124
+ where status = 'processing'
125
+ `.execute(db);
126
+ await sql`
127
+ create index flow_event_delivery_event_idx
128
+ on core.flow_event_delivery (event_id, status, id)
129
+ `.execute(db);
130
+ await sql`
131
+ create index flow_event_secret_expiry_idx
132
+ on core.flow_event_secret (expires_at, event_id)
133
+ `.execute(db);
134
+ await sql`
135
+ create index flow_event_operation_history_idx
136
+ on core.flow_event_operation (event_id, created_at desc, id desc)
137
+ `.execute(db);
138
+
139
+ await sql`
140
+ do $$
141
+ begin
142
+ if exists (select 1 from pg_roles where rolname = 'application') then
143
+ grant usage on schema core to application;
144
+ grant select, insert, update, delete
145
+ on core.flow_event,
146
+ core.flow_event_delivery,
147
+ core.flow_event_operation,
148
+ core.flow_event_secret
149
+ to application;
150
+ end if;
151
+ end
152
+ $$
153
+ `.execute(db);
154
+ }
155
+
156
+ export async function down(db: Kysely<unknown>): Promise<void> {
157
+ await sql`drop table if exists core.flow_event_operation`.execute(db);
158
+ await sql`drop table if exists core.flow_event_secret`.execute(db);
159
+ await sql`drop table if exists core.flow_event_delivery`.execute(db);
160
+ await sql`drop table if exists core.flow_event`.execute(db);
161
+ await sql`drop type if exists core.flow_event_operation_action`.execute(db);
162
+ await sql`drop type if exists core.flow_event_status`.execute(db);
163
+ }