@composable-os/cli 0.1.0-beta.11 → 0.1.0-beta.14

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 (166) hide show
  1. package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
  2. package/dist/app-OSPZHKOI.js +43 -0
  3. package/dist/app-upload-LH4PXKI4.js +198 -0
  4. package/dist/{blocks-5IFU25J6.js → blocks-6DZIP47W.js} +207 -25
  5. package/dist/{chunk-XIDLW3EP.js → chunk-2B6YUYZV.js} +23499 -11766
  6. package/dist/{run-7P4RQALH.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/{chunk-DHS2CV2M.js → chunk-5D7BDF4Y.js} +15 -13
  9. package/dist/{chunk-MBIKVRAC.js → chunk-5YDYQM6V.js} +830 -47
  10. package/dist/{chunk-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
  11. package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
  12. package/dist/chunk-EPDTPGZJ.js +330 -0
  13. package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
  14. package/dist/chunk-HCG6T53D.js +61904 -0
  15. package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
  16. package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
  17. package/dist/chunk-KMFMD52Z.js +13276 -0
  18. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  19. package/dist/{chunk-PDD2ETVF.js → chunk-O37YUZ52.js} +172 -16
  20. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  21. package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-YFTLIEWU.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-LO6WGU5Y.js → chunk-U72NEGCT.js} +8089 -3464
  26. package/dist/{chunk-P6UWZ6QQ.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/{chunk-2IVEVPMY.js → chunk-Z7OVQ6BC.js} +105 -61
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-5UNY5G5L.js → cli-OKQHHSOE.js} +17 -25
  30. package/dist/{cli-UYZMVQMR.js → cli-VYSD2S4Z.js} +227 -81
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-SE4EKG6W.js → client-repo-MPCPTXLF.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-FTWWMD7N.js → fields-GTHE6TTS.js} +27 -3
  35. package/dist/gateway-DPPEQZT4.js +25 -0
  36. package/dist/grants-6JY4XZO7.js +27 -0
  37. package/dist/invoke-MRONRZ5Q.js +33 -0
  38. package/dist/moves-UAUCMWL5.js +36 -0
  39. package/dist/{order-U4KZLLSJ.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-UHLVBVLR.js → person-YOZTFV4Q.js} +11 -10
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  43. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  44. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  45. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  46. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  47. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  48. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  49. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  50. package/dist/platform/migrations/0815_checklists.sql +237 -0
  51. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  52. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  53. package/dist/platform/migrations/0850_agreements.sql +271 -0
  54. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  55. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  56. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  57. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  58. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  59. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  60. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  61. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  62. package/dist/platform/migrations/0911_texts.sql +60 -0
  63. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  64. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  65. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  66. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  67. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  68. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  69. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  70. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  71. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  72. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  73. package/dist/platform/migrations/0970_published_text.sql +59 -0
  74. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  75. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  76. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  77. package/dist/platform/migrations/1030_invoices.sql +747 -0
  78. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  79. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  80. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  81. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  82. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  83. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  84. package/dist/platform/migrations/1130_devices.sql +162 -0
  85. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  86. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  87. package/dist/platform/migrations/1151_payouts.sql +202 -0
  88. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  89. package/dist/platform/migrations/1170_journeys.sql +318 -0
  90. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  91. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  92. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  93. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  94. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  95. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  96. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  97. package/dist/platform/migrations/1271_leave.sql +249 -0
  98. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  99. package/dist/platform/migrations/1330_accounting.sql +260 -0
  100. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  101. package/dist/platform/migrations/1390_locations.sql +77 -0
  102. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  103. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  104. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  105. package/dist/platform/sdk/acknowledge.ts +100 -0
  106. package/dist/platform/sdk/agreements.ts +185 -0
  107. package/dist/platform/sdk/allowance.ts +98 -8
  108. package/dist/platform/sdk/automation-check.ts +120 -9
  109. package/dist/platform/sdk/automation.ts +78 -10
  110. package/dist/platform/sdk/behaviour.ts +26 -6
  111. package/dist/platform/sdk/calendar-entry.ts +258 -0
  112. package/dist/platform/sdk/capacity.ts +1 -2
  113. package/dist/platform/sdk/checklists.ts +118 -0
  114. package/dist/platform/sdk/contract.ts +489 -27
  115. package/dist/platform/sdk/discount.ts +408 -0
  116. package/dist/platform/sdk/document.ts +4 -2
  117. package/dist/platform/sdk/field-types.ts +5 -0
  118. package/dist/platform/sdk/formula/compile.ts +10 -0
  119. package/dist/platform/sdk/index.ts +17 -0
  120. package/dist/platform/sdk/intake.ts +380 -0
  121. package/dist/platform/sdk/journey-signals.ts +215 -0
  122. package/dist/platform/sdk/manifest.ts +1236 -65
  123. package/dist/platform/sdk/mine.ts +161 -0
  124. package/dist/platform/sdk/occupies.ts +192 -0
  125. package/dist/platform/sdk/questions.ts +681 -0
  126. package/dist/platform/sdk/recurring.ts +90 -9
  127. package/dist/platform/sdk/restricted-fields.ts +129 -0
  128. package/dist/platform/sdk/rich-text.ts +112 -0
  129. package/dist/platform/sdk/routine-mail.ts +1272 -0
  130. package/dist/platform/sdk/runtime.ts +25 -12
  131. package/dist/platform/sdk/screens.ts +91 -0
  132. package/dist/platform/sdk/sellable.ts +344 -0
  133. package/dist/platform/sdk/slug.ts +7 -2
  134. package/dist/platform/sdk/staffed.ts +474 -0
  135. package/dist/platform/sdk/states.ts +68 -26
  136. package/dist/platform/sdk/stock.ts +332 -0
  137. package/dist/platform/sdk/team.ts +83 -0
  138. package/dist/platform/sdk/testing.ts +70 -6
  139. package/dist/platform/sdk/worked-on.ts +134 -0
  140. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  141. package/dist/run-7ID64RA6.js +65 -0
  142. package/dist/run-FH7OGGTV.js +131 -0
  143. package/dist/run-OYZIAP6M.js +3363 -0
  144. package/dist/{run-ZCGFMEZF.js → run-QOFRRSZV.js} +41 -60
  145. package/dist/{run-PO3W5BFA.js → run-TJAEWR5U.js} +21 -51
  146. package/dist/{src-I3IJ4NKC.js → src-52VERUPK.js} +306 -4
  147. package/dist/src-PHRELWN5.js +40 -0
  148. package/dist/{visibility-TVXZ67FD.js → visibility-SZCINPP4.js} +21 -4
  149. package/package.json +2 -2
  150. package/dist/chunk-2TWV3SVB.js +0 -349
  151. package/dist/chunk-2XC5JLBO.js +0 -1931
  152. package/dist/chunk-324WJXNP.js +0 -320
  153. package/dist/chunk-6ELZRNUU.js +0 -116
  154. package/dist/chunk-6WGFA2IV.js +0 -643
  155. package/dist/chunk-7CQGMVDO.js +0 -63
  156. package/dist/chunk-7OGCMQXG.js +0 -284
  157. package/dist/chunk-FJIZGS5Q.js +0 -698
  158. package/dist/chunk-UB4I2QKR.js +0 -31575
  159. package/dist/chunk-WHKJGF36.js +0 -2418
  160. package/dist/chunk-YNUZRTPH.js +0 -182
  161. package/dist/chunk-ZZFWMVJO.js +0 -704
  162. package/dist/grants-LOW6TSWD.js +0 -32
  163. package/dist/invoke-6KVT6WJI.js +0 -41
  164. package/dist/moves-E2HKYCTK.js +0 -41
  165. package/dist/plan-on-sale-TJPFCNL7.js +0 -26
  166. package/dist/src-CRO35DYY.js +0 -46
