@composable-os/cli 0.1.0-beta.7 → 0.1.0-beta.9
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.
- package/dist/accounts-D6N77JGP.js +44 -0
- package/dist/{blocks-HVTAQ5W2.js → blocks-4CA4PP3P.js} +4 -4
- package/dist/{chunk-X6I2LWB3.js → chunk-2MLAPQU7.js} +1471 -359
- package/dist/{chunk-2S7375SC.js → chunk-3MIEYXRY.js} +3 -301
- package/dist/chunk-56A6RJIU.js +1040 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/{chunk-STJZJIRA.js → chunk-7O4F3NU4.js} +1039 -1380
- package/dist/chunk-7U3OZLTK.js +1370 -0
- package/dist/{chunk-ZRWF6QZS.js → chunk-DU6OIFOU.js} +1 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/{chunk-E5JLTLRM.js → chunk-GYTEW66E.js} +1 -1
- package/dist/chunk-LPD2VR5M.js +284 -0
- package/dist/chunk-NDKVIG23.js +2641 -0
- package/dist/chunk-NSSUOI3Z.js +333 -0
- package/dist/chunk-OV3OFYPI.js +746 -0
- package/dist/{chunk-2P4AZTFL.js → chunk-P6YZMS3F.js} +11 -5
- package/dist/{chunk-Y7PQ7NIL.js → chunk-RFJOPI42.js} +138 -14
- package/dist/{chunk-VL62IMBN.js → chunk-RXRD34QW.js} +10 -8
- package/dist/{chunk-BEKL3CA2.js → chunk-STBMGUTM.js} +7065 -4580
- package/dist/chunk-VTQNOQGA.js +150 -0
- package/dist/chunk-XSE37HK5.js +426 -0
- package/dist/{chunk-MINQ46RU.js → chunk-Z52IX5WL.js} +257 -844
- package/dist/chunk-ZSR5H4O7.js +552 -0
- package/dist/{cli-XHC6QBQW.js → cli-7A5XHYML.js} +16 -9
- package/dist/{cli-BRSACHSG.js → cli-T6MJM7TU.js} +20 -8
- package/dist/cli.js +4 -4
- package/dist/{client-repo-G55G4OLV.js → client-repo-PC4ISVEZ.js} +4 -4
- package/dist/create.js +4 -4
- package/dist/{fields-64BQJA62.js → fields-L24XAIOA.js} +13 -11
- package/dist/invoke-TLYT6CFI.js +33 -0
- package/dist/order-IKQBUFCV.js +19 -0
- package/dist/{person-AZFEPZOE.js → person-DXEOOH2A.js} +9 -5
- package/dist/plan-on-sale-LRCK5MJG.js +24 -0
- package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/sdk/allowance.ts +508 -0
- package/dist/platform/sdk/capacity.ts +89 -0
- package/dist/platform/sdk/contract.ts +97 -1
- package/dist/platform/sdk/document.ts +161 -14
- package/dist/platform/sdk/field-types.ts +5 -0
- package/dist/platform/sdk/index.ts +3 -0
- package/dist/platform/sdk/manifest.ts +206 -5
- package/dist/platform/sdk/media.ts +62 -0
- package/dist/platform/sdk/recurring.ts +314 -0
- package/dist/platform/sdk/rich-text.ts +52 -4
- package/dist/platform/sdk/sellable.ts +120 -0
- package/dist/platform/sdk/states.ts +162 -1
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/run-BMSZKTN3.js +648 -0
- package/dist/run-GMQBAV6H.js +309 -0
- package/dist/{run-4JNFLYXV.js → run-TNHMG5RI.js} +51 -390
- package/dist/src-7DXXGZG2.js +38 -0
- package/dist/{src-J27WXNLY.js → src-BDG7PERB.js} +73 -1
- package/dist/visibility-XSBZTO66.js +33 -0
- package/package.json +2 -2
- package/dist/chunk-MAXLUQKB.js +0 -1287
- package/dist/invoke-5SR5ZWW7.js +0 -26
- package/dist/src-SSVOBH6L.js +0 -31
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
-- Migration 0532: continuation offers (stage 32a, build step 4, ADRs 0491 and 0492).
|
|
2
|
+
--
|
|
3
|
+
-- A company moving its subscribers from another platform imports, for each one, an offer: an
|
|
4
|
+
-- address (and the one live Person who has it), a plan object and record, and the date the person
|
|
5
|
+
-- has already paid up to. The person signs in as that address and subscribes again, free until
|
|
6
|
+
-- the date, then charged by the provider. An offer is taken once (the provider's verified event
|
|
7
|
+
-- that makes the subscription marks it), expires on its date, and can be withdrawn by staff.
|
|
8
|
+
--
|
|
9
|
+
-- While a provider page opened from an offer can still be completed, the offer holds it
|
|
10
|
+
-- (`checkout_id`). The trigger refuses holding a page for an offer that already holds one, has
|
|
11
|
+
-- been taken or was withdrawn: of two takes at once, the second one's whole batch fails, so its
|
|
12
|
+
-- page is never handed out. A hold is released (`checkout_id` back to null) only once the
|
|
13
|
+
-- provider says the page expired uncompleted, as a trial's is (ADR 0489).
|
|
14
|
+
CREATE TABLE core_subscription_offer (
|
|
15
|
+
-- sof_… (ids.ts).
|
|
16
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
17
|
+
-- The plan: any record of an object that declares `sellable.recurring`.
|
|
18
|
+
plan_object TEXT NOT NULL,
|
|
19
|
+
plan_id TEXT NOT NULL,
|
|
20
|
+
-- The address the offer is for. Taking it needs a customer signed in as this address.
|
|
21
|
+
email TEXT NOT NULL CHECK (email = lower(trim(email)) AND email <> ''),
|
|
22
|
+
-- The one live Person who had the address when it was imported (people come in first; an
|
|
23
|
+
-- import never creates one). A merge repoints it. Null after the person is erased.
|
|
24
|
+
person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
|
|
25
|
+
-- Free until this moment (the date the person has paid up to elsewhere); charged from then.
|
|
26
|
+
free_until TEXT NOT NULL,
|
|
27
|
+
-- The other platform's reference for the old subscription, as the source gave it.
|
|
28
|
+
reference TEXT,
|
|
29
|
+
imported_by TEXT NOT NULL,
|
|
30
|
+
created_at TEXT NOT NULL,
|
|
31
|
+
updated_at TEXT NOT NULL,
|
|
32
|
+
-- The provider page that holds it while it can be completed (core_subscription_checkout.id).
|
|
33
|
+
checkout_id TEXT,
|
|
34
|
+
checkout_ref TEXT,
|
|
35
|
+
checkout_url TEXT,
|
|
36
|
+
held_until TEXT,
|
|
37
|
+
-- The subscription that took it, once the provider confirmed it. Final.
|
|
38
|
+
subscription_id TEXT,
|
|
39
|
+
taken_at TEXT,
|
|
40
|
+
-- Taken with too little time left for free time at the provider (under 48 hours): charged at
|
|
41
|
+
-- once, never given the plan's trial instead.
|
|
42
|
+
charged_from_start INTEGER NOT NULL DEFAULT 0 CHECK (charged_from_start IN (0, 1)),
|
|
43
|
+
withdrawn_at TEXT,
|
|
44
|
+
withdrawn_by TEXT,
|
|
45
|
+
withdraw_reason TEXT,
|
|
46
|
+
-- A withdrawn offer staff replaced with a fresh one (`reissue`): the new offer's id.
|
|
47
|
+
reissued_as TEXT,
|
|
48
|
+
-- Bumped by exactly one on every change (below): a write made from an older read fails.
|
|
49
|
+
version INTEGER NOT NULL DEFAULT 0
|
|
50
|
+
);
|
|
51
|
+
-- One offer per address per plan object, the import's key; a withdrawn one stays as it was, and
|
|
52
|
+
-- staff may reissue the address a fresh offer beside it.
|
|
53
|
+
CREATE UNIQUE INDEX core_subscription_offer_key_uq ON core_subscription_offer (plan_object, email)
|
|
54
|
+
WHERE withdrawn_at IS NULL;
|
|
55
|
+
CREATE INDEX core_subscription_offer_person_idx ON core_subscription_offer (person_id, plan_object);
|
|
56
|
+
|
|
57
|
+
CREATE TRIGGER core_subscription_offer_one_page
|
|
58
|
+
BEFORE UPDATE OF checkout_id ON core_subscription_offer
|
|
59
|
+
WHEN NEW.checkout_id IS NOT NULL
|
|
60
|
+
AND (OLD.checkout_id IS NOT NULL OR OLD.subscription_id IS NOT NULL OR OLD.withdrawn_at IS NOT NULL)
|
|
61
|
+
BEGIN
|
|
62
|
+
SELECT RAISE(ABORT, 'core_subscription_offer_held');
|
|
63
|
+
END;
|
|
64
|
+
|
|
65
|
+
-- A write made from an older read of the offer fails, and its whole batch (as a subscription's,
|
|
66
|
+
-- migration 0530): an import, a withdrawal or a take that read the offer before another change
|
|
67
|
+
-- to it commits nothing, and is told to try again.
|
|
68
|
+
CREATE TRIGGER core_subscription_offer_stale
|
|
69
|
+
BEFORE UPDATE ON core_subscription_offer
|
|
70
|
+
WHEN NEW.version <> OLD.version + 1
|
|
71
|
+
BEGIN
|
|
72
|
+
SELECT RAISE(ABORT, 'core_subscription_offer_stale');
|
|
73
|
+
END;
|
|
74
|
+
|
|
75
|
+
-- A checkout opened from an offer names it, so the provider's confirmation marks the offer taken.
|
|
76
|
+
ALTER TABLE core_subscription_checkout ADD COLUMN offer_id TEXT;
|
|
77
|
+
|
|
78
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
79
|
+
UPDATE core_meta SET value = '532'
|
|
80
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 532;
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
-- Migration 0533: allowances (stage 32b, the `allowance` behaviour, ADRs 0493 and 0494).
|
|
2
|
+
--
|
|
3
|
+
-- A holder (a pass, a membership: a record of an object that declares `allowance.holds`) grants a
|
|
4
|
+
-- number of uses per total, week or month, or unlimited uses. A spender (a booking, a visit)
|
|
5
|
+
-- spends one when it enters a state its object declares, and gives it back on entering another.
|
|
6
|
+
--
|
|
7
|
+
-- The rule is in the database, as capacity's is (migration 0440): a spend is a fixed set of
|
|
8
|
+
-- statements in the spender's own batch, with no read of the count first, and the database
|
|
9
|
+
-- refuses one past the limit, so the whole batch (the booking with it) is not applied. D1 runs one
|
|
10
|
+
-- statement at a time per database, so concurrent spends cannot both take the last use.
|
|
11
|
+
--
|
|
12
|
+
-- * core_allowance: the holder's terms, derived by the host in SQL from the stored record in the
|
|
13
|
+
-- batch of every write to it (so two writes to different fields cannot leave a stale row), and
|
|
14
|
+
-- for every record by the reconciler when the declaration is installed or changed.
|
|
15
|
+
-- * core_allowance_period: one counter per holder per period the holder has been spent in
|
|
16
|
+
-- (`total`, `week:<Monday's local date>`, `month:<YYYY-MM>`), with the period's real limit. A
|
|
17
|
+
-- period may sit above a limit lowered after its uses were spent; it then takes no spend until
|
|
18
|
+
-- it is back under. core_allowance_refuse_spend (a BEFORE trigger) is the guarantee: it refuses
|
|
19
|
+
-- any change that takes `used` up past the limit, whoever writes it, with the numbers
|
|
20
|
+
-- (errors.ts parses `core_allowance_refused {json}`). The one exception is a re-keying after a
|
|
21
|
+
-- change of period or zone (`rekeyed` goes up with `used`): uses already spent move to their
|
|
22
|
+
-- new period even past its limit, as a lowered limit leaves them.
|
|
23
|
+
-- * core_allowance_use: one row per use, spent and perhaps given back. A BEFORE trigger refuses
|
|
24
|
+
-- a use the holder cannot pay at the spender's time: it is not theirs, not live, its Person is
|
|
25
|
+
-- in the Trash, it is not yet valid or has ended. One open use per spender (a partial unique
|
|
26
|
+
-- index).
|
|
27
|
+
CREATE TABLE core_allowance (
|
|
28
|
+
-- The holder's object (as in core_transition_history.object_type) and id.
|
|
29
|
+
object_type TEXT NOT NULL,
|
|
30
|
+
record_id TEXT NOT NULL,
|
|
31
|
+
-- The Person whose uses they are (the holder, not the payer: ADR 0487).
|
|
32
|
+
person_id TEXT,
|
|
33
|
+
-- Uses per period. NULL: unlimited (only where the object declares an empty field so).
|
|
34
|
+
uses INTEGER CHECK (uses IS NULL OR uses >= 0),
|
|
35
|
+
per TEXT NOT NULL CHECK (per IN ('total', 'week', 'month')),
|
|
36
|
+
-- The IANA zone weeks, months and dates are counted in: the holder's own field, else the
|
|
37
|
+
-- organisation's when the holder was first written (ADR 0494: it does not follow the setting).
|
|
38
|
+
zone TEXT NOT NULL,
|
|
39
|
+
-- A datetime `from` / `until`, as ISO instants: a spender's time must be >= from and < until.
|
|
40
|
+
valid_from TEXT,
|
|
41
|
+
valid_until TEXT,
|
|
42
|
+
-- A date `from` / `until` (YYYY-MM-DD in `zone`): the spender's local date must be in them.
|
|
43
|
+
from_date TEXT,
|
|
44
|
+
until_date TEXT,
|
|
45
|
+
-- 1 when the record's state is one its object declares live (or it has no states).
|
|
46
|
+
live INTEGER NOT NULL CHECK (live IN (0, 1)),
|
|
47
|
+
-- 1 while the record is in the Trash.
|
|
48
|
+
trashed INTEGER NOT NULL DEFAULT 0 CHECK (trashed IN (0, 1)),
|
|
49
|
+
-- A change of period or zone applies from the end of the period running when it was made
|
|
50
|
+
-- (ADR 0494): before `applies_from`, spends are counted by `prev_per` in `prev_zone`.
|
|
51
|
+
prev_per TEXT CHECK (prev_per IS NULL OR prev_per IN ('total', 'week', 'month')),
|
|
52
|
+
prev_zone TEXT,
|
|
53
|
+
applies_from TEXT,
|
|
54
|
+
updated_at TEXT NOT NULL,
|
|
55
|
+
PRIMARY KEY (object_type, record_id)
|
|
56
|
+
) WITHOUT ROWID;
|
|
57
|
+
-- What a spend reads: the Person's holders of the objects that may pay for it.
|
|
58
|
+
CREATE INDEX core_allowance_person_idx ON core_allowance (person_id, object_type);
|
|
59
|
+
|
|
60
|
+
CREATE TABLE core_allowance_period (
|
|
61
|
+
object_type TEXT NOT NULL,
|
|
62
|
+
record_id TEXT NOT NULL,
|
|
63
|
+
period TEXT NOT NULL,
|
|
64
|
+
per TEXT NOT NULL CHECK (per IN ('total', 'week', 'month')),
|
|
65
|
+
zone TEXT NOT NULL,
|
|
66
|
+
-- The period's bounds, ISO instants: [starts_at, ends_at).
|
|
67
|
+
starts_at TEXT NOT NULL,
|
|
68
|
+
ends_at TEXT NOT NULL,
|
|
69
|
+
-- The limit for this period, from the terms (followed by current and future periods when the
|
|
70
|
+
-- terms change). NULL: unlimited.
|
|
71
|
+
uses INTEGER CHECK (uses IS NULL OR uses >= 0),
|
|
72
|
+
used INTEGER NOT NULL DEFAULT 0 CHECK (used >= 0),
|
|
73
|
+
-- Uses moved here by a re-keying (a change of period or zone): see the header.
|
|
74
|
+
rekeyed INTEGER NOT NULL DEFAULT 0,
|
|
75
|
+
updated_at TEXT NOT NULL,
|
|
76
|
+
-- Keyed with its keying (per and zone), so a holder's periods under an old keying never mix
|
|
77
|
+
-- with those under the new one.
|
|
78
|
+
PRIMARY KEY (object_type, record_id, period, zone),
|
|
79
|
+
FOREIGN KEY (object_type, record_id) REFERENCES core_allowance (object_type, record_id)
|
|
80
|
+
ON DELETE CASCADE
|
|
81
|
+
) WITHOUT ROWID;
|
|
82
|
+
|
|
83
|
+
-- Refuses a spend that would take a period past its limit, with the real limit. A period over a
|
|
84
|
+
-- lowered limit takes no spend; giving uses back (used going down) is never refused.
|
|
85
|
+
CREATE TRIGGER core_allowance_refuse_spend
|
|
86
|
+
BEFORE UPDATE OF used ON core_allowance_period
|
|
87
|
+
WHEN NEW.uses IS NOT NULL AND NEW.used > NEW.uses AND NEW.used > OLD.used
|
|
88
|
+
AND NEW.rekeyed = OLD.rekeyed
|
|
89
|
+
BEGIN
|
|
90
|
+
SELECT RAISE(ABORT, 'core_allowance_refused ' || json_object(
|
|
91
|
+
'reason', 'no_uses_left',
|
|
92
|
+
'object', NEW.object_type,
|
|
93
|
+
'record', NEW.record_id,
|
|
94
|
+
'period', NEW.period,
|
|
95
|
+
'uses', NEW.uses,
|
|
96
|
+
'used', OLD.used,
|
|
97
|
+
'asked', NEW.used - OLD.used
|
|
98
|
+
));
|
|
99
|
+
END;
|
|
100
|
+
|
|
101
|
+
CREATE TABLE core_allowance_use (
|
|
102
|
+
-- alu_… (ids.ts).
|
|
103
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
104
|
+
-- The holder spent from, and the period (with its zone) the use counts in.
|
|
105
|
+
object_type TEXT NOT NULL,
|
|
106
|
+
record_id TEXT NOT NULL,
|
|
107
|
+
period TEXT NOT NULL,
|
|
108
|
+
zone TEXT NOT NULL,
|
|
109
|
+
-- The spender's record.
|
|
110
|
+
spender_type TEXT NOT NULL,
|
|
111
|
+
spender_id TEXT NOT NULL,
|
|
112
|
+
person_id TEXT NOT NULL,
|
|
113
|
+
-- The spender's time (a class's start), an ISO instant, and its local date in the holder's
|
|
114
|
+
-- zone: the period is the one containing it.
|
|
115
|
+
at TEXT NOT NULL,
|
|
116
|
+
at_date TEXT NOT NULL,
|
|
117
|
+
spent_at TEXT NOT NULL,
|
|
118
|
+
-- Who made the move that spent it: staff, customer or host.
|
|
119
|
+
spent_by TEXT NOT NULL CHECK (spent_by IN ('staff', 'customer', 'host')),
|
|
120
|
+
returned_at TEXT,
|
|
121
|
+
returned_by TEXT CHECK (returned_by IS NULL OR returned_by IN ('staff', 'customer', 'host')),
|
|
122
|
+
-- The use this one took over, when a spender was moved to another, or re-spent after what it
|
|
123
|
+
-- points at changed (ADR 0493).
|
|
124
|
+
moved_from TEXT,
|
|
125
|
+
FOREIGN KEY (object_type, record_id, period, zone)
|
|
126
|
+
REFERENCES core_allowance_period (object_type, record_id, period, zone) ON DELETE CASCADE,
|
|
127
|
+
CHECK ((returned_at IS NULL) = (returned_by IS NULL))
|
|
128
|
+
);
|
|
129
|
+
CREATE INDEX core_allowance_use_holder_idx ON core_allowance_use (object_type, record_id, period);
|
|
130
|
+
CREATE INDEX core_allowance_use_person_idx ON core_allowance_use (person_id);
|
|
131
|
+
-- A spender holds at most one use at a time (and its open use is found by this index).
|
|
132
|
+
CREATE UNIQUE INDEX core_allowance_use_open_idx ON core_allowance_use (spender_type, spender_id)
|
|
133
|
+
WHERE returned_at IS NULL;
|
|
134
|
+
|
|
135
|
+
-- Refuses a use the holder cannot pay at the spender's time, saying why.
|
|
136
|
+
CREATE TRIGGER core_allowance_refuse_use
|
|
137
|
+
BEFORE INSERT ON core_allowance_use
|
|
138
|
+
WHEN NOT EXISTS (
|
|
139
|
+
SELECT 1 FROM core_allowance a
|
|
140
|
+
WHERE a.object_type = NEW.object_type AND a.record_id = NEW.record_id
|
|
141
|
+
AND a.person_id = NEW.person_id AND a.live = 1 AND a.trashed = 0
|
|
142
|
+
AND (a.valid_from IS NULL OR a.valid_from <= NEW.at)
|
|
143
|
+
AND (a.valid_until IS NULL OR NEW.at < a.valid_until)
|
|
144
|
+
AND (a.from_date IS NULL OR a.from_date <= NEW.at_date)
|
|
145
|
+
AND (a.until_date IS NULL OR NEW.at_date <= a.until_date)
|
|
146
|
+
) OR NOT EXISTS (
|
|
147
|
+
SELECT 1 FROM core_person p WHERE p.id = NEW.person_id AND p.deleted_at IS NULL
|
|
148
|
+
)
|
|
149
|
+
BEGIN
|
|
150
|
+
SELECT RAISE(ABORT, 'core_allowance_refused ' || json_object(
|
|
151
|
+
'reason', coalesce((
|
|
152
|
+
SELECT CASE
|
|
153
|
+
WHEN a.person_id IS NOT NEW.person_id THEN 'not_theirs'
|
|
154
|
+
WHEN NOT EXISTS (
|
|
155
|
+
SELECT 1 FROM core_person p WHERE p.id = NEW.person_id AND p.deleted_at IS NULL
|
|
156
|
+
) THEN 'not_live'
|
|
157
|
+
WHEN a.live = 0 OR a.trashed = 1 THEN 'not_live'
|
|
158
|
+
WHEN (a.valid_from IS NOT NULL AND NEW.at < a.valid_from)
|
|
159
|
+
OR (a.from_date IS NOT NULL AND NEW.at_date < a.from_date) THEN 'not_yet_valid'
|
|
160
|
+
ELSE 'ended'
|
|
161
|
+
END
|
|
162
|
+
FROM core_allowance a
|
|
163
|
+
WHERE a.object_type = NEW.object_type AND a.record_id = NEW.record_id
|
|
164
|
+
), 'no_holder'),
|
|
165
|
+
'object', NEW.object_type,
|
|
166
|
+
'record', NEW.record_id,
|
|
167
|
+
'at', NEW.at
|
|
168
|
+
));
|
|
169
|
+
END;
|
|
170
|
+
|
|
171
|
+
-- The read views, for query.sql and dashboards.
|
|
172
|
+
--
|
|
173
|
+
-- rec_allowance: one row per holder, its terms and the current period's numbers (`left` is what
|
|
174
|
+
-- can be spent now: 0 when it cannot be spent now, NULL when unlimited). A date `from` / `until`
|
|
175
|
+
-- is compared with today's date in UTC here (the host's own answers use the holder's zone).
|
|
176
|
+
-- rec_allowance_period: every period a holder was spent in, with its numbers.
|
|
177
|
+
-- rec_allowance_use: every use, spent or given back.
|
|
178
|
+
CREATE VIEW rec_allowance AS
|
|
179
|
+
WITH c AS (
|
|
180
|
+
SELECT
|
|
181
|
+
a.*,
|
|
182
|
+
strftime('%Y-%m-%dT%H:%M:%fZ', 'now') AS now_at,
|
|
183
|
+
CASE WHEN a.applies_from IS NOT NULL AND a.applies_from > strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
|
|
184
|
+
THEN a.prev_per ELSE a.per END AS cur_per,
|
|
185
|
+
CASE WHEN a.applies_from IS NOT NULL AND a.applies_from > strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
|
|
186
|
+
THEN a.prev_zone ELSE a.zone END AS cur_zone
|
|
187
|
+
FROM core_allowance a
|
|
188
|
+
), n AS (
|
|
189
|
+
SELECT
|
|
190
|
+
c.*,
|
|
191
|
+
coalesce((
|
|
192
|
+
SELECT p.used FROM core_allowance_period p
|
|
193
|
+
WHERE p.object_type = c.object_type AND p.record_id = c.record_id
|
|
194
|
+
AND p.per = c.cur_per AND p.zone = c.cur_zone
|
|
195
|
+
AND p.starts_at <= c.now_at AND p.ends_at > c.now_at
|
|
196
|
+
), 0) AS cur_used
|
|
197
|
+
FROM c
|
|
198
|
+
)
|
|
199
|
+
SELECT
|
|
200
|
+
object_type,
|
|
201
|
+
record_id,
|
|
202
|
+
person_id,
|
|
203
|
+
uses,
|
|
204
|
+
per,
|
|
205
|
+
zone,
|
|
206
|
+
valid_from,
|
|
207
|
+
valid_until,
|
|
208
|
+
from_date,
|
|
209
|
+
until_date,
|
|
210
|
+
live,
|
|
211
|
+
trashed,
|
|
212
|
+
cur_used AS used,
|
|
213
|
+
CASE
|
|
214
|
+
WHEN live = 0 OR trashed = 1
|
|
215
|
+
OR (valid_from IS NOT NULL AND valid_from > now_at)
|
|
216
|
+
OR (valid_until IS NOT NULL AND valid_until <= now_at)
|
|
217
|
+
OR (from_date IS NOT NULL AND from_date > date('now'))
|
|
218
|
+
OR (until_date IS NOT NULL AND until_date < date('now'))
|
|
219
|
+
THEN 0
|
|
220
|
+
WHEN uses IS NULL THEN NULL
|
|
221
|
+
ELSE max(uses - cur_used, 0)
|
|
222
|
+
END AS "left",
|
|
223
|
+
updated_at
|
|
224
|
+
FROM n;
|
|
225
|
+
|
|
226
|
+
CREATE VIEW rec_allowance_period AS
|
|
227
|
+
SELECT
|
|
228
|
+
object_type,
|
|
229
|
+
record_id,
|
|
230
|
+
period,
|
|
231
|
+
per,
|
|
232
|
+
zone,
|
|
233
|
+
starts_at,
|
|
234
|
+
ends_at,
|
|
235
|
+
uses,
|
|
236
|
+
used,
|
|
237
|
+
CASE WHEN uses IS NULL THEN NULL ELSE max(uses - used, 0) END AS "left",
|
|
238
|
+
updated_at
|
|
239
|
+
FROM core_allowance_period;
|
|
240
|
+
|
|
241
|
+
CREATE VIEW rec_allowance_use AS
|
|
242
|
+
SELECT
|
|
243
|
+
id,
|
|
244
|
+
object_type,
|
|
245
|
+
record_id,
|
|
246
|
+
period,
|
|
247
|
+
zone,
|
|
248
|
+
spender_type,
|
|
249
|
+
spender_id,
|
|
250
|
+
person_id,
|
|
251
|
+
at,
|
|
252
|
+
at_date,
|
|
253
|
+
spent_at,
|
|
254
|
+
spent_by,
|
|
255
|
+
returned_at,
|
|
256
|
+
returned_by,
|
|
257
|
+
moved_from
|
|
258
|
+
FROM core_allowance_use;
|
|
259
|
+
|
|
260
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
261
|
+
UPDATE core_meta SET value = '533'
|
|
262
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 533;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
-- Migration 0560: the `recurring` behaviour's bookkeeping (stage 31, ADR 0500).
|
|
2
|
+
--
|
|
3
|
+
-- A series (a record of an object that declares `recurring`) has one row here: when the host
|
|
4
|
+
-- next makes its records (the minute cron picks rows whose time has come; a write to the series
|
|
5
|
+
-- sets it to now) and up to which local date it has made them. Each record it made has one
|
|
6
|
+
-- occurrence row, keyed on the series and the local date: the idempotency key. A second run, or
|
|
7
|
+
-- two runs at once, cannot make a date twice, because the second insert of its key fails and with
|
|
8
|
+
-- it the whole batch that would have created the record. An occurrence edited by someone other
|
|
9
|
+
-- than the host is marked, and the series never writes to it again.
|
|
10
|
+
CREATE TABLE core_recurrence_series (
|
|
11
|
+
object_type TEXT NOT NULL,
|
|
12
|
+
record_id TEXT NOT NULL,
|
|
13
|
+
-- When the host next makes this series' records (ISO instant).
|
|
14
|
+
next_run_at TEXT NOT NULL,
|
|
15
|
+
-- The last local date (YYYY-MM-DD) it has made records up to, or none yet.
|
|
16
|
+
generated_until TEXT,
|
|
17
|
+
-- Bumped by every write to the series and every run of it: a run or a change that read an
|
|
18
|
+
-- older version fails its batch (core_write_guard below), so neither leaves stale records.
|
|
19
|
+
version INTEGER NOT NULL DEFAULT 0,
|
|
20
|
+
updated_at TEXT NOT NULL,
|
|
21
|
+
PRIMARY KEY (object_type, record_id)
|
|
22
|
+
) WITHOUT ROWID;
|
|
23
|
+
CREATE INDEX core_recurrence_series_due_idx ON core_recurrence_series (next_run_at);
|
|
24
|
+
|
|
25
|
+
CREATE TABLE core_recurrence_occurrence (
|
|
26
|
+
series_object TEXT NOT NULL,
|
|
27
|
+
series_id TEXT NOT NULL,
|
|
28
|
+
-- The occurrence's local date in the series' zone (YYYY-MM-DD).
|
|
29
|
+
local_date TEXT NOT NULL,
|
|
30
|
+
-- The record made for it.
|
|
31
|
+
object_type TEXT NOT NULL,
|
|
32
|
+
record_id TEXT NOT NULL,
|
|
33
|
+
-- Set when someone other than the host changed it: the series leaves it alone from then on.
|
|
34
|
+
edited_at TEXT,
|
|
35
|
+
created_at TEXT NOT NULL,
|
|
36
|
+
PRIMARY KEY (series_object, series_id, local_date)
|
|
37
|
+
) WITHOUT ROWID;
|
|
38
|
+
CREATE UNIQUE INDEX core_recurrence_occurrence_record_idx
|
|
39
|
+
ON core_recurrence_occurrence (object_type, record_id);
|
|
40
|
+
|
|
41
|
+
-- Guards for a batch that was planned from a read (stage 31, ADRs 0500 and 0501). A statement
|
|
42
|
+
-- inserts one row saying what it read; the trigger compares it with what is there now, in the
|
|
43
|
+
-- batch, and either drops the row silently (still true) or fails the whole batch. Nothing is ever
|
|
44
|
+
-- stored here.
|
|
45
|
+
-- kind 'claimed': a capacity counter's held + taken is still `expected`
|
|
46
|
+
-- kind 'claimant': the claimant record still has `expected` live claims (held or confirmed)
|
|
47
|
+
-- kind 'series': a series' version is still `expected`
|
|
48
|
+
CREATE TABLE core_write_guard (
|
|
49
|
+
kind TEXT NOT NULL CHECK (kind IN ('claimed', 'claimant', 'series')),
|
|
50
|
+
object_type TEXT NOT NULL,
|
|
51
|
+
record_id TEXT NOT NULL,
|
|
52
|
+
expected INTEGER NOT NULL
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
CREATE TRIGGER core_write_guard_check
|
|
56
|
+
BEFORE INSERT ON core_write_guard
|
|
57
|
+
BEGIN
|
|
58
|
+
SELECT CASE
|
|
59
|
+
WHEN NEW.kind = 'claimed' AND coalesce((SELECT held + taken FROM core_capacity
|
|
60
|
+
WHERE object_type = NEW.object_type AND record_id = NEW.record_id), 0) != NEW.expected
|
|
61
|
+
THEN RAISE(ABORT, 'core_recurrence_stale')
|
|
62
|
+
WHEN NEW.kind = 'series' AND coalesce((SELECT version FROM core_recurrence_series
|
|
63
|
+
WHERE object_type = NEW.object_type AND record_id = NEW.record_id), 0) != NEW.expected
|
|
64
|
+
THEN RAISE(ABORT, 'core_recurrence_stale')
|
|
65
|
+
WHEN NEW.kind = 'claimant' AND (SELECT count(*) FROM core_capacity_claim
|
|
66
|
+
WHERE for_type = NEW.object_type AND for_id = NEW.record_id
|
|
67
|
+
AND state IN ('held', 'confirmed')) != NEW.expected
|
|
68
|
+
THEN RAISE(ABORT, 'core_claim_live')
|
|
69
|
+
ELSE RAISE(IGNORE)
|
|
70
|
+
END;
|
|
71
|
+
END;
|
|
72
|
+
|
|
73
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
74
|
+
UPDATE core_meta SET value = '560'
|
|
75
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 560;
|