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

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 (176) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/accounts-X4CKM3HL.js +46 -0
  4. package/dist/blocks-YWDHEBOK.js +233 -0
  5. package/dist/chunk-2ABSVMGB.js +13 -0
  6. package/dist/chunk-5KSXSK7Y.js +47 -0
  7. package/dist/chunk-6GDEDOV7.js +157 -0
  8. package/dist/chunk-6UPUF4ML.js +29998 -0
  9. package/dist/chunk-6UWRT4VM.js +220 -0
  10. package/dist/chunk-6VXHPKGG.js +1302 -0
  11. package/dist/chunk-7CQGMVDO.js +63 -0
  12. package/dist/chunk-A4NTSYWV.js +11092 -0
  13. package/dist/chunk-CP6BXI6F.js +25825 -0
  14. package/dist/chunk-DU6OIFOU.js +2358 -0
  15. package/dist/chunk-E4WCYTCT.js +347 -0
  16. package/dist/chunk-GYSGVT6R.js +284 -0
  17. package/dist/chunk-GYTEW66E.js +50 -0
  18. package/dist/chunk-HDIOVFAZ.js +544 -0
  19. package/dist/chunk-IFG4FRW5.js +504 -0
  20. package/dist/chunk-JJHHNV46.js +227 -0
  21. package/dist/chunk-K6T42YCH.js +704 -0
  22. package/dist/chunk-KGHA67HG.js +765 -0
  23. package/dist/chunk-LJFLENFC.js +88 -0
  24. package/dist/chunk-LLULPZAJ.js +365 -0
  25. package/dist/chunk-M7A6FR2Z.js +2586 -0
  26. package/dist/chunk-O2UIZHND.js +249 -0
  27. package/dist/chunk-Q4N55M2Z.js +552 -0
  28. package/dist/chunk-QGGJ2G6Z.js +349 -0
  29. package/dist/chunk-R4W5XAUD.js +837 -0
  30. package/dist/chunk-TQFGGSA4.js +748 -0
  31. package/dist/chunk-VY64DMQO.js +15302 -0
  32. package/dist/chunk-WC5LHBBA.js +426 -0
  33. package/dist/chunk-WO6D3XP3.js +9351 -0
  34. package/dist/chunk-Y7JZFSTQ.js +1425 -0
  35. package/dist/chunk-YJJHF65I.js +759 -0
  36. package/dist/chunk-Z3RMMB6S.js +668 -0
  37. package/dist/cli-2DFENR62.js +134 -0
  38. package/dist/cli-YIOVSKOD.js +24531 -0
  39. package/dist/cli.js +25 -0
  40. package/dist/client-repo-PRU3VCQQ.js +55 -0
  41. package/dist/create.js +25 -0
  42. package/dist/fields-2NC7LSYE.js +50 -0
  43. package/dist/grants-QXMKYMGB.js +32 -0
  44. package/dist/invoke-I3NNNXVL.js +39 -0
  45. package/dist/order-PUWD65BC.js +19 -0
  46. package/dist/person-WIQQ7ITU.js +68 -0
  47. package/dist/plan-on-sale-NB774CQL.js +26 -0
  48. package/dist/platform/migrations/0001_init.sql +59 -0
  49. package/dist/platform/migrations/0002_users_settings.sql +34 -0
  50. package/dist/platform/migrations/0003_people_organisations.sql +225 -0
  51. package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
  52. package/dist/platform/migrations/0005_views_audit.sql +79 -0
  53. package/dist/platform/migrations/0006_search.sql +223 -0
  54. package/dist/platform/migrations/0007_crm_views.sql +141 -0
  55. package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
  56. package/dist/platform/migrations/0010_approval_queue.sql +90 -0
  57. package/dist/platform/migrations/0011_connected_apps.sql +36 -0
  58. package/dist/platform/migrations/0020_extensions.sql +373 -0
  59. package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
  60. package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
  61. package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
  62. package/dist/platform/migrations/0061_audit_retention.sql +95 -0
  63. package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
  64. package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
  65. package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
  66. package/dist/platform/migrations/0065_storage_stats.sql +13 -0
  67. package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
  68. package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
  69. package/dist/platform/migrations/0100_stage_history.sql +54 -0
  70. package/dist/platform/migrations/0110_integrations.sql +103 -0
  71. package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
  72. package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
  73. package/dist/platform/migrations/0130_automations.sql +113 -0
  74. package/dist/platform/migrations/0131_notifications.sql +29 -0
  75. package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
  76. package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
  77. package/dist/platform/migrations/0160_releases.sql +41 -0
  78. package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
  79. package/dist/platform/migrations/0170_automation_connections.sql +32 -0
  80. package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
  81. package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
  82. package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
  83. package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
  84. package/dist/platform/migrations/0210_daily_digest.sql +33 -0
  85. package/dist/platform/migrations/0220_entitlements.sql +13 -0
  86. package/dist/platform/migrations/0221_support_grants.sql +33 -0
  87. package/dist/platform/migrations/0250_judge.sql +11 -0
  88. package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
  89. package/dist/platform/migrations/0252_judge_usage.sql +14 -0
  90. package/dist/platform/migrations/0255_summary_checks.sql +9 -0
  91. package/dist/platform/migrations/0256_outbound_check.sql +9 -0
  92. package/dist/platform/migrations/0260_ai_usage.sql +20 -0
  93. package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
  94. package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
  95. package/dist/platform/migrations/0320_duplicates.sql +41 -0
  96. package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
  97. package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
  98. package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
  99. package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
  100. package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
  101. package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
  102. package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
  103. package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
  104. package/dist/platform/migrations/0362_voice_usage.sql +17 -0
  105. package/dist/platform/migrations/0365_routines.sql +38 -0
  106. package/dist/platform/migrations/0370_approval_execution.sql +20 -0
  107. package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
  108. package/dist/platform/migrations/0380_rec_views.sql +94 -0
  109. package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
  110. package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
  111. package/dist/platform/migrations/0402_erase_requests.sql +34 -0
  112. package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
  113. package/dist/platform/migrations/0411_device_sessions.sql +33 -0
  114. package/dist/platform/migrations/0420_transition_history.sql +100 -0
  115. package/dist/platform/migrations/0421_orders.sql +134 -0
  116. package/dist/platform/migrations/0422_order_payments.sql +109 -0
  117. package/dist/platform/migrations/0440_capacity.sql +100 -0
  118. package/dist/platform/migrations/0441_capacity_views.sql +40 -0
  119. package/dist/platform/migrations/0450_order_grants.sql +21 -0
  120. package/dist/platform/migrations/0451_pack_roles.sql +18 -0
  121. package/dist/platform/migrations/0460_content.sql +59 -0
  122. package/dist/platform/migrations/0461_media.sql +52 -0
  123. package/dist/platform/migrations/0470_publish_versions.sql +36 -0
  124. package/dist/platform/migrations/0471_permission_grants.sql +18 -0
  125. package/dist/platform/migrations/0490_staff_invites.sql +40 -0
  126. package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
  127. package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
  128. package/dist/platform/migrations/0500_published_media_field.sql +25 -0
  129. package/dist/platform/migrations/0501_import_keys.sql +23 -0
  130. package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
  131. package/dist/platform/migrations/0516_order_tax.sql +63 -0
  132. package/dist/platform/migrations/0520_publish_requests.sql +20 -0
  133. package/dist/platform/migrations/0530_subscriptions.sql +173 -0
  134. package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
  135. package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
  136. package/dist/platform/migrations/0533_allowances.sql +262 -0
  137. package/dist/platform/migrations/0560_recurring.sql +75 -0
  138. package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
  139. package/dist/platform/migrations/0581_once_per_person.sql +34 -0
  140. package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
  141. package/dist/platform/sdk/allowance.ts +522 -0
  142. package/dist/platform/sdk/automation-check.ts +534 -0
  143. package/dist/platform/sdk/automation.ts +743 -0
  144. package/dist/platform/sdk/behaviour.ts +215 -0
  145. package/dist/platform/sdk/capacity.ts +201 -0
  146. package/dist/platform/sdk/contract.ts +884 -0
  147. package/dist/platform/sdk/document.ts +935 -0
  148. package/dist/platform/sdk/field-types.ts +111 -0
  149. package/dist/platform/sdk/formula/ast.ts +72 -0
  150. package/dist/platform/sdk/formula/compile.ts +454 -0
  151. package/dist/platform/sdk/formula/index.ts +4 -0
  152. package/dist/platform/sdk/formula/parser.ts +417 -0
  153. package/dist/platform/sdk/index.ts +36 -0
  154. package/dist/platform/sdk/manifest.ts +1709 -0
  155. package/dist/platform/sdk/media.ts +108 -0
  156. package/dist/platform/sdk/permissions.ts +60 -0
  157. package/dist/platform/sdk/recurring.ts +321 -0
  158. package/dist/platform/sdk/renamed.ts +63 -0
  159. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  160. package/dist/platform/sdk/rich-text.ts +221 -0
  161. package/dist/platform/sdk/runtime.ts +410 -0
  162. package/dist/platform/sdk/sellable.ts +888 -0
  163. package/dist/platform/sdk/slug.ts +25 -0
  164. package/dist/platform/sdk/states.ts +484 -0
  165. package/dist/platform/sdk/testing.ts +847 -0
  166. package/dist/platform/sdk/unique.ts +81 -0
  167. package/dist/products-2H5DR232.js +11 -0
  168. package/dist/run-PZUCSDDL.js +508 -0
  169. package/dist/run-RT2HVWMK.js +648 -0
  170. package/dist/run-Y7T4TVOL.js +309 -0
  171. package/dist/src-GYS6NBQB.js +44 -0
  172. package/dist/src-XTOD7EXW.js +348 -0
  173. package/dist/token-MOJUA2U6.js +70 -0
  174. package/dist/token-util-TENRN4V7.js +6 -0
  175. package/dist/visibility-DLGHU4YN.js +33 -0
  176. package/package.json +33 -4
