Skip to main content

max / goingson

2.9 KB · 69 lines History Blame Raw
1 -- A time session records which feature started it, and when a countdown was
2 -- meant to end.
3 --
4 -- Track and Focus write the same row. `time-tracking.js` told them apart with
5 -- `activeMode`, a variable in the JS process, and `focus-timer.js` held the
6 -- countdown as `remainingSeconds` in a `setInterval`. Neither survives a
7 -- reload, and the JS says so: a timer found running afterwards reads as
8 -- Tracking whichever it was.
9 --
10 -- Two things could not be built on that. The running-timer band cannot name the
11 -- mode, because nothing it can read carries the fact. And a countdown is the
12 -- instant it ends at, which cannot be computed from `started_at` plus the split
13 -- on the address: the address carries a split whether or not a focus session is
14 -- running, so every tracked timer would draw one.
15 --
16 -- `mode` defaults to 'track', which is what every existing row was displayed as
17 -- anyway, so no backfill is owed. `ends_at` stays NULL on those rows and on
18 -- every Track session; a Focus session started before this migration is one the
19 -- countdown cannot be rebuilt for, and there is no honest value to invent.
20 --
21 -- Reaching `ends_at` is not a state change. Nothing closes the session there:
22 -- the countdown is drawn against it and the session ends when somebody stops
23 -- it, the same as Track.
24
25 ALTER TABLE time_sessions ADD COLUMN mode TEXT NOT NULL DEFAULT 'track';
26 ALTER TABLE time_sessions ADD COLUMN ends_at TEXT;
27
28 -- The sync triggers name their columns one by one, so a new column is invisible
29 -- to a peer until they are rewritten. Both are replaced rather than amended;
30 -- SQLite has no ALTER TRIGGER.
31
32 DROP TRIGGER IF EXISTS sync_trg_time_sessions_insert;
33 CREATE TRIGGER sync_trg_time_sessions_insert
34 AFTER INSERT ON time_sessions
35 WHEN (SELECT value FROM sync_state WHERE key = 'applying_remote') != '1'
36 BEGIN
37 INSERT INTO sync_changelog (table_name, op, row_id, data)
38 VALUES ('time_sessions', 'INSERT', NEW.id, json_object(
39 'id', NEW.id,
40 'task_id', NEW.task_id,
41 'user_id', NEW.user_id,
42 'started_at', NEW.started_at,
43 'ended_at', NEW.ended_at,
44 'duration_minutes', NEW.duration_minutes,
45 'created_at', NEW.created_at,
46 'mode', NEW.mode,
47 'ends_at', NEW.ends_at
48 ));
49 END;
50
51 DROP TRIGGER IF EXISTS sync_trg_time_sessions_update;
52 CREATE TRIGGER sync_trg_time_sessions_update
53 AFTER UPDATE ON time_sessions
54 WHEN (SELECT value FROM sync_state WHERE key = 'applying_remote') != '1'
55 BEGIN
56 INSERT INTO sync_changelog (table_name, op, row_id, data)
57 VALUES ('time_sessions', 'UPDATE', NEW.id, json_object(
58 'id', NEW.id,
59 'task_id', NEW.task_id,
60 'user_id', NEW.user_id,
61 'started_at', NEW.started_at,
62 'ended_at', NEW.ended_at,
63 'duration_minutes', NEW.duration_minutes,
64 'created_at', NEW.created_at,
65 'mode', NEW.mode,
66 'ends_at', NEW.ends_at
67 ));
68 END;
69