@crombolo/data 0.76.0 → 0.78.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.76.0",
3
+ "version": "0.78.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/core": "0.76.0",
32
+ "@crombolo/core": "0.78.0",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
package/src/index.ts CHANGED
@@ -163,6 +163,7 @@ export * from './schema/workspace-links.ts'
163
163
  export * from './schema/workspace-merge.ts'
164
164
  export * from './schema/workspace-privacy.ts'
165
165
  export * from './schema/workspace-processes.ts'
166
+ export * from './schema/workspace-schedules.ts'
166
167
  export * from './schema/workspace-schema.ts'
167
168
  export * from './schema/workspaces.ts'
168
169
  export * from './schema-ops.ts'
@@ -2,6 +2,8 @@
2
2
  * A cluster-wide pause on editing, held in `key_value` so every node sees it on
3
3
  * its next write rather than its next poll. Readers are never affected; the
4
4
  * write gate (`assertNodeMayWrite`) refuses editor writes while it is set.
5
+ * Story 409: a pause may carry when it ends; past that, by the reading node's own
6
+ * clock, it is no pause, so nothing has to run for it to end.
5
7
  */
6
8
  import type { Db } from './database.ts'
7
9
  import { readKeyValue, writeKeyValue } from './migrations.ts'
@@ -12,34 +14,59 @@ export interface WritesPaused {
12
14
  reason: string
13
15
  by: string | null
14
16
  at: string
17
+ /** When it ends, as an ISO time; null for a pause that lasts until resumed. */
18
+ until: string | null
15
19
  }
16
20
 
