@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,36 @@
1
+ -- Stage 41, `publishable` without its public half (ADR 0446): the published versions of a
2
+ -- record's content.
3
+ --
4
+ -- A publishable object's records are the working copy staff edit. Publishing one snapshots its
5
+ -- content fields (the object's `publishable.content`) into the next version here, in the batch
6
+ -- of `record.publish`, and makes it the current one: the version readers are given (an
7
+ -- assignment pins a program's). Editing the working copy changes no row here. Restoring copies a
8
+ -- version back into the working copy and changes no row here either.
9
+ --
10
+ -- At most one current version per record (the partial unique index). Two publishes racing each
11
+ -- other compute the same next number and the second fails its batch on the primary key, so no
12
+ -- version is ever written twice. Stage 28 adds unpublishing (current set to 0, the versions kept)
13
+ -- and public reads of the current version.
14
+ --
15
+ -- Only packages/core/src/records/publish.ts writes here.
16
+ CREATE TABLE core_publish_version (
17
+ object_type TEXT NOT NULL,
18
+ record_id TEXT NOT NULL,
19
+ version INTEGER NOT NULL CHECK (version >= 1),
20
+ -- The content fields' values when it was published, as the record operations return them.
21
+ content TEXT NOT NULL,
22
+ bytes INTEGER NOT NULL,
23
+ current INTEGER NOT NULL DEFAULT 0 CHECK (current IN (0, 1)),
24
+ published_at TEXT NOT NULL,
25
+ -- The staff user who published it.
26
+ published_by TEXT,
27
+ note TEXT CHECK (note IS NULL OR length(note) <= 500),
28
+ PRIMARY KEY (object_type, record_id, version)
29
+ );
30
+ CREATE UNIQUE INDEX core_publish_version_current_idx
31
+ ON core_publish_version (object_type, record_id) WHERE current = 1;
32
+
33
+ -- For dashboards and query.sql: every version, without its content.
34
+ CREATE VIEW rec_publish_version AS
35
+ SELECT object_type, record_id, version, current, bytes, published_at, published_by, note
36
+ FROM core_publish_version;
@@ -0,0 +1,18 @@
1
+ -- Stage 41, permissions granted one by one (ADR 0449): a human admin grants a staff account
2
+ -- named permissions on top of its role, from a fixed list (`records:publish`, `orders:refund`)
3
+ -- and the tool permissions installed packs declare grantable. A coach who is a member publishes
4
+ -- programs.
5
+ --
6
+ -- `granted_permissions` is a JSON list of permission names, or null for none. The operations
7
+ -- pipeline reads it on every staff call, in the statement that reads `pack_role`, and a
8
+ -- permission there counts only if it is still grantable. While a pack role is held, it is all the
9
+ -- account may do and grants count for nothing (ADR 0436). Only `user.setPermissions` (a human
10
+ -- admin) writes it.
11
+ ALTER TABLE core_user ADD COLUMN granted_permissions TEXT;
12
+
13
+ -- The list of users shows what an account was granted.
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.granted_permissions, 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,40 @@
1
+ -- Staff invites, and native staff sign-in through the system browser (ADR 0455).
2
+ --
3
+ -- core_user_invite: a human admin invites a person by address, with the role, pack role
4
+ -- (ADR 0436) and grants (ADR 0449) the account will hold from the start. Accepting it is signing
5
+ -- in with a code emailed to that address (Better Auth), or the first request under Cloudflare
6
+ -- Access: either makes the account in one insert with what the invite says, after marking the
7
+ -- invite accepted. An invite holds no secret; the code is Better Auth's, stored hashed in
8
+ -- core_verification. One pending invite per address.
9
+ CREATE TABLE core_user_invite (
10
+ id TEXT PRIMARY KEY,
11
+ email TEXT NOT NULL,
12
+ role TEXT NOT NULL CHECK (role IN ('admin', 'member', 'viewer')),
13
+ pack_role TEXT,
14
+ granted_permissions TEXT,
15
+ invited_by TEXT NOT NULL,
16
+ created_at TEXT NOT NULL,
17
+ updated_at TEXT NOT NULL,
18
+ expires_at TEXT NOT NULL,
19
+ accepted_at TEXT,
20
+ accepted_user_id TEXT,
21
+ revoked_at TEXT,
22
+ revoked_by TEXT
23
+ );
24
+
25
+ CREATE UNIQUE INDEX core_user_invite_pending ON core_user_invite (email)
26
+ WHERE accepted_at IS NULL AND revoked_at IS NULL;
27
+ CREATE INDEX core_user_invite_created ON core_user_invite (created_at);
28
+
29
+ -- core_native_handoff: the one-time code /native/staff-sign-in hands a native app through the
30
+ -- system browser, for staff whose sign-in is browser-based (Google, Cloudflare Access). Only its
31
+ -- SHA-256 hash is stored. It is deleted when presented, right or wrong, and lives 60 seconds; it
32
+ -- is bound to the app's PKCE challenge (S256) and the redirect address it was sent to.
33
+ CREATE TABLE core_native_handoff (
34
+ code_hash TEXT PRIMARY KEY,
35
+ user_id TEXT NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
36
+ challenge TEXT NOT NULL,
37
+ redirect_uri TEXT NOT NULL,
38
+ created_at TEXT NOT NULL,
39
+ expires_at TEXT NOT NULL
40
+ );
@@ -0,0 +1,10 @@
1
+ -- The host's sign-in mail (ADR 0456): email-send's sign-in kind, made known so a human admin can
2
+ -- let it apply without review before any code is asked for. It is auto-approvable and not
3
+ -- auto-approved: sign-in by code says it is not set up until an admin turns it on
4
+ -- (outbound.setAutoApprove). Choosing the host's mail connection makes its vendor's kind known
5
+ -- too (customer.settings.update), so a deployment on another vendor gets its own.
6
+ INSERT INTO core_outbound_kind
7
+ (tag, vendor, label, auto_approvable, auto_approve, first_seen_at, updated_at)
8
+ VALUES ('email.signin', 'email-send', 'Send sign-in codes', 1, 0,
9
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
10
+ ON CONFLICT (tag) DO UPDATE SET auto_approvable = 1;
@@ -0,0 +1,30 @@
1
+ -- ADR 0457: a staff member confirms their own address, and the native sign-in page asks before it
2
+ -- hands a code to an app.
3
+ --
4
+ -- core_email_confirmation: one pending confirmation per account. The code (six digits, sent as the
5
+ -- host's sign-in mail to the account's own address) is stored as a hash, bound to the address it
6
+ -- was sent to: if the account's address is no longer that one, the code confirms nothing. It
7
+ -- lives five minutes and allows three tries; confirming deletes it.
8
+ CREATE TABLE core_email_confirmation (
9
+ user_id TEXT PRIMARY KEY REFERENCES core_user (id) ON DELETE CASCADE,
10
+ email TEXT NOT NULL,
11
+ code_hash TEXT NOT NULL,
12
+ attempts INTEGER NOT NULL DEFAULT 0,
13
+ created_at TEXT NOT NULL,
14
+ expires_at TEXT NOT NULL
15
+ );
16
+
17
+ -- core_native_sign_in_confirm: the one-time token on /native/staff-sign-in's confirm page. It is
18
+ -- bound to the signed-in account and to the request's redirect address, challenge and state;
19
+ -- only its hash is stored, it lives five minutes, and the confirming POST deletes it, right or
20
+ -- wrong. Only then is a handoff code (core_native_handoff) issued.
21
+ CREATE TABLE core_native_sign_in_confirm (
22
+ token_hash TEXT PRIMARY KEY,
23
+ user_id TEXT NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
24
+ redirect_uri TEXT NOT NULL,
25
+ challenge TEXT NOT NULL,
26
+ state TEXT NOT NULL,
27
+ created_at TEXT NOT NULL,
28
+ expires_at TEXT NOT NULL
29
+ );
30
+ CREATE INDEX core_native_sign_in_confirm_expires ON core_native_sign_in_confirm (expires_at);
@@ -0,0 +1,25 @@
1
+ -- Stage 28 (ADR 0444, amended for issue #181): which field of a record's current version
2
+ -- references each media item. A picture is served to someone outside the org only when the field
3
+ -- that shows it is one they may read; a picture in a field the public does not read (a staff-only
4
+ -- image among the content) is not theirs, though the version is published.
5
+ --
6
+ -- The table is rebuilt with the field in its key, and filled from the current versions: every
7
+ -- `media` key in a version's content, under the top-level field it sits in
8
+ -- (`$.body.blocks[2].media` is the field `body`, `$.cover.media` is `cover`).
9
+ CREATE TABLE core_published_media_next (
10
+ media_id TEXT NOT NULL,
11
+ object_type TEXT NOT NULL,
12
+ record_id TEXT NOT NULL,
13
+ field TEXT NOT NULL,
14
+ PRIMARY KEY (media_id, object_type, record_id, field)
15
+ ) WITHOUT ROWID;
16
+
17
+ INSERT OR IGNORE INTO core_published_media_next (media_id, object_type, record_id, field)
18
+ SELECT j.value, v.object_type, v.record_id,
19
+ substr(substr(j.fullkey, 3), 1, instr(substr(j.fullkey, 3) || '.', '.') - 1)
20
+ FROM core_publish_version v, json_tree(v.content) j
21
+ WHERE v.current = 1 AND j.key = 'media' AND j.type = 'text' AND j.value LIKE 'med\_%' ESCAPE '\';
22
+
23
+ DROP TABLE core_published_media;
24
+ ALTER TABLE core_published_media_next RENAME TO core_published_media;
25
+ CREATE INDEX core_published_media_record_idx ON core_published_media (object_type, record_id);
@@ -0,0 +1,23 @@
1
+ -- The import door (ADR 0470): `cos content import` and `record.import`.
2
+ --
3
+ -- A picture's SHA-256, so an upload of a file the library already has returns that item instead
4
+ -- of storing it twice (a re-run of an import uploads nothing). Older items have none until they
5
+ -- are uploaded again.
6
+ ALTER TABLE core_media ADD COLUMN sha256 TEXT;
7
+ CREATE INDEX core_media_sha256_idx ON core_media (sha256) WHERE deleted_at IS NULL AND sha256 IS NOT NULL;
8
+
9
+ -- What an import made, by the key the import gave it: the record, and the values the import last
10
+ -- wrote, as the record operations return them, so a re-run creates nothing twice, updates a
11
+ -- record only while it still holds exactly what the import wrote, and reports a record a person
12
+ -- has changed since as a conflict instead of overwriting it. Only record.import writes here.
13
+ CREATE TABLE core_import_key (
14
+ object_type TEXT NOT NULL,
15
+ import_key TEXT NOT NULL CHECK (length(import_key) BETWEEN 1 AND 200),
16
+ record_id TEXT NOT NULL,
17
+ -- JSON: field name → the value the import wrote.
18
+ written TEXT NOT NULL,
19
+ imported_at TEXT NOT NULL,
20
+ imported_by TEXT NOT NULL,
21
+ PRIMARY KEY (object_type, import_key)
22
+ );
23
+ CREATE INDEX core_import_key_record_idx ON core_import_key (object_type, record_id);
@@ -0,0 +1,109 @@
1
+ -- Stage 42, Courses (ADRs 0475 and 0477): videos and downloadable files in the media library, and
2
+ -- which part of a published document each media item is in.
3
+ --
4
+ -- 1. core_media gains a kind. A `video` is held in Cloudflare Stream on the deployment's own
5
+ -- account: the row keeps Stream's id for it (`stream_uid`), its state (uploading, processing,
6
+ -- ready, failed, and Stream's reason when it failed), its duration and dimensions; nothing of
7
+ -- it is in R2, so it has no `key`. A `file` (a PDF, for downloads) is in R2 like a picture.
8
+ -- SQLite cannot change a CHECK, so the table is rebuilt with its rows, indexes and trigger.
9
+ -- 2. Deleting a row (the Trash purge) queues what it holds on core_blob_deletion in the same
10
+ -- batch: its R2 key, or `stream/<uid>` for a video, which the drain deletes from Stream.
11
+ -- 3. core_media_caption: a video's caption tracks, one per language (WebVTT, uploaded by staff or
12
+ -- generated by Stream).
13
+ -- 4. core_published_media gains `reach`: who outside the org reads the part of the field the item
14
+ -- is in (all who read the field, only holders of access to the record, or no one: a private
15
+ -- part). Existing rows are `all`, which is what media.serve has read them as until now; the
16
+ -- next publish of a record writes its exact reach.
17
+ CREATE TABLE core_media_next (
18
+ id TEXT PRIMARY KEY NOT NULL,
19
+ kind TEXT NOT NULL DEFAULT 'image' CHECK (kind IN ('image', 'video', 'file')),
20
+ key TEXT UNIQUE,
21
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 200),
22
+ type TEXT NOT NULL,
23
+ bytes INTEGER NOT NULL CHECK (bytes > 0),
24
+ width INTEGER,
25
+ height INTEGER,
26
+ alt TEXT CHECK (alt IS NULL OR length(alt) <= 500),
27
+ stream_uid TEXT UNIQUE,
28
+ state TEXT,
29
+ state_reason TEXT CHECK (state_reason IS NULL OR length(state_reason) <= 200),
30
+ duration REAL,
31
+ created_at TEXT NOT NULL,
32
+ created_by TEXT,
33
+ updated_at TEXT NOT NULL,
34
+ updated_by TEXT,
35
+ deleted_at TEXT,
36
+ deleted_by TEXT,
37
+ sha256 TEXT,
38
+ CHECK (
39
+ (kind = 'image' AND type IN ('image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif')
40
+ AND key IS NOT NULL AND stream_uid IS NULL AND state IS NULL)
41
+ OR (kind = 'file' AND type IN ('application/pdf')
42
+ AND key IS NOT NULL AND stream_uid IS NULL AND state IS NULL)
43
+ OR (kind = 'video' AND type = 'video/stream' AND key IS NULL AND stream_uid IS NOT NULL
44
+ AND state IN ('uploading', 'processing', 'ready', 'failed'))
45
+ )
46
+ );
47
+
48
+ INSERT INTO core_media_next (id, kind, key, name, type, bytes, width, height, alt, created_at,
49
+ created_by, updated_at, updated_by, deleted_at, deleted_by, sha256)
50
+ SELECT id, 'image', key, name, type, bytes, width, height, alt, created_at, created_by,
51
+ updated_at, updated_by, deleted_at, deleted_by, sha256
52
+ FROM core_media;
53
+
54
+ DROP VIEW rec_media;
55
+ DROP TABLE core_media;
56
+ ALTER TABLE core_media_next RENAME TO core_media;
57
+
58
+ CREATE INDEX core_media_live_idx ON core_media (created_at) WHERE deleted_at IS NULL;
59
+ CREATE INDEX core_media_deleted_idx ON core_media (deleted_at) WHERE deleted_at IS NOT NULL;
60
+ CREATE INDEX core_media_sha256_idx ON core_media (sha256) WHERE deleted_at IS NULL AND sha256 IS NOT NULL;
61
+
62
+ -- The purge's delete queues what the row held, for the drain to delete after the commit.
63
+ CREATE TRIGGER core_media_object_ad AFTER DELETE ON core_media
64
+ BEGIN
65
+ INSERT OR IGNORE INTO core_blob_deletion (key, queued_at)
66
+ SELECT COALESCE(OLD.key, 'stream/' || OLD.stream_uid), strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
67
+ WHERE OLD.key IS NOT NULL OR OLD.stream_uid IS NOT NULL;
68
+ END;
69
+
70
+ -- The live media items, as staff list them. Outsiders read none of it: an item reaches them only
71
+ -- through media.serve (pictures and files) or media.play (videos).
72
+ CREATE VIEW rec_media AS
73
+ SELECT id, kind, key, name, type, bytes, width, height, alt, stream_uid, state, state_reason,
74
+ duration, created_at, created_by, updated_at, updated_by
75
+ FROM core_media WHERE deleted_at IS NULL;
76
+
77
+ -- A video's caption tracks: one per language. `state` is Stream's for a generated one.
78
+ CREATE TABLE core_media_caption (
79
+ media_id TEXT NOT NULL,
80
+ language TEXT NOT NULL CHECK (length(language) BETWEEN 2 AND 35),
81
+ label TEXT NOT NULL CHECK (length(label) BETWEEN 1 AND 100),
82
+ generated INTEGER NOT NULL DEFAULT 0 CHECK (generated IN (0, 1)),
83
+ state TEXT NOT NULL CHECK (state IN ('processing', 'ready', 'failed')),
84
+ created_at TEXT NOT NULL,
85
+ created_by TEXT,
86
+ PRIMARY KEY (media_id, language)
87
+ ) WITHOUT ROWID;
88
+
89
+ -- A purged video takes its captions with it (Stream deletes them with the video).
90
+ CREATE TRIGGER core_media_caption_ad AFTER DELETE ON core_media
91
+ BEGIN
92
+ DELETE FROM core_media_caption WHERE media_id = OLD.id;
93
+ END;
94
+
95
+ CREATE TABLE core_published_media_next (
96
+ media_id TEXT NOT NULL,
97
+ object_type TEXT NOT NULL,
98
+ record_id TEXT NOT NULL,
99
+ field TEXT NOT NULL,
100
+ reach TEXT NOT NULL DEFAULT 'all' CHECK (reach IN ('all', 'holders', 'private')),
101
+ PRIMARY KEY (media_id, object_type, record_id, field, reach)
102
+ ) WITHOUT ROWID;
103
+
104
+ INSERT INTO core_published_media_next (media_id, object_type, record_id, field, reach)
105
+ SELECT media_id, object_type, record_id, field, 'all' FROM core_published_media;
106
+
107
+ DROP TABLE core_published_media;
108
+ ALTER TABLE core_published_media_next RENAME TO core_published_media;
109
+ CREATE INDEX core_published_media_record_idx ON core_published_media (object_type, record_id);
@@ -0,0 +1,63 @@
1
+ -- Migration 0516: tax at checkout (stage 42 section 4, ADR 0479).
2
+ --
3
+ -- Prices are tax-inclusive. When a human admin turns tax on (core_settings `order_tax`) and the
4
+ -- object bought declares a product tax code (`sellable.tax`), the payments gatekeeper asks the
5
+ -- provider (Stripe Tax) to calculate tax inside each payment. What the provider says is stored
6
+ -- here, payment by payment, and never recalculated by the host. Totals do not change: tax is
7
+ -- inside them.
8
+
9
+ -- A checkout opened with tax: the lines its amount was made of, each with its code and place,
10
+ -- as sent to the provider (JSON). Null: opened without tax, exactly as before.
11
+ ALTER TABLE core_order_checkout ADD COLUMN tax_lines TEXT;
12
+
13
+ -- The provider's tax on one verified payment and the country it was charged for. Null: the
14
+ -- checkout was not taxed.
15
+ ALTER TABLE core_order_payment ADD COLUMN tax_amount INTEGER CHECK (tax_amount IS NULL OR tax_amount >= 0);
16
+ ALTER TABLE core_order_payment ADD COLUMN tax_country TEXT;
17
+
18
+ -- The order's running tax: the sum of its applied payments' tax, what of it the provider said
19
+ -- went back with refunds, and the country of the latest taxed payment.
20
+ ALTER TABLE core_order ADD COLUMN tax_amount INTEGER NOT NULL DEFAULT 0 CHECK (tax_amount >= 0);
21
+ ALTER TABLE core_order ADD COLUMN tax_refunded INTEGER NOT NULL DEFAULT 0 CHECK (tax_refunded >= 0);
22
+ ALTER TABLE core_order ADD COLUMN tax_country TEXT;
23
+
24
+ -- A line's tax: the sum of the provider's tax on its share of each payment, and the code it was
25
+ -- last charged at.
26
+ ALTER TABLE core_order_line ADD COLUMN tax_amount INTEGER NOT NULL DEFAULT 0 CHECK (tax_amount >= 0);
27
+ ALTER TABLE core_order_line ADD COLUMN tax_code TEXT;
28
+
29
+ -- The tax a refund gave back, when the provider's refund event says. Null: it did not say
30
+ -- (Stripe's Refund carries no tax figure; ADR 0479).
31
+ ALTER TABLE core_order_refund ADD COLUMN tax_amount INTEGER CHECK (tax_amount IS NULL OR tax_amount >= 0);
32
+
33
+ -- The version check covers the order's tax too.
34
+ DROP TRIGGER core_order_stale;
35
+ CREATE TRIGGER core_order_stale
36
+ BEFORE UPDATE OF state, paid, refunded, total, deposit, balance_due_at, tax_amount, tax_refunded
37
+ ON core_order
38
+ WHEN NEW.version <> OLD.version + 1
39
+ BEGIN
40
+ SELECT RAISE(ABORT, 'core_order_stale');
41
+ END;
42
+
43
+ -- `net`: what the company keeps, net of tax: paid less refunded, less the tax still held.
44
+ DROP VIEW rec_order;
45
+ CREATE VIEW rec_order AS
46
+ SELECT
47
+ id, person_id, email, currency, total, paid, total - paid AS balance, refunded, state, pay,
48
+ deposit, balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at,
49
+ updated_at, tax_amount, tax_refunded, tax_country,
50
+ (paid - refunded) - (tax_amount - tax_refunded) AS net
51
+ FROM core_order;
52
+
53
+ DROP VIEW rec_order_line;
54
+ CREATE VIEW rec_order_line AS
55
+ SELECT
56
+ l.id, l.order_id, o.person_id, l.position, l.object_type, l.record_id, l.title, l.quantity,
57
+ l.unit_price, l.line_total, l.created_at, l.tax_amount, l.tax_code
58
+ FROM core_order_line l
59
+ JOIN core_order o ON o.id = l.order_id;
60
+
61
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
62
+ UPDATE core_meta SET value = '516'
63
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 516;
@@ -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;