@composable-os/cli 0.0.0-stage → 0.1.0-beta.8

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 (140) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/blocks-DEELKGDC.js +233 -0
  4. package/dist/chunk-2ABSVMGB.js +13 -0
  5. package/dist/chunk-2S7375SC.js +802 -0
  6. package/dist/chunk-5KSXSK7Y.js +47 -0
  7. package/dist/chunk-6UWRT4VM.js +220 -0
  8. package/dist/chunk-E5JLTLRM.js +50 -0
  9. package/dist/chunk-G3ORKXVP.js +1178 -0
  10. package/dist/chunk-HDIOVFAZ.js +544 -0
  11. package/dist/chunk-KGHA67HG.js +765 -0
  12. package/dist/chunk-KOKYDJ7Z.js +1423 -0
  13. package/dist/chunk-KTXGEXTY.js +358 -0
  14. package/dist/chunk-LTQDLCP7.js +16486 -0
  15. package/dist/chunk-MAXLUQKB.js +1287 -0
  16. package/dist/chunk-STJZJIRA.js +11378 -0
  17. package/dist/chunk-X6I2LWB3.js +24319 -0
  18. package/dist/chunk-XDDBLYKW.js +29154 -0
  19. package/dist/chunk-ZRWF6QZS.js +2357 -0
  20. package/dist/cli-BRSACHSG.js +31393 -0
  21. package/dist/cli-DTMGNDXN.js +121 -0
  22. package/dist/cli.js +25 -0
  23. package/dist/client-repo-ZVZ6KW5S.js +55 -0
  24. package/dist/create.js +25 -0
  25. package/dist/fields-64BQJA62.js +48 -0
  26. package/dist/invoke-NHHOLLF6.js +26 -0
  27. package/dist/person-WMWRF2HV.js +58 -0
  28. package/dist/platform/migrations/0001_init.sql +59 -0
  29. package/dist/platform/migrations/0002_users_settings.sql +34 -0
  30. package/dist/platform/migrations/0003_people_organisations.sql +225 -0
  31. package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
  32. package/dist/platform/migrations/0005_views_audit.sql +79 -0
  33. package/dist/platform/migrations/0006_search.sql +223 -0
  34. package/dist/platform/migrations/0007_crm_views.sql +141 -0
  35. package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
  36. package/dist/platform/migrations/0010_approval_queue.sql +90 -0
  37. package/dist/platform/migrations/0011_connected_apps.sql +36 -0
  38. package/dist/platform/migrations/0020_extensions.sql +373 -0
  39. package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
  40. package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
  41. package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
  42. package/dist/platform/migrations/0061_audit_retention.sql +95 -0
  43. package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
  44. package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
  45. package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
  46. package/dist/platform/migrations/0065_storage_stats.sql +13 -0
  47. package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
  48. package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
  49. package/dist/platform/migrations/0100_stage_history.sql +54 -0
  50. package/dist/platform/migrations/0110_integrations.sql +103 -0
  51. package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
  52. package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
  53. package/dist/platform/migrations/0130_automations.sql +113 -0
  54. package/dist/platform/migrations/0131_notifications.sql +29 -0
  55. package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
  56. package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
  57. package/dist/platform/migrations/0160_releases.sql +41 -0
  58. package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
  59. package/dist/platform/migrations/0170_automation_connections.sql +32 -0
  60. package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
  61. package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
  62. package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
  63. package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
  64. package/dist/platform/migrations/0210_daily_digest.sql +33 -0
  65. package/dist/platform/migrations/0220_entitlements.sql +13 -0
  66. package/dist/platform/migrations/0221_support_grants.sql +33 -0
  67. package/dist/platform/migrations/0250_judge.sql +11 -0
  68. package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
  69. package/dist/platform/migrations/0252_judge_usage.sql +14 -0
  70. package/dist/platform/migrations/0255_summary_checks.sql +9 -0
  71. package/dist/platform/migrations/0256_outbound_check.sql +9 -0
  72. package/dist/platform/migrations/0260_ai_usage.sql +20 -0
  73. package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
  74. package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
  75. package/dist/platform/migrations/0320_duplicates.sql +41 -0
  76. package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
  77. package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
  78. package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
  79. package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
  80. package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
  81. package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
  82. package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
  83. package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
  84. package/dist/platform/migrations/0362_voice_usage.sql +17 -0
  85. package/dist/platform/migrations/0365_routines.sql +38 -0
  86. package/dist/platform/migrations/0370_approval_execution.sql +20 -0
  87. package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
  88. package/dist/platform/migrations/0380_rec_views.sql +94 -0
  89. package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
  90. package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
  91. package/dist/platform/migrations/0402_erase_requests.sql +34 -0
  92. package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
  93. package/dist/platform/migrations/0411_device_sessions.sql +33 -0
  94. package/dist/platform/migrations/0420_transition_history.sql +100 -0
  95. package/dist/platform/migrations/0421_orders.sql +134 -0
  96. package/dist/platform/migrations/0422_order_payments.sql +109 -0
  97. package/dist/platform/migrations/0440_capacity.sql +100 -0
  98. package/dist/platform/migrations/0441_capacity_views.sql +40 -0
  99. package/dist/platform/migrations/0450_order_grants.sql +21 -0
  100. package/dist/platform/migrations/0451_pack_roles.sql +18 -0
  101. package/dist/platform/migrations/0460_content.sql +59 -0
  102. package/dist/platform/migrations/0461_media.sql +52 -0
  103. package/dist/platform/migrations/0470_publish_versions.sql +36 -0
  104. package/dist/platform/migrations/0471_permission_grants.sql +18 -0
  105. package/dist/platform/migrations/0490_staff_invites.sql +40 -0
  106. package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
  107. package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
  108. package/dist/platform/migrations/0500_published_media_field.sql +25 -0
  109. package/dist/platform/migrations/0501_import_keys.sql +23 -0
  110. package/dist/platform/migrations/0520_publish_requests.sql +20 -0
  111. package/dist/platform/sdk/automation-check.ts +534 -0
  112. package/dist/platform/sdk/automation.ts +743 -0
  113. package/dist/platform/sdk/behaviour.ts +215 -0
  114. package/dist/platform/sdk/capacity.ts +112 -0
  115. package/dist/platform/sdk/contract.ts +766 -0
  116. package/dist/platform/sdk/document.ts +788 -0
  117. package/dist/platform/sdk/field-types.ts +106 -0
  118. package/dist/platform/sdk/formula/ast.ts +72 -0
  119. package/dist/platform/sdk/formula/compile.ts +454 -0
  120. package/dist/platform/sdk/formula/index.ts +4 -0
  121. package/dist/platform/sdk/formula/parser.ts +417 -0
  122. package/dist/platform/sdk/index.ts +33 -0
  123. package/dist/platform/sdk/manifest.ts +1484 -0
  124. package/dist/platform/sdk/media.ts +46 -0
  125. package/dist/platform/sdk/permissions.ts +60 -0
  126. package/dist/platform/sdk/renamed.ts +63 -0
  127. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  128. package/dist/platform/sdk/rich-text.ts +173 -0
  129. package/dist/platform/sdk/runtime.ts +410 -0
  130. package/dist/platform/sdk/sellable.ts +396 -0
  131. package/dist/platform/sdk/slug.ts +25 -0
  132. package/dist/platform/sdk/states.ts +162 -0
  133. package/dist/platform/sdk/testing.ts +836 -0
  134. package/dist/products-2H5DR232.js +11 -0
  135. package/dist/run-4JNFLYXV.js +847 -0
  136. package/dist/src-G6OF5SKW.js +31 -0
  137. package/dist/src-J27WXNLY.js +270 -0
  138. package/dist/token-MOJUA2U6.js +70 -0
  139. package/dist/token-util-TENRN4V7.js +6 -0
  140. package/package.json +33 -4
