@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,113 @@
1
+ -- Migration 1230: note levels (stage 56 step 1, ADR 0830).
2
+ --
3
+ -- A note gains a level: who may read it. `staff` (every existing note, and the default) is read by
4
+ -- every staff account as today. `coaches` is read by its author, the staff who look after the one
5
+ -- Person it is about (core_person_carer, migration 1231), admins and holders of `notes:clients`.
6
+ -- `sensitive` is read by its author, admins and holders of `notes:sensitive`, each opening
7
+ -- audited. Only a `note` takes a level other than `staff`; emails, calls, meetings and system
8
+ -- activities stay `staff`.
9
+ --
10
+ -- Levels 2 and 3 are out of every bulk path, as a private synced email is (migration 0111, ADR
11
+ -- 0112): not in core_base_activities, and so not in rec_activity (the reconciler rebuilds it over
12
+ -- core_base_activities), query.sql, dashboards or exports; not in rec_activity_link; not in the
13
+ -- search index. The host reads them only through the seam's note scope (visibility/notes.ts).
14
+ --
15
+ -- The other columns are the note's, filled by later steps of the stage: its kind (core_note_kind,
16
+ -- step 2; no foreign key until that table exists), when and by whom it was shared with the client
17
+ -- (step 5), the customer account that sent it (step 6), whether it is evidence (copied from its
18
+ -- kind at write, step 7) and its template's filled sections as JSON (step 2).
19
+
20
+ ALTER TABLE core_activity ADD COLUMN level TEXT NOT NULL DEFAULT 'staff'
21
+ CHECK (level IN ('staff', 'coaches', 'sensitive'));
22
+ ALTER TABLE core_activity ADD COLUMN kind_id TEXT;
23
+ ALTER TABLE core_activity ADD COLUMN shared_at TEXT;
24
+ ALTER TABLE core_activity ADD COLUMN shared_by TEXT;
25
+ ALTER TABLE core_activity ADD COLUMN from_customer TEXT;
26
+ ALTER TABLE core_activity ADD COLUMN evidence INTEGER NOT NULL DEFAULT 0 CHECK (evidence IN (0, 1));
27
+ ALTER TABLE core_activity ADD COLUMN sections TEXT CHECK (sections IS NULL OR json_valid(sections));
28
+
29
+ -- Only a note is kept from the staff: every other type stays `staff`, for every writer.
30
+ CREATE TRIGGER core_activity_level_note_insert
31
+ BEFORE INSERT ON core_activity
32
+ WHEN NEW.level != 'staff' AND NEW.type != 'note'
33
+ BEGIN
34
+ SELECT RAISE(ABORT, 'core_activity_level_not_note');
35
+ END;
36
+
37
+ CREATE TRIGGER core_activity_level_note_update
38
+ BEFORE UPDATE OF level, type ON core_activity
39
+ WHEN NEW.level != 'staff' AND NEW.type != 'note'
40
+ BEGIN
41
+ SELECT RAISE(ABORT, 'core_activity_level_not_note');
42
+ END;
43
+
44
+ -- The notes kept from the whole staff, for the paths that leave them out (visibility/notes.ts):
45
+ -- a partial index, so finding them reads only them, not every synced email.
46
+ CREATE INDEX core_activity_kept_idx ON core_activity (id) WHERE level != 'staff';
47
+
48
+ -- A note's change planned from a read of its level carries that level; the batch fails whole when
49
+ -- the level changed meanwhile (`core_note_changed`), so a stale edit never puts back a level
50
+ -- someone else raised, and a lowering is judged against the level it lowers. Nothing is stored.
51
+ CREATE TABLE core_note_guard (
52
+ expected INTEGER NOT NULL,
53
+ actual INTEGER NOT NULL
54
+ );
55
+
56
+ CREATE TRIGGER core_note_guard_check
57
+ BEFORE INSERT ON core_note_guard
58
+ BEGIN
59
+ SELECT CASE
60
+ WHEN NEW.actual != NEW.expected THEN RAISE(ABORT, 'core_note_changed')
61
+ ELSE RAISE(IGNORE)
62
+ END;
63
+ END;
64
+
65
+ -- The base view keeps only `staff` rows. rec_activity reads it by name, so it follows.
66
+ DROP VIEW core_base_activities;
67
+ CREATE VIEW core_base_activities AS
68
+ SELECT
69
+ activity.id,
70
+ activity.type,
71
+ activity.subject,
72
+ activity.body,
73
+ activity.payload,
74
+ activity.occurred_at,
75
+ activity.author_user_id,
76
+ activity.author_actor,
77
+ activity.created_at,
78
+ activity.updated_at
79
+ FROM core_activity activity
80
+ WHERE activity.deleted_at IS NULL AND activity.private_to IS NULL AND activity.level = 'staff';
81
+
82
+ DROP VIEW rec_activity_link;
83
+ CREATE VIEW rec_activity_link AS
84
+ SELECT l.activity_id, l.object_type, l.record_id
85
+ FROM core_activity_links l
86
+ JOIN core_activity a ON a.id = l.activity_id AND a.deleted_at IS NULL AND a.private_to IS NULL
87
+ AND a.level = 'staff';
88
+
89
+ -- Search holds only `staff` rows: raising a note's level takes it out, lowering puts it back.
90
+ DROP TRIGGER core_activity_search_ai;
91
+ CREATE TRIGGER core_activity_search_ai AFTER INSERT ON core_activity
92
+ BEGIN
93
+ DELETE FROM core_search_docs WHERE object_type = 'activity' AND record_id = NEW.id;
94
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
95
+ SELECT 'activity', a.id, coalesce(a.subject, ''), coalesce(a.body, '')
96
+ FROM core_activity a
97
+ WHERE a.id = NEW.id AND a.deleted_at IS NULL AND a.private_to IS NULL AND a.level = 'staff';
98
+ END;
99
+
100
+ DROP TRIGGER core_activity_search_au;
101
+ CREATE TRIGGER core_activity_search_au AFTER UPDATE OF subject, body, deleted_at, private_to, level
102
+ ON core_activity
103
+ BEGIN
104
+ DELETE FROM core_search_docs WHERE object_type = 'activity' AND record_id = NEW.id;
105
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
106
+ SELECT 'activity', a.id, coalesce(a.subject, ''), coalesce(a.body, '')
107
+ FROM core_activity a
108
+ WHERE a.id = NEW.id AND a.deleted_at IS NULL AND a.private_to IS NULL AND a.level = 'staff';
109
+ END;
110
+
111
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
112
+ UPDATE core_meta SET value = '1230'
113
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1230;
@@ -0,0 +1,33 @@
1
+ -- Migration 1231: who looks after whom (stage 56 step 1, ADR 0831).
2
+ --
3
+ -- A staff account that looks after a Person (a client's coach) reads the Person's notes kept to
4
+ -- their coaches (level `coaches`, migration 1230). Staff with `people:link` set it on the
5
+ -- Person's page, a person only (`carer.set`, `carer.end`); a coach does not add themselves. It is
6
+ -- chosen, never worked out from bookings: one covered class must not open a member's notes to a
7
+ -- casual coach (stage 56, decision 3).
8
+ --
9
+ -- An ended row is kept, with who ended it and when, as the record of who could read whose notes;
10
+ -- it gives nothing. One live row per Person and account. Ending it takes effect on the next read:
11
+ -- nothing caches it.
12
+ --
13
+ -- An erase (a hard delete of the Person) removes their rows by the foreign key; so does removing
14
+ -- the account.
15
+ CREATE TABLE core_person_carer (
16
+ id TEXT PRIMARY KEY,
17
+ person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
18
+ user_id TEXT NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
19
+ -- The staff account (usr_…) that set it, and when it started.
20
+ set_by TEXT NOT NULL,
21
+ started_at TEXT NOT NULL,
22
+ -- Set when it ends; it never starts again (a new row is made instead).
23
+ ended_at TEXT,
24
+ ended_by TEXT
25
+ );
26
+ CREATE UNIQUE INDEX core_person_carer_live_uq
27
+ ON core_person_carer (person_id, user_id) WHERE ended_at IS NULL;
28
+ -- The seam asks "does this account look after this Person now" on every restricted note it reads.
29
+ CREATE INDEX core_person_carer_user_idx ON core_person_carer (user_id, person_id, ended_at);
30
+
31
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
32
+ UPDATE core_meta SET value = '1231'
33
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1231;
@@ -0,0 +1,122 @@
1
+ -- Migration 1232: note kinds and their templates (stage 56 step 2, ADR 0833).
2
+ --
3
+ -- A note (an Activity of type `note`, migration 1230) has a kind: the business's kinds of note,
4
+ -- kept by people holding `notes:manage` in Settings → Notes. A kind carries a template (labelled
5
+ -- sections a person fills in: Worked on / How it went / Next time), the level a note of it starts
6
+ -- at and the lowest level one may have. A kind is never deleted: retired, it is no longer offered
7
+ -- and keeps its notes. Not personal: every staff account reads the list.
8
+ --
9
+ -- Its other columns are read by later steps of the stage and set from them: whether files may be
10
+ -- attached (record files, step 3), whether its notes are evidence an erase keeps for a while and
11
+ -- how many whole years its notes are kept (erase and retention, step 7). Until then they hold the
12
+ -- seeds' values and do nothing.
13
+ --
14
+ -- Four kinds ship. `note` is the kind of every note written without one (the assistant's drafts,
15
+ -- an import, the API) and of every note already held: it is never retired and stays at `staff`,
16
+ -- so a note written without a kind is read as notes always were. `session`, `sensitive` and
17
+ -- `from_client` are the stage's own; later steps find them by `key`. A business's own kinds have
18
+ -- no key.
19
+ CREATE TABLE core_note_kind (
20
+ id TEXT PRIMARY KEY NOT NULL,
21
+ key TEXT UNIQUE CHECK (key IS NULL OR key IN ('note', 'session', 'sensitive', 'from_client')),
22
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80),
23
+ default_level TEXT NOT NULL DEFAULT 'staff'
24
+ CHECK (default_level IN ('staff', 'coaches', 'sensitive')),
25
+ lowest_level TEXT NOT NULL DEFAULT 'staff'
26
+ CHECK (lowest_level IN ('staff', 'coaches', 'sensitive')),
27
+ -- The template: [{ key, label, hint, required }], at most 12 (the operation's limit).
28
+ sections TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(sections) AND json_type(sections) = 'array'),
29
+ files_allowed INTEGER NOT NULL DEFAULT 0 CHECK (files_allowed IN (0, 1)),
30
+ evidence INTEGER NOT NULL DEFAULT 0 CHECK (evidence IN (0, 1)),
31
+ -- Whole years after the note; null keeps notes until a person deletes them (stage 56, decision 8).
32
+ retention_years INTEGER CHECK (retention_years IS NULL OR retention_years BETWEEN 1 AND 100),
33
+ active INTEGER NOT NULL DEFAULT 1 CHECK (active IN (0, 1)),
34
+ -- Where it sits in the composer's list.
35
+ position INTEGER NOT NULL DEFAULT 0,
36
+ created_by TEXT,
37
+ created_at TEXT NOT NULL,
38
+ updated_by TEXT,
39
+ updated_at TEXT NOT NULL,
40
+ -- A note starts at or above the lowest level its kind allows.
41
+ CHECK (
42
+ (CASE default_level WHEN 'staff' THEN 0 WHEN 'coaches' THEN 1 ELSE 2 END) >=
43
+ (CASE lowest_level WHEN 'staff' THEN 0 WHEN 'coaches' THEN 1 ELSE 2 END)
44
+ ),
45
+ -- The kind of a note written without one stays every staff account's, and in use.
46
+ CHECK (key IS NOT 'note' OR (default_level = 'staff' AND lowest_level = 'staff' AND active = 1))
47
+ );
48
+ -- Two kinds of one name would be one choice in the composer.
49
+ CREATE UNIQUE INDEX core_note_kind_name_uq ON core_note_kind (lower(name));
50
+
51
+ INSERT INTO core_note_kind
52
+ (id, key, name, default_level, lowest_level, sections, files_allowed, position, created_at,
53
+ updated_at)
54
+ VALUES
55
+ ('nkd_note', 'note', 'Note', 'staff', 'staff', '[]', 0, 0,
56
+ '2026-11-24T00:00:00.000Z', '2026-11-24T00:00:00.000Z'),
57
+ ('nkd_session', 'session', 'Session note', 'coaches', 'staff',
58
+ '[{"key":"worked_on","label":"Worked on","hint":"What you worked on together","required":true},'
59
+ || '{"key":"how_it_went","label":"How it went","hint":null,"required":false},'
60
+ || '{"key":"next_time","label":"Next time","hint":null,"required":false}]',
61
+ 0, 1, '2026-11-24T00:00:00.000Z', '2026-11-24T00:00:00.000Z'),
62
+ ('nkd_sensitive', 'sensitive', 'Sensitive', 'sensitive', 'sensitive', '[]', 0, 2,
63
+ '2026-11-24T00:00:00.000Z', '2026-11-24T00:00:00.000Z'),
64
+ ('nkd_from_client', 'from_client', 'From the client', 'coaches', 'staff', '[]', 1, 3,
65
+ '2026-11-24T00:00:00.000Z', '2026-11-24T00:00:00.000Z');
66
+
67
+ -- Note's template never has a required section: a note written without a kind has a body only,
68
+ -- and a required section would refuse every one (the operation says why first).
69
+ CREATE TRIGGER core_note_kind_note_insert
70
+ BEFORE INSERT ON core_note_kind
71
+ WHEN NEW.key IS 'note'
72
+ AND EXISTS (SELECT 1 FROM json_each(NEW.sections) s WHERE json_extract(s.value, '$.required') = 1)
73
+ BEGIN
74
+ SELECT RAISE(ABORT, 'core_note_kind_default');
75
+ END;
76
+
77
+ CREATE TRIGGER core_note_kind_note_update
78
+ BEFORE UPDATE OF sections, key ON core_note_kind
79
+ WHEN NEW.key IS 'note'
80
+ AND EXISTS (SELECT 1 FROM json_each(NEW.sections) s WHERE json_extract(s.value, '$.required') = 1)
81
+ BEGIN
82
+ SELECT RAISE(ABORT, 'core_note_kind_default');
83
+ END;
84
+
85
+ -- Every note already held is a Note. Before the triggers below: it changes no level.
86
+ UPDATE core_activity SET kind_id = 'nkd_note' WHERE type = 'note' AND kind_id IS NULL;
87
+
88
+ -- A kind is only a note's, and a note is never below its kind's lowest level, for every writer:
89
+ -- the operations check it first and say why; this holds it against a kind changed meanwhile.
90
+ CREATE TRIGGER core_activity_kind_insert
91
+ BEFORE INSERT ON core_activity
92
+ WHEN NEW.kind_id IS NOT NULL
93
+ BEGIN
94
+ SELECT CASE
95
+ WHEN NEW.type != 'note' THEN RAISE(ABORT, 'core_activity_kind_not_note')
96
+ WHEN NOT EXISTS (SELECT 1 FROM core_note_kind k WHERE k.id = NEW.kind_id)
97
+ THEN RAISE(ABORT, 'core_activity_kind_unknown')
98
+ WHEN (CASE NEW.level WHEN 'staff' THEN 0 WHEN 'coaches' THEN 1 ELSE 2 END) <
99
+ (SELECT CASE k.lowest_level WHEN 'staff' THEN 0 WHEN 'coaches' THEN 1 ELSE 2 END
100
+ FROM core_note_kind k WHERE k.id = NEW.kind_id)
101
+ THEN RAISE(ABORT, 'core_note_below_kind')
102
+ END;
103
+ END;
104
+
105
+ CREATE TRIGGER core_activity_kind_update
106
+ BEFORE UPDATE OF kind_id, level, type ON core_activity
107
+ WHEN NEW.kind_id IS NOT NULL
108
+ BEGIN
109
+ SELECT CASE
110
+ WHEN NEW.type != 'note' THEN RAISE(ABORT, 'core_activity_kind_not_note')
111
+ WHEN NOT EXISTS (SELECT 1 FROM core_note_kind k WHERE k.id = NEW.kind_id)
112
+ THEN RAISE(ABORT, 'core_activity_kind_unknown')
113
+ WHEN (CASE NEW.level WHEN 'staff' THEN 0 WHEN 'coaches' THEN 1 ELSE 2 END) <
114
+ (SELECT CASE k.lowest_level WHEN 'staff' THEN 0 WHEN 'coaches' THEN 1 ELSE 2 END
115
+ FROM core_note_kind k WHERE k.id = NEW.kind_id)
116
+ THEN RAISE(ABORT, 'core_note_below_kind')
117
+ END;
118
+ END;
119
+
120
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
121
+ UPDATE core_meta SET value = '1232'
122
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1232;
@@ -0,0 +1,145 @@
1
+ -- Migration 1270: time entries, the one record of time worked (stage 58 step 1, ADR 0850).
2
+ --
3
+ -- One stretch of time a staff member worked: a start and a finish as whole unix seconds (the
4
+ -- host's clock for a clocked entry, never the phone's), unpaid break minutes they entered, the
5
+ -- zone it was given in and its local date (of its start, in the organisation's zone: the pay
6
+ -- period it belongs to), and what it was against: a shift (a record of a `staffed` object), a
7
+ -- record of an object that declares `workedOn` (a job), or nothing (other work). How it was made
8
+ -- (clocked, entered, or entered by an approver, with why) and its state (open while clocked in,
9
+ -- waiting for an approver, approved, rejected). Approval (step 2) keeps the times as recorded
10
+ -- beside the approved ones.
11
+ --
12
+ -- Keyed by a staff account (core_user.id), text with no foreign key, as core_staff_time is.
13
+ -- About someone's working day: in no rec_* view, so no member's SQL, export, dashboard or the
14
+ -- assistant's record reads see it.
15
+ --
16
+ -- The database refuses two entries of one account that overlap, for every writer and statement
17
+ -- shape, as core_staff_time refuses two shifts: half-open ranges (09:00 to 12:00 and 12:00 to
18
+ -- 15:00 do not overlap), and an open entry runs on from its start (it overlaps everything after).
19
+ -- D1 runs with recursive triggers off, so the rows an INSERT OR REPLACE or an UPDATE OR REPLACE
20
+ -- deletes fire no delete trigger: the checks are BEFORE INSERT (a REPLACE and an upsert are
21
+ -- inserts first) and BEFORE UPDATE (an id update is an update), and each leaves out only the
22
+ -- row's own id, which such a statement replaces, so a REPLACE can only refuse more. An entry is
23
+ -- separate from the shift claim: it may run past its shift into another shift's rostered time.
24
+ --
25
+ -- One open entry per account (a partial unique index): of twenty starts at once, one lands. A
26
+ -- start retried with its idempotency key (`start_key`) finds the entry it made.
27
+ CREATE TABLE core_time_entry (
28
+ -- NOT NULL: a rowid table's TEXT key would otherwise take a NULL, which no comparison matches.
29
+ id TEXT PRIMARY KEY NOT NULL,
30
+ account_id TEXT NOT NULL,
31
+ starts INTEGER NOT NULL,
32
+ -- Null while open (clocked in, not yet finished).
33
+ ends INTEGER,
34
+ break_minutes INTEGER NOT NULL DEFAULT 0 CHECK (break_minutes >= 0 AND break_minutes <= 1440),
35
+ zone TEXT NOT NULL,
36
+ local_date TEXT NOT NULL CHECK (local_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
37
+ -- What it was against: a shift or a workedOn record (its object's registry name and id), or
38
+ -- both null for other work.
39
+ against_object TEXT,
40
+ against_record TEXT,
41
+ how TEXT NOT NULL CHECK (how IN ('clocked', 'entered', 'by_approver')),
42
+ -- Why an approver entered it for someone (`by_approver` only).
43
+ how_why TEXT CHECK (how_why IS NULL OR length(how_why) <= 500),
44
+ state TEXT NOT NULL CHECK (state IN ('open', 'waiting', 'approved', 'rejected')),
45
+ -- The host found it open past its shift's end (or long after its start): the person is told to
46
+ -- enter their finish. No finish is ever invented.
47
+ finish_missing INTEGER NOT NULL DEFAULT 0 CHECK (finish_missing IN (0, 1)),
48
+ -- The times as recorded, kept beside when an approver approves it changed (step 2).
49
+ recorded_starts INTEGER,
50
+ recorded_ends INTEGER,
51
+ recorded_break_minutes INTEGER,
52
+ -- An account, or 'host' when it matched the roster (step 2).
53
+ approved_by TEXT,
54
+ approved_at TEXT,
55
+ rejected_why TEXT CHECK (rejected_why IS NULL OR length(rejected_why) <= 500),
56
+ note TEXT CHECK (note IS NULL OR length(note) <= 500),
57
+ -- The idempotency key of the start that made it (a clocked entry), unique per account.
58
+ start_key TEXT CHECK (start_key IS NULL OR length(start_key) BETWEEN 16 AND 128),
59
+ -- Who made it: the person, or an approver for them.
60
+ made_by TEXT NOT NULL,
61
+ created_at TEXT NOT NULL,
62
+ updated_at TEXT NOT NULL,
63
+ CHECK ((state = 'open') = (ends IS NULL)),
64
+ CHECK (ends IS NULL OR ends > starts),
65
+ CHECK (ends IS NULL OR break_minutes * 60 < ends - starts),
66
+ CHECK ((against_object IS NULL) = (against_record IS NULL)),
67
+ CHECK ((how = 'by_approver') = (how_why IS NOT NULL)),
68
+ CHECK (finish_missing = 0 OR state = 'open'),
69
+ CHECK ((recorded_starts IS NULL) = (recorded_ends IS NULL))
70
+ );
71
+
72
+ -- One open entry per account: a second start while one is open fails on this, however many race.
73
+ CREATE UNIQUE INDEX core_time_entry_open ON core_time_entry (account_id) WHERE ends IS NULL;
74
+
75
+ -- A retried start finds its entry; two starts with one key make one.
76
+ CREATE UNIQUE INDEX core_time_entry_start_key ON core_time_entry (account_id, start_key)
77
+ WHERE start_key IS NOT NULL;
78
+
79
+ -- By account and end (an open entry's end read as the far future, the expression the overlap
80
+ -- triggers use): an overlap check reads only the entries that end after the new one starts, never
81
+ -- an account's history.
82
+ CREATE INDEX core_time_entry_account_end
83
+ ON core_time_entry (account_id, ifnull(ends, 9223372036854775807), starts);
84
+
85
+ -- A person's timesheet and a pay period's entries: by account and local date.
86
+ CREATE INDEX core_time_entry_account_date ON core_time_entry (account_id, local_date);
87
+
88
+ -- A period's entries of everyone, and a shift's or a job's entries.
89
+ CREATE INDEX core_time_entry_date ON core_time_entry (local_date);
90
+ CREATE INDEX core_time_entry_against ON core_time_entry (against_object, against_record)
91
+ WHERE against_object IS NOT NULL;
92
+
93
+ -- The open entries the minute cron looks at for a missing finish, oldest first.
94
+ CREATE INDEX core_time_entry_open_starts ON core_time_entry (starts)
95
+ WHERE ends IS NULL AND finish_missing = 0;
96
+
97
+ CREATE TRIGGER core_time_entry_overlap_insert
98
+ BEFORE INSERT ON core_time_entry
99
+ BEGIN
100
+ -- A second open entry (of twenty starts at once, all but one): said as such, before the overlap
101
+ -- it also is.
102
+ SELECT RAISE(ABORT, 'core_time_open')
103
+ WHERE NEW.ends IS NULL AND EXISTS (
104
+ SELECT 1 FROM core_time_entry e
105
+ WHERE e.account_id = NEW.account_id
106
+ AND e.id IS NOT NEW.id
107
+ AND e.ends IS NULL
108
+ );
109
+ SELECT RAISE(ABORT, 'core_time_overlap')
110
+ WHERE EXISTS (
111
+ SELECT 1 FROM core_time_entry e
112
+ WHERE e.account_id = NEW.account_id
113
+ AND e.id IS NOT NEW.id
114
+ AND ifnull(e.ends, 9223372036854775807) > NEW.starts
115
+ AND e.starts < ifnull(NEW.ends, 9223372036854775807)
116
+ );
117
+ END;
118
+
119
+ CREATE TRIGGER core_time_entry_overlap_update
120
+ BEFORE UPDATE ON core_time_entry
121
+ BEGIN
122
+ -- A second open entry (of twenty starts at once, all but one): said as such, before the overlap
123
+ -- it also is.
124
+ SELECT RAISE(ABORT, 'core_time_open')
125
+ WHERE NEW.ends IS NULL AND EXISTS (
126
+ SELECT 1 FROM core_time_entry e
127
+ WHERE e.account_id = NEW.account_id
128
+ AND e.id IS NOT OLD.id
129
+ AND e.id IS NOT NEW.id
130
+ AND e.ends IS NULL
131
+ );
132
+ SELECT RAISE(ABORT, 'core_time_overlap')
133
+ WHERE EXISTS (
134
+ SELECT 1 FROM core_time_entry e
135
+ WHERE e.account_id = NEW.account_id
136
+ AND e.id IS NOT OLD.id
137
+ AND e.id IS NOT NEW.id
138
+ AND ifnull(e.ends, 9223372036854775807) > NEW.starts
139
+ AND e.starts < ifnull(NEW.ends, 9223372036854775807)
140
+ );
141
+ END;
142
+
143
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
144
+ UPDATE core_meta SET value = '1270'
145
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1270;
@@ -0,0 +1,249 @@
1
+ -- Migration 1271: leave types, leave requests and their days, the balance ledger, and the
2
+ -- unavailability row approved leave writes (stage 58 step 4, ADR 0853).
3
+ --
4
+ -- Leave is a request a person makes (or an approver makes for them) for time off of a type the
5
+ -- business sets; approved, the host writes the existing unavailability row for it
6
+ -- (`core_staff_away`, kind `leave`, naming the request), so every stage 45 rule about being away
7
+ -- holds unchanged. A balance is the sum of a ledger per person and type: entries a pay holder sets
8
+ -- or adjusts, and the host's own when leave is approved or an approved request cancelled. The host
9
+ -- never accrues. Amounts are integers in the type's unit: minutes for an hours type, hundredths of
10
+ -- a day for a days type. No money anywhere.
11
+ --
12
+ -- All of it is personal (a sick day is health information): in no rec_* view, so no member's SQL,
13
+ -- export, dashboard or the assistant's record reads reach it.
14
+ --
15
+ -- D1 runs with recursive triggers off, so an INSERT OR REPLACE deletes the rows it replaces
16
+ -- without firing a delete or update trigger: a request and a balance entry are never replaced (a
17
+ -- BEFORE INSERT trigger refuses an id that exists, before a REPLACE could delete it), as an offer
18
+ -- is (migration 0812). The lock of a pay period over leave (refusing every change to a request or
19
+ -- day dated in a locked period) is migration 1272's (stage 58 step 3).
20
+
21
+ -- A kind of leave the business offers (annual, personal, unpaid): none ship. `unit` is fixed once
22
+ -- anything is recorded in it (the amounts would mean something else). Never deleted once used:
23
+ -- a type no longer offered is made inactive.
24
+ CREATE TABLE core_leave_type (
25
+ id TEXT PRIMARY KEY,
26
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80),
27
+ unit TEXT NOT NULL CHECK (unit IN ('hours', 'days')),
28
+ -- 0: the type keeps no balance (unpaid leave, say); nothing is written to the ledger for it.
29
+ keeps_balance INTEGER NOT NULL CHECK (keeps_balance IN (0, 1)),
30
+ -- What approving leave that takes the balance below zero does (decision 9).
31
+ below_zero TEXT NOT NULL CHECK (below_zero IN ('warn', 'refuse')),
32
+ -- Whether it is paid leave: a label for the pay period's export only, never a rate.
33
+ paid INTEGER NOT NULL CHECK (paid IN (0, 1)),
34
+ active INTEGER NOT NULL DEFAULT 1 CHECK (active IN (0, 1)),
35
+ position INTEGER NOT NULL DEFAULT 0,
36
+ created_by TEXT NOT NULL,
37
+ created_at TEXT NOT NULL,
38
+ updated_by TEXT NOT NULL,
39
+ updated_at TEXT NOT NULL
40
+ );
41
+
42
+ CREATE UNIQUE INDEX core_leave_type_name ON core_leave_type (lower(name));
43
+
44
+ -- One ask for time off: whole local days from start_date to end_date, or one date with a local
45
+ -- start and end time, in `zone` (the organisation's when asked). `requested` moves once, to
46
+ -- `approved`, `declined` or `cancelled`; an approved one may later be `cancelled`. Nothing else
47
+ -- moves, and what it asks for never changes (cancel and ask again).
48
+ CREATE TABLE core_leave_request (
49
+ id TEXT PRIMARY KEY,
50
+ account_id TEXT NOT NULL,
51
+ type_id TEXT NOT NULL REFERENCES core_leave_type (id),
52
+ start_date TEXT NOT NULL CHECK (start_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
53
+ end_date TEXT NOT NULL CHECK (end_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
54
+ start_time TEXT CHECK (start_time IS NULL OR start_time GLOB '[0-2][0-9]:[0-5][0-9]'),
55
+ end_time TEXT CHECK (end_time IS NULL OR end_time GLOB '[0-2][0-9]:[0-5][0-9]'),
56
+ zone TEXT NOT NULL,
57
+ -- The range as instants (ISO), worked out from the dates and times in the zone when asked: what
58
+ -- the unavailability row and the shifts it covers are read by.
59
+ starts_at TEXT NOT NULL,
60
+ ends_at TEXT NOT NULL,
61
+ state TEXT NOT NULL CHECK (state IN ('requested', 'approved', 'declined', 'cancelled')),
62
+ -- May say why (a medical reason): redacted from the audit log, never in a notice or an error,
63
+ -- kept from the assistant.
64
+ note TEXT CHECK (note IS NULL OR length(note) <= 500),
65
+ -- The person, or an approver asking for them.
66
+ asked_by TEXT NOT NULL,
67
+ asked_at TEXT NOT NULL,
68
+ decided_by TEXT,
69
+ decided_at TEXT,
70
+ -- Why it was declined or cancelled.
71
+ why TEXT CHECK (why IS NULL OR length(why) <= 200),
72
+ updated_at TEXT NOT NULL,
73
+ CHECK (end_date >= start_date),
74
+ CHECK (ends_at > starts_at),
75
+ CHECK ((start_time IS NULL) = (end_time IS NULL)),
76
+ CHECK (start_time IS NULL OR (end_date = start_date AND end_time > start_time)),
77
+ CHECK (state = 'requested' OR (decided_by IS NOT NULL AND decided_at IS NOT NULL))
78
+ );
79
+
80
+ -- A person's requests by their end (their own list, and leaving's cancel of what still waits).
81
+ CREATE INDEX core_leave_request_account ON core_leave_request (account_id, end_date);
82
+ -- The requests waiting for an approver, soonest first; and every request by its dates (a pay
83
+ -- period's lock and leave lines read the period's only).
84
+ CREATE INDEX core_leave_request_waiting ON core_leave_request (start_date)
85
+ WHERE state = 'requested';
86
+ CREATE INDEX core_leave_request_dates ON core_leave_request (start_date, end_date);
87
+
88
+ CREATE TRIGGER core_leave_request_insert
89
+ BEFORE INSERT ON core_leave_request
90
+ BEGIN
91
+ SELECT RAISE(ABORT, 'core_leave_request_fixed')
92
+ WHERE EXISTS (SELECT 1 FROM core_leave_request WHERE id = NEW.id);
93
+ SELECT RAISE(ABORT, 'core_leave_request_closed') WHERE NEW.state != 'requested';
94
+ END;
95
+
96
+ -- `requested` moves once; `approved` only to `cancelled`; a decided request never moves again. Of
97
+ -- two approvals at once the second fails whole here. Approved leave is cancelled only once its
98
+ -- unavailability row is gone and its ledger entry reversed (earlier in the same batch), so a write
99
+ -- planned while it waited (a cancel racing the approval) fails whole rather than leave it away for
100
+ -- good. What was asked never changes; its note may only be cleared (erasing a person's notes).
101
+ CREATE TRIGGER core_leave_request_update
102
+ BEFORE UPDATE ON core_leave_request
103
+ BEGIN
104
+ SELECT RAISE(ABORT, 'core_leave_request_held')
105
+ WHERE OLD.state = 'approved' AND NEW.state = 'cancelled' AND (
106
+ EXISTS (SELECT 1 FROM core_staff_away WHERE leave_request = OLD.id)
107
+ OR (EXISTS (SELECT 1 FROM core_leave_balance_entry WHERE request_id = OLD.id
108
+ AND why = 'approved')
109
+ AND NOT EXISTS (SELECT 1 FROM core_leave_balance_entry WHERE request_id = OLD.id
110
+ AND why = 'cancelled')));
111
+ SELECT RAISE(ABORT, 'core_leave_request_closed')
112
+ WHERE NOT (
113
+ (OLD.state = 'requested' AND NEW.state IN ('approved', 'declined', 'cancelled'))
114
+ OR (OLD.state = 'approved' AND NEW.state = 'cancelled')
115
+ OR (NEW.state = OLD.state AND NEW.note IS NULL AND OLD.note IS NOT NULL)
116
+ );
117
+ SELECT RAISE(ABORT, 'core_leave_request_fixed')
118
+ WHERE NEW.id != OLD.id OR NEW.account_id != OLD.account_id OR NEW.type_id != OLD.type_id
119
+ OR NEW.start_date != OLD.start_date OR NEW.end_date != OLD.end_date
120
+ OR NEW.start_time IS NOT OLD.start_time OR NEW.end_time IS NOT OLD.end_time
121
+ OR NEW.zone != OLD.zone OR NEW.starts_at != OLD.starts_at OR NEW.ends_at != OLD.ends_at
122
+ OR NEW.asked_by != OLD.asked_by OR NEW.asked_at != OLD.asked_at
123
+ OR (NEW.note IS NOT OLD.note AND NEW.note IS NOT NULL);
124
+ END;
125
+
126
+ -- Approved leave is a payroll record: never deleted.
127
+ CREATE TRIGGER core_leave_request_delete
128
+ BEFORE DELETE ON core_leave_request
129
+ BEGIN
130
+ SELECT RAISE(ABORT, 'core_leave_request_fixed') WHERE OLD.state = 'approved';
131
+ END;
132
+
133
+ -- Each date of a request and its amount: asked (proposed by the host from the roster, changed by
134
+ -- the person), and approved (set by the approver at approval). Integers in the type's unit.
135
+ CREATE TABLE core_leave_day (
136
+ request_id TEXT NOT NULL REFERENCES core_leave_request (id),
137
+ date TEXT NOT NULL CHECK (date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
138
+ asked INTEGER NOT NULL CHECK (asked BETWEEN 0 AND 1440),
139
+ approved INTEGER CHECK (approved IS NULL OR approved BETWEEN 0 AND 1440),
140
+ PRIMARY KEY (request_id, date)
141
+ ) WITHOUT ROWID;
142
+
143
+ -- A pay period's leave lines read its dates only.
144
+ CREATE INDEX core_leave_day_date ON core_leave_day (date);
145
+
146
+ -- A request's days change only while it waits (the approval writes their amounts before it moves
147
+ -- the request), and an approved request's days are never removed.
148
+ CREATE TRIGGER core_leave_day_insert
149
+ BEFORE INSERT ON core_leave_day
150
+ BEGIN
151
+ SELECT RAISE(ABORT, 'core_leave_request_closed')
152
+ WHERE NOT EXISTS (SELECT 1 FROM core_leave_request r WHERE r.id = NEW.request_id
153
+ AND r.state = 'requested');
154
+ END;
155
+
156
+ CREATE TRIGGER core_leave_day_update
157
+ BEFORE UPDATE ON core_leave_day
158
+ BEGIN
159
+ SELECT RAISE(ABORT, 'core_leave_request_closed')
160
+ WHERE NOT EXISTS (SELECT 1 FROM core_leave_request r WHERE r.id = OLD.request_id
161
+ AND r.state = 'requested');
162
+ SELECT RAISE(ABORT, 'core_leave_request_fixed')
163
+ WHERE NEW.request_id != OLD.request_id OR NEW.date != OLD.date;
164
+ END;
165
+
166
+ CREATE TRIGGER core_leave_day_delete
167
+ BEFORE DELETE ON core_leave_day
168
+ BEGIN
169
+ SELECT RAISE(ABORT, 'core_leave_request_fixed')
170
+ WHERE EXISTS (SELECT 1 FROM core_leave_request r WHERE r.id = OLD.request_id
171
+ AND r.state = 'approved');
172
+ END;
173
+
174
+ -- The balance ledger: insert only, the balance is the sum. `set` and `adjust` are a pay holder's
175
+ -- (a set is written as the difference from the sum before it); `approved` and `cancelled` are the
176
+ -- host's, one each per request at most.
177
+ CREATE TABLE core_leave_balance_entry (
178
+ id TEXT PRIMARY KEY,
179
+ account_id TEXT NOT NULL,
180
+ type_id TEXT NOT NULL REFERENCES core_leave_type (id),
181
+ amount INTEGER NOT NULL,
182
+ why TEXT NOT NULL CHECK (why IN ('set', 'adjust', 'approved', 'cancelled')),
183
+ request_id TEXT REFERENCES core_leave_request (id),
184
+ note TEXT CHECK (note IS NULL OR length(note) <= 500),
185
+ made_by TEXT NOT NULL,
186
+ made_at TEXT NOT NULL,
187
+ CHECK ((why IN ('approved', 'cancelled')) = (request_id IS NOT NULL))
188
+ );
189
+
190
+ CREATE INDEX core_leave_balance_entry_account ON core_leave_balance_entry (account_id, type_id);
191
+ CREATE UNIQUE INDEX core_leave_balance_entry_request ON core_leave_balance_entry (request_id, why)
192
+ WHERE request_id IS NOT NULL;
193
+
194
+ -- Never replaced, changed or removed. Approving leave of a type that refuses a balance below zero
195
+ -- is refused here when it would take the sum below zero, whatever was approved a moment before
196
+ -- (decision 9): of two approvals at once that together would, the second fails whole.
197
+ CREATE TRIGGER core_leave_balance_entry_insert
198
+ BEFORE INSERT ON core_leave_balance_entry
199
+ BEGIN
200
+ SELECT RAISE(ABORT, 'core_leave_balance_fixed')
201
+ WHERE EXISTS (SELECT 1 FROM core_leave_balance_entry WHERE id = NEW.id);
202
+ SELECT RAISE(ABORT, 'core_leave_below_zero')
203
+ WHERE NEW.why = 'approved'
204
+ AND EXISTS (SELECT 1 FROM core_leave_type t WHERE t.id = NEW.type_id
205
+ AND t.below_zero = 'refuse')
206
+ AND NEW.amount + coalesce((SELECT sum(e.amount) FROM core_leave_balance_entry e
207
+ WHERE e.account_id = NEW.account_id AND e.type_id = NEW.type_id), 0) < 0;
208
+ END;
209
+
210
+ CREATE TRIGGER core_leave_balance_entry_update
211
+ BEFORE UPDATE ON core_leave_balance_entry
212
+ BEGIN
213
+ SELECT RAISE(ABORT, 'core_leave_balance_fixed');
214
+ END;
215
+
216
+ CREATE TRIGGER core_leave_balance_entry_delete
217
+ BEFORE DELETE ON core_leave_balance_entry
218
+ BEGIN
219
+ SELECT RAISE(ABORT, 'core_leave_balance_fixed');
220
+ END;
221
+
222
+ -- A type's unit never changes once a request or a balance entry is in it, and a type in use is
223
+ -- never removed.
224
+ CREATE TRIGGER core_leave_type_update
225
+ BEFORE UPDATE ON core_leave_type
226
+ BEGIN
227
+ SELECT RAISE(ABORT, 'core_leave_type_used')
228
+ WHERE (NEW.unit != OLD.unit OR NEW.id != OLD.id)
229
+ AND (EXISTS (SELECT 1 FROM core_leave_request WHERE type_id = OLD.id)
230
+ OR EXISTS (SELECT 1 FROM core_leave_balance_entry WHERE type_id = OLD.id));
231
+ END;
232
+
233
+ CREATE TRIGGER core_leave_type_delete
234
+ BEFORE DELETE ON core_leave_type
235
+ BEGIN
236
+ SELECT RAISE(ABORT, 'core_leave_type_used')
237
+ WHERE EXISTS (SELECT 1 FROM core_leave_request WHERE type_id = OLD.id)
238
+ OR EXISTS (SELECT 1 FROM core_leave_balance_entry WHERE type_id = OLD.id);
239
+ END;
240
+
241
+ -- The unavailability approved leave wrote (amends ADR 0622): null for one a person said. At most
242
+ -- one row per request; `staffing.back` refuses to remove it (cancel the request instead).
243
+ ALTER TABLE core_staff_away ADD COLUMN leave_request TEXT;
244
+ CREATE UNIQUE INDEX core_staff_away_leave_request ON core_staff_away (leave_request)
245
+ WHERE leave_request IS NOT NULL;
246
+
247
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
248
+ UPDATE core_meta SET value = '1271'
249
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1271;