@@ -0,0 +1,20 @@
1
+ -- An AI actor asks for a publish (ADR 0480, stage 28 criterion 12): its request waits for a
2
+ -- person, and the version history says who asked and who approved.
3
+ --
4
+ -- A version published by approving a request keeps the person who approved it in `published_by`
5
+ -- (or `scheduled_by`), and the request beside it: `requested_by`, the staff user the AI actor
6
+ -- acted for, and `requested_via`, the actor (assistant, mcp, automation, extension). A person's
7
+ -- own publish leaves both empty. A scheduled publish carries them to the version the minute cron
8
+ -- publishes. Only packages/core/src/records/publish.ts writes either table.
9
+ ALTER TABLE core_publish_version ADD COLUMN requested_by TEXT;
10
+ ALTER TABLE core_publish_version ADD COLUMN requested_via TEXT
11
+ CHECK (requested_via IS NULL OR requested_via IN ('human', 'assistant', 'mcp', 'automation', 'extension'));
12
+ ALTER TABLE core_publish_schedule ADD COLUMN requested_by TEXT;
13
+ ALTER TABLE core_publish_schedule ADD COLUMN requested_via TEXT
14
+ CHECK (requested_via IS NULL OR requested_via IN ('human', 'assistant', 'mcp', 'automation', 'extension'));
15
+
16
+ DROP VIEW rec_publish_version;
17
+ CREATE VIEW rec_publish_version AS
18
+ SELECT object_type, record_id, version, current, bytes, published_at, published_by, note,
19
+ requested_by, requested_via
20
+ FROM core_publish_version;
@@ -0,0 +1,173 @@
1
+ -- Migration 0530: subscriptions (stage 32a, ADRs 0485 to 0487).
2
+ --
3
+ -- The payment provider runs the schedule: it keeps the card, charges each period, retries and ends
4
+ -- the subscription. The host mirrors what the provider's verified events say (core_subscription),
5
+ -- and writes a paid order for every paid period (core_order's period columns). Nothing here
6
+ -- schedules or charges anything.
7
+
8
+ -- A subscription checkout the host opened: who it is for, the plan as priced then, and the trial.
9
+ -- The subscription itself exists only once the provider's verified event says it does; then
10
+ -- core_subscription is made with this row's id (the id the provider carries as metadata).
11
+ CREATE TABLE core_subscription_checkout (
12
+ -- sbs_… (ids.ts): the subscription's id to be.
13
+ id TEXT PRIMARY KEY NOT NULL,
14
+ provider TEXT NOT NULL,
15
+ -- The provider's checkout reference (a Stripe Checkout Session id).
16
+ ref TEXT NOT NULL,
17
+ -- Who pays (the provider's customer is theirs) and who holds what it grants. The same Person
18
+ -- in 32a; 32b lets them differ (a parent pays for a child, ADR 0487).
19
+ payer_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
20
+ holder_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
21
+ email TEXT CHECK (email IS NULL OR (email = lower(trim(email)) AND email <> '')),
22
+ placed_as TEXT NOT NULL CHECK (placed_as IN ('customer', 'public', 'staff')),
23
+ -- The plan: any record of an object that declares `sellable.recurring`.
24
+ plan_object TEXT NOT NULL,
25
+ plan_id TEXT NOT NULL,
26
+ plan_title TEXT NOT NULL,
27
+ -- One period's price as the host read it from the plan, minor units, tax included.
28
+ amount INTEGER NOT NULL CHECK (amount > 0),
29
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
30
+ interval TEXT NOT NULL CHECK (interval IN ('week', 'month', 'year')),
31
+ -- The free first period asked for: days, or until a moment (a continuation). At most one.
32
+ trial_days INTEGER CHECK (trial_days IS NULL OR trial_days > 0),
33
+ trial_end TEXT,
34
+ -- The product tax code sent, when tax was asked for (ADR 0479). Null: nothing about tax sent.
35
+ tax_code TEXT,
36
+ -- The provider's subscription it started, once its first verified event arrived.
37
+ subscription_ref TEXT,
38
+ created_at TEXT NOT NULL,
39
+ CHECK (trial_days IS NULL OR trial_end IS NULL)
40
+ );
41
+ CREATE UNIQUE INDEX core_subscription_checkout_ref_uq ON core_subscription_checkout (provider, ref);
42
+ CREATE INDEX core_subscription_checkout_holder_idx
43
+ ON core_subscription_checkout (holder_person_id, plan_object);
44
+
45
+ CREATE TABLE core_subscription (
46
+ -- sbs_…: the checkout's id.
47
+ id TEXT PRIMARY KEY NOT NULL,
48
+ -- The Person who pays: the provider's customer is theirs; the portal, cancel and resume are
49
+ -- theirs. Null after the person is erased.
50
+ payer_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
51
+ -- The Person who holds what it grants (access, 32b's allowances, the one-trial rule). The payer
52
+ -- in 32a (ADR 0487).
53
+ holder_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
54
+ email TEXT CHECK (email IS NULL OR (email = lower(trim(email)) AND email <> '')),
55
+ placed_as TEXT NOT NULL CHECK (placed_as IN ('customer', 'public', 'staff')),
56
+ plan_object TEXT NOT NULL,
57
+ plan_id TEXT NOT NULL,
58
+ -- The plan's title and price as they were at the start: a period is charged what was agreed.
59
+ plan_title TEXT NOT NULL,
60
+ amount INTEGER NOT NULL CHECK (amount > 0),
61
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
62
+ interval TEXT NOT NULL CHECK (interval IN ('week', 'month', 'year')),
63
+ tax_code TEXT,
64
+ -- The provider's state, mirrored: every move is the host's, from a verified event.
65
+ state TEXT NOT NULL DEFAULT 'incomplete'
66
+ CHECK (state IN ('incomplete', 'expired', 'trialing', 'active', 'past_due', 'unpaid', 'paused', 'ended')),
67
+ provider TEXT NOT NULL,
68
+ provider_ref TEXT NOT NULL,
69
+ customer_ref TEXT,
70
+ trial_end TEXT,
71
+ current_period_start TEXT,
72
+ current_period_end TEXT,
73
+ -- Set to end when the current period does (a flag on trialing or active, not a state).
74
+ cancel_at_period_end INTEGER NOT NULL DEFAULT 0 CHECK (cancel_at_period_end IN (0, 1)),
75
+ -- A date it is set to end on that was set another way (the provider's dashboard or a schedule):
76
+ -- the host's resume does not undo it (ADR 0485).
77
+ cancel_at TEXT,
78
+ canceled_at TEXT,
79
+ ended_at TEXT,
80
+ -- Why it ended or is ending, in the provider's words (cancellation_requested, payment_failed…).
81
+ end_reason TEXT,
82
+ -- The provider's page where the payer confirms a renewal (3D Secure), while one waits.
83
+ action_url TEXT,
84
+ -- When its renewal first failed, while it is failing.
85
+ failing_since TEXT,
86
+ -- The time of the provider read the row was last moved from: an older read changes nothing.
87
+ observed_at TEXT NOT NULL,
88
+ created_at TEXT NOT NULL,
89
+ updated_at TEXT NOT NULL,
90
+ version INTEGER NOT NULL DEFAULT 0
91
+ );
92
+ CREATE UNIQUE INDEX core_subscription_provider_ref_uq ON core_subscription (provider, provider_ref);
93
+ CREATE INDEX core_subscription_payer_idx ON core_subscription (payer_person_id, created_at);
94
+ CREATE INDEX core_subscription_holder_idx ON core_subscription (holder_person_id, plan_object, state);
95
+ CREATE INDEX core_subscription_failing_idx ON core_subscription (failing_since)
96
+ WHERE state IN ('past_due', 'unpaid');
97
+
98
+ -- A write made from an older read of the subscription fails, and its whole batch (as orders).
99
+ CREATE TRIGGER core_subscription_stale
100
+ BEFORE UPDATE OF state, cancel_at_period_end, current_period_end, observed_at ON core_subscription
101
+ WHEN NEW.version <> OLD.version + 1
102
+ BEGIN
103
+ SELECT RAISE(ABORT, 'core_subscription_stale');
104
+ END;
105
+
106
+ CREATE VIEW rec_subscription AS
107
+ SELECT
108
+ id, payer_person_id, holder_person_id, email, placed_as, plan_object, plan_id, plan_title,
109
+ amount, currency, interval, tax_code IS NOT NULL AS taxed, state, provider, provider_ref,
110
+ trial_end, current_period_start, current_period_end, cancel_at_period_end, cancel_at, canceled_at,
111
+ ended_at, end_reason, action_url, failing_since, created_at, updated_at
112
+ FROM core_subscription;
113
+
114
+ -- An order per paid period (ADR 0486): the subscription it is for, the period it paid, and the
115
+ -- provider's invoice. Null on every other order.
116
+ ALTER TABLE core_order ADD COLUMN subscription_id TEXT REFERENCES core_subscription (id) ON DELETE SET NULL;
117
+ ALTER TABLE core_order ADD COLUMN period_start TEXT;
118
+ ALTER TABLE core_order ADD COLUMN period_end TEXT;
119
+ ALTER TABLE core_order ADD COLUMN invoice_ref TEXT;
120
+ -- A period paid other than as agreed, recorded for what was actually paid and flagged for staff:
121
+ -- less (a coupon, the customer's balance), more, or on terms (currency, interval, price) that are
122
+ -- not the subscription's. Null: paid as agreed.
123
+ ALTER TABLE core_order ADD COLUMN period_flag TEXT
124
+ CHECK (period_flag IS NULL OR period_flag IN ('paid_less', 'paid_more', 'not_as_agreed'));
125
+ -- One order per invoice, whichever event or delivery reports it paid.
126
+ CREATE UNIQUE INDEX core_order_invoice_uq ON core_order (provider, invoice_ref)
127
+ WHERE invoice_ref IS NOT NULL;
128
+ CREATE INDEX core_order_subscription_idx ON core_order (subscription_id, period_start)
129
+ WHERE subscription_id IS NOT NULL;
130
+
131
+ -- The invoice a payment paid, so its refund is credited on that invoice.
132
+ ALTER TABLE core_order_payment ADD COLUMN invoice_ref TEXT;
133
+
134
+ -- A credit note's tax that arrived before its refund's confirmation waits on the refund asked
135
+ -- for (ADR 0486), and is applied when the confirmation comes.
136
+ ALTER TABLE core_order_refund ADD COLUMN reported_tax INTEGER CHECK (reported_tax IS NULL OR reported_tax >= 0);
137
+
138
+ -- core_payment_event gains the kind `subscription`: SQLite cannot change a CHECK, so the table is
139
+ -- rebuilt, its rows kept.
140
+ CREATE TABLE core_payment_event_new (
141
+ provider TEXT NOT NULL,
142
+ event_id TEXT NOT NULL,
143
+ kind TEXT NOT NULL CHECK (kind IN ('payment', 'refund', 'subscription')),
144
+ connection_id TEXT,
145
+ payload TEXT NOT NULL,
146
+ received_at TEXT NOT NULL,
147
+ applied_at TEXT,
148
+ outcome TEXT,
149
+ attempts INTEGER NOT NULL DEFAULT 0,
150
+ PRIMARY KEY (provider, event_id)
151
+ ) WITHOUT ROWID;
152
+ INSERT INTO core_payment_event_new
153
+ (provider, event_id, kind, connection_id, payload, received_at, applied_at, outcome, attempts)
154
+ SELECT provider, event_id, kind, connection_id, payload, received_at, applied_at, outcome, attempts
155
+ FROM core_payment_event;
156
+ DROP TABLE core_payment_event;
157
+ ALTER TABLE core_payment_event_new RENAME TO core_payment_event;
158
+ CREATE INDEX core_payment_event_unapplied_idx ON core_payment_event (received_at)
159
+ WHERE applied_at IS NULL;
160
+
161
+ DROP VIEW rec_order;
162
+ CREATE VIEW rec_order AS
163
+ SELECT
164
+ id, person_id, email, currency, total, paid, total - paid AS balance, refunded, state, pay,
165
+ deposit, balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at,
166
+ updated_at, tax_amount, tax_refunded, tax_country,
167
+ (paid - refunded) - (tax_amount - tax_refunded) AS net,
168
+ subscription_id, period_start, period_end, period_flag
169
+ FROM core_order;
170
+
171
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
172
+ UPDATE core_meta SET value = '530'
173
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 530;
@@ -0,0 +1,56 @@
1
+ -- Migration 0531: one free trial per Person per plan object (stage 32a, build step 3, ADR 0489).
2
+ --
3
+ -- A trial is given only to a holder whose identity is proved (a signed-in customer starting their
4
+ -- own subscription, or staff starting one for a Person): a guest who only typed an address never
5
+ -- gets one, so nobody can spend someone else's trial or learn whether they had one.
6
+ --
7
+ -- Each checkout the host opens with a trial holds the holder's trial for that plan object while
8
+ -- the provider's page can still be completed (`held_until`). The trial is used once the provider
9
+ -- confirms a subscription with the plan's trial on it (`used_at`, in the verified event's batch).
10
+ -- A hold that lapses gives the trial back only once the provider says its page expired
11
+ -- uncompleted (`released_at`; the host asks before giving the trial again). The trigger refuses a
12
+ -- second hold while another is unreleased or the trial is used, so two starts at once give one
13
+ -- trial: the second start's whole batch fails.
14
+ CREATE TABLE core_subscription_trial (
15
+ -- The checkout that holds it (core_subscription_checkout.id, the subscription's id to be).
16
+ checkout_id TEXT PRIMARY KEY NOT NULL
17
+ REFERENCES core_subscription_checkout (id) ON DELETE CASCADE,
18
+ holder_person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
19
+ -- The plan object the trial is for: one trial per Person across every record of it.
20
+ plan_object TEXT NOT NULL,
21
+ plan_id TEXT NOT NULL,
22
+ -- The provider's page and the provider's reference to it, so the same caller starting again
23
+ -- while it is open gets the same page (and no one else ever does), and its status can be asked.
24
+ url TEXT NOT NULL,
25
+ checkout_ref TEXT NOT NULL,
26
+ -- Who opened it: `customer:<customer id>` or `staff:<user id>`. The page goes back only to them.
27
+ opened_by TEXT NOT NULL,
28
+ -- The terms the page was opened with, returned with it: one period's price and the trial's days.
29
+ amount INTEGER NOT NULL,
30
+ trial_days INTEGER NOT NULL CHECK (trial_days > 0),
31
+ -- Until when the page can still be completed and its confirmation arrive.
32
+ held_until TEXT NOT NULL,
33
+ -- When the provider confirmed a subscription with this trial: from then on it is used for good.
34
+ used_at TEXT,
35
+ -- When a lapsed hold was released, once the provider said its page expired uncompleted.
36
+ released_at TEXT,
37
+ created_at TEXT NOT NULL
38
+ );
39
+ CREATE INDEX core_subscription_trial_holder_idx
40
+ ON core_subscription_trial (holder_person_id, plan_object);
41
+
42
+ CREATE TRIGGER core_subscription_trial_one
43
+ BEFORE INSERT ON core_subscription_trial
44
+ WHEN EXISTS (
45
+ SELECT 1 FROM core_subscription_trial t
46
+ WHERE t.holder_person_id = NEW.holder_person_id
47
+ AND t.plan_object = NEW.plan_object
48
+ AND (t.used_at IS NOT NULL OR t.released_at IS NULL)
49
+ )
50
+ BEGIN
51
+ SELECT RAISE(ABORT, 'core_subscription_trial_taken');
52
+ END;
53
+
54
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
55
+ UPDATE core_meta SET value = '531'
56
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 531;
@@ -0,0 +1,80 @@
1
+ -- Migration 0532: continuation offers (stage 32a, build step 4, ADRs 0491 and 0492).
2
+ --
3
+ -- A company moving its subscribers from another platform imports, for each one, an offer: an
4
+ -- address (and the one live Person who has it), a plan object and record, and the date the person
5
+ -- has already paid up to. The person signs in as that address and subscribes again, free until
6
+ -- the date, then charged by the provider. An offer is taken once (the provider's verified event
7
+ -- that makes the subscription marks it), expires on its date, and can be withdrawn by staff.
8
+ --
9
+ -- While a provider page opened from an offer can still be completed, the offer holds it
10
+ -- (`checkout_id`). The trigger refuses holding a page for an offer that already holds one, has
11
+ -- been taken or was withdrawn: of two takes at once, the second one's whole batch fails, so its
12
+ -- page is never handed out. A hold is released (`checkout_id` back to null) only once the
13
+ -- provider says the page expired uncompleted, as a trial's is (ADR 0489).
14
+ CREATE TABLE core_subscription_offer (
15
+ -- sof_… (ids.ts).
16
+ id TEXT PRIMARY KEY NOT NULL,
17
+ -- The plan: any record of an object that declares `sellable.recurring`.
18
+ plan_object TEXT NOT NULL,
19
+ plan_id TEXT NOT NULL,
20
+ -- The address the offer is for. Taking it needs a customer signed in as this address.
21
+ email TEXT NOT NULL CHECK (email = lower(trim(email)) AND email <> ''),
22
+ -- The one live Person who had the address when it was imported (people come in first; an
23
+ -- import never creates one). A merge repoints it. Null after the person is erased.
24
+ person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
25
+ -- Free until this moment (the date the person has paid up to elsewhere); charged from then.
26
+ free_until TEXT NOT NULL,
27
+ -- The other platform's reference for the old subscription, as the source gave it.
28
+ reference TEXT,
29
+ imported_by TEXT NOT NULL,
30
+ created_at TEXT NOT NULL,
31
+ updated_at TEXT NOT NULL,
32
+ -- The provider page that holds it while it can be completed (core_subscription_checkout.id).
33
+ checkout_id TEXT,
34
+ checkout_ref TEXT,
35
+ checkout_url TEXT,
36
+ held_until TEXT,
37
+ -- The subscription that took it, once the provider confirmed it. Final.
38
+ subscription_id TEXT,
39
+ taken_at TEXT,
40
+ -- Taken with too little time left for free time at the provider (under 48 hours): charged at
41
+ -- once, never given the plan's trial instead.
42
+ charged_from_start INTEGER NOT NULL DEFAULT 0 CHECK (charged_from_start IN (0, 1)),
43
+ withdrawn_at TEXT,
44
+ withdrawn_by TEXT,
45
+ withdraw_reason TEXT,
46
+ -- A withdrawn offer staff replaced with a fresh one (`reissue`): the new offer's id.
47
+ reissued_as TEXT,
48
+ -- Bumped by exactly one on every change (below): a write made from an older read fails.
49
+ version INTEGER NOT NULL DEFAULT 0
50
+ );
51
+ -- One offer per address per plan object, the import's key; a withdrawn one stays as it was, and
52
+ -- staff may reissue the address a fresh offer beside it.
53
+ CREATE UNIQUE INDEX core_subscription_offer_key_uq ON core_subscription_offer (plan_object, email)
54
+ WHERE withdrawn_at IS NULL;
55
+ CREATE INDEX core_subscription_offer_person_idx ON core_subscription_offer (person_id, plan_object);
56
+
57
+ CREATE TRIGGER core_subscription_offer_one_page
58
+ BEFORE UPDATE OF checkout_id ON core_subscription_offer
59
+ WHEN NEW.checkout_id IS NOT NULL
60
+ AND (OLD.checkout_id IS NOT NULL OR OLD.subscription_id IS NOT NULL OR OLD.withdrawn_at IS NOT NULL)
61
+ BEGIN
62
+ SELECT RAISE(ABORT, 'core_subscription_offer_held');
63
+ END;
64
+
65
+ -- A write made from an older read of the offer fails, and its whole batch (as a subscription's,
66
+ -- migration 0530): an import, a withdrawal or a take that read the offer before another change
67
+ -- to it commits nothing, and is told to try again.
68
+ CREATE TRIGGER core_subscription_offer_stale
69
+ BEFORE UPDATE ON core_subscription_offer
70
+ WHEN NEW.version <> OLD.version + 1
71
+ BEGIN
72
+ SELECT RAISE(ABORT, 'core_subscription_offer_stale');
73
+ END;
74
+
75
+ -- A checkout opened from an offer names it, so the provider's confirmation marks the offer taken.
76
+ ALTER TABLE core_subscription_checkout ADD COLUMN offer_id TEXT;
77
+
78
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
79
+ UPDATE core_meta SET value = '532'
80
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 532;
@@ -0,0 +1,262 @@
1
+ -- Migration 0533: allowances (stage 32b, the `allowance` behaviour, ADRs 0493 and 0494).
2
+ --
3
+ -- A holder (a pass, a membership: a record of an object that declares `allowance.holds`) grants a
4
+ -- number of uses per total, week or month, or unlimited uses. A spender (a booking, a visit)
5
+ -- spends one when it enters a state its object declares, and gives it back on entering another.
6
+ --
7
+ -- The rule is in the database, as capacity's is (migration 0440): a spend is a fixed set of
8
+ -- statements in the spender's own batch, with no read of the count first, and the database
9
+ -- refuses one past the limit, so the whole batch (the booking with it) is not applied. D1 runs one
10
+ -- statement at a time per database, so concurrent spends cannot both take the last use.
11
+ --
12
+ -- * core_allowance: the holder's terms, derived by the host in SQL from the stored record in the
13
+ -- batch of every write to it (so two writes to different fields cannot leave a stale row), and
14
+ -- for every record by the reconciler when the declaration is installed or changed.
15
+ -- * core_allowance_period: one counter per holder per period the holder has been spent in
16
+ -- (`total`, `week:<Monday's local date>`, `month:<YYYY-MM>`), with the period's real limit. A
17
+ -- period may sit above a limit lowered after its uses were spent; it then takes no spend until
18
+ -- it is back under. core_allowance_refuse_spend (a BEFORE trigger) is the guarantee: it refuses
19
+ -- any change that takes `used` up past the limit, whoever writes it, with the numbers
20
+ -- (errors.ts parses `core_allowance_refused {json}`). The one exception is a re-keying after a
21
+ -- change of period or zone (`rekeyed` goes up with `used`): uses already spent move to their
22
+ -- new period even past its limit, as a lowered limit leaves them.
23
+ -- * core_allowance_use: one row per use, spent and perhaps given back. A BEFORE trigger refuses
24
+ -- a use the holder cannot pay at the spender's time: it is not theirs, not live, its Person is
25
+ -- in the Trash, it is not yet valid or has ended. One open use per spender (a partial unique
26
+ -- index).
27
+ CREATE TABLE core_allowance (
28
+ -- The holder's object (as in core_transition_history.object_type) and id.
29
+ object_type TEXT NOT NULL,
30
+ record_id TEXT NOT NULL,
31
+ -- The Person whose uses they are (the holder, not the payer: ADR 0487).
32
+ person_id TEXT,
33
+ -- Uses per period. NULL: unlimited (only where the object declares an empty field so).
34
+ uses INTEGER CHECK (uses IS NULL OR uses >= 0),
35
+ per TEXT NOT NULL CHECK (per IN ('total', 'week', 'month')),
36
+ -- The IANA zone weeks, months and dates are counted in: the holder's own field, else the
37
+ -- organisation's when the holder was first written (ADR 0494: it does not follow the setting).
38
+ zone TEXT NOT NULL,
39
+ -- A datetime `from` / `until`, as ISO instants: a spender's time must be >= from and < until.
40
+ valid_from TEXT,
41
+ valid_until TEXT,
42
+ -- A date `from` / `until` (YYYY-MM-DD in `zone`): the spender's local date must be in them.
43
+ from_date TEXT,
44
+ until_date TEXT,
45
+ -- 1 when the record's state is one its object declares live (or it has no states).
46
+ live INTEGER NOT NULL CHECK (live IN (0, 1)),
47
+ -- 1 while the record is in the Trash.
48
+ trashed INTEGER NOT NULL DEFAULT 0 CHECK (trashed IN (0, 1)),
49
+ -- A change of period or zone applies from the end of the period running when it was made
50
+ -- (ADR 0494): before `applies_from`, spends are counted by `prev_per` in `prev_zone`.
51
+ prev_per TEXT CHECK (prev_per IS NULL OR prev_per IN ('total', 'week', 'month')),
52
+ prev_zone TEXT,
53
+ applies_from TEXT,
54
+ updated_at TEXT NOT NULL,
55
+ PRIMARY KEY (object_type, record_id)
56
+ ) WITHOUT ROWID;
57
+ -- What a spend reads: the Person's holders of the objects that may pay for it.
58
+ CREATE INDEX core_allowance_person_idx ON core_allowance (person_id, object_type);
59
+
60
+ CREATE TABLE core_allowance_period (
61
+ object_type TEXT NOT NULL,
62
+ record_id TEXT NOT NULL,
63
+ period TEXT NOT NULL,
64
+ per TEXT NOT NULL CHECK (per IN ('total', 'week', 'month')),
65
+ zone TEXT NOT NULL,
66
+ -- The period's bounds, ISO instants: [starts_at, ends_at).
67
+ starts_at TEXT NOT NULL,
68
+ ends_at TEXT NOT NULL,
69
+ -- The limit for this period, from the terms (followed by current and future periods when the
70
+ -- terms change). NULL: unlimited.
71
+ uses INTEGER CHECK (uses IS NULL OR uses >= 0),
72
+ used INTEGER NOT NULL DEFAULT 0 CHECK (used >= 0),
73
+ -- Uses moved here by a re-keying (a change of period or zone): see the header.
74
+ rekeyed INTEGER NOT NULL DEFAULT 0,
75
+ updated_at TEXT NOT NULL,
76
+ -- Keyed with its keying (per and zone), so a holder's periods under an old keying never mix
77
+ -- with those under the new one.
78
+ PRIMARY KEY (object_type, record_id, period, zone),
79
+ FOREIGN KEY (object_type, record_id) REFERENCES core_allowance (object_type, record_id)
80
+ ON DELETE CASCADE
81
+ ) WITHOUT ROWID;
82
+
83
+ -- Refuses a spend that would take a period past its limit, with the real limit. A period over a
84
+ -- lowered limit takes no spend; giving uses back (used going down) is never refused.
85
+ CREATE TRIGGER core_allowance_refuse_spend
86
+ BEFORE UPDATE OF used ON core_allowance_period
87
+ WHEN NEW.uses IS NOT NULL AND NEW.used > NEW.uses AND NEW.used > OLD.used
88
+ AND NEW.rekeyed = OLD.rekeyed
89
+ BEGIN
90
+ SELECT RAISE(ABORT, 'core_allowance_refused ' || json_object(
91
+ 'reason', 'no_uses_left',
92
+ 'object', NEW.object_type,
93
+ 'record', NEW.record_id,
94
+ 'period', NEW.period,
95
+ 'uses', NEW.uses,
96
+ 'used', OLD.used,
97
+ 'asked', NEW.used - OLD.used
98
+ ));
99
+ END;
100
+
101
+ CREATE TABLE core_allowance_use (
102
+ -- alu_… (ids.ts).
103
+ id TEXT PRIMARY KEY NOT NULL,
104
+ -- The holder spent from, and the period (with its zone) the use counts in.
105
+ object_type TEXT NOT NULL,
106
+ record_id TEXT NOT NULL,
107
+ period TEXT NOT NULL,
108
+ zone TEXT NOT NULL,
109
+ -- The spender's record.
110
+ spender_type TEXT NOT NULL,
111
+ spender_id TEXT NOT NULL,
112
+ person_id TEXT NOT NULL,
113
+ -- The spender's time (a class's start), an ISO instant, and its local date in the holder's
114
+ -- zone: the period is the one containing it.
115
+ at TEXT NOT NULL,
116
+ at_date TEXT NOT NULL,
117
+ spent_at TEXT NOT NULL,
118
+ -- Who made the move that spent it: staff, customer or host.
119
+ spent_by TEXT NOT NULL CHECK (spent_by IN ('staff', 'customer', 'host')),
120
+ returned_at TEXT,
121
+ returned_by TEXT CHECK (returned_by IS NULL OR returned_by IN ('staff', 'customer', 'host')),
122
+ -- The use this one took over, when a spender was moved to another, or re-spent after what it
123
+ -- points at changed (ADR 0493).
124
+ moved_from TEXT,
125
+ FOREIGN KEY (object_type, record_id, period, zone)
126
+ REFERENCES core_allowance_period (object_type, record_id, period, zone) ON DELETE CASCADE,
127
+ CHECK ((returned_at IS NULL) = (returned_by IS NULL))
128
+ );
129
+ CREATE INDEX core_allowance_use_holder_idx ON core_allowance_use (object_type, record_id, period);
130
+ CREATE INDEX core_allowance_use_person_idx ON core_allowance_use (person_id);
131
+ -- A spender holds at most one use at a time (and its open use is found by this index).
132
+ CREATE UNIQUE INDEX core_allowance_use_open_idx ON core_allowance_use (spender_type, spender_id)
133
+ WHERE returned_at IS NULL;
134
+
135
+ -- Refuses a use the holder cannot pay at the spender's time, saying why.
136
+ CREATE TRIGGER core_allowance_refuse_use
137
+ BEFORE INSERT ON core_allowance_use
138
+ WHEN NOT EXISTS (
139
+ SELECT 1 FROM core_allowance a
140
+ WHERE a.object_type = NEW.object_type AND a.record_id = NEW.record_id
141
+ AND a.person_id = NEW.person_id AND a.live = 1 AND a.trashed = 0
142
+ AND (a.valid_from IS NULL OR a.valid_from <= NEW.at)
143
+ AND (a.valid_until IS NULL OR NEW.at < a.valid_until)
144
+ AND (a.from_date IS NULL OR a.from_date <= NEW.at_date)
145
+ AND (a.until_date IS NULL OR NEW.at_date <= a.until_date)
146
+ ) OR NOT EXISTS (
147
+ SELECT 1 FROM core_person p WHERE p.id = NEW.person_id AND p.deleted_at IS NULL
148
+ )
149
+ BEGIN
150
+ SELECT RAISE(ABORT, 'core_allowance_refused ' || json_object(
151
+ 'reason', coalesce((
152
+ SELECT CASE
153
+ WHEN a.person_id IS NOT NEW.person_id THEN 'not_theirs'
154
+ WHEN NOT EXISTS (
155
+ SELECT 1 FROM core_person p WHERE p.id = NEW.person_id AND p.deleted_at IS NULL
156
+ ) THEN 'not_live'
157
+ WHEN a.live = 0 OR a.trashed = 1 THEN 'not_live'
158
+ WHEN (a.valid_from IS NOT NULL AND NEW.at < a.valid_from)
159
+ OR (a.from_date IS NOT NULL AND NEW.at_date < a.from_date) THEN 'not_yet_valid'
160
+ ELSE 'ended'
161
+ END
162
+ FROM core_allowance a
163
+ WHERE a.object_type = NEW.object_type AND a.record_id = NEW.record_id
164
+ ), 'no_holder'),
165
+ 'object', NEW.object_type,
166
+ 'record', NEW.record_id,
167
+ 'at', NEW.at
168
+ ));
169
+ END;
170
+
171
+ -- The read views, for query.sql and dashboards.
172
+ --
173
+ -- rec_allowance: one row per holder, its terms and the current period's numbers (`left` is what
174
+ -- can be spent now: 0 when it cannot be spent now, NULL when unlimited). A date `from` / `until`
175
+ -- is compared with today's date in UTC here (the host's own answers use the holder's zone).
176
+ -- rec_allowance_period: every period a holder was spent in, with its numbers.
177
+ -- rec_allowance_use: every use, spent or given back.
178
+ CREATE VIEW rec_allowance AS
179
+ WITH c AS (
180
+ SELECT
181
+ a.*,
182
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now') AS now_at,
183
+ CASE WHEN a.applies_from IS NOT NULL AND a.applies_from > strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
184
+ THEN a.prev_per ELSE a.per END AS cur_per,
185
+ CASE WHEN a.applies_from IS NOT NULL AND a.applies_from > strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
186
+ THEN a.prev_zone ELSE a.zone END AS cur_zone
187
+ FROM core_allowance a
188
+ ), n AS (
189
+ SELECT
190
+ c.*,
191
+ coalesce((
192
+ SELECT p.used FROM core_allowance_period p
193
+ WHERE p.object_type = c.object_type AND p.record_id = c.record_id
194
+ AND p.per = c.cur_per AND p.zone = c.cur_zone
195
+ AND p.starts_at <= c.now_at AND p.ends_at > c.now_at
196
+ ), 0) AS cur_used
197
+ FROM c
198
+ )
199
+ SELECT
200
+ object_type,
201
+ record_id,
202
+ person_id,
203
+ uses,
204
+ per,
205
+ zone,
206
+ valid_from,
207
+ valid_until,
208
+ from_date,
209
+ until_date,
210
+ live,
211
+ trashed,
212
+ cur_used AS used,
213
+ CASE
214
+ WHEN live = 0 OR trashed = 1
215
+ OR (valid_from IS NOT NULL AND valid_from > now_at)
216
+ OR (valid_until IS NOT NULL AND valid_until <= now_at)
217
+ OR (from_date IS NOT NULL AND from_date > date('now'))
218
+ OR (until_date IS NOT NULL AND until_date < date('now'))
219
+ THEN 0
220
+ WHEN uses IS NULL THEN NULL
221
+ ELSE max(uses - cur_used, 0)
222
+ END AS "left",
223
+ updated_at
224
+ FROM n;
225
+
226
+ CREATE VIEW rec_allowance_period AS
227
+ SELECT
228
+ object_type,
229
+ record_id,
230
+ period,
231
+ per,
232
+ zone,
233
+ starts_at,
234
+ ends_at,
235
+ uses,
236
+ used,
237
+ CASE WHEN uses IS NULL THEN NULL ELSE max(uses - used, 0) END AS "left",
238
+ updated_at
239
+ FROM core_allowance_period;
240
+
241
+ CREATE VIEW rec_allowance_use AS
242
+ SELECT
243
+ id,
244
+ object_type,
245
+ record_id,
246
+ period,
247
+ zone,
248
+ spender_type,
249
+ spender_id,
250
+ person_id,
251
+ at,
252
+ at_date,
253
+ spent_at,
254
+ spent_by,
255
+ returned_at,
256
+ returned_by,
257
+ moved_from
258
+ FROM core_allowance_use;
259
+
260
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
261
+ UPDATE core_meta SET value = '533'
262
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 533;
@@ -0,0 +1,75 @@
1
+ -- Migration 0560: the `recurring` behaviour's bookkeeping (stage 31, ADR 0500).
2
+ --
3
+ -- A series (a record of an object that declares `recurring`) has one row here: when the host
4
+ -- next makes its records (the minute cron picks rows whose time has come; a write to the series
5
+ -- sets it to now) and up to which local date it has made them. Each record it made has one
6
+ -- occurrence row, keyed on the series and the local date: the idempotency key. A second run, or
7
+ -- two runs at once, cannot make a date twice, because the second insert of its key fails and with
8
+ -- it the whole batch that would have created the record. An occurrence edited by someone other
9
+ -- than the host is marked, and the series never writes to it again.
10
+ CREATE TABLE core_recurrence_series (
11
+ object_type TEXT NOT NULL,
12
+ record_id TEXT NOT NULL,
13
+ -- When the host next makes this series' records (ISO instant).
14
+ next_run_at TEXT NOT NULL,
15
+ -- The last local date (YYYY-MM-DD) it has made records up to, or none yet.
16
+ generated_until TEXT,
17
+ -- Bumped by every write to the series and every run of it: a run or a change that read an
18
+ -- older version fails its batch (core_write_guard below), so neither leaves stale records.
19
+ version INTEGER NOT NULL DEFAULT 0,
20
+ updated_at TEXT NOT NULL,
21
+ PRIMARY KEY (object_type, record_id)
22
+ ) WITHOUT ROWID;
23
+ CREATE INDEX core_recurrence_series_due_idx ON core_recurrence_series (next_run_at);
24
+
25
+ CREATE TABLE core_recurrence_occurrence (
26
+ series_object TEXT NOT NULL,
27
+ series_id TEXT NOT NULL,
28
+ -- The occurrence's local date in the series' zone (YYYY-MM-DD).
29
+ local_date TEXT NOT NULL,
30
+ -- The record made for it.
31
+ object_type TEXT NOT NULL,
32
+ record_id TEXT NOT NULL,
33
+ -- Set when someone other than the host changed it: the series leaves it alone from then on.
34
+ edited_at TEXT,
35
+ created_at TEXT NOT NULL,
36
+ PRIMARY KEY (series_object, series_id, local_date)
37
+ ) WITHOUT ROWID;
38
+ CREATE UNIQUE INDEX core_recurrence_occurrence_record_idx
39
+ ON core_recurrence_occurrence (object_type, record_id);
40
+
41
+ -- Guards for a batch that was planned from a read (stage 31, ADRs 0500 and 0501). A statement
42
+ -- inserts one row saying what it read; the trigger compares it with what is there now, in the
43
+ -- batch, and either drops the row silently (still true) or fails the whole batch. Nothing is ever
44
+ -- stored here.
45
+ -- kind 'claimed': a capacity counter's held + taken is still `expected`
46
+ -- kind 'claimant': the claimant record still has `expected` live claims (held or confirmed)
47
+ -- kind 'series': a series' version is still `expected`
48
+ CREATE TABLE core_write_guard (
49
+ kind TEXT NOT NULL CHECK (kind IN ('claimed', 'claimant', 'series')),
50
+ object_type TEXT NOT NULL,
51
+ record_id TEXT NOT NULL,
52
+ expected INTEGER NOT NULL
53
+ );
54
+
55
+ CREATE TRIGGER core_write_guard_check
56
+ BEFORE INSERT ON core_write_guard
57
+ BEGIN
58
+ SELECT CASE
59
+ WHEN NEW.kind = 'claimed' AND coalesce((SELECT held + taken FROM core_capacity
60
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id), 0) != NEW.expected
61
+ THEN RAISE(ABORT, 'core_recurrence_stale')
62
+ WHEN NEW.kind = 'series' AND coalesce((SELECT version FROM core_recurrence_series
63
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id), 0) != NEW.expected
64
+ THEN RAISE(ABORT, 'core_recurrence_stale')
65
+ WHEN NEW.kind = 'claimant' AND (SELECT count(*) FROM core_capacity_claim
66
+ WHERE for_type = NEW.object_type AND for_id = NEW.record_id
67
+ AND state IN ('held', 'confirmed')) != NEW.expected
68
+ THEN RAISE(ABORT, 'core_claim_live')
69
+ ELSE RAISE(IGNORE)
70
+ END;
71
+ END;
72
+
73
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
74
+ UPDATE core_meta SET value = '560'
75
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 560;