@@ -0,0 +1,202 @@
1
+ -- Migration 1151: payouts read from the payment provider (stage 52 step 4, ADR 0793).
2
+ --
3
+ -- A payout is money the provider sent from the business's balance to its bank; its lines are the
4
+ -- payments, refunds, disputes, adjustments and fees it settled. The host reads them once a day
5
+ -- through the payments gatekeeper (read only: nothing moves, and the bank account a payout went
6
+ -- to is never read) and keeps them here as the provider reported them, so the Payouts report and
7
+ -- each month's payout entries (ADR 0791) can tie what landed in the bank to what was sold.
8
+ --
9
+ -- What the provider reported is never rewritten. A payout is recorded once, when it has arrived
10
+ -- (`paid`) and every one of its lines has been read; its lines are written while it is read (a
11
+ -- payout of many lines can take several runs) and never once it is recorded. A payout that fails
12
+ -- after it arrived (the bank returned it) is a correction of its own (`core_payout_failure`),
13
+ -- never an edit. Reading the same payouts again records nothing again: everything is keyed by the
14
+ -- provider's own ids.
15
+ --
16
+ -- All of it is the business's figures: core tables in no rec_* view, so no dashboard, query.sql,
17
+ -- export.csv or the assistant's record reads reach them; they are read only through the finance
18
+ -- operations, by an account holding `finance:read`. Nothing here names a person.
19
+ --
20
+ -- D1 runs with recursive triggers off, so each table's BEFORE INSERT also refuses a REPLACE or an
21
+ -- upsert onto a row it holds, and its BEFORE UPDATE every change (an UPDATE OR REPLACE is one).
22
+
23
+ -- One payout, as the provider reported it once it arrived. `amount` is what reached the bank;
24
+ -- `line_count` and `lines_net` (its lines' amounts less their fees) are checked against its lines
25
+ -- when it is recorded; a difference between `amount` and `lines_net` is the provider's, shown and
26
+ -- told, never adjusted. `itemised` 0: a payout made by hand at the provider, with no lines.
27
+ -- `status` is what it was when recorded: `paid` (it arrived), or `failed` (it never arrived: read
28
+ -- when it failed, or from the reversal line of the payout that paid its money again, so the
29
+ -- payments it settled are explained), always with its failure beside it. `unreadable_lines`: lines
30
+ -- the gatekeeper could not read, left out and counted (they show as the payout's difference).
31
+ CREATE TABLE core_payout (
32
+ provider TEXT NOT NULL,
33
+ ref TEXT NOT NULL,
34
+ amount INTEGER NOT NULL,
35
+ currency TEXT NOT NULL CHECK (currency GLOB '[A-Z][A-Z][A-Z]'),
36
+ arrival_date TEXT NOT NULL
37
+ CHECK (arrival_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
38
+ status TEXT NOT NULL CHECK (status IN ('paid', 'failed')),
39
+ itemised INTEGER NOT NULL CHECK (itemised IN (0, 1)),
40
+ line_count INTEGER NOT NULL CHECK (line_count >= 0),
41
+ lines_net INTEGER NOT NULL,
42
+ unreadable_lines INTEGER NOT NULL DEFAULT 0 CHECK (unreadable_lines >= 0),
43
+ created_at TEXT NOT NULL,
44
+ read_at TEXT NOT NULL,
45
+ PRIMARY KEY (provider, ref)
46
+ ) WITHOUT ROWID;
47
+
48
+ CREATE INDEX core_payout_arrival ON core_payout (arrival_date);
49
+
50
+ -- One line of a payout: `amount` into the balance (positive) or out of it (negative), before the
51
+ -- provider's `fee` (positive: charged). `payment_ref` is the payment it is for, as the host stores
52
+ -- it on core_order_payment; `refund_ref` a refund's own reference (core_order_refund's
53
+ -- provider_refund_ref). A line is the provider's movement: it is in one payout only. A
54
+ -- `payout_reversal` is an earlier payout's money back in the balance after the bank returned it
55
+ -- (`reversal_of` names that payout).
56
+ CREATE TABLE core_payout_line (
57
+ provider TEXT NOT NULL,
58
+ ref TEXT NOT NULL,
59
+ payout_ref TEXT NOT NULL,
60
+ kind TEXT NOT NULL
61
+ CHECK (kind IN ('payment', 'refund', 'dispute', 'adjustment', 'fee', 'payout_reversal')),
62
+ amount INTEGER NOT NULL,
63
+ fee INTEGER NOT NULL,
64
+ currency TEXT NOT NULL,
65
+ payment_ref TEXT,
66
+ refund_ref TEXT,
67
+ reversal_of TEXT,
68
+ occurred_at TEXT NOT NULL,
69
+ PRIMARY KEY (provider, ref)
70
+ ) WITHOUT ROWID;
71
+
72
+ CREATE INDEX core_payout_line_payout ON core_payout_line (provider, payout_ref);
73
+ CREATE INDEX core_payout_line_payment ON core_payout_line (provider, payment_ref)
74
+ WHERE payment_ref IS NOT NULL;
75
+ CREATE INDEX core_payout_line_refund ON core_payout_line (provider, refund_ref)
76
+ WHERE refund_ref IS NOT NULL;
77
+
78
+ -- A recorded payout the provider later reported failed (the bank returned it; its money went back
79
+ -- to the balance, to be paid out again). `date` is the business's day the host saw it, the day it
80
+ -- is counted on.
81
+ CREATE TABLE core_payout_failure (
82
+ provider TEXT NOT NULL,
83
+ payout_ref TEXT NOT NULL,
84
+ date TEXT NOT NULL CHECK (date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
85
+ seen_at TEXT NOT NULL,
86
+ PRIMARY KEY (provider, payout_ref)
87
+ ) WITHOUT ROWID;
88
+
89
+ CREATE INDEX core_payout_failure_date ON core_payout_failure (date);
90
+
91
+ -- Where the daily read is, one row per provider and account (`account`: the connection and the
92
+ -- resource it binds), so another account starts its own history: the pass in progress (from which
93
+ -- arrival day, the provider's page cursor, the payout whose lines are being read, as the provider
94
+ -- reported it, and `pending`, failed payouts named by a reversal line still to read), the
95
+ -- business's day the pass started and whether it finished, `last_ok_date`, the day of the last pass
96
+ -- that read everything (the next pass reads from 14 days before it, however long the connection
97
+ -- was refused or failing since), whether the connection may read payouts at all, and `next_at`, the
98
+ -- instant the next run is due (the next day's pass, the rest of a backlog, a retry after the
99
+ -- provider failed; a row of an account no longer bound is retired with a `next_at` far ahead):
100
+ -- a quiet minute reads these rows by their key and nothing else. The one table here that changes;
101
+ -- it holds no figure. `version` keeps two runs at once from moving it back.
102
+ CREATE TABLE core_payout_sync (
103
+ provider TEXT NOT NULL,
104
+ account TEXT NOT NULL,
105
+ pass_date TEXT NOT NULL,
106
+ last_ok_date TEXT,
107
+ pending TEXT CHECK (pending IS NULL OR json_valid(pending)),
108
+ done INTEGER NOT NULL CHECK (done IN (0, 1)),
109
+ since TEXT NOT NULL,
110
+ cursor TEXT,
111
+ reading TEXT CHECK (reading IS NULL OR json_valid(reading)),
112
+ line_cursor TEXT,
113
+ state TEXT NOT NULL CHECK (state IN ('ok', 'unavailable', 'failing')),
114
+ reason TEXT,
115
+ failures INTEGER NOT NULL DEFAULT 0 CHECK (failures >= 0),
116
+ next_at TEXT NOT NULL,
117
+ version INTEGER NOT NULL CHECK (version >= 1),
118
+ updated_at TEXT NOT NULL,
119
+ PRIMARY KEY (provider, account)
120
+ );
121
+
122
+ -- Recorded once, with exactly the lines read for it; never replaced, changed or removed.
123
+ CREATE TRIGGER core_payout_insert
124
+ BEFORE INSERT ON core_payout
125
+ BEGIN
126
+ SELECT RAISE(ABORT, 'core_payout_fixed')
127
+ WHERE EXISTS (SELECT 1 FROM core_payout p WHERE p.provider = NEW.provider AND p.ref = NEW.ref)
128
+ OR NEW.line_count != (SELECT count(*) FROM core_payout_line l
129
+ WHERE l.provider = NEW.provider AND l.payout_ref = NEW.ref)
130
+ OR NEW.lines_net != (SELECT coalesce(sum(l.amount - l.fee), 0) FROM core_payout_line l
131
+ WHERE l.provider = NEW.provider AND l.payout_ref = NEW.ref)
132
+ OR (NEW.itemised = 0 AND NEW.line_count != 0);
133
+ END;
134
+
135
+ CREATE TRIGGER core_payout_update
136
+ BEFORE UPDATE ON core_payout
137
+ BEGIN
138
+ SELECT RAISE(ABORT, 'core_payout_fixed');
139
+ END;
140
+
141
+ CREATE TRIGGER core_payout_delete
142
+ BEFORE DELETE ON core_payout
143
+ BEGIN
144
+ SELECT RAISE(ABORT, 'core_payout_fixed');
145
+ END;
146
+
147
+ -- A line is written while its payout is being read, once; never after the payout is recorded.
148
+ CREATE TRIGGER core_payout_line_insert
149
+ BEFORE INSERT ON core_payout_line
150
+ BEGIN
151
+ SELECT RAISE(ABORT, 'core_payout_fixed')
152
+ WHERE EXISTS (SELECT 1 FROM core_payout_line l WHERE l.provider = NEW.provider AND l.ref = NEW.ref)
153
+ OR EXISTS (SELECT 1 FROM core_payout p
154
+ WHERE p.provider = NEW.provider AND p.ref = NEW.payout_ref);
155
+ END;
156
+
157
+ CREATE TRIGGER core_payout_line_update
158
+ BEFORE UPDATE ON core_payout_line
159
+ BEGIN
160
+ SELECT RAISE(ABORT, 'core_payout_fixed');
161
+ END;
162
+
163
+ CREATE TRIGGER core_payout_line_delete
164
+ BEFORE DELETE ON core_payout_line
165
+ BEGIN
166
+ SELECT RAISE(ABORT, 'core_payout_fixed');
167
+ END;
168
+
169
+ -- A failure is of a recorded payout, once; never changed or removed.
170
+ CREATE TRIGGER core_payout_failure_insert
171
+ BEFORE INSERT ON core_payout_failure
172
+ BEGIN
173
+ SELECT RAISE(ABORT, 'core_payout_fixed')
174
+ WHERE EXISTS (SELECT 1 FROM core_payout_failure f
175
+ WHERE f.provider = NEW.provider AND f.payout_ref = NEW.payout_ref)
176
+ OR NOT EXISTS (SELECT 1 FROM core_payout p
177
+ WHERE p.provider = NEW.provider AND p.ref = NEW.payout_ref);
178
+ END;
179
+
180
+ CREATE TRIGGER core_payout_failure_update
181
+ BEFORE UPDATE ON core_payout_failure
182
+ BEGIN
183
+ SELECT RAISE(ABORT, 'core_payout_fixed');
184
+ END;
185
+
186
+ CREATE TRIGGER core_payout_failure_delete
187
+ BEFORE DELETE ON core_payout_failure
188
+ BEGIN
189
+ SELECT RAISE(ABORT, 'core_payout_fixed');
190
+ END;
191
+
192
+ -- Matching a payout's lines to what the host recorded: a provider payment by its reference, and
193
+ -- the provider's payments received in a span (what is not yet paid out). Indexes only: no column,
194
+ -- row or trigger of the order tables changes.
195
+ CREATE INDEX core_order_payment_ref_idx ON core_order_payment (provider, payment_ref)
196
+ WHERE method = 'provider' AND payment_ref IS NOT NULL;
197
+ CREATE INDEX core_order_payment_provider_received_idx ON core_order_payment (received_at)
198
+ WHERE method = 'provider';
199
+
200
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
201
+ UPDATE core_meta SET value = '1151'
202
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1151;
@@ -0,0 +1,18 @@
1
+ -- Migration 1153: the dates finance counts money on, indexed (stage 52 step 3, ADR 0796).
2
+ --
3
+ -- A month's day summaries, Sales, Tax and Refunds read the payments the host applied in it (by
4
+ -- `received_at`), the late payments staff honoured in it (by `resolved_at`) and the refunds the
5
+ -- provider confirmed in it (by `confirmed_at`). Without these indexes each read would scan every
6
+ -- payment and refund the business ever took; with them a month reads its own rows. Indexes only:
7
+ -- no column, no row and no trigger of the order tables changes. (1151 and 1152 are the spec's
8
+ -- for payouts and the subscription history, stage 52 steps 4 and 5.)
9
+ CREATE INDEX core_order_payment_received_idx ON core_order_payment (received_at)
10
+ WHERE outcome = 'applied';
11
+ CREATE INDEX core_order_payment_honoured_idx ON core_order_payment (resolved_at)
12
+ WHERE outcome = 'late' AND resolved = 'honoured';
13
+ CREATE INDEX core_order_refund_confirmed_idx ON core_order_refund (confirmed_at)
14
+ WHERE status = 'confirmed';
15
+
16
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
17
+ UPDATE core_meta SET value = '1153'
18
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1153;
@@ -0,0 +1,318 @@
1
+ -- Migration 1170: journeys, their versions, entries and step log (stage 53 step 3, ADR 0800).
2
+ --
3
+ -- A journey is a named set of steps the host takes for one Person at a time (a welcome series, a
4
+ -- nudge when someone stops coming). Staff with `journeys:manage` (and the assistant for them) write
5
+ -- its working copy, `draft`: the entry rule, conditions, exits, re-entry rule, the reply-to and the
6
+ -- steps with their words, as one JSON document the host checks. A person with `journeys:publish`
7
+ -- publishes it as a numbered version, which nobody can change or delete afterwards; only a
8
+ -- published version is ever run (the engine and sending are stage 53 step 4).
9
+ --
10
+ -- None of these tables is a record: no rec_* view reads them, so query.sql, dashboards, exports,
11
+ -- search and the assistant's bulk reads never see them. Entries and the step log name a Person and
12
+ -- are reached by erase; the step log keeps no words.
13
+ CREATE TABLE core_journey (
14
+ id TEXT PRIMARY KEY,
15
+ name TEXT NOT NULL,
16
+ -- Fixed when the journey is made: a promotional journey's sends need a recorded yes (stage 30).
17
+ kind TEXT NOT NULL CHECK (kind IN ('service', 'promotional')),
18
+ -- draft: never live; live: entering and running people; paused: entries wait; archived: final.
19
+ status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'live', 'paused', 'archived')),
20
+ -- Who gets a task step assigned to "the owner" (a staff user), or nobody yet.
21
+ owner_id TEXT,
22
+ -- The working copy (JSON): what the editor changes, and what a publish snapshots.
23
+ draft TEXT NOT NULL,
24
+ -- Bumped by every save of the working copy: a publish names the one it was made from.
25
+ draft_rev INTEGER NOT NULL DEFAULT 0,
26
+ draft_updated_at TEXT NOT NULL,
27
+ draft_updated_by TEXT NOT NULL,
28
+ -- The actor of the last save (human, assistant, mcp, …, or host for a starter).
29
+ draft_updated_via TEXT NOT NULL DEFAULT 'human',
30
+ -- 1 when an AI actor wrote the working copy since the last publish: the publish dialog says
31
+ -- "drafted by the assistant", and the version keeps it.
32
+ draft_ai INTEGER NOT NULL DEFAULT 0 CHECK (draft_ai IN (0, 1)),
33
+ -- The newest published version: new entries start on it.
34
+ current_version_id TEXT,
35
+ -- A starter the host seeded (welcome, not_seen, win_back, trial_ended), or null.
36
+ starter TEXT,
37
+ created_at TEXT NOT NULL,
38
+ created_by TEXT NOT NULL,
39
+ status_at TEXT,
40
+ status_by TEXT
41
+ );
42
+ CREATE INDEX core_journey_status_idx ON core_journey (status);
43
+
44
+ -- Published steps and words, kept for good. Entries point at the one they started on.
45
+ CREATE TABLE core_journey_version (
46
+ id TEXT PRIMARY KEY,
47
+ journey_id TEXT NOT NULL REFERENCES core_journey (id),
48
+ number INTEGER NOT NULL CHECK (number >= 1),
49
+ -- The working copy at draft_rev, as stored: the checked document.
50
+ definition TEXT NOT NULL,
51
+ -- The plain-words summary the publisher was shown.
52
+ summary TEXT NOT NULL,
53
+ -- SHA-256 (hex) of the canonical JSON of the kind and the definition.
54
+ hash TEXT NOT NULL CHECK (length(hash) = 64),
55
+ draft_rev INTEGER NOT NULL,
56
+ -- Who wrote the working copy since the last publish: a person, or (in part) the assistant.
57
+ drafted_by TEXT NOT NULL CHECK (drafted_by IN ('person', 'assistant')),
58
+ published_at TEXT NOT NULL,
59
+ -- The staff account of the person who published it, or who approved an AI's request (ADR 0480).
60
+ published_by TEXT NOT NULL,
61
+ -- An AI's request: the account it acted for, and the actor. Both null for a person's own publish.
62
+ requested_by TEXT,
63
+ requested_via TEXT,
64
+ UNIQUE (journey_id, number)
65
+ );
66
+
67
+ -- Nobody changes or deletes a published version, by an operation or a later statement. D1 runs
68
+ -- with recursive triggers off, so an INSERT OR REPLACE fires no delete trigger: the insert
69
+ -- trigger refuses an existing id too.
70
+ CREATE TRIGGER core_journey_version_no_update
71
+ BEFORE UPDATE ON core_journey_version
72
+ BEGIN
73
+ SELECT RAISE(ABORT, 'core_journey_version_immutable');
74
+ END;
75
+
76
+ CREATE TRIGGER core_journey_version_no_delete
77
+ BEFORE DELETE ON core_journey_version
78
+ BEGIN
79
+ SELECT RAISE(ABORT, 'core_journey_version_immutable');
80
+ END;
81
+
82
+ -- A version is made only from the working copy as it is at commit (the rev the person reviewed),
83
+ -- of a journey that is not archived, numbered next, holding exactly the copy, saying truly whether
84
+ -- an AI wrote it, and published by a staff account (never the adjudicator, which is not one).
85
+ CREATE TRIGGER core_journey_version_from_draft
86
+ BEFORE INSERT ON core_journey_version
87
+ BEGIN
88
+ SELECT CASE
89
+ WHEN EXISTS (SELECT 1 FROM core_journey_version v WHERE v.id = NEW.id)
90
+ THEN RAISE(ABORT, 'core_journey_version_immutable')
91
+ WHEN NOT EXISTS (
92
+ SELECT 1 FROM core_journey j
93
+ WHERE j.id = NEW.journey_id AND j.draft_rev = NEW.draft_rev AND j.status != 'archived'
94
+ AND j.draft = NEW.definition
95
+ AND (NEW.drafted_by = 'assistant') = (j.draft_ai = 1)
96
+ )
97
+ THEN RAISE(ABORT, 'core_journey_draft_changed')
98
+ WHEN NEW.number != 1 + COALESCE(
99
+ (SELECT MAX(v.number) FROM core_journey_version v WHERE v.journey_id = NEW.journey_id), 0)
100
+ THEN RAISE(ABORT, 'core_journey_draft_changed')
101
+ WHEN NOT EXISTS (SELECT 1 FROM core_user u WHERE u.id = NEW.published_by)
102
+ THEN RAISE(ABORT, 'core_journey_publisher')
103
+ END;
104
+ END;
105
+
106
+ -- A new journey starts as a draft with nothing published, and never over an existing id.
107
+ CREATE TRIGGER core_journey_new
108
+ BEFORE INSERT ON core_journey
109
+ BEGIN
110
+ SELECT CASE
111
+ WHEN EXISTS (SELECT 1 FROM core_journey j WHERE j.id = NEW.id)
112
+ THEN RAISE(ABORT, 'core_journey_fixed')
113
+ WHEN NEW.status != 'draft' OR NEW.current_version_id IS NOT NULL OR NEW.draft_rev != 0
114
+ THEN RAISE(ABORT, 'core_journey_new')
115
+ END;
116
+ END;
117
+
118
+ -- The working copy's rev moves by exactly one per save (compare-and-set), and any change to the
119
+ -- copy moves it, raw writes included: a reviewed rev never covers different content.
120
+ CREATE TRIGGER core_journey_draft_rev
121
+ BEFORE UPDATE OF draft_rev ON core_journey
122
+ WHEN NEW.draft_rev != OLD.draft_rev + 1
123
+ BEGIN
124
+ SELECT RAISE(ABORT, 'core_journey_draft_changed');
125
+ END;
126
+
127
+ CREATE TRIGGER core_journey_draft_changes
128
+ BEFORE UPDATE OF draft, draft_updated_at, draft_updated_by, draft_updated_via, draft_ai
129
+ ON core_journey
130
+ WHEN (NEW.draft IS NOT OLD.draft OR NEW.draft_updated_at IS NOT OLD.draft_updated_at
131
+ OR NEW.draft_updated_by IS NOT OLD.draft_updated_by
132
+ OR NEW.draft_updated_via IS NOT OLD.draft_updated_via OR NEW.draft_ai IS NOT OLD.draft_ai)
133
+ AND NEW.draft_rev IS NOT OLD.draft_rev + 1
134
+ BEGIN
135
+ SELECT RAISE(ABORT, 'core_journey_draft_changed');
136
+ END;
137
+
138
+ -- A journey keeps its id and kind.
139
+ CREATE TRIGGER core_journey_fixed
140
+ BEFORE UPDATE OF id, kind ON core_journey
141
+ WHEN NEW.id IS NOT OLD.id OR NEW.kind IS NOT OLD.kind
142
+ BEGIN
143
+ SELECT RAISE(ABORT, 'core_journey_fixed');
144
+ END;
145
+
146
+ -- Live or paused only with a published version; never back to draft; archived is final.
147
+ CREATE TRIGGER core_journey_status
148
+ BEFORE UPDATE OF status ON core_journey
149
+ WHEN NEW.status IS NOT OLD.status
150
+ BEGIN
151
+ SELECT CASE
152
+ WHEN OLD.status = 'archived' OR NEW.status = 'draft'
153
+ THEN RAISE(ABORT, 'core_journey_status')
154
+ WHEN NEW.status IN ('live', 'paused') AND NEW.current_version_id IS NULL
155
+ THEN RAISE(ABORT, 'core_journey_status')
156
+ END;
157
+ END;
158
+
159
+ -- The current version is one of its own and only ever a newer one.
160
+ CREATE TRIGGER core_journey_current
161
+ BEFORE UPDATE OF current_version_id ON core_journey
162
+ WHEN NEW.current_version_id IS NOT OLD.current_version_id
163
+ BEGIN
164
+ SELECT RAISE(ABORT, 'core_journey_current')
165
+ WHERE NEW.current_version_id IS NULL OR NOT EXISTS (
166
+ SELECT 1 FROM core_journey_version v
167
+ WHERE v.id = NEW.current_version_id AND v.journey_id = NEW.id
168
+ AND v.number > COALESCE(
169
+ (SELECT o.number FROM core_journey_version o WHERE o.id = OLD.current_version_id), 0)
170
+ );
171
+ END;
172
+
173
+ -- An archived journey stays as it was.
174
+ CREATE TRIGGER core_journey_archived
175
+ BEFORE UPDATE ON core_journey
176
+ WHEN OLD.status = 'archived'
177
+ BEGIN
178
+ SELECT RAISE(ABORT, 'core_journey_status');
179
+ END;
180
+
181
+ -- Journeys are the business's words, kept with their versions: archived, never deleted.
182
+ CREATE TRIGGER core_journey_no_delete
183
+ BEFORE DELETE ON core_journey
184
+ BEGIN
185
+ SELECT RAISE(ABORT, 'core_journey_fixed');
186
+ END;
187
+
188
+ -- One Person on one journey. `step` is the id of the step it is at (in its version's steps);
189
+ -- `next_at` is when the engine next looks at it (step 4's minute tick reads it by the index).
190
+ CREATE TABLE core_journey_entry (
191
+ id TEXT PRIMARY KEY,
192
+ journey_id TEXT NOT NULL REFERENCES core_journey (id),
193
+ version_id TEXT NOT NULL REFERENCES core_journey_version (id),
194
+ person_id TEXT NOT NULL,
195
+ state TEXT NOT NULL DEFAULT 'active'
196
+ CHECK (state IN ('active', 'waiting', 'held', 'finished', 'stopped')),
197
+ step TEXT NOT NULL,
198
+ next_at TEXT,
199
+ entered_at TEXT NOT NULL,
200
+ -- How: a signal, the daily scan, a birthday, or a person adding them (`journey.enter`).
201
+ entered_by TEXT NOT NULL CHECK (entered_by IN ('signal', 'scan', 'birthday', 'person')),
202
+ -- The staff account that added them, for a manual entry.
203
+ entered_by_user TEXT,
204
+ -- The standing of what put them on it: a person, the host, or an AI no person approved. The
205
+ -- send decision (step 4) holds every message of an `ai` entry for a person.
206
+ cause_standing TEXT NOT NULL CHECK (cause_standing IN ('person', 'host', 'ai')),
207
+ -- The record whose signal entered them (a membership), or null.
208
+ cause_record TEXT,
209
+ ended_at TEXT,
210
+ stopped_by TEXT,
211
+ stop_reason TEXT,
212
+ CHECK ((state IN ('finished', 'stopped')) = (ended_at IS NOT NULL))
213
+ );
214
+ -- One live entry per Person per journey.
215
+ CREATE UNIQUE INDEX core_journey_entry_live_idx ON core_journey_entry (journey_id, person_id)
216
+ WHERE state IN ('active', 'waiting', 'held');
217
+ -- The tick: due entries by state and time, in batches.
218
+ CREATE INDEX core_journey_entry_due_idx ON core_journey_entry (state, next_at);
219
+ CREATE INDEX core_journey_entry_person_idx ON core_journey_entry (person_id, journey_id);
220
+ -- A journey's entries by state, newest first: its counts and its list of entries, finished and
221
+ -- stopped ones included, without reading every entry ever made.
222
+ CREATE INDEX core_journey_entry_journey_idx ON core_journey_entry (journey_id, state, entered_at);
223
+
224
+ -- An entry is made only on a live journey, on that journey's own version, and never over an
225
+ -- existing id: D1 runs with recursive triggers off, so the row an INSERT OR REPLACE deletes fires
226
+ -- no trigger, and a REPLACE could otherwise bring back a stopped entry with another standing.
227
+ CREATE TRIGGER core_journey_entry_new
228
+ BEFORE INSERT ON core_journey_entry
229
+ BEGIN
230
+ SELECT CASE
231
+ WHEN EXISTS (SELECT 1 FROM core_journey_entry e WHERE e.id = NEW.id)
232
+ THEN RAISE(ABORT, 'core_journey_entry_fixed')
233
+ WHEN NEW.state IN ('finished', 'stopped') OR NOT EXISTS (
234
+ SELECT 1 FROM core_journey j JOIN core_journey_version v ON v.journey_id = j.id
235
+ WHERE j.id = NEW.journey_id AND v.id = NEW.version_id AND j.status = 'live'
236
+ )
237
+ THEN RAISE(ABORT, 'core_journey_not_live')
238
+ END;
239
+ END;
240
+
241
+ -- An entry stays on its journey and Person, keeps how and why it entered, and moves only to a
242
+ -- newer version of its own journey (a publish that moves entries); a finished or stopped one
243
+ -- stays so.
244
+ CREATE TRIGGER core_journey_entry_fixed
245
+ BEFORE UPDATE ON core_journey_entry
246
+ WHEN NEW.id IS NOT OLD.id OR NEW.journey_id IS NOT OLD.journey_id
247
+ OR NEW.person_id IS NOT OLD.person_id OR NEW.entered_at IS NOT OLD.entered_at
248
+ OR NEW.entered_by IS NOT OLD.entered_by OR NEW.entered_by_user IS NOT OLD.entered_by_user
249
+ OR NEW.cause_standing IS NOT OLD.cause_standing OR NEW.cause_record IS NOT OLD.cause_record
250
+ OR OLD.state IN ('finished', 'stopped')
251
+ OR (NEW.version_id IS NOT OLD.version_id AND NOT EXISTS (
252
+ SELECT 1 FROM core_journey_version v
253
+ WHERE v.id = NEW.version_id AND v.journey_id = OLD.journey_id
254
+ AND v.number > (SELECT o.number FROM core_journey_version o WHERE o.id = OLD.version_id)
255
+ ))
256
+ BEGIN
257
+ SELECT RAISE(ABORT, 'core_journey_entry_fixed');
258
+ END;
259
+
260
+ -- What happened at each step of an entry. Words are never stored here: a sent message's row in
261
+ -- the routine messages log (stage 30) keeps what it keeps; this names it.
262
+ CREATE TABLE core_journey_step_log (
263
+ id TEXT PRIMARY KEY,
264
+ entry_id TEXT NOT NULL REFERENCES core_journey_entry (id),
265
+ step TEXT NOT NULL,
266
+ at TEXT NOT NULL,
267
+ what TEXT NOT NULL CHECK (what IN (
268
+ 'entered', 'sent', 'held', 'skipped', 'waited', 'branch', 'task', 'finished', 'stopped', 'moved'
269
+ )),
270
+ -- Why, the branch taken, the wait's end, the message or task id: JSON, never words.
271
+ detail TEXT
272
+ );
273
+ CREATE INDEX core_journey_step_log_entry_idx ON core_journey_step_log (entry_id, at);
274
+
275
+ -- The log is added to, never rewritten (erase deletes it): no update, and no insert over an
276
+ -- existing id (a REPLACE is an insert).
277
+ CREATE TRIGGER core_journey_step_log_no_update
278
+ BEFORE UPDATE ON core_journey_step_log
279
+ BEGIN
280
+ SELECT RAISE(ABORT, 'core_journey_step_log_fixed');
281
+ END;
282
+
283
+ CREATE TRIGGER core_journey_step_log_new
284
+ BEFORE INSERT ON core_journey_step_log
285
+ WHEN EXISTS (SELECT 1 FROM core_journey_step_log l WHERE l.id = NEW.id)
286
+ BEGIN
287
+ SELECT RAISE(ABORT, 'core_journey_step_log_fixed');
288
+ END;
289
+
290
+ -- The four starter journeys (decision 9), seeded as drafts: never live, and with no reply-to, so
291
+ -- none can be published until a person edits it, publishes it and sets it live. Their signals are
292
+ -- the ones Memberships and Bookings declare (stage 53 step 2, ADR 0802).
293
+ INSERT INTO core_journey (id, name, kind, draft, draft_updated_at, draft_updated_by,
294
+ draft_updated_via, starter, created_at, created_by) VALUES
295
+ (
296
+ 'jny_starter_welcome', 'Welcome series', 'service',
297
+ '{"entry":{"type":"signal","signal":"memberships.started"},"conditions":[],"exits":["memberships.ended"],"reentry":{"rule":"once"},"includeBulk":false,"replyTo":null,"steps":[{"id":"welcome","type":"send","channel":"email","subject":"Welcome to {business}","body":"Hi {firstName},\n\nWelcome to {business}. We are glad you joined.\n\nIf you have any questions, just reply to this email.\n\n{unsubscribe}"},{"id":"wait_3","type":"wait","days":3,"hours":0,"weekdaysOnly":false},{"id":"settling","type":"send","channel":"email","subject":"How is your first week going?","body":"Hi {firstName},\n\nHow are your first few days going? Reply and tell us if there is anything we can help with.\n\n{unsubscribe}"},{"id":"wait_7","type":"wait","days":7,"hours":0,"weekdaysOnly":false},{"id":"habit","type":"send","channel":"email","subject":"Making it a habit","body":"Hi {firstName},\n\nYou have been with us for ten days. Booking your next few sessions ahead is the easiest way to keep going.\n\n{unsubscribe}"},{"id":"end","type":"end"}]}',
298
+ '2026-11-24T00:00:00.000Z', 'host', 'host', 'welcome', '2026-11-24T00:00:00.000Z', 'host'
299
+ ),
300
+ (
301
+ 'jny_starter_not_seen', 'We have not seen you', 'service',
302
+ '{"entry":{"type":"quiet","signal":"bookings.attended","days":14},"conditions":[{"test":"liveMembership","not":false}],"exits":["bookings.booked","bookings.attended"],"reentry":{"rule":"after","days":30},"includeBulk":false,"replyTo":null,"steps":[{"id":"nudge","type":"send","channel":"email","subject":"We have not seen you for a while","body":"Hi {firstName},\n\nWe have not seen you at {business} for two weeks. Your membership is still here for you, and we would love to see you back.\n\n{unsubscribe}"},{"id":"wait_7","type":"wait","days":7,"hours":0,"weekdaysOnly":false},{"id":"back","type":"branch","condition":{"test":"signalSinceEntry","signal":"bookings.attended","not":false},"goTo":"end","otherwise":"call"},{"id":"call","type":"task","title":"Call them: not seen for three weeks","assignee":"owner","dueDays":2},{"id":"end","type":"end"}]}',
303
+ '2026-11-24T00:00:00.000Z', 'host', 'host', 'not_seen', '2026-11-24T00:00:00.000Z', 'host'
304
+ ),
305
+ (
306
+ 'jny_starter_win_back', 'Win-back after a membership ends', 'promotional',
307
+ '{"entry":{"type":"signal","signal":"memberships.ended"},"conditions":[],"exits":["memberships.started","memberships.passStarted"],"reentry":{"rule":"after","days":180},"includeBulk":false,"replyTo":null,"steps":[{"id":"wait_14","type":"wait","days":14,"hours":0,"weekdaysOnly":false},{"id":"miss_you","type":"send","channel":"email","subject":"We miss you at {business}","body":"Hi {firstName},\n\nIt has been a couple of weeks since your membership ended. If you would like to come back, we would be glad to have you.\n\n{unsubscribe}"},{"id":"wait_21","type":"wait","days":21,"hours":0,"weekdaysOnly":false},{"id":"last","type":"send","channel":"email","subject":"Still thinking about it?","body":"Hi {firstName},\n\nJust checking in one last time. Reply to this email if there is anything we could do better.\n\n{unsubscribe}"},{"id":"end","type":"end"}]}',
308
+ '2026-11-24T00:00:00.000Z', 'host', 'host', 'win_back', '2026-11-24T00:00:00.000Z', 'host'
309
+ ),
310
+ (
311
+ 'jny_starter_trial_ended', 'Trial ended without joining', 'promotional',
312
+ '{"entry":{"type":"signal","signal":"memberships.trialEnded"},"conditions":[{"test":"liveMembership","not":true}],"exits":["memberships.started","memberships.passStarted"],"reentry":{"rule":"once"},"includeBulk":false,"replyTo":null,"steps":[{"id":"wait_1","type":"wait","days":1,"hours":0,"weekdaysOnly":false},{"id":"thanks","type":"send","channel":"email","subject":"Thanks for trying {business}","body":"Hi {firstName},\n\nThank you for trying {business}. We hope you enjoyed it. If you would like to keep going, we would love to have you as a member.\n\n{unsubscribe}"},{"id":"wait_5","type":"wait","days":5,"hours":0,"weekdaysOnly":false},{"id":"question","type":"send","channel":"email","subject":"Any questions about joining?","body":"Hi {firstName},\n\nIf anything is stopping you from joining, reply and let us know. We are happy to help.\n\n{unsubscribe}"},{"id":"end","type":"end"}]}',
313
+ '2026-11-24T00:00:00.000Z', 'host', 'host', 'trial_ended', '2026-11-24T00:00:00.000Z', 'host'
314
+ );
315
+
316
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
317
+ UPDATE core_meta SET value = '1170'
318
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1170;
@@ -0,0 +1,52 @@
1
+ -- Migration 1171: a birthday on Person (stage 53 step 7, ADR 0803).
2
+ --
3
+ -- Day and month only (decision 6 of the stage's spec): the host keeps no year and no date of
4
+ -- birth, so a birthday tells nobody a member's age, and a minor stays what the family link or the
5
+ -- Minor mark says (stage 44). Optional, both null or both set, and a day the month has (29
6
+ -- February is a birthday; a birthday journey keeps it on 28 February in other years). Set by staff
7
+ -- (`person.setBirthday`) or by the member for themselves (`me.birthday.set`), with a value mark
8
+ -- like any field (the pipeline, ADR 0590). In no `rec_*` view, so in no export or dashboard; erase
9
+ -- deletes it with the Person's row, and a merge keeps the survivor's unless it has none.
10
+ --
11
+ -- `core_birthday_guard` stores nothing: a birthday write's batch carries one row with the value the
12
+ -- write was planned from, and fails (`core_birthday_changed`) unless the Person still has it, so a
13
+ -- birthday set meanwhile (the member's own, while an AI's write was planned) is never overwritten
14
+ -- unseen.
15
+ --
16
+ -- The partial index is the birthday entry rule's: the People with a birthday on one day, read by
17
+ -- (month, day) in one indexed query.
18
+ ALTER TABLE core_person ADD COLUMN birth_day INTEGER
19
+ CHECK (birth_day IS NULL OR birth_day BETWEEN 1 AND 31);
20
+ ALTER TABLE core_person ADD COLUMN birth_month INTEGER
21
+ CHECK (
22
+ CASE
23
+ WHEN birth_month IS NULL THEN birth_day IS NULL
24
+ ELSE birth_month BETWEEN 1 AND 12
25
+ AND birth_day IS NOT NULL
26
+ AND birth_day <= CASE
27
+ WHEN birth_month = 2 THEN 29
28
+ WHEN birth_month IN (4, 6, 9, 11) THEN 30
29
+ ELSE 31
30
+ END
31
+ END
32
+ );
33
+
34
+ CREATE INDEX core_person_birthday_idx ON core_person (birth_month, birth_day, id)
35
+ WHERE birth_month IS NOT NULL AND deleted_at IS NULL;
36
+
37
+ CREATE TABLE core_birthday_guard (
38
+ person_id TEXT NOT NULL,
39
+ birth_day INTEGER,
40
+ birth_month INTEGER
41
+ );
42
+
43
+ CREATE TRIGGER core_birthday_guard_check
44
+ BEFORE INSERT ON core_birthday_guard
45
+ BEGIN
46
+ SELECT CASE
47
+ WHEN NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = NEW.person_id
48
+ AND p.birth_day IS NEW.birth_day AND p.birth_month IS NEW.birth_month)
49
+ THEN RAISE(ABORT, 'core_birthday_changed')
50
+ ELSE RAISE(IGNORE)
51
+ END;
52
+ END;
@@ -0,0 +1,29 @@
1
+ -- Migration 1175: journey signals (stage 53 step 2, ADR 0802).
2
+ --
3
+ -- The last time each Person had each journey signal a shipped pack declares (`journeySignals` in
4
+ -- its manifest: `memberships.started`, `bookings.attended`): one row per Person and signal,
5
+ -- written by the host only, in the batch of the write that made it (the pipeline, over the
6
+ -- write's recorded changes). A later write of the same signal replaces the row, so the table holds
7
+ -- one row per Person per signal however often it happens; an earlier time never replaces a later
8
+ -- one. `object_type` and `record_id` name the record that made it (the membership, the booking);
9
+ -- `standing` is who stood behind that write, as value marks say it (ADR 0590: `person`,
10
+ -- `customer`, `public`, `host`, `ai`), so a journey can hold what an AI's unapproved change
11
+ -- started; `bulk` is 1 when the write was a bulk one (an import, a change to many records), which
12
+ -- enters nobody unless a journey says so.
13
+ --
14
+ -- The index on (signal, last_at) is the daily scan's: "no `bookings.attended` for 14 days" reads
15
+ -- one signal's rows by time. Signals are a visit history by another name: they are in no `rec_*`
16
+ -- view, export or dashboard. Erase deletes a Person's rows; a merge keeps the later of each.
17
+ CREATE TABLE core_person_signal (
18
+ person_id TEXT NOT NULL,
19
+ signal TEXT NOT NULL,
20
+ last_at TEXT NOT NULL,
21
+ object_type TEXT NOT NULL,
22
+ record_id TEXT NOT NULL,
23
+ standing TEXT NOT NULL
24
+ CHECK (standing IN ('person', 'customer', 'public', 'default', 'host', 'ai')),
25
+ bulk INTEGER NOT NULL DEFAULT 0 CHECK (bulk IN (0, 1)),
26
+ PRIMARY KEY (person_id, signal)
27
+ ) WITHOUT ROWID;
28
+
29
+ CREATE INDEX core_person_signal_scan_idx ON core_person_signal (signal, last_at);
@@ -0,0 +1,25 @@
1
+ -- Migration 1190: restricted fields (stage 54 step 2, ADR 0811).
2
+ --
3
+ -- A pack may declare a text field `restricted: "<pack>:<word>"`, naming one of its restricted
4
+ -- permissions (ADR 0625). Its value is kept here, never in the object's x_* table: so it is in no
5
+ -- rec_* view, and so in no `query.sql`, dashboard, export, search entry, embedding, summary or
6
+ -- timeline. The record's customer (its author) writes it through the visibility seam and reads
7
+ -- their own; staff read it only through `record.readRestricted`, from a tool of the pack that
8
+ -- names the permission, called by a person themselves, every read audited with no value.
9
+ --
10
+ -- Keyed by the record (its object's registry name and id) and the field's registry name, with no
11
+ -- foreign key, as core_value_mark is. Deleted with the record's author when they are erased or
12
+ -- purged, scrubbed of an erased subject's terms like other free text, and dropped with its field or
13
+ -- object (extension.dropHidden).
14
+ CREATE TABLE core_restricted_value (
15
+ object_type TEXT NOT NULL,
16
+ record_id TEXT NOT NULL,
17
+ field TEXT NOT NULL,
18
+ value TEXT NOT NULL CHECK (length(value) BETWEEN 1 AND 4000),
19
+ written_at TEXT NOT NULL,
20
+ PRIMARY KEY (object_type, record_id, field)
21
+ ) WITHOUT ROWID;
22
+
23
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
24
+ UPDATE core_meta SET value = '1190'
25
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1190;