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

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 (184) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/accounts-UHYLCLEY.js +45 -0
  4. package/dist/blocks-5IFU25J6.js +233 -0
  5. package/dist/chunk-2ABSVMGB.js +13 -0
  6. package/dist/chunk-2IVEVPMY.js +669 -0
  7. package/dist/chunk-2TWV3SVB.js +349 -0
  8. package/dist/chunk-2XC5JLBO.js +1931 -0
  9. package/dist/chunk-324WJXNP.js +320 -0
  10. package/dist/chunk-5KSXSK7Y.js +47 -0
  11. package/dist/chunk-6ELZRNUU.js +116 -0
  12. package/dist/chunk-6UWRT4VM.js +220 -0
  13. package/dist/chunk-6WGFA2IV.js +643 -0
  14. package/dist/chunk-6XTU2MTP.js +11278 -0
  15. package/dist/chunk-7CQGMVDO.js +63 -0
  16. package/dist/chunk-7D4GKZOX.js +615 -0
  17. package/dist/chunk-7OGCMQXG.js +284 -0
  18. package/dist/chunk-DHS2CV2M.js +139 -0
  19. package/dist/chunk-DU6OIFOU.js +2358 -0
  20. package/dist/chunk-E4WCYTCT.js +347 -0
  21. package/dist/chunk-E64SG24J.js +88 -0
  22. package/dist/chunk-FJIZGS5Q.js +698 -0
  23. package/dist/chunk-GYTEW66E.js +50 -0
  24. package/dist/chunk-HDIOVFAZ.js +544 -0
  25. package/dist/chunk-K34UKWEI.js +298 -0
  26. package/dist/chunk-KGHA67HG.js +765 -0
  27. package/dist/chunk-LAAL4YGR.js +365 -0
  28. package/dist/chunk-LO6WGU5Y.js +26189 -0
  29. package/dist/chunk-MBIKVRAC.js +1425 -0
  30. package/dist/chunk-P6UWZ6QQ.js +426 -0
  31. package/dist/chunk-PDD2ETVF.js +1302 -0
  32. package/dist/chunk-UB4I2QKR.js +31575 -0
  33. package/dist/chunk-WHKJGF36.js +2418 -0
  34. package/dist/chunk-XIDLW3EP.js +15471 -0
  35. package/dist/chunk-YFTLIEWU.js +504 -0
  36. package/dist/chunk-YNUZRTPH.js +182 -0
  37. package/dist/chunk-ZAWES2UD.js +9364 -0
  38. package/dist/chunk-ZZFWMVJO.js +704 -0
  39. package/dist/cli-5UNY5G5L.js +136 -0
  40. package/dist/cli-UYZMVQMR.js +24534 -0
  41. package/dist/cli.js +25 -0
  42. package/dist/client-repo-SE4EKG6W.js +55 -0
  43. package/dist/create.js +25 -0
  44. package/dist/fields-FTWWMD7N.js +50 -0
  45. package/dist/grants-LOW6TSWD.js +32 -0
  46. package/dist/invoke-6KVT6WJI.js +41 -0
  47. package/dist/moves-E2HKYCTK.js +41 -0
  48. package/dist/order-U4KZLLSJ.js +19 -0
  49. package/dist/person-UHLVBVLR.js +67 -0
  50. package/dist/plan-on-sale-TJPFCNL7.js +26 -0
  51. package/dist/platform/migrations/0001_init.sql +59 -0
  52. package/dist/platform/migrations/0002_users_settings.sql +34 -0
  53. package/dist/platform/migrations/0003_people_organisations.sql +225 -0
  54. package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
  55. package/dist/platform/migrations/0005_views_audit.sql +79 -0
  56. package/dist/platform/migrations/0006_search.sql +223 -0
  57. package/dist/platform/migrations/0007_crm_views.sql +141 -0
  58. package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
  59. package/dist/platform/migrations/0010_approval_queue.sql +90 -0
  60. package/dist/platform/migrations/0011_connected_apps.sql +36 -0
  61. package/dist/platform/migrations/0020_extensions.sql +373 -0
  62. package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
  63. package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
  64. package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
  65. package/dist/platform/migrations/0061_audit_retention.sql +95 -0
  66. package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
  67. package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
  68. package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
  69. package/dist/platform/migrations/0065_storage_stats.sql +13 -0
  70. package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
  71. package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
  72. package/dist/platform/migrations/0100_stage_history.sql +54 -0
  73. package/dist/platform/migrations/0110_integrations.sql +103 -0
  74. package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
  75. package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
  76. package/dist/platform/migrations/0130_automations.sql +113 -0
  77. package/dist/platform/migrations/0131_notifications.sql +29 -0
  78. package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
  79. package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
  80. package/dist/platform/migrations/0160_releases.sql +41 -0
  81. package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
  82. package/dist/platform/migrations/0170_automation_connections.sql +32 -0
  83. package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
  84. package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
  85. package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
  86. package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
  87. package/dist/platform/migrations/0210_daily_digest.sql +33 -0
  88. package/dist/platform/migrations/0220_entitlements.sql +13 -0
  89. package/dist/platform/migrations/0221_support_grants.sql +33 -0
  90. package/dist/platform/migrations/0250_judge.sql +11 -0
  91. package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
  92. package/dist/platform/migrations/0252_judge_usage.sql +14 -0
  93. package/dist/platform/migrations/0255_summary_checks.sql +9 -0
  94. package/dist/platform/migrations/0256_outbound_check.sql +9 -0
  95. package/dist/platform/migrations/0260_ai_usage.sql +20 -0
  96. package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
  97. package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
  98. package/dist/platform/migrations/0320_duplicates.sql +41 -0
  99. package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
  100. package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
  101. package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
  102. package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
  103. package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
  104. package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
  105. package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
  106. package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
  107. package/dist/platform/migrations/0362_voice_usage.sql +17 -0
  108. package/dist/platform/migrations/0365_routines.sql +38 -0
  109. package/dist/platform/migrations/0370_approval_execution.sql +20 -0
  110. package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
  111. package/dist/platform/migrations/0380_rec_views.sql +94 -0
  112. package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
  113. package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
  114. package/dist/platform/migrations/0402_erase_requests.sql +34 -0
  115. package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
  116. package/dist/platform/migrations/0411_device_sessions.sql +33 -0
  117. package/dist/platform/migrations/0420_transition_history.sql +100 -0
  118. package/dist/platform/migrations/0421_orders.sql +134 -0
  119. package/dist/platform/migrations/0422_order_payments.sql +109 -0
  120. package/dist/platform/migrations/0440_capacity.sql +100 -0
  121. package/dist/platform/migrations/0441_capacity_views.sql +40 -0
  122. package/dist/platform/migrations/0450_order_grants.sql +21 -0
  123. package/dist/platform/migrations/0451_pack_roles.sql +18 -0
  124. package/dist/platform/migrations/0460_content.sql +59 -0
  125. package/dist/platform/migrations/0461_media.sql +52 -0
  126. package/dist/platform/migrations/0470_publish_versions.sql +36 -0
  127. package/dist/platform/migrations/0471_permission_grants.sql +18 -0
  128. package/dist/platform/migrations/0490_staff_invites.sql +40 -0
  129. package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
  130. package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
  131. package/dist/platform/migrations/0500_published_media_field.sql +25 -0
  132. package/dist/platform/migrations/0501_import_keys.sql +23 -0
  133. package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
  134. package/dist/platform/migrations/0516_order_tax.sql +63 -0
  135. package/dist/platform/migrations/0520_publish_requests.sql +20 -0
  136. package/dist/platform/migrations/0530_subscriptions.sql +173 -0
  137. package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
  138. package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
  139. package/dist/platform/migrations/0533_allowances.sql +262 -0
  140. package/dist/platform/migrations/0560_recurring.sql +75 -0
  141. package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
  142. package/dist/platform/migrations/0581_once_per_person.sql +34 -0
  143. package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
  144. package/dist/platform/migrations/0610_waitlists.sql +54 -0
  145. package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
  146. package/dist/platform/migrations/0650_person_links.sql +90 -0
  147. package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
  148. package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
  149. package/dist/platform/sdk/allowance.ts +522 -0
  150. package/dist/platform/sdk/automation-check.ts +534 -0
  151. package/dist/platform/sdk/automation.ts +743 -0
  152. package/dist/platform/sdk/behaviour.ts +215 -0
  153. package/dist/platform/sdk/capacity.ts +303 -0
  154. package/dist/platform/sdk/contract.ts +909 -0
  155. package/dist/platform/sdk/document.ts +935 -0
  156. package/dist/platform/sdk/field-types.ts +111 -0
  157. package/dist/platform/sdk/formula/ast.ts +72 -0
  158. package/dist/platform/sdk/formula/compile.ts +454 -0
  159. package/dist/platform/sdk/formula/index.ts +4 -0
  160. package/dist/platform/sdk/formula/parser.ts +417 -0
  161. package/dist/platform/sdk/index.ts +36 -0
  162. package/dist/platform/sdk/manifest.ts +1785 -0
  163. package/dist/platform/sdk/media.ts +108 -0
  164. package/dist/platform/sdk/permissions.ts +60 -0
  165. package/dist/platform/sdk/recurring.ts +321 -0
  166. package/dist/platform/sdk/renamed.ts +63 -0
  167. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  168. package/dist/platform/sdk/rich-text.ts +221 -0
  169. package/dist/platform/sdk/runtime.ts +410 -0
  170. package/dist/platform/sdk/sellable.ts +1138 -0
  171. package/dist/platform/sdk/slug.ts +25 -0
  172. package/dist/platform/sdk/states.ts +598 -0
  173. package/dist/platform/sdk/testing.ts +847 -0
  174. package/dist/platform/sdk/unique.ts +81 -0
  175. package/dist/products-2H5DR232.js +11 -0
  176. package/dist/run-7P4RQALH.js +648 -0
  177. package/dist/run-PO3W5BFA.js +508 -0
  178. package/dist/run-ZCGFMEZF.js +309 -0
  179. package/dist/src-CRO35DYY.js +46 -0
  180. package/dist/src-I3IJ4NKC.js +354 -0
  181. package/dist/token-MOJUA2U6.js +70 -0
  182. package/dist/token-util-TENRN4V7.js +6 -0
  183. package/dist/visibility-TVXZ67FD.js +41 -0
  184. package/package.json +33 -4