@@ -0,0 +1,142 @@
1
+ -- Migration 0400: the audit log accepts the actors `customer` and `public` (stage 22, ADR 0402,
2
+ -- ADR 0412).
3
+ --
4
+ -- A customer's write is audited like any write, with actor `customer` and the customer account's
5
+ -- id (cus_…) in user_id. A call with no account has actor `public` and no user_id. Successful
6
+ -- reads by either are counted in core_audit_read_counts, as for non-human staff callers
7
+ -- (ADR 0090).
8
+ --
9
+ -- SQLite cannot alter a CHECK constraint, so both tables are rebuilt with the same rows, as
10
+ -- migration 0020 did for the `extension` actor. This rebuild drops the CHECK on `actor`
11
+ -- altogether: the pipeline validates it in code, so this is the last rebuild an actor needs. core_audit's children (core_audit_changes,
12
+ -- core_audit_redactions) reference it by name; foreign keys are deferred so the swap is checked
13
+ -- once, at commit. The two triggers on core_audit_redactions that name core_audit (one writes
14
+ -- it, one reads it) are dropped first and recreated after, so the rename sees a consistent
15
+ -- schema. The append-only triggers on core_audit go with the table. All are recreated exactly as
16
+ -- migration 0061 left them.
17
+ PRAGMA defer_foreign_keys = ON;
18
+
19
+ DROP TRIGGER core_audit_redactions_apply_audit;
20
+ DROP TRIGGER core_audit_redactions_no_delete;
21
+
22
+ CREATE TABLE core_audit_next (
23
+ id TEXT PRIMARY KEY NOT NULL,
24
+ at TEXT NOT NULL,
25
+ operation TEXT NOT NULL,
26
+ risk TEXT NOT NULL
27
+ CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
28
+ -- No CHECK: the pipeline validates the actor (ACTORS and OUTSIDE_ACTORS in principals.ts), so
29
+ -- a new actor never needs this table rebuilt again.
30
+ actor TEXT NOT NULL,
31
+ -- The staff user (usr_…), the customer account (cus_…) for actor `customer`, or null.
32
+ user_id TEXT,
33
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
34
+ input TEXT NOT NULL CHECK (json_valid(input)),
35
+ result TEXT CHECK (result IS NULL OR json_valid(result)),
36
+ gate_decision TEXT NOT NULL CHECK (gate_decision IN ('auto', 'review', 'deny')),
37
+ outcome TEXT NOT NULL CHECK (outcome IN ('ok', 'forbidden', 'denied', 'review', 'error')),
38
+ error TEXT CHECK (error IS NULL OR json_valid(error)),
39
+ -- Migration 0010: the pending action an audit row queued or applied.
40
+ pending_action_id TEXT
41
+ );
42
+
43
+ INSERT INTO core_audit_next
44
+ (id, at, operation, risk, actor, user_id, origin, input, result, gate_decision, outcome, error,
45
+ pending_action_id)
46
+ SELECT id, at, operation, risk, actor, user_id, origin, input, result, gate_decision, outcome, error,
47
+ pending_action_id
48
+ FROM core_audit;
49
+
50
+ DROP TABLE core_audit;
51
+ ALTER TABLE core_audit_next RENAME TO core_audit;
52
+
53
+ CREATE INDEX core_audit_at_idx ON core_audit (at, id);
54
+ CREATE INDEX core_audit_user_idx ON core_audit (user_id, at);
55
+ CREATE INDEX core_audit_operation_idx ON core_audit (operation, at);
56
+ CREATE INDEX core_audit_actor_idx ON core_audit (actor, at);
57
+ CREATE INDEX core_audit_pending_action_idx ON core_audit (pending_action_id)
58
+ WHERE pending_action_id IS NOT NULL;
59
+
60
+ CREATE TRIGGER core_audit_no_update BEFORE UPDATE ON core_audit
61
+ WHEN NOT (
62
+ EXISTS (SELECT 1 FROM core_audit_redactions r WHERE r.audit_id = OLD.id)
63
+ AND NEW.id IS OLD.id
64
+ AND NEW.at IS OLD.at
65
+ AND NEW.operation IS OLD.operation
66
+ AND NEW.risk IS OLD.risk
67
+ AND NEW.actor IS OLD.actor
68
+ AND NEW.user_id IS OLD.user_id
69
+ AND NEW.origin IS OLD.origin
70
+ AND NEW.gate_decision IS OLD.gate_decision
71
+ AND NEW.outcome IS OLD.outcome
72
+ AND NEW.input IS '"[erased]"'
73
+ AND NEW.result IS (CASE WHEN OLD.result IS NULL THEN NULL ELSE '"[erased]"' END)
74
+ AND NEW.error IS (CASE WHEN OLD.error IS NULL THEN NULL
75
+ ELSE json_object('code', json_extract(OLD.error, '$.code'), 'message', '[erased]') END)
76
+ )
77
+ BEGIN
78
+ SELECT RAISE(ABORT, 'core_audit is append-only');
79
+ END;
80
+
81
+ CREATE TRIGGER core_audit_no_delete BEFORE DELETE ON core_audit
82
+ WHEN NOT (
83
+ (SELECT json_extract(value, '$.years') FROM core_settings WHERE key = 'audit_retention')
84
+ IS NOT NULL
85
+ AND OLD.at < coalesce((SELECT max(cutoff) FROM core_audit_purges), '')
86
+ )
87
+ BEGIN
88
+ SELECT RAISE(ABORT, 'core_audit is append-only');
89
+ END;
90
+
91
+ CREATE TRIGGER core_audit_redactions_apply_audit AFTER INSERT ON core_audit_redactions
92
+ WHEN NEW.audit_id IS NOT NULL
93
+ BEGIN
94
+ UPDATE core_audit SET
95
+ input = '"[erased]"',
96
+ result = CASE WHEN result IS NULL THEN NULL ELSE '"[erased]"' END,
97
+ error = CASE WHEN error IS NULL THEN NULL
98
+ ELSE json_object('code', json_extract(error, '$.code'), 'message', '[erased]') END
99
+ WHERE id = NEW.audit_id;
100
+ END;
101
+
102
+ CREATE TRIGGER core_audit_redactions_no_delete BEFORE DELETE ON core_audit_redactions
103
+ WHEN NOT (
104
+ (SELECT json_extract(value, '$.years') FROM core_settings WHERE key = 'audit_retention')
105
+ IS NOT NULL
106
+ AND coalesce(
107
+ (SELECT a.at FROM core_audit a WHERE a.id = OLD.audit_id),
108
+ (SELECT c.at FROM core_audit_changes c WHERE c.id = OLD.change_id)
109
+ ) < coalesce((SELECT max(cutoff) FROM core_audit_purges), '')
110
+ )
111
+ BEGIN
112
+ SELECT RAISE(ABORT, 'core_audit_redactions is append-only');
113
+ END;
114
+
115
+ -- Read counts: the same two actors.
116
+ CREATE TABLE core_audit_read_counts_next (
117
+ -- The UTC hour, 'YYYY-MM-DDTHH:00:00.000Z'.
118
+ hour TEXT NOT NULL,
119
+ actor TEXT NOT NULL,
120
+ -- The staff user, the customer account (cus_…) for actor `customer`, '' for actor `public`.
121
+ user_id TEXT NOT NULL,
122
+ -- Who exactly, within the actor: the connected app (cap_…) for MCP, the extension id, the
123
+ -- automation id, else the MCP client id, else ''.
124
+ caller TEXT NOT NULL DEFAULT '',
125
+ operation TEXT NOT NULL,
126
+ count INTEGER NOT NULL DEFAULT 0,
127
+ -- How many of them saw the session's simulated pending changes (ADR 0032).
128
+ simulated INTEGER NOT NULL DEFAULT 0,
129
+ first_at TEXT NOT NULL,
130
+ last_at TEXT NOT NULL,
131
+ PRIMARY KEY (hour, actor, user_id, caller, operation)
132
+ ) WITHOUT ROWID;
133
+
134
+ INSERT INTO core_audit_read_counts_next
135
+ (hour, actor, user_id, caller, operation, count, simulated, first_at, last_at)
136
+ SELECT hour, actor, user_id, caller, operation, count, simulated, first_at, last_at
137
+ FROM core_audit_read_counts;
138
+
139
+ DROP TABLE core_audit_read_counts;
140
+ ALTER TABLE core_audit_read_counts_next RENAME TO core_audit_read_counts;
141
+
142
+ CREATE INDEX core_audit_read_counts_user_idx ON core_audit_read_counts (user_id, hour);
@@ -0,0 +1,61 @@
1
+ -- Migration 0401: customer accounts (stage 22 step 4, ADR 0402).
2
+ --
3
+ -- A customer is a Person who can sign in. Customers sign in through a second Better Auth
4
+ -- configuration (packages/auth/src/customer-auth.ts) with its own tables, cookie and secret, so a
5
+ -- customer session is never in core_session and a staff session never in core_customer_session.
6
+ -- A customer is never a core_user row and never holds a role.
7
+ --
8
+ -- Column names are snake_case; packages/auth maps Better Auth's camelCase fields onto them.
9
+ -- No table here stores a client address: Better Auth's ip_address and user_agent session columns
10
+ -- are left out on purpose, and the customer configuration strips both before a session is written.
11
+
12
+ CREATE TABLE core_customer (
13
+ -- cus_… (ids.ts).
14
+ id TEXT PRIMARY KEY NOT NULL,
15
+ -- The Person the account is linked to. Null until the first sign-in links it, and while the
16
+ -- address matches several people (link_state 'needs_merge'). One account per Person.
17
+ -- Erasing the Person deletes the account (and, through it, its sessions).
18
+ person_id TEXT REFERENCES core_person (id) ON DELETE CASCADE,
19
+ -- The verified sign-in address, lower-cased and trimmed.
20
+ email TEXT NOT NULL UNIQUE CHECK (email = lower(trim(email)) AND email <> ''),
21
+ email_verified INTEGER NOT NULL DEFAULT 0 CHECK (email_verified IN (0, 1)),
22
+ -- 'disabled': cannot sign in, and an existing session resolves to no customer.
23
+ status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'disabled')),
24
+ -- 'needs_merge': the address matched several people at sign-in; staff merge them first.
25
+ link_state TEXT NOT NULL DEFAULT 'linked' CHECK (link_state IN ('linked', 'needs_merge')),
26
+ created_at DATE NOT NULL,
27
+ updated_at DATE NOT NULL,
28
+ last_sign_in_at TEXT
29
+ );
30
+ CREATE UNIQUE INDEX core_customer_person_uq ON core_customer (person_id) WHERE person_id IS NOT NULL;
31
+
32
+ CREATE TABLE core_customer_session (
33
+ id TEXT PRIMARY KEY NOT NULL,
34
+ customer_id TEXT NOT NULL REFERENCES core_customer (id) ON DELETE CASCADE,
35
+ token TEXT NOT NULL UNIQUE,
36
+ expires_at DATE NOT NULL,
37
+ created_at DATE NOT NULL,
38
+ updated_at DATE NOT NULL
39
+ );
40
+ CREATE INDEX core_customer_session_customer_idx ON core_customer_session (customer_id);
41
+
42
+ -- One-time sign-in codes, stored hashed. The identifier names the address the code was sent to.
43
+ CREATE TABLE core_customer_verification (
44
+ id TEXT PRIMARY KEY NOT NULL,
45
+ identifier TEXT NOT NULL,
46
+ value TEXT NOT NULL,
47
+ expires_at DATE NOT NULL,
48
+ created_at DATE NOT NULL,
49
+ updated_at DATE NOT NULL
50
+ );
51
+ CREATE INDEX core_customer_verification_identifier_idx ON core_customer_verification (identifier);
52
+
53
+ -- Addresses staff invited (customer.invite). With sign-up `invite_only`, a code is sent only to an
54
+ -- invited address or one that already has an account.
55
+ CREATE TABLE core_customer_invite (
56
+ email TEXT PRIMARY KEY NOT NULL CHECK (email = lower(trim(email)) AND email <> ''),
57
+ invited_by TEXT NOT NULL,
58
+ invited_at TEXT NOT NULL
59
+ );
60
+
61
+ UPDATE core_meta SET value = '401' WHERE key = 'schema_version';
@@ -0,0 +1,34 @@
1
+ -- Migration 0402: erase requests (stage 22 step 7, ADR 0402).
2
+ --
3
+ -- A customer who deletes their account (me.deleteAccount) is disabled and signed out at once, and
4
+ -- an erase request names their Person for staff to action by a due date: erase the person
5
+ -- (person.erase closes it as 'erased') or decline with a reason (eraseRequest.decline). A request
6
+ -- past its due date notifies admins at most once a day (last_notified_at).
7
+ --
8
+ -- Erasing the Person deletes their requests with everything else about them: only the audit trail
9
+ -- remains. No client address is stored.
10
+ CREATE TABLE core_erase_request (
11
+ -- erq_… (ids.ts).
12
+ id TEXT PRIMARY KEY NOT NULL,
13
+ person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
14
+ -- The account that asked, while it exists.
15
+ customer_id TEXT REFERENCES core_customer (id) ON DELETE SET NULL,
16
+ requested_at TEXT NOT NULL,
17
+ due_at TEXT NOT NULL,
18
+ status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'erased', 'declined')),
19
+ -- Why staff declined (a record the org must keep). Emailed to the person.
20
+ decline_reason TEXT,
21
+ closed_at TEXT,
22
+ -- The staff user who closed it.
23
+ closed_by TEXT,
24
+ -- When admins were last told the request is overdue.
25
+ last_notified_at TEXT,
26
+ CHECK ((status = 'open') = (closed_at IS NULL)),
27
+ CHECK (status <> 'declined' OR decline_reason IS NOT NULL)
28
+ );
29
+ -- One open request per Person.
30
+ CREATE UNIQUE INDEX core_erase_request_open_uq ON core_erase_request (person_id) WHERE status = 'open';
31
+ CREATE INDEX core_erase_request_due_idx ON core_erase_request (status, due_at);
32
+ CREATE INDEX core_erase_request_customer_idx ON core_erase_request (customer_id);
33
+
34
+ UPDATE core_meta SET value = '402' WHERE key = 'schema_version';
@@ -0,0 +1,45 @@
1
+ -- Migration 0410: idempotency keys for the HTTP surface (stage 23, ADR 0403, ADR 0416).
2
+ --
3
+ -- Every write over POST /api/v1/op/<name> carries an Idempotency-Key. A key is the caller's own:
4
+ -- its scope is the principal (`staff:<user id>`, `customer:<account id>`, or `public`).
5
+ --
6
+ -- core_idempotency_key is the record of a write that committed: it is inserted in the write's own
7
+ -- D1 batch, so the key and the write commit together or not at all, and a second batch for the
8
+ -- same key fails on the primary key and applies nothing. It holds the operation, a hash of the
9
+ -- input and the response the caller was sent. A response computed after the commit (an
10
+ -- operation that reads its output back) is filled in just after it.
11
+ --
12
+ -- core_idempotency_claim marks a key whose call is running, so the same key sent twice at once
13
+ -- runs the handler once: the second call waits for the first's response. A claim is removed when
14
+ -- its call ends; one left by a Worker that died is taken over after a minute.
15
+ --
16
+ -- Both are purged by the daily maintenance cron 24 hours after they were made. No client address
17
+ -- is stored.
18
+ CREATE TABLE core_idempotency_key (
19
+ scope TEXT NOT NULL,
20
+ idempotency_key TEXT NOT NULL,
21
+ operation TEXT NOT NULL,
22
+ -- SHA-256 of the operation name and the input as sent, in hex.
23
+ input_hash TEXT NOT NULL,
24
+ -- The claim that committed it, so its own call can fill in a late response.
25
+ claim TEXT NOT NULL,
26
+ -- The HTTP status and JSON body the caller was sent; NULL until a late response is filled in.
27
+ status INTEGER,
28
+ response TEXT,
29
+ created_at TEXT NOT NULL,
30
+ PRIMARY KEY (scope, idempotency_key)
31
+ ) WITHOUT ROWID;
32
+ CREATE INDEX core_idempotency_key_created_idx ON core_idempotency_key (created_at);
33
+
34
+ CREATE TABLE core_idempotency_claim (
35
+ scope TEXT NOT NULL,
36
+ idempotency_key TEXT NOT NULL,
37
+ operation TEXT NOT NULL,
38
+ input_hash TEXT NOT NULL,
39
+ claim TEXT NOT NULL,
40
+ claimed_at TEXT NOT NULL,
41
+ PRIMARY KEY (scope, idempotency_key)
42
+ ) WITHOUT ROWID;
43
+ CREATE INDEX core_idempotency_claim_claimed_idx ON core_idempotency_claim (claimed_at);
44
+
45
+ UPDATE core_meta SET value = '410' WHERE key = 'schema_version';
@@ -0,0 +1,33 @@
1
+ -- Migration 0411: sessions for native apps on the HTTP surface (stage 23, ADR 0403, ADR 0419).
2
+ --
3
+ -- A phone or a TV exchanges its sign-in for an access token that lasts minutes and a refresh
4
+ -- token. Only SHA-256 hashes of the tokens are stored. Every refresh rotates both; the refresh
5
+ -- hashes already used are kept on the row, and presenting one again ends the session (the row is
6
+ -- deleted). A session unused for the period in the `device_session_idle_days` setting (90 days by
7
+ -- default) is ended when it is next presented.
8
+ --
9
+ -- A session is one audience's: staff (user_id) or a customer (customer_id), never both. Deleting
10
+ -- the user or the customer account deletes its sessions. No client address is stored.
11
+ CREATE TABLE core_device_session (
12
+ -- dvs_… (ids.ts).
13
+ id TEXT PRIMARY KEY NOT NULL,
14
+ audience TEXT NOT NULL CHECK (audience IN ('staff', 'customer')),
15
+ user_id TEXT REFERENCES core_user (id) ON DELETE CASCADE,
16
+ customer_id TEXT REFERENCES core_customer (id) ON DELETE CASCADE,
17
+ -- What the app called the device ("Ada's iPhone"), so a person can tell their sessions apart.
18
+ device_name TEXT NOT NULL,
19
+ access_hash TEXT NOT NULL,
20
+ access_expires_at TEXT NOT NULL,
21
+ refresh_hash TEXT NOT NULL,
22
+ -- JSON array of refresh hashes this session has already rotated past.
23
+ used_refresh_hashes TEXT NOT NULL DEFAULT '[]',
24
+ created_at TEXT NOT NULL,
25
+ last_used_at TEXT NOT NULL,
26
+ CHECK ((audience = 'staff') = (user_id IS NOT NULL)),
27
+ CHECK ((audience = 'customer') = (customer_id IS NOT NULL))
28
+ );
29
+ CREATE UNIQUE INDEX core_device_session_access_uq ON core_device_session (access_hash);
30
+ CREATE INDEX core_device_session_user_idx ON core_device_session (user_id);
31
+ CREATE INDEX core_device_session_customer_idx ON core_device_session (customer_id);
32
+
33
+ UPDATE core_meta SET value = '411' WHERE key = 'schema_version';
@@ -0,0 +1,100 @@
1
+ -- Migration 0420: one history table for the transition engine (stage 25, ADR 0425).
2
+ --
3
+ -- `stages` (ADR 0100) and `states` are two declarations on one engine, and the engine keeps one
4
+ -- history: a row per position a record has been in, with the times it entered and left.
5
+ -- `core_stage_history` (migration 0100) becomes that table, under the name of what it now holds:
6
+ -- `core_transition_history`. Its `stage_id` and `stage_type` columns become `position_id` and
7
+ -- `position_type`, and it gains:
8
+ -- form which declaration the row belongs to: 'stages' or 'states';
9
+ -- entered_as who made the move: 'staff', 'customer' or 'host' (platform code).
10
+ -- A stage row holds the stage record's id and open, won or lost. A state row holds the state's
11
+ -- value (there is no stage record), no group, and 'open' or 'final'.
12
+ --
13
+ -- Every existing row is copied with its id: all of them are stage rows, entered by staff (nothing
14
+ -- else could move a record before this migration). The read views keep their names and columns.
15
+ CREATE TABLE core_transition_history (
16
+ id TEXT PRIMARY KEY NOT NULL,
17
+ -- Registry name of the object: `deals.deal`.
18
+ object_type TEXT NOT NULL,
19
+ record_id TEXT NOT NULL,
20
+ form TEXT NOT NULL DEFAULT 'stages' CHECK (form IN ('stages', 'states')),
21
+ -- The position entered: a stage record's id, or a state's value.
22
+ position_id TEXT NOT NULL,
23
+ -- The stage's group (pipeline) when the record entered it, if the behaviour has groups.
24
+ group_id TEXT,
25
+ -- Stages: the stage type when the record entered it (open, won, lost).
26
+ -- States: 'final' when the state ends the lifecycle, else 'open'.
27
+ position_type TEXT NOT NULL,
28
+ entered_at TEXT NOT NULL,
29
+ left_at TEXT,
30
+ entered_by TEXT,
31
+ entered_as TEXT NOT NULL DEFAULT 'staff' CHECK (entered_as IN ('staff', 'customer', 'host')),
32
+ CHECK (
33
+ (form = 'stages' AND position_type IN ('open', 'won', 'lost'))
34
+ OR (form = 'states' AND position_type IN ('open', 'final'))
35
+ )
36
+ );
37
+
38
+ INSERT INTO core_transition_history
39
+ (id, object_type, record_id, form, position_id, group_id, position_type, entered_at, left_at,
40
+ entered_by, entered_as)
41
+ SELECT id, object_type, record_id, 'stages', stage_id, group_id, stage_type, entered_at, left_at,
42
+ entered_by, 'staff'
43
+ FROM core_stage_history;
44
+
45
+ -- Dropping the old table drops its indexes with it.
46
+ DROP VIEW rec_stage_history;
47
+ DROP TABLE core_stage_history;
48
+
49
+ -- The indexes of migration 0100, on the new names. An object has one lifecycle (stages or
50
+ -- states, never both), so a record still has at most one current row.
51
+ CREATE INDEX core_transition_history_record_idx
52
+ ON core_transition_history (object_type, record_id, entered_at);
53
+ CREATE UNIQUE INDEX core_transition_history_current_idx
54
+ ON core_transition_history (object_type, record_id) WHERE left_at IS NULL;
55
+ CREATE INDEX core_transition_history_position_idx
56
+ ON core_transition_history (position_id, entered_at);
57
+
58
+ -- Stage-driven objects: the name, columns and numbers the view has had since 0100 and 0380.
59
+ CREATE VIEW rec_stage_history AS
60
+ SELECT
61
+ id,
62
+ object_type,
63
+ record_id,
64
+ position_id AS stage_id,
65
+ group_id,
66
+ position_type AS stage_type,
67
+ entered_at,
68
+ left_at,
69
+ left_at IS NULL AS is_current,
70
+ round(
71
+ julianday(coalesce(left_at, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))) - julianday(entered_at),
72
+ 4
73
+ ) AS days_in_stage,
74
+ entered_by
75
+ FROM core_transition_history
76
+ WHERE form = 'stages';
77
+
78
+ -- State-driven objects. `days_in_state` runs to now for the current state.
79
+ CREATE VIEW rec_state_history AS
80
+ SELECT
81
+ id,
82
+ object_type,
83
+ record_id,
84
+ position_id AS state,
85
+ position_type = 'final' AS is_final,
86
+ entered_at,
87
+ left_at,
88
+ left_at IS NULL AS is_current,
89
+ round(
90
+ julianday(coalesce(left_at, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))) - julianday(entered_at),
91
+ 4
92
+ ) AS days_in_state,
93
+ entered_by,
94
+ entered_as
95
+ FROM core_transition_history
96
+ WHERE form = 'states';
97
+
98
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
99
+ UPDATE core_meta SET value = '420'
100
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 420;
@@ -0,0 +1,134 @@
1
+ -- Migration 0421: orders, order lines and the payments applied to them (stage 25, ADR 0426).
2
+ --
3
+ -- An Order is a base record in core: what someone is buying, priced by the host from the records
4
+ -- being bought, in the deployment's one currency. Every amount is an integer in the currency's
5
+ -- minor unit (cents), so nothing here is ever rounded twice.
6
+ --
7
+ -- An order's state is a fixed machine declared in core (fields/order.ts) and held by the
8
+ -- transition engine (ADR 0425): pending -> paid -> refunded; pending -> cancelled; pending ->
9
+ -- expired; expired -> paid (a late payment staff honour); and, for an order paid by deposit,
10
+ -- pending -> part_paid -> paid and part_paid -> cancelled. paid, part_paid, refunded and expired
11
+ -- are moves only platform code makes. The CHECK below is the list of states, not the rule: the
12
+ -- engine is the rule, and its history is in core_transition_history (object_type 'order').
13
+ --
14
+ -- Nothing here holds a card number, and no column could: paying happens on the provider's page.
15
+
16
+ CREATE TABLE core_order (
17
+ -- ord_… (ids.ts).
18
+ id TEXT PRIMARY KEY NOT NULL,
19
+ -- Whose order it is. Null when the address it was placed with matched several people (staff
20
+ -- merge them), and after the person is erased: the order stays, as an amount with no one on it.
21
+ person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
22
+ -- The address the order was placed with, lower-cased and trimmed. Null after an erase.
23
+ email TEXT CHECK (email IS NULL OR (email = lower(trim(email)) AND email <> '')),
24
+ -- ISO 4217, the deployment's currency when the order was started.
25
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
26
+ total INTEGER NOT NULL CHECK (total >= 0),
27
+ -- The sum of the verified payments applied. Only applyVerifiedPayment raises it.
28
+ paid INTEGER NOT NULL DEFAULT 0 CHECK (paid >= 0),
29
+ state TEXT NOT NULL DEFAULT 'pending'
30
+ CHECK (state IN ('pending', 'part_paid', 'paid', 'cancelled', 'expired', 'refunded')),
31
+ -- How the buyer chose to pay: all at once, or a deposit now and the balance by a date.
32
+ pay TEXT NOT NULL DEFAULT 'full' CHECK (pay IN ('full', 'deposit')),
33
+ -- pay = 'deposit': what is due first, and when the balance is due. Both from the records bought.
34
+ deposit INTEGER CHECK (deposit IS NULL OR deposit > 0),
35
+ balance_due_at TEXT,
36
+ -- When an order nothing has been paid on expires (the minute cron moves it). Not read once
37
+ -- anything is paid: a part-paid order has a balance due date instead.
38
+ expires_at TEXT,
39
+ -- Who started it: a signed-in customer, the public (a guest) or staff.
40
+ placed_as TEXT NOT NULL CHECK (placed_as IN ('customer', 'public', 'staff')),
41
+ -- The provider's own reference for the order (set by checkout), for finding it from a webhook.
42
+ provider TEXT,
43
+ provider_ref TEXT,
44
+ -- A guest's way back to this one order: the SHA-256 of a random token the buyer is given once.
45
+ -- access_until is null while the order is open (pending or part paid) and set when it closes.
46
+ access_hash TEXT,
47
+ access_until TEXT,
48
+ paid_at TEXT,
49
+ cancelled_at TEXT,
50
+ refunded_at TEXT,
51
+ created_at TEXT NOT NULL,
52
+ updated_at TEXT NOT NULL,
53
+ -- Counts the order's updates. Platform code reads the order, decides, and writes version + 1;
54
+ -- core_order_stale (below) fails a write made from an older read, and its whole batch.
55
+ version INTEGER NOT NULL DEFAULT 0,
56
+ CHECK (paid <= total),
57
+ CHECK (pay = 'full' OR (deposit IS NOT NULL AND balance_due_at IS NOT NULL))
58
+ );
59
+ CREATE INDEX core_order_person_idx ON core_order (person_id, created_at);
60
+ CREATE INDEX core_order_email_idx ON core_order (email) WHERE email IS NOT NULL;
61
+ -- The expiry sweep and the overdue list read one state each, in date order.
62
+ CREATE INDEX core_order_expiry_idx ON core_order (expires_at) WHERE state = 'pending';
63
+ CREATE INDEX core_order_balance_due_idx ON core_order (balance_due_at) WHERE state = 'part_paid';
64
+ CREATE UNIQUE INDEX core_order_provider_ref_uq ON core_order (provider, provider_ref)
65
+ WHERE provider_ref IS NOT NULL;
66
+ CREATE UNIQUE INDEX core_order_access_uq ON core_order (access_hash) WHERE access_hash IS NOT NULL;
67
+
68
+ -- Two payments for one order at the same moment must not both be judged against the same read
69
+ -- (the second would be lost, or the order left part paid with nothing owing). Every update of an
70
+ -- order's money or state says which version it read; a stale one is refused and can be retried.
71
+ CREATE TRIGGER core_order_stale
72
+ BEFORE UPDATE OF state, paid, total, deposit, balance_due_at ON core_order
73
+ WHEN NEW.version <> OLD.version + 1
74
+ BEGIN
75
+ SELECT RAISE(ABORT, 'core_order_stale');
76
+ END;
77
+
78
+ CREATE TABLE core_order_line (
79
+ -- orl_… (ids.ts).
80
+ id TEXT PRIMARY KEY NOT NULL,
81
+ order_id TEXT NOT NULL REFERENCES core_order (id) ON DELETE CASCADE,
82
+ position INTEGER NOT NULL,
83
+ -- What is bought: any record whose object is sellable (the object's registry name and the id).
84
+ object_type TEXT NOT NULL,
85
+ record_id TEXT NOT NULL,
86
+ -- The record's title when the order was started, so a line reads the same after a rename.
87
+ title TEXT NOT NULL,
88
+ quantity INTEGER NOT NULL CHECK (quantity > 0),
89
+ -- Read from the record by the host when the order was started. Never from a caller.
90
+ unit_price INTEGER NOT NULL CHECK (unit_price >= 0),
91
+ line_total INTEGER NOT NULL CHECK (line_total = unit_price * quantity),
92
+ created_at TEXT NOT NULL,
93
+ UNIQUE (order_id, position)
94
+ );
95
+ CREATE INDEX core_order_line_record_idx ON core_order_line (object_type, record_id);
96
+
97
+ -- A verified payment the provider reported for an order, once per provider event. The primary
98
+ -- key is what makes a redelivered webhook change nothing: a second insert of the same event
99
+ -- fails its batch. Only applyVerifiedPayment writes here (orders/payments.ts).
100
+ CREATE TABLE core_order_payment (
101
+ provider TEXT NOT NULL,
102
+ event_id TEXT NOT NULL,
103
+ order_id TEXT NOT NULL REFERENCES core_order (id) ON DELETE CASCADE,
104
+ amount INTEGER NOT NULL,
105
+ currency TEXT NOT NULL,
106
+ -- applied: it was added to the order. mismatch: the amount or currency was not one the order
107
+ -- could be charged, so nothing moved. late: the order was already closed, so nothing moved.
108
+ outcome TEXT NOT NULL CHECK (outcome IN ('applied', 'mismatch', 'late')),
109
+ -- The provider's reference for the payment itself (for a refund), when it gave one.
110
+ payment_ref TEXT,
111
+ received_at TEXT NOT NULL,
112
+ PRIMARY KEY (provider, event_id)
113
+ ) WITHOUT ROWID;
114
+ CREATE INDEX core_order_payment_order_idx ON core_order_payment (order_id, received_at);
115
+
116
+ -- What list operations and the visibility seam read. The guest token's hash and the provider's
117
+ -- reference are not record fields and are not in the view.
118
+ CREATE VIEW rec_order AS
119
+ SELECT
120
+ id, person_id, email, currency, total, paid, total - paid AS balance, state, pay, deposit,
121
+ balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at, updated_at
122
+ FROM core_order;
123
+
124
+ -- A line carries its order's person, so the seam shows a customer the lines of their own orders.
125
+ CREATE VIEW rec_order_line AS
126
+ SELECT
127
+ l.id, l.order_id, o.person_id, l.position, l.object_type, l.record_id, l.title, l.quantity,
128
+ l.unit_price, l.line_total, l.created_at
129
+ FROM core_order_line l
130
+ JOIN core_order o ON o.id = l.order_id;
131
+
132
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
133
+ UPDATE core_meta SET value = '421'
134
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 421;
@@ -0,0 +1,109 @@
1
+ -- Migration 0422: checkouts, verified payment events, refunds and late payments (stage 25, the
2
+ -- gatekeeper half; ADR 0429).
3
+ --
4
+ -- The payments gatekeeper verifies the provider's webhook and hands each verified event to the
5
+ -- host's own adapter, which stores it here (core_payment_event) before anything reads it. The
6
+ -- operation that applies an event (order.applyPaymentEvent) takes only an event's key and reads the
7
+ -- event from this table: nothing a caller sends can stand in for the provider's word. Nothing here
8
+ -- holds a card number, and no column could: paying happens on the provider's own page.
9
+
10
+ -- A checkout the host opened on the provider's page for an order: what it was for and how much.
11
+ -- A webhook finds its order by the provider's reference here.
12
+ CREATE TABLE core_order_checkout (
13
+ provider TEXT NOT NULL,
14
+ ref TEXT NOT NULL,
15
+ order_id TEXT NOT NULL REFERENCES core_order (id) ON DELETE CASCADE,
16
+ amount INTEGER NOT NULL CHECK (amount > 0),
17
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
18
+ -- What the payment was to be: the whole price, the deposit, the whole balance or a part of it.
19
+ kind TEXT NOT NULL CHECK (kind IN ('full', 'deposit', 'balance', 'part')),
20
+ created_at TEXT NOT NULL,
21
+ PRIMARY KEY (provider, ref)
22
+ ) WITHOUT ROWID;
23
+ CREATE INDEX core_order_checkout_order_idx ON core_order_checkout (order_id, created_at);
24
+
25
+ -- A verified event as the gatekeeper delivered it: a payment or a refund the provider confirmed.
26
+ -- Written only by the host's adapter (recordPaymentEvent), never by an operation. `payload` holds
27
+ -- ids, amounts and the currency: never a person's details, never card data.
28
+ CREATE TABLE core_payment_event (
29
+ provider TEXT NOT NULL,
30
+ event_id TEXT NOT NULL,
31
+ kind TEXT NOT NULL CHECK (kind IN ('payment', 'refund')),
32
+ connection_id TEXT,
33
+ payload TEXT NOT NULL,
34
+ received_at TEXT NOT NULL,
35
+ -- Set once order.applyPaymentEvent has applied it (whatever the outcome).
36
+ applied_at TEXT,
37
+ outcome TEXT,
38
+ attempts INTEGER NOT NULL DEFAULT 0,
39
+ PRIMARY KEY (provider, event_id)
40
+ ) WITHOUT ROWID;
41
+ -- The minute cron applies any event its first delivery did not.
42
+ CREATE INDEX core_payment_event_unapplied_idx ON core_payment_event (received_at)
43
+ WHERE applied_at IS NULL;
44
+
45
+ -- A refund: asked for by staff (requested, with the outbound action a person approves) and then
46
+ -- confirmed by the provider's webhook (confirmed). A refund made at the provider directly arrives
47
+ -- confirmed with no request. An order's `refunded` is the sum of its confirmed refunds.
48
+ CREATE TABLE core_order_refund (
49
+ -- rfd_… (ids.ts). Sent to the provider as the refund's metadata and idempotency key.
50
+ id TEXT PRIMARY KEY NOT NULL,
51
+ order_id TEXT NOT NULL REFERENCES core_order (id) ON DELETE CASCADE,
52
+ provider TEXT NOT NULL,
53
+ -- The provider's reference of the payment the money goes back to.
54
+ payment_ref TEXT,
55
+ amount INTEGER NOT NULL CHECK (amount > 0),
56
+ currency TEXT NOT NULL,
57
+ -- Staff's words. Cleared when the order's person is erased.
58
+ reason TEXT,
59
+ status TEXT NOT NULL CHECK (status IN ('requested', 'confirmed')),
60
+ -- The outbound action (core_pending_action) a person approves, for a requested refund.
61
+ pending_action_id TEXT,
62
+ requested_by TEXT,
63
+ requested_at TEXT,
64
+ provider_refund_ref TEXT,
65
+ event_id TEXT,
66
+ confirmed_at TEXT,
67
+ created_at TEXT NOT NULL
68
+ );
69
+ CREATE INDEX core_order_refund_order_idx ON core_order_refund (order_id, created_at);
70
+ -- A refund the provider reports twice (created, then updated) is counted once.
71
+ CREATE UNIQUE INDEX core_order_refund_provider_uq ON core_order_refund (provider, provider_refund_ref)
72
+ WHERE provider_refund_ref IS NOT NULL;
73
+
74
+ -- What has gone back to the buyer, in minor units: the sum of the confirmed refunds.
75
+ ALTER TABLE core_order ADD COLUMN refunded INTEGER NOT NULL DEFAULT 0 CHECK (refunded >= 0);
76
+ -- The confirmation email's own link to the order (the SHA-256 of a token minted when it is sent),
77
+ -- beside the token a guest was given at the start. Either opens this one order.
78
+ ALTER TABLE core_order ADD COLUMN email_link_hash TEXT;
79
+ CREATE UNIQUE INDEX core_order_email_link_uq ON core_order (email_link_hash)
80
+ WHERE email_link_hash IS NOT NULL;
81
+
82
+ -- The version check covers what has been refunded too.
83
+ DROP TRIGGER core_order_stale;
84
+ CREATE TRIGGER core_order_stale
85
+ BEFORE UPDATE OF state, paid, refunded, total, deposit, balance_due_at ON core_order
86
+ WHEN NEW.version <> OLD.version + 1
87
+ BEGIN
88
+ SELECT RAISE(ABORT, 'core_order_stale');
89
+ END;
90
+
91
+ -- A late payment (outcome 'late') stays on the paid-late list until staff honour it or it is
92
+ -- refunded.
93
+ ALTER TABLE core_order_payment ADD COLUMN resolved TEXT
94
+ CHECK (resolved IS NULL OR resolved IN ('honoured', 'refunded'));
95
+ ALTER TABLE core_order_payment ADD COLUMN resolved_at TEXT;
96
+ CREATE INDEX core_order_payment_late_idx ON core_order_payment (received_at)
97
+ WHERE outcome = 'late' AND resolved IS NULL;
98
+
99
+ DROP VIEW rec_order;
100
+ CREATE VIEW rec_order AS
101
+ SELECT
102
+ id, person_id, email, currency, total, paid, total - paid AS balance, refunded, state, pay,
103
+ deposit, balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at,
104
+ updated_at
105
+ FROM core_order;
106
+
107
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
108
+ UPDATE core_meta SET value = '422'
109
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 422;