17
- export function parseWritesPaused(value: string | null | undefined): WritesPaused | undefined {
21
+ /** Whether a pause read back is over by now: it had an end, and that has passed. Pure. */
22
+ export const pauseEnded = (paused: { until: string | null }, now = new Date()): boolean =>
23
+ paused.until !== null && Date.parse(paused.until) <= now.getTime()
24
+
25
+ /** The pause a stored value holds, or undefined for none, or one whose time has passed. */
26
+ export function parseWritesPaused(
27
+ value: string | null | undefined,
28
+ now = new Date(),
29
+ ): WritesPaused | undefined {
18
30
  if (!value) return undefined
31
+ let paused: WritesPaused
19
32
  try {
20
33
  const parsed = JSON.parse(value) as Partial<WritesPaused>
21
- return {
34
+ paused = {
22
35
  reason: String(parsed.reason ?? ''),
23
36
  by: parsed.by ?? null,
24
37
  at: String(parsed.at ?? ''),
38
+ // A pause stored before story 409 has no end: it lasts until resumed.
39
+ until: typeof parsed.until === 'string' && parsed.until ? parsed.until : null,
25
40
  }
26
41
  } catch {
27
- return { reason: '', by: null, at: '' }
42
+ return { reason: '', by: null, at: '', until: null }
28
43
  }
44
+ return pauseEnded(paused, now) ? undefined : paused
29
45
  }
30
46
 
31
- export async function readWritesPaused(db: Db): Promise<WritesPaused | undefined> {
32
- return parseWritesPaused(await readKeyValue(db, WRITES_PAUSED_KEY))
47
+ export async function readWritesPaused(
48
+ db: Db,
49
+ now = new Date(),
50
+ ): Promise<WritesPaused | undefined> {
51
+ return parseWritesPaused(await readKeyValue(db, WRITES_PAUSED_KEY), now)
33
52
  }
34
53
 
54
+ /**
55
+ * Pauses editing, until `until` or until resumed. Pausing again for the same reason keeps
56
+ * who paused and when, and moves the end; another reason replaces the pause.
57
+ */
35
58
  export async function pauseWrites(
36
59
  db: Db,
37
- options: { reason: string; by?: string },
60
+ options: { reason: string; by?: string | null; until?: Date | null },
61
+ now = new Date(),
38
62
  ): Promise<WritesPaused> {
63
+ const before = await readWritesPaused(db, now)
64
+ const same = before !== undefined && before.reason === options.reason
39
65
  const paused: WritesPaused = {
40
66
  reason: options.reason,
41
- by: options.by ?? null,
42
- at: new Date().toISOString(),
67
+ by: same ? before.by : (options.by ?? null),
68
+ at: same ? before.at : now.toISOString(),
69
+ until: options.until ? options.until.toISOString() : null,
43
70
  }
44
71
  await writeKeyValue(db, WRITES_PAUSED_KEY, JSON.stringify(paused))
45
72
  return paused
@@ -21,6 +21,24 @@ export const EDIT_WINDOW_MS = 10 * 60 * 1000
21
21
  /** Where a change keeps a page's schedule among its items (story 395): `schedules/<page key>`. */
22
22
  export const SCHEDULE_PREFIX = 'schedules/'
23
23
 
24
+ /** Story 411: where a change keeps a page's place and whether it was published: `places/<key>`. */
25
+ export const PLACE_PREFIX = 'places/'
26
+
27
+ /** Story 411: where a change keeps the order of a parent's children: `orders/<key>` or `orders/root`. */
28
+ export const ORDER_PREFIX = 'orders/'
29
+
30
+ /** Where a page stood, beside its content, as a change carries it (story 411). */
31
+ export interface PagePlace {
32
+ parentKey: string | null
33
+ published: boolean
34
+ /** In the recycle bin. */
35
+ trashed?: boolean
36
+ }
37
+
38
+ /** The item path for a parent's children's order. */
39
+ export const orderPath = (parentKey: string | null) =>
40
+ `${ORDER_PREFIX}${parentKey ? normaliseUuid(parentKey) : 'root'}`
41
+
24
42
  export interface Change {
25
43
  id: string
26
44
  kind: ChangeSource
@@ -252,6 +270,167 @@ export class ChangeRepository {
252
270
  await this.#touch(id, now)
253
271
  }
254
272
 
273
+ /**
274
+ * A page a person moved, restored or unpublished straight in Shared (story 411): in
275
+ * their open change, with where it stood before their first such change and now.
276
+ */
277
+ async placed(
278
+ madeBy: string,
279
+ page: { key: string; kind: string },
280
+ before: PagePlace,
281
+ after: PagePlace,
282
+ now = new Date(),
283
+ ): Promise<boolean> {
284
+ const id = await this.#openEdit(madeBy, now)
285
+ if (!(await this.#recordPage(id, page))) return false
286
+ await this.#keepItem(id, `${PLACE_PREFIX}${normaliseUuid(page.key)}`, before, after)
287
+ await this.#touch(id, now)
288
+ return true
289
+ }
290
+
291
+ /** A parent's children a person reordered straight in Shared (story 411), before and now. */
292
+ async ordered(
293
+ madeBy: string,
294
+ parentKey: string | null,
295
+ before: readonly string[],
296
+ after: readonly string[],
297
+ now = new Date(),
298
+ ): Promise<void> {
299
+ const id = await this.#openEdit(madeBy, now)
300
+ await this.#keepItem(id, orderPath(parentKey), before, after)
301
+ await this.#touch(id, now)
302
+ }
303
+
304
+ /**
305
+ * A page a person deleted straight in Shared, to the recycle bin or for good (story
306
+ * 411): in their open change as a removal, keeping the page as it stood.
307
+ */
308
+ async removedBy(
309
+ madeBy: string,
310
+ page: { key: string; kind: string },
311
+ was: object,
312
+ now = new Date(),
313
+ ): Promise<void> {
314
+ const id = await this.#openEdit(madeBy, now)
315
+ const nodeKey = normaliseUuid(page.key)
316
+ const recorded = await this.#db.q
317
+ .selectFrom('content_transfer_change')
318
+ .select('id')
319
+ .where('run_id', '=', id)
320
+ .where('node_key', '=', nodeKey)
321
+ .execute()
322
+ // Gone for good, it cannot be read back: one the change already holds keeps its row.
323
+ if (recorded.length === 0 && !(await this.#recordPage(id, page)))
324
+ await this.#db.q
325
+ .insertInto('content_transfer_change')
326
+ .values({
327
+ run_id: id,
328
+ node_key: nodeKey,
329
+ kind: page.kind,
330
+ action: 'recycle',
331
+ before_event_id: null,
332
+ before_published_event_id: null,
333
+ event_id: null,
334
+ })
335
+ .execute()
336
+ await this.#db.q
337
+ .updateTable('content_transfer_change')
338
+ .set({ action: 'recycle', event_id: null })
339
+ .where('run_id', '=', id)
340
+ .where('node_key', '=', nodeKey)
341
+ .execute()
342
+ const path = `pages/${nodeKey}`
343
+ const found = await this.#db.q
344
+ .selectFrom('change_item')
345
+ .select('id')
346
+ .where('run_id', '=', id)
347
+ .where('path', '=', path)
348
+ .execute()
349
+ if (!found[0])
350
+ await this.item(id, {
351
+ path,
352
+ kind: 'page',
353
+ class: 'none',
354
+ before: JSON.stringify(was),
355
+ after: null,
356
+ })
357
+ await this.#touch(id, now)
358
+ }
359
+
360
+ /**
361
+ * Story 413: a person's newest change that holds a page, made by their own edits or by
362
+ * the merge of their own workspace, closed if it was still open.
363
+ */
364
+ async closeHolding(
365
+ madeBy: string,
366
+ pageKey: string,
367
+ now = new Date(),
368
+ ): Promise<{ id: string; key: string; name: string | null; merged: boolean } | undefined> {
369
+ const row = await this.#db.q
370
+ .selectFrom('content_transfer_change as c')
371
+ .innerJoin('content_transfer_run as r', 'r.id', 'c.run_id')
372
+ .select(['r.id', 'r.change_kind', 'r.closed_at'])
373
+ .where('c.node_key', '=', normaliseUuid(pageKey))
374
+ .where('r.made_by', '=', normaliseUuid(madeBy))
375
+ .where('r.change_kind', 'in', ['edit', 'merge'])
376
+ .orderBy('r.started_at', 'desc')
377
+ .executeTakeFirst()
378
+ if (!row) return undefined
379
+ const id = String(row.id)
380
+ const merged = row.change_kind === 'merge'
381
+ if (!merged && row.closed_at === null) {
382
+ const closed = await this.#close(id, now)
383
+ return { id, ...closed, merged }
384
+ }
385
+ const run = await this.byId(id)
386
+ return run ? { id, key: run.key, name: run.name, merged } : undefined
387
+ }
388
+
389
+ /** Story 413: a page's schedule set by an approval, kept in the change that holds the page. */
390
+ async scheduleIn(changeId: string, pageKey: string, entries: readonly unknown[]): Promise<void> {
391
+ await this.#keepItem(changeId, `${SCHEDULE_PREFIX}${normaliseUuid(pageKey)}`, null, entries)
392
+ }
393
+
394
+ /** Closes a person's open change (story 411): finished, to be passed on. */
395
+ async closeOpen(
396
+ madeBy: string,
397
+ now = new Date(),
398
+ ): Promise<{ key: string; name: string | null } | undefined> {
399
+ const open = await this.#db.q
400
+ .selectFrom('content_transfer_run')
401
+ .select('id')
402
+ .where('change_kind', '=', 'edit')
403
+ .where('made_by', '=', madeBy)
404
+ .where('closed_at', 'is', null)
405
+ .orderBy('started_at', 'desc')
406
+ .executeTakeFirst()
407
+ return open ? this.#close(String(open.id), now) : undefined
408
+ }
409
+
410
+ /** An item that keeps its first "before" and takes the latest "after". */
411
+ async #keepItem(id: string, path: string, before: unknown, after: unknown) {
412
+ const found = await this.#db.q
413
+ .selectFrom('change_item')
414
+ .select('id')
415
+ .where('run_id', '=', id)
416
+ .where('path', '=', path)
417
+ .execute()
418
+ if (found[0])
419
+ await this.#db.q
420
+ .updateTable('change_item')
421
+ .set({ after_hash: await this.#put(JSON.stringify(after)) })
422
+ .where('id', '=', Number(found[0].id))
423
+ .execute()
424
+ else
425
+ await this.item(id, {
426
+ path,
427
+ kind: 'page',
428
+ class: 'none',
429
+ before: JSON.stringify(before),
430
+ after: JSON.stringify(after),
431
+ })
432
+ }
433
+
255
434
  /**
256
435
  * A page a person published straight in Shared (story 388): in their open change as
257
436
  * it is now, and the change closed, so it is finished and can be passed on. Their
@@ -264,6 +443,10 @@ export class ChangeRepository {
264
443
  ): Promise<{ key: string; name: string | null } | undefined> {
265
444
  const id = await this.#openEdit(madeBy, now)
266
445
  if (!(await this.#recordPage(id, page))) return undefined
446
+ return this.#close(id, now)
447
+ }
448
+
449
+ async #close(id: string, now: Date): Promise<{ key: string; name: string | null }> {
267
450
  await this.#touch(id, now)
268
451
  await this.#db.q
269
452
  .updateTable('content_transfer_run')
@@ -694,6 +694,25 @@ export class DocumentRepository {
694
694
  }
695
695
 
696
696
  /** The workspace's latest event for a page, with its name, template and date. */
697
+ /** A version as a page's head reads it (story 411): for a workspace page with none of its own. */
698
+ async #versionHead(versionId: number | null) {
699
+ if (versionId === null) return undefined
700
+ const rows = await this.#db.q
701
+ .selectFrom('content_version as cv')
702
+ .innerJoin('document_version as dv', 'dv.id', 'cv.id')
703
+ .leftJoin('node as tn', 'tn.id', 'dv.template_id')
704
+ .select([
705
+ 'cv.id',
706
+ 'cv.text',
707
+ 'cv.version_date',
708
+ 'dv.template_id',
709
+ 'tn.unique_id as template_key',
710
+ ])
711
+ .where('cv.id', '=', versionId)
712
+ .execute()
713
+ return rows[0]
714
+ }
715
+
697
716
  async #workspaceHead(nodeId: number) {
698
717
  const rows = await this.#db.q
699
718
  .selectFrom('content_version as cv')
@@ -786,7 +805,9 @@ export class DocumentRepository {
786
805
  .where('c.node_id', '=', node.id)
787
806
  .execute()
788
807
  const row = rows[0]
789
- const head = await this.#workspaceHead(node.id)
808
+ // Unpublished there and not saved there, it has no version of its own: Shared's it began from.
809
+ const head =
810
+ (await this.#workspaceHead(node.id)) ?? (await this.#versionHead(wd.baseEventId ?? null))
790
811
  if (!row || !head) return undefined
791
812
  const propertyTypes = await this.#propertyTypes(Number(row.content_type_id))
792
813
  const languages = await this.#languages()
@@ -3108,6 +3129,31 @@ export class DocumentRepository {
3108
3129
  const languages = await this.#languages()
3109
3130
  const workspace = this.#workspace ?? null
3110
3131
  await this.#db.transaction(async (tx) => {
3132
+ // Story 414: the first time a workspace sets one, Shared's as it was then; a publish
3133
+ // that names no time, and finds none to clear, sets none.
3134
+ const timed = entries.some((e) => e.publishTime !== null || e.unpublishTime !== null)
3135
+ const clears =
3136
+ !timed &&
3137
+ workspace !== null &&
3138
+ ((await scheduleEntriesOf(tx, node.id, workspace)).length > 0 ||
3139
+ (await scheduleEntriesOf(tx, node.id, null)).length > 0)
3140
+ if (workspace !== null && (timed || clears)) {
3141
+ const held = await tx.q
3142
+ .selectFrom('workspace_schedule')
3143
+ .select('node_id')
3144
+ .where('workspace_id', '=', workspace)
3145
+ .where('node_id', '=', node.id)
3146
+ .execute()
3147
+ if (held.length === 0)
3148
+ await tx.q
3149
+ .insertInto('workspace_schedule')
3150
+ .values({
3151
+ workspace_id: workspace,
3152
+ node_id: node.id,
3153
+ base: JSON.stringify(await scheduleEntriesOf(tx, node.id, null)),
3154
+ })
3155
+ .execute()
3156
+ }
3111
3157
  for (const entry of entries) {
3112
3158
  const languageId = entry.culture
3113
3159
  ? (languages.find((l) => l.isoCode.toLowerCase() === entry.culture?.toLowerCase())?.id ??
@@ -3165,17 +3211,44 @@ export class DocumentRepository {
3165
3211
  }))
3166
3212
  }
3167
3213
 
3214
+ /**
3215
+ * A page's schedule as a change carries it (story 414): per language, when it goes live
3216
+ * and comes down, in this repository's view, Shared's or the workspace's.
3217
+ */
3218
+ async scheduleEntries(key: string): Promise<ScheduleEntryAt[]> {
3219
+ const node = await this.#nodes.byKey(key)
3220
+ if (!node) return []
3221
+ return scheduleEntriesOf(this.#db, node.id, this.#workspace ?? null)
3222
+ }
3223
+
3224
+ /** Story 414: the change that set the page's schedule in Shared, named when it fires. */
3225
+ async scheduleSetBy(key: string, changeKey: string): Promise<void> {
3226
+ const node = await this.#nodes.byKey(key)
3227
+ if (!node) return
3228
+ await this.#db.q
3229
+ .updateTable('content_schedule')
3230
+ .set({ change_key: changeKey })
3231
+ .where('node_id', '=', node.id)
3232
+ .where('workspace_id', 'is', null)
3233
+ .execute()
3234
+ }
3235
+
3168
3236
  /** Schedules whose time has come, oldest first. */
3169
- async dueSchedules(
3170
- now: Date,
3171
- ): Promise<
3172
- Array<{ id: number; key: string; culture: string | null; action: 'Release' | 'Expire' }>
3237
+ async dueSchedules(now: Date): Promise<
3238
+ Array<{
3239
+ id: number
3240
+ key: string
3241
+ culture: string | null
3242
+ action: 'Release' | 'Expire'
3243
+ /** Story 414: the change that set it, when one did. */
3244
+ changeKey: string | null
3245
+ }>
3173
3246
  > {
3174
3247
  const rows = await this.#db.q
3175
3248
  .selectFrom('content_schedule as s')
3176
3249
  .innerJoin('node as n', 'n.id', 's.node_id')
3177
3250
  .leftJoin('language as l', 'l.id', 's.language_id')
3178
- .select(['s.id', 's.action', 'n.unique_id', 'l.iso_code'])
3251
+ .select(['s.id', 's.action', 'n.unique_id', 'l.iso_code', 's.change_key'])
3179
3252
  .where('s.date', '<=', DbDate.toDb(now))
3180
3253
  .where('n.node_object_type', '=', this.objectType)
3181
3254
  // A workspace's schedules wait for it to be served (story 379).
@@ -3188,6 +3261,7 @@ export class DocumentRepository {
3188
3261
  key: normaliseUuid(String(row.unique_id)),
3189
3262
  culture: (row.iso_code as string | null) ?? null,
3190
3263
  action: String(row.action) as 'Release' | 'Expire',
3264
+ changeKey: (row.change_key as string | null) ?? null,
3191
3265
  }))
3192
3266
  }
3193
3267
 
@@ -5029,6 +5103,7 @@ export async function deleteNodeRows(
5029
5103
  )
5030
5104
  await tx.q.deleteFrom('property_value').where('node_id', 'in', nodes()).execute()
5031
5105
  await tx.q.deleteFrom('content_schedule').where('node_id', 'in', nodes()).execute()
5106
+ await tx.q.deleteFrom('workspace_schedule').where('node_id', 'in', nodes()).execute()
5032
5107
  await tx.q.deleteFrom('user_notification').where('node_id', 'in', nodes()).execute()
5033
5108
  await tx.q.deleteFrom('domain').where('node_id', 'in', nodes()).execute()
5034
5109
  await tx.q
@@ -5069,3 +5144,35 @@ export async function deleteNodeRows(
5069
5144
  )
5070
5145
  .execute()
5071
5146
  }
5147
+
5148
+ /** A page's schedule per language, as a change carries it (story 414). */
5149
+ export interface ScheduleEntryAt {
5150
+ culture: string | null
5151
+ publishTime: string | null
5152
+ unpublishTime: string | null
5153
+ }
5154
+
5155
+ /** A page's schedule rows as entries, in Shared (`workspace` null) or a workspace, by language. */
5156
+ async function scheduleEntriesOf(
5157
+ db: Db,
5158
+ nodeId: number,
5159
+ workspace: number | null,
5160
+ ): Promise<ScheduleEntryAt[]> {
5161
+ const rows = await db.q
5162
+ .selectFrom('content_schedule as s')
5163
+ .leftJoin('language as l', 'l.id', 's.language_id')
5164
+ .select(['s.action', 's.date', 'l.iso_code'])
5165
+ .where('s.node_id', '=', nodeId)
5166
+ .where('s.workspace_id', workspace === null ? 'is' : '=', workspace)
5167
+ .execute()
5168
+ const byCulture = new Map<string | null, ScheduleEntryAt>()
5169
+ for (const row of rows) {
5170
+ const culture = (row.iso_code as string | null) ?? null
5171
+ const entry = byCulture.get(culture) ?? { culture, publishTime: null, unpublishTime: null }
5172
+ const at = (DbDate.fromDb(row.date) ?? new Date(0)).toISOString()
5173
+ if (row.action === 'Release') entry.publishTime = at
5174
+ else entry.unpublishTime = at
5175
+ byCulture.set(culture, entry)
5176
+ }
5177
+ return [...byCulture.values()].sort((a, b) => (a.culture ?? '').localeCompare(b.culture ?? ''))
5178
+ }
@@ -294,6 +294,8 @@ export class WorkspaceRepository {
294
294
  await tx.q.deleteFrom('document_version').where('id', 'in', versions()).execute()
295
295
  await tx.q.deleteFrom('content_version').where('workspace_id', '=', id).execute()
296
296
  await tx.q.deleteFrom('content_schedule').where('workspace_id', '=', id).execute()
297
+ await tx.q.deleteFrom('workspace_schedule').where('workspace_id', '=', id).execute()
298
+ await tx.q.deleteFrom('workspace_schedule').where('workspace_id', '=', id).execute()
297
299
  for (const table of [
298
300
  'workspace_pending',
299
301
  'workspace_document_culture',
@@ -419,6 +421,41 @@ export class WorkspaceRepository {
419
421
  }
420
422
 
421
423
  /** Drops what waits for the merge for one page, or one marker: the change is not to go. */
424
+ /**
425
+ * The pages a workspace set a schedule for (story 414), each with Shared's schedule as
426
+ * it was when it first did.
427
+ */
428
+ async scheduled(
429
+ id: number,
430
+ ): Promise<Array<{ nodeId: number; key: string; name: string; base: unknown[] }>> {
431
+ const rows = await this.#db.q
432
+ .selectFrom('workspace_schedule as w')
433
+ .innerJoin('node as n', 'n.id', 'w.node_id')
434
+ .select(['w.node_id', 'n.unique_id', 'n.text', 'w.base'])
435
+ .where('w.workspace_id', '=', id)
436
+ .execute()
437
+ return rows.map((r) => ({
438
+ nodeId: Number(r.node_id),
439
+ key: normaliseUuid(String(r.unique_id)),
440
+ name: String(r.text ?? ''),
441
+ base: JSON.parse(String(r.base)) as unknown[],
442
+ }))
443
+ }
444
+
445
+ /** Story 414: a page's schedule gone into Shared with a merge of it alone. */
446
+ async dropSchedule(id: number, nodeId: number): Promise<void> {
447
+ await this.#db.q
448
+ .deleteFrom('content_schedule')
449
+ .where('workspace_id', '=', id)
450
+ .where('node_id', '=', nodeId)
451
+ .execute()
452
+ await this.#db.q
453
+ .deleteFrom('workspace_schedule')
454
+ .where('workspace_id', '=', id)
455
+ .where('node_id', '=', nodeId)
456
+ .execute()
457
+ }
458
+
422
459
  async dropPending(id: number, nodeId: number): Promise<void> {
423
460
  await this.#db.q
424
461
  .deleteFrom('workspace_pending')
@@ -474,6 +511,7 @@ export class WorkspaceRepository {
474
511
  await this.#db.q.deleteFrom('document_version').where('id', 'in', versions()).execute()
475
512
  await this.#db.q.deleteFrom('content_version').where('workspace_id', '=', id).execute()
476
513
  await this.#db.q.deleteFrom('content_schedule').where('workspace_id', '=', id).execute()
514
+ await this.#db.q.deleteFrom('workspace_schedule').where('workspace_id', '=', id).execute()
477
515
  for (const table of [
478
516
  'workspace_pending',
479
517
  'workspace_document_culture',
@@ -107,6 +107,7 @@ import { workspaceLinksMigration } from './workspace-links.ts'
107
107
  import { workspaceMergeMigration } from './workspace-merge.ts'
108
108
  import { workspacePrivacyMigration } from './workspace-privacy.ts'
109
109
  import { workspaceProcessesMigration } from './workspace-processes.ts'
110
+ import { workspaceSchedulesMigration } from './workspace-schedules.ts'
110
111
  import { workspaceSchemaMigration } from './workspace-schema.ts'
111
112
  import { workspacesMigration } from './workspaces.ts'
112
113
 
@@ -207,4 +208,5 @@ export const cromboloPlan = new MigrationPlan([
207
208
  changeSourcesMigration,
208
209
  groupStartKeysMigration,
209
210
  workspaceErrorsMigration,
211
+ workspaceSchedulesMigration,
210
212
  ])
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Migration 097 — a schedule set in a workspace goes with its change (story 414): which
3
+ * pages a workspace set a schedule for, with Shared's as it was then, so its merge knows
4
+ * a clear from no change and a schedule changed in Shared since; and which change set
5
+ * each schedule Shared holds, named when it fires.
6
+ */
7
+ import type { Db } from '../database.ts'
8
+ import type { Migration } from '../migrations.ts'
9
+ import { sql } from '../query.ts'
10
+ import { STATE_WORKSPACE_ERRORS } from './workspace-errors.ts'
11
+
12
+ export const STATE_WORKSPACE_SCHEDULES = '{3b9f6d52-0097-4e1a-b7c4-2a8d5f1e0097}'
13
+
14
+ export const workspaceSchedulesMigration: Migration = {
15
+ from: STATE_WORKSPACE_ERRORS,
16
+ to: STATE_WORKSPACE_SCHEDULES,
17
+ name: 'AddWorkspaceSchedules',
18
+ kind: 'expand',
19
+ release: '0.77.0',
20
+ async up(db: Db) {
21
+ const t = db.dialect.types
22
+ await db.q.schema
23
+ .createTable('workspace_schedule')
24
+ .addColumn('workspace_id', sql.raw(t.integer), (c) => c.notNull())
25
+ .addColumn('node_id', sql.raw(t.integer), (c) => c.notNull())
26
+ .addColumn('base', sql.raw(t.text), (c) => c.notNull())
27
+ .addPrimaryKeyConstraint('pk_workspace_schedule', ['workspace_id', 'node_id'])
28
+ .execute()
29
+ await db.q.schema
30
+ .alterTable('content_schedule')
31
+ .addColumn('change_key', sql.raw(t.varchar(64)))
32
+ .execute()
33
+ },
34
+ }
package/src/tables.gen.ts CHANGED
@@ -256,6 +256,7 @@ export interface CoreTables {
256
256
  }
257
257
  content_schedule: {
258
258
  action: string
259
+ change_key: string | null
259
260
  date: Timestamp
260
261
  id: Generated<number>
261
262
  language_id: number | null
@@ -1128,4 +1129,9 @@ export interface CoreTables {
1128
1129
  reason: string
1129
1130
  workspace_key: string
1130
1131
  }
1132
+ workspace_schedule: {
1133
+ base: string
1134
+ node_id: number
1135
+ workspace_id: Generated<number>
1136
+ }
1131
1137
  }