@@ -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;
@@ -0,0 +1,100 @@
1
+ -- Migration 0440: capacity counters and claims (stage 26, the `capacity` behaviour, ADR 0430).
2
+ --
3
+ -- A record of an object that declares `capacity` has one counter row: how many places it has
4
+ -- (NULL is unlimited), how many are held while someone pays and how many are taken. The record's
5
+ -- own number field is what staff edit; this row is the host's. A record may draw on one parent's
6
+ -- counter as well (its pool: a ticket type's event), named on the counter row. One level only.
7
+ --
8
+ -- The rule is in the database, not in a read before the write: a claim is a plain
9
+ -- `held = held + n` on the record's counter and on its pool's, in the same D1 batch as the write
10
+ -- that needs it. If either would go past its places, the statement fails, and with it the whole
11
+ -- batch (a batch is one transaction): the order or booking it belonged to is not created either.
12
+ -- D1 runs one statement at a time per database, so concurrent claims cannot both pass.
13
+ --
14
+ -- * core_capacity_within_places (CHECK) is the guarantee: no row can ever hold more than its
15
+ -- places, whatever writes it.
16
+ -- * core_capacity_refuse_* (BEFORE triggers, same condition) refuse first, with a message
17
+ -- carrying the numbers, so the host can say which record refused, what was asked and what was
18
+ -- left without reading anything (errors.ts parses `core_capacity_refused {json}`).
19
+ CREATE TABLE core_capacity (
20
+ -- The record's object (as in core_stage_history.object_type) and id.
21
+ object_type TEXT NOT NULL,
22
+ record_id TEXT NOT NULL,
23
+ -- How many places there are. NULL: unlimited (claims are still counted).
24
+ places INTEGER CHECK (places IS NULL OR places >= 0),
25
+ held INTEGER NOT NULL DEFAULT 0 CHECK (held >= 0),
26
+ taken INTEGER NOT NULL DEFAULT 0 CHECK (taken >= 0),
27
+ -- The parent counter this record's claims also draw on, or none.
28
+ pool_object_type TEXT,
29
+ pool_record_id TEXT,
30
+ updated_at TEXT NOT NULL,
31
+ PRIMARY KEY (object_type, record_id),
32
+ CONSTRAINT core_capacity_within_places CHECK (places IS NULL OR held + taken <= places),
33
+ CHECK ((pool_object_type IS NULL) = (pool_record_id IS NULL)),
34
+ CHECK (pool_object_type IS NULL OR NOT (pool_object_type = object_type AND pool_record_id = record_id)),
35
+ -- A pool deleted for good (its object's table dropped) leaves its records without one.
36
+ FOREIGN KEY (pool_object_type, pool_record_id) REFERENCES core_capacity (object_type, record_id)
37
+ ON DELETE SET NULL
38
+ ) WITHOUT ROWID;
39
+ CREATE INDEX core_capacity_pool_idx ON core_capacity (pool_object_type, pool_record_id);
40
+
41
+ -- A claim: places taken from a counter (and its pool) for a reason. `held` while paying,
42
+ -- `confirmed` once paid, `released` when given back. A hold has no clock of its own: what it was
43
+ -- taken for (`for_type`, `for_id`: an order line, later a booking) confirms or releases it in the
44
+ -- batch of its own write. The pool it drew on is copied from the counter when the claim is made,
45
+ -- so giving it back returns the places to the same pool.
46
+ CREATE TABLE core_capacity_claim (
47
+ -- clm_… (ids.ts).
48
+ id TEXT PRIMARY KEY NOT NULL,
49
+ object_type TEXT NOT NULL,
50
+ record_id TEXT NOT NULL,
51
+ pool_object_type TEXT,
52
+ pool_record_id TEXT,
53
+ quantity INTEGER NOT NULL CHECK (quantity > 0),
54
+ state TEXT NOT NULL DEFAULT 'held' CHECK (state IN ('held', 'confirmed', 'released')),
55
+ for_type TEXT NOT NULL,
56
+ for_id TEXT NOT NULL,
57
+ created_at TEXT NOT NULL,
58
+ updated_at TEXT NOT NULL,
59
+ confirmed_at TEXT,
60
+ released_at TEXT,
61
+ -- A claim names a counter that exists: claiming on a record without one fails the batch.
62
+ FOREIGN KEY (object_type, record_id) REFERENCES core_capacity (object_type, record_id),
63
+ CHECK ((pool_object_type IS NULL) = (pool_record_id IS NULL))
64
+ );
65
+ CREATE INDEX core_capacity_claim_record_idx ON core_capacity_claim (object_type, record_id, state);
66
+ CREATE INDEX core_capacity_claim_for_idx ON core_capacity_claim (for_type, for_id);
67
+
68
+ -- Refuses a claim (held or taken going up) past the places, or places set below what is claimed,
69
+ -- with the numbers. Same condition as core_capacity_within_places.
70
+ CREATE TRIGGER core_capacity_refuse_update
71
+ BEFORE UPDATE OF places, held, taken ON core_capacity
72
+ WHEN NEW.places IS NOT NULL AND NEW.held + NEW.taken > NEW.places
73
+ BEGIN
74
+ SELECT RAISE(ABORT, 'core_capacity_refused ' || json_object(
75
+ 'object', NEW.object_type,
76
+ 'record', NEW.record_id,
77
+ 'places', NEW.places,
78
+ 'held', OLD.held,
79
+ 'taken', OLD.taken,
80
+ 'asked', (NEW.held + NEW.taken) - (OLD.held + OLD.taken),
81
+ 'available', max(coalesce(OLD.places, NEW.places) - OLD.held - OLD.taken, 0)
82
+ ));
83
+ END;
84
+
85
+ CREATE TRIGGER core_capacity_refuse_insert
86
+ BEFORE INSERT ON core_capacity
87
+ WHEN NEW.places IS NOT NULL AND NEW.held + NEW.taken > NEW.places
88
+ BEGIN
89
+ SELECT RAISE(ABORT, 'core_capacity_refused ' || json_object(
90
+ 'object', NEW.object_type,
91
+ 'record', NEW.record_id,
92
+ 'places', NEW.places,
93
+ 'held', NEW.held,
94
+ 'taken', NEW.taken,
95
+ 'asked', 0,
96
+ 'available', 0
97
+ ));
98
+ END;
99
+
100
+ UPDATE core_meta SET value = '440' WHERE key = 'schema_version';
@@ -0,0 +1,40 @@
1
+ -- Migration 0441: the capacity read views (stage 26, ADR 0431), for query.sql and dashboards.
2
+ --
3
+ -- rec_capacity: one row per record with a capacity. `available` is the record's own places left
4
+ -- (null when unlimited); a record with a pool may have fewer, see `pool_available`.
5
+ -- rec_capacity_claim: every claim, held, confirmed or released, and what it was taken for.
6
+ CREATE VIEW rec_capacity AS
7
+ SELECT
8
+ c.object_type,
9
+ c.record_id,
10
+ c.places,
11
+ c.held,
12
+ c.taken,
13
+ CASE WHEN c.places IS NULL THEN NULL ELSE max(c.places - c.held - c.taken, 0) END AS available,
14
+ c.pool_object_type,
15
+ c.pool_record_id,
16
+ CASE WHEN p.places IS NULL THEN NULL ELSE max(p.places - p.held - p.taken, 0) END
17
+ AS pool_available,
18
+ c.updated_at
19
+ FROM core_capacity c
20
+ LEFT JOIN core_capacity p
21
+ ON p.object_type = c.pool_object_type AND p.record_id = c.pool_record_id;
22
+
23
+ CREATE VIEW rec_capacity_claim AS
24
+ SELECT
25
+ id,
26
+ object_type,
27
+ record_id,
28
+ pool_object_type,
29
+ pool_record_id,
30
+ quantity,
31
+ state,
32
+ for_type,
33
+ for_id,
34
+ created_at,
35
+ updated_at,
36
+ confirmed_at,
37
+ released_at
38
+ FROM core_capacity_claim;
39
+
40
+ UPDATE core_meta SET value = '441' WHERE key = 'schema_version';
@@ -0,0 +1,21 @@
1
+ -- Stage 27, the sellable behaviour (ADR 0435): what a paid order line granted.
2
+ --
3
+ -- When an order is first paid (the move to `paid`, or to `part_paid` for a deposit), the host
4
+ -- creates one record of the line's declared granted object per unit (a ticket per place), in the
5
+ -- batch that makes the move, and one row here for each. The row is how the host finds them again
6
+ -- (to void them when the order is refunded in full or cancelled) and, when the grant asks for a
7
+ -- code, how a code is kept unique across every granted record and found by its code.
8
+ --
9
+ -- Only the move hook in packages/core/src/orders/grants.ts writes here.
10
+ CREATE TABLE core_order_grant (
11
+ -- The granted record: its object's registry name and its id.
12
+ object_type TEXT NOT NULL,
13
+ record_id TEXT NOT NULL,
14
+ order_id TEXT NOT NULL REFERENCES core_order (id) ON DELETE CASCADE,
15
+ line_id TEXT NOT NULL REFERENCES core_order_line (id) ON DELETE CASCADE,
16
+ -- The host's unguessable code, when the grant declares one. Two equal codes fail the batch.
17
+ code TEXT UNIQUE,
18
+ created_at TEXT NOT NULL,
19
+ PRIMARY KEY (object_type, record_id)
20
+ );
21
+ CREATE INDEX core_order_grant_order_idx ON core_order_grant (order_id);
@@ -0,0 +1,18 @@
1
+ -- Stage 27, the door role (ADR 0436): a staff account may hold one fixed role a pack declares,
2
+ -- such as Ticketing's door role (`ticketing.door`: check tickets in, read events, ticket types and
3
+ -- tickets, and nothing else).
4
+ --
5
+ -- `pack_role` names the role as `<extension id>.<role>`. While it is set, the account may do
6
+ -- exactly what the role lists, whatever its `role` says: the operations pipeline reads this
7
+ -- column on every call by a staff account and refuses everything else. The role's definition is
8
+ -- in the installed extension's manifest; if the extension is not installed and active, the
9
+ -- account may do nothing. Only `user.setPackRole` (a human admin) writes it, and it sets `role`
10
+ -- to `viewer` in the same write, so code that reads `role` alone sees the least it could.
11
+ ALTER TABLE core_user ADD COLUMN pack_role TEXT;
12
+
13
+ -- The list of users shows the role an account holds.
14
+ DROP VIEW rec_user;
15
+ CREATE VIEW rec_user AS
16
+ SELECT u.id, u.name, u.email, u.role, u.pack_role, u.image, u.created_at
17
+ FROM core_user u
18
+ WHERE u.kind = 'human' AND u.support_grant_id IS NULL;
@@ -0,0 +1,59 @@
1
+ -- Stage 28, Content (ADRs 0441 to 0443): the public half of `publishable`.
2
+ --
3
+ -- A scheduled publish. Scheduling takes the snapshot when a person schedules (it is what they
4
+ -- decided to publish), and the minute cron publishes it when `publish_at` passes, through the
5
+ -- operations pipeline (record.publishDue). At most one per record: scheduling again replaces it;
6
+ -- publishing now, unpublishing, or the cron publishing it removes it. Only
7
+ -- packages/core/src/records/publish.ts writes here.
8
+ CREATE TABLE core_publish_schedule (
9
+ object_type TEXT NOT NULL,
10
+ record_id TEXT NOT NULL,
11
+ publish_at TEXT NOT NULL,
12
+ -- The content fields' values when it was scheduled, as record.publish takes them.
13
+ content TEXT NOT NULL,
14
+ bytes INTEGER NOT NULL,
15
+ note TEXT CHECK (note IS NULL OR length(note) <= 500),
16
+ -- The person who scheduled it: the cron publishes in their name.
17
+ scheduled_by TEXT NOT NULL,
18
+ scheduled_at TEXT NOT NULL,
19
+ PRIMARY KEY (object_type, record_id)
20
+ );
21
+ CREATE INDEX core_publish_schedule_due_idx ON core_publish_schedule (publish_at);
22
+
23
+ -- An old address of a published record (ADR 0442). When the slug of a record that has been
24
+ -- published changes, its old slug points here to the record, and the public is told where it
25
+ -- now is. A record that takes a slug removes any redirect of that slug, so a live slug always
26
+ -- wins and a deleted record's slug can be used again.
27
+ CREATE TABLE core_slug_redirect (
28
+ object_type TEXT NOT NULL,
29
+ slug TEXT NOT NULL,
30
+ record_id TEXT NOT NULL,
31
+ created_at TEXT NOT NULL,
32
+ PRIMARY KEY (object_type, slug)
33
+ );
34
+ CREATE INDEX core_slug_redirect_record_idx ON core_slug_redirect (object_type, record_id);
35
+
36
+ -- A preview link (ADR 0443): who may read one record's working copy without signing in, until
37
+ -- when. The link holds a random token; only its SHA-256 is kept. It names one record and is
38
+ -- refused for any other, and after `expires_at`.
39
+ CREATE TABLE core_preview_link (
40
+ token_hash TEXT PRIMARY KEY NOT NULL,
41
+ object_type TEXT NOT NULL,
42
+ record_id TEXT NOT NULL,
43
+ expires_at TEXT NOT NULL,
44
+ created_by TEXT NOT NULL,
45
+ created_at TEXT NOT NULL
46
+ );
47
+ CREATE INDEX core_preview_link_expiry_idx ON core_preview_link (expires_at);
48
+
49
+ -- What the public reads of an object, by generation (ADR 0442): every committed change to a
50
+ -- record of a publishable object the public reads bumps its row in the change's own batch, so a
51
+ -- cached public read keyed by the generation is never served after a publish, an unpublish or a
52
+ -- slug change. `epoch` is random per deployment and object, so two deployments' caches never
53
+ -- share a key.
54
+ CREATE TABLE core_content_generation (
55
+ object_type TEXT PRIMARY KEY NOT NULL,
56
+ generation INTEGER NOT NULL DEFAULT 1,
57
+ epoch TEXT NOT NULL,
58
+ changed_at TEXT NOT NULL
59
+ );
@@ -0,0 +1,52 @@
1
+ -- Stage 28, Media (ADR 0444): the media library, a base record.
2
+ --
3
+ -- A media item is one stored original in R2 (`key`, under `media/`), what the host found it to
4
+ -- be when it was uploaded (its type from its first bytes, never the name or the client's word;
5
+ -- its size and dimensions), its alt text and who uploaded it. Nothing executable is stored: the
6
+ -- upload takes only the raster image types it can recognise (no SVG).
7
+ --
8
+ -- Deleting one is a soft delete (Trash). The Trash purge deletes the row, and the trigger below
9
+ -- queues its R2 object on core_blob_deletion in the same batch: the existing blob deletion path
10
+ -- (ADR 0320) deletes it after the commit, and the 10-minute cron retries.
11
+ CREATE TABLE core_media (
12
+ id TEXT PRIMARY KEY NOT NULL,
13
+ key TEXT NOT NULL UNIQUE,
14
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 200),
15
+ type TEXT NOT NULL CHECK (type IN ('image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif')),
16
+ bytes INTEGER NOT NULL CHECK (bytes > 0),
17
+ width INTEGER,
18
+ height INTEGER,
19
+ alt TEXT CHECK (alt IS NULL OR length(alt) <= 500),
20
+ created_at TEXT NOT NULL,
21
+ created_by TEXT,
22
+ updated_at TEXT NOT NULL,
23
+ updated_by TEXT,
24
+ deleted_at TEXT,
25
+ deleted_by TEXT
26
+ );
27
+ CREATE INDEX core_media_live_idx ON core_media (created_at) WHERE deleted_at IS NULL;
28
+ CREATE INDEX core_media_deleted_idx ON core_media (deleted_at) WHERE deleted_at IS NOT NULL;
29
+
30
+ CREATE TRIGGER core_media_object_ad AFTER DELETE ON core_media
31
+ BEGIN
32
+ INSERT OR IGNORE INTO core_blob_deletion (key, queued_at)
33
+ VALUES (OLD.key, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'));
34
+ END;
35
+
36
+ -- The live media items, as staff list them. Outsiders read none of it: a picture reaches the
37
+ -- public only through media.serve, for an item published content references.
38
+ CREATE VIEW rec_media AS
39
+ SELECT id, key, name, type, bytes, width, height, alt, created_at, created_by, updated_at,
40
+ updated_by
41
+ FROM core_media WHERE deleted_at IS NULL;
42
+
43
+ -- Which media items each record's current version references (ADR 0444): written with the
44
+ -- version by records/publish.ts (a publish replaces a record's rows; an unpublish removes them),
45
+ -- so serving a picture to the public is an index read and never a scan of versions.
46
+ CREATE TABLE core_published_media (
47
+ media_id TEXT NOT NULL,
48
+ object_type TEXT NOT NULL,
49
+ record_id TEXT NOT NULL,
50
+ PRIMARY KEY (media_id, object_type, record_id)
51
+ ) WITHOUT ROWID;
52
+ CREATE INDEX core_published_media_record_idx ON core_published_media (object_type, record_id);