@crombolo/data 0.69.1 → 0.70.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 +2 -2
- package/src/environments.ts +44 -0
- package/src/index.ts +15 -0
- package/src/relations.ts +2 -1
- package/src/repositories/changes.ts +525 -0
- package/src/repositories/content-types.ts +432 -4
- package/src/repositories/data-types.ts +141 -2
- package/src/repositories/documents.ts +1584 -78
- package/src/repositories/nodes.ts +47 -2
- package/src/repositories/transfer-runs.ts +6 -0
- package/src/repositories/workspace-files.ts +208 -0
- package/src/repositories/workspace-links.ts +97 -0
- package/src/repositories/workspaces.ts +547 -0
- package/src/schema/change-transfer.ts +30 -0
- package/src/schema/changes.ts +67 -0
- package/src/schema/content-places.ts +56 -0
- package/src/schema/environment-values.ts +27 -0
- package/src/schema/form-library-types.ts +2 -0
- package/src/schema/plan.ts +18 -0
- package/src/schema/seed.ts +5 -2
- package/src/schema/workspace-links.ts +37 -0
- package/src/schema/workspace-merge.ts +58 -0
- package/src/schema/workspace-privacy.ts +32 -0
- package/src/schema/workspace-schema.ts +113 -0
- package/src/schema/workspaces.ts +138 -0
- package/src/site-search.ts +1 -0
- package/src/tables.gen.ts +111 -0
- package/src/workspace-keys.ts +22 -0
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspaces (story 375): a named place to change pages apart from Shared,
|
|
3
|
+
* its owner's and shared with the people they choose. Opening one is a single
|
|
4
|
+
* row; discarding one removes every row that carries it.
|
|
5
|
+
*/
|
|
6
|
+
import { normaliseUuid } from '@crombolo/core'
|
|
7
|
+
import type { Db } from '../database.ts'
|
|
8
|
+
import { DbDate, fromDbBool } from '../dialect.ts'
|
|
9
|
+
import { Locks } from '../locks.ts'
|
|
10
|
+
import { type ContentKind, deleteNodeRows } from './documents.ts'
|
|
11
|
+
import { WorkspaceFileRepository } from './workspace-files.ts'
|
|
12
|
+
|
|
13
|
+
export interface Workspace {
|
|
14
|
+
id: number
|
|
15
|
+
key: string
|
|
16
|
+
name: string
|
|
17
|
+
ownerKey: string
|
|
18
|
+
memberKeys: string[]
|
|
19
|
+
/** A person's own, which `workspaces.required` opens for them (story 380). */
|
|
20
|
+
personal: boolean
|
|
21
|
+
/** Open, or merged into Shared (story 380), when it takes nothing more. */
|
|
22
|
+
state: 'open' | 'merged'
|
|
23
|
+
mergedAt: Date | null
|
|
24
|
+
mergedBy: string | null
|
|
25
|
+
createdAt: Date
|
|
26
|
+
updatedAt: Date
|
|
27
|
+
/** Changes from another site it holds (story 383), which its merge applies. */
|
|
28
|
+
stagedChanges: Array<{ key: string; name: string | null }> | null
|
|
29
|
+
/** Staged to be looked at and applied: nobody edits it. A refused apply's is open to edit. */
|
|
30
|
+
stagedReadOnly: boolean
|
|
31
|
+
/** Who can see it (story 384): its members, or everyone who may edit, reading only. */
|
|
32
|
+
visibility: 'members' | 'editors'
|
|
33
|
+
/** Whether someone opening an item it changed is told so, and by whose workspace. */
|
|
34
|
+
tellOthers: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A move, reorder or delete waiting for the merge, with where its page stood (story 380). */
|
|
38
|
+
export interface WorkspacePending {
|
|
39
|
+
/** Which kind it is, once the merge has looked (story 390). */
|
|
40
|
+
objectKind?: ContentKind
|
|
41
|
+
/** Null for a page Shared has since deleted. */
|
|
42
|
+
key: string | null
|
|
43
|
+
nodeId: number
|
|
44
|
+
name: string
|
|
45
|
+
kind: 'move' | 'sort' | 'trash' | 'delete' | 'orphaned' | 'gone'
|
|
46
|
+
targetParentId: number | null
|
|
47
|
+
sortOrder: number | null
|
|
48
|
+
baseParentId: number | null
|
|
49
|
+
baseSortOrder: number | null
|
|
50
|
+
baseEventId: number | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What a workspace holds that Shared does not, page by page. */
|
|
54
|
+
export interface WorkspaceChange {
|
|
55
|
+
key: string
|
|
56
|
+
name: string
|
|
57
|
+
/** `changed` and `made` are its versions; the rest wait for the merge. */
|
|
58
|
+
kind: 'changed' | 'made' | 'move' | 'sort' | 'trash' | 'delete' | 'orphaned' | 'gone'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class WorkspaceRepository {
|
|
62
|
+
#db: Db
|
|
63
|
+
|
|
64
|
+
constructor(db: Db) {
|
|
65
|
+
this.#db = db
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async open(input: {
|
|
69
|
+
name: string
|
|
70
|
+
ownerKey: string
|
|
71
|
+
personal?: boolean
|
|
72
|
+
visibility?: 'members' | 'editors'
|
|
73
|
+
tellOthers?: boolean
|
|
74
|
+
}): Promise<Workspace> {
|
|
75
|
+
const key = normaliseUuid(crypto.randomUUID())
|
|
76
|
+
const now = DbDate.toDb(new Date())
|
|
77
|
+
await this.#db.q
|
|
78
|
+
.insertInto('workspace')
|
|
79
|
+
.values({
|
|
80
|
+
key,
|
|
81
|
+
name: input.name,
|
|
82
|
+
owner_key: normaliseUuid(input.ownerKey),
|
|
83
|
+
personal: this.#db.dialect.boolValue(input.personal === true) as boolean,
|
|
84
|
+
visibility: input.visibility ?? 'members',
|
|
85
|
+
tell_others: this.#db.dialect.boolValue(input.tellOthers !== false) as boolean,
|
|
86
|
+
created_at: now,
|
|
87
|
+
updated_at: now,
|
|
88
|
+
})
|
|
89
|
+
.execute()
|
|
90
|
+
return (await this.byKey(key)) as Workspace
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A person's own unnamed workspace, while it is open (story 380). */
|
|
94
|
+
async ownOf(userKey: string): Promise<Workspace | undefined> {
|
|
95
|
+
const rows = await this.#db.q
|
|
96
|
+
.selectFrom('workspace')
|
|
97
|
+
.selectAll()
|
|
98
|
+
.where('owner_key', '=', normaliseUuid(userKey))
|
|
99
|
+
.where('personal', '=', this.#db.dialect.boolValue(true) as boolean)
|
|
100
|
+
.where('state', '=', 'open')
|
|
101
|
+
.execute()
|
|
102
|
+
return (await this.#withMembers(rows))[0]
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async byKey(key: string): Promise<Workspace | undefined> {
|
|
106
|
+
const rows = await this.#db.q
|
|
107
|
+
.selectFrom('workspace')
|
|
108
|
+
.selectAll()
|
|
109
|
+
.where('key', '=', normaliseUuid(key))
|
|
110
|
+
.execute()
|
|
111
|
+
return (await this.#withMembers(rows))[0]
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Those a person owns or is a member of, newest first. */
|
|
115
|
+
async forUser(userKey: string): Promise<Workspace[]> {
|
|
116
|
+
const user = normaliseUuid(userKey)
|
|
117
|
+
const rows = await this.#db.q
|
|
118
|
+
.selectFrom('workspace')
|
|
119
|
+
.selectAll()
|
|
120
|
+
.where((eb) =>
|
|
121
|
+
eb.or([
|
|
122
|
+
eb('owner_key', '=', user),
|
|
123
|
+
eb(
|
|
124
|
+
'id',
|
|
125
|
+
'in',
|
|
126
|
+
eb.selectFrom('workspace_member').select('workspace_id').where('user_key', '=', user),
|
|
127
|
+
),
|
|
128
|
+
]),
|
|
129
|
+
)
|
|
130
|
+
.orderBy('id', 'desc')
|
|
131
|
+
.execute()
|
|
132
|
+
return this.#withMembers(rows)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Every workspace, for an administrator clearing ones left behind. */
|
|
136
|
+
async all(): Promise<Workspace[]> {
|
|
137
|
+
const rows = await this.#db.q
|
|
138
|
+
.selectFrom('workspace')
|
|
139
|
+
.selectAll()
|
|
140
|
+
.orderBy('id', 'desc')
|
|
141
|
+
.execute()
|
|
142
|
+
return this.#withMembers(rows)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async rename(id: number, name: string): Promise<void> {
|
|
146
|
+
await this.#db.q
|
|
147
|
+
.updateTable('workspace')
|
|
148
|
+
.set({ name, updated_at: DbDate.toDb(new Date()) })
|
|
149
|
+
.where('id', '=', id)
|
|
150
|
+
.execute()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Adds a member; false when they already are one. */
|
|
154
|
+
async addMember(id: number, userKey: string): Promise<boolean> {
|
|
155
|
+
const user = normaliseUuid(userKey)
|
|
156
|
+
const existing = await this.#db.q
|
|
157
|
+
.selectFrom('workspace_member')
|
|
158
|
+
.select('user_key')
|
|
159
|
+
.where('workspace_id', '=', id)
|
|
160
|
+
.where('user_key', '=', user)
|
|
161
|
+
.execute()
|
|
162
|
+
if (existing.length > 0) return false
|
|
163
|
+
await this.#db.q
|
|
164
|
+
.insertInto('workspace_member')
|
|
165
|
+
.values({ workspace_id: id, user_key: user, added_at: DbDate.toDb(new Date()) })
|
|
166
|
+
.execute()
|
|
167
|
+
return true
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Removes a member; false when they were not one. */
|
|
171
|
+
async removeMember(id: number, userKey: string): Promise<boolean> {
|
|
172
|
+
const user = normaliseUuid(userKey)
|
|
173
|
+
const existing = await this.#db.q
|
|
174
|
+
.selectFrom('workspace_member')
|
|
175
|
+
.select('user_key')
|
|
176
|
+
.where('workspace_id', '=', id)
|
|
177
|
+
.where('user_key', '=', user)
|
|
178
|
+
.execute()
|
|
179
|
+
if (existing.length === 0) return false
|
|
180
|
+
await this.#db.q
|
|
181
|
+
.deleteFrom('workspace_member')
|
|
182
|
+
.where('workspace_id', '=', id)
|
|
183
|
+
.where('user_key', '=', user)
|
|
184
|
+
.execute()
|
|
185
|
+
return true
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The pages it changed or made, and what waits for the merge, by page. */
|
|
189
|
+
async changes(id: number): Promise<WorkspaceChange[]> {
|
|
190
|
+
const touched = await this.#db.q
|
|
191
|
+
.selectFrom('workspace_document as wd')
|
|
192
|
+
.innerJoin('node as n', 'n.id', 'wd.node_id')
|
|
193
|
+
.select(['n.unique_id', 'n.text', 'wd.created', 'wd.node_id'])
|
|
194
|
+
.where('wd.workspace_id', '=', id)
|
|
195
|
+
.orderBy('wd.node_id')
|
|
196
|
+
.execute()
|
|
197
|
+
const heads = new Map<number, string>()
|
|
198
|
+
for (const row of await this.#db.q
|
|
199
|
+
.selectFrom('content_version')
|
|
200
|
+
.select(['node_id', 'text', 'id'])
|
|
201
|
+
.where('workspace_id', '=', id)
|
|
202
|
+
.orderBy('id')
|
|
203
|
+
.execute())
|
|
204
|
+
heads.set(Number(row.node_id), String(row.text ?? ''))
|
|
205
|
+
const pending = await this.#db.q
|
|
206
|
+
.selectFrom('workspace_pending as p')
|
|
207
|
+
.leftJoin('node as n', 'n.id', 'p.node_id')
|
|
208
|
+
.select(['n.unique_id', 'n.text', 'p.kind', 'p.name', 'p.node_id'])
|
|
209
|
+
.where('p.workspace_id', '=', id)
|
|
210
|
+
.orderBy('p.id')
|
|
211
|
+
.execute()
|
|
212
|
+
return [
|
|
213
|
+
...touched.map((row) => ({
|
|
214
|
+
key: normaliseUuid(String(row.unique_id)),
|
|
215
|
+
name: heads.get(Number(row.node_id)) ?? String(row.text ?? ''),
|
|
216
|
+
kind: (Number(row.created) ? 'made' : 'changed') as WorkspaceChange['kind'],
|
|
217
|
+
})),
|
|
218
|
+
...pending.map((row) => ({
|
|
219
|
+
// A page Shared deleted since is named by what it was called.
|
|
220
|
+
key: row.unique_id ? normaliseUuid(String(row.unique_id)) : `gone:${row.node_id}`,
|
|
221
|
+
name: String(row.text ?? row.name ?? ''),
|
|
222
|
+
kind: String(row.kind) as WorkspaceChange['kind'],
|
|
223
|
+
})),
|
|
224
|
+
]
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Removes the workspace and everything carrying it; Shared is as it was. */
|
|
228
|
+
async discard(id: number): Promise<void> {
|
|
229
|
+
await this.#db.locks.withLock(Locks.ContentTree, () =>
|
|
230
|
+
this.#db.transaction(async (tx) => {
|
|
231
|
+
const versions = () =>
|
|
232
|
+
tx.q.selectFrom('content_version').select('id').where('workspace_id', '=', id)
|
|
233
|
+
const made = (
|
|
234
|
+
await tx.q.selectFrom('node').select('id').where('workspace_id', '=', id).execute()
|
|
235
|
+
).map((row) => Number(row.id))
|
|
236
|
+
await tx.q.deleteFrom('property_value').where('workspace_id', '=', id).execute()
|
|
237
|
+
await tx.q
|
|
238
|
+
.deleteFrom('content_version_culture_variation')
|
|
239
|
+
.where('version_id', 'in', versions())
|
|
240
|
+
.execute()
|
|
241
|
+
await tx.q.deleteFrom('document_version').where('id', 'in', versions()).execute()
|
|
242
|
+
await tx.q.deleteFrom('content_version').where('workspace_id', '=', id).execute()
|
|
243
|
+
await tx.q.deleteFrom('content_schedule').where('workspace_id', '=', id).execute()
|
|
244
|
+
for (const table of [
|
|
245
|
+
'workspace_pending',
|
|
246
|
+
'workspace_document_culture',
|
|
247
|
+
'workspace_document',
|
|
248
|
+
'workspace_member',
|
|
249
|
+
'workspace_link',
|
|
250
|
+
] as const)
|
|
251
|
+
await tx.q.deleteFrom(table).where('workspace_id', '=', id).execute()
|
|
252
|
+
if (made.length > 0) await deleteNodeRows(tx, { ids: made })
|
|
253
|
+
await dropWorkspaceSchema(tx, id)
|
|
254
|
+
await tx.q.deleteFrom('workspace').where('id', '=', id).execute()
|
|
255
|
+
}),
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Whether the workspace has rows of its own for its types yet (story 376). */
|
|
260
|
+
/** Its owner's choice of who sees it and whether others are told (story 384). */
|
|
261
|
+
async setPrivacy(
|
|
262
|
+
id: number,
|
|
263
|
+
privacy: { visibility?: 'members' | 'editors'; tellOthers?: boolean },
|
|
264
|
+
): Promise<void> {
|
|
265
|
+
const set: Record<string, unknown> = {}
|
|
266
|
+
if (privacy.visibility) set.visibility = privacy.visibility
|
|
267
|
+
if (privacy.tellOthers !== undefined)
|
|
268
|
+
set.tell_others = this.#db.dialect.boolValue(privacy.tellOthers)
|
|
269
|
+
if (Object.keys(set).length === 0) return
|
|
270
|
+
await this.#db.q.updateTable('workspace').set(set).where('id', '=', id).execute()
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Open workspaces, other than `except`'s own, that changed a page and say so (story 384). */
|
|
274
|
+
async touching(nodeId: number, exceptUser: string): Promise<Workspace[]> {
|
|
275
|
+
const documents = await this.#db.q
|
|
276
|
+
.selectFrom('workspace_document')
|
|
277
|
+
.select('workspace_id')
|
|
278
|
+
.where('node_id', '=', nodeId)
|
|
279
|
+
.execute()
|
|
280
|
+
const pending = await this.#db.q
|
|
281
|
+
.selectFrom('workspace_pending')
|
|
282
|
+
.select('workspace_id')
|
|
283
|
+
.where('node_id', '=', nodeId)
|
|
284
|
+
.execute()
|
|
285
|
+
const ids = [...new Set([...documents, ...pending].map((r) => Number(r.workspace_id)))]
|
|
286
|
+
if (ids.length === 0) return []
|
|
287
|
+
const rows = await this.#db.q
|
|
288
|
+
.selectFrom('workspace')
|
|
289
|
+
.selectAll()
|
|
290
|
+
.where('id', 'in', ids)
|
|
291
|
+
.where('state', '=', 'open')
|
|
292
|
+
.execute()
|
|
293
|
+
return (await this.#withMembers(rows)).filter(
|
|
294
|
+
(w) =>
|
|
295
|
+
w.tellOthers &&
|
|
296
|
+
w.ownerKey !== normaliseUuid(exceptUser) &&
|
|
297
|
+
!w.memberKeys.includes(normaliseUuid(exceptUser)),
|
|
298
|
+
)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Marks a workspace as holding changes from another site, read-only or open to edit (story 383). */
|
|
302
|
+
async setStaged(
|
|
303
|
+
id: number,
|
|
304
|
+
changes: ReadonlyArray<{ key: string; name: string | null }> | null,
|
|
305
|
+
readOnly = true,
|
|
306
|
+
): Promise<void> {
|
|
307
|
+
await this.#db.q
|
|
308
|
+
.updateTable('workspace')
|
|
309
|
+
.set({ staged_changes: changes ? JSON.stringify({ readOnly, changes }) : null })
|
|
310
|
+
.where('id', '=', id)
|
|
311
|
+
.execute()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async schemaBuilt(id: number): Promise<boolean> {
|
|
315
|
+
const rows = await this.#db.q
|
|
316
|
+
.selectFrom('workspace')
|
|
317
|
+
.select('schema_built')
|
|
318
|
+
.where('id', '=', id)
|
|
319
|
+
.execute()
|
|
320
|
+
return fromDbBool(rows[0]?.schema_built)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Marks the workspace's types as built, or not, so the next save builds them again. */
|
|
324
|
+
async setSchemaBuilt(id: number, built: boolean): Promise<void> {
|
|
325
|
+
await this.#db.q
|
|
326
|
+
.updateTable('workspace')
|
|
327
|
+
.set({ schema_built: this.#db.dialect.boolValue(built) as boolean })
|
|
328
|
+
.where('id', '=', id)
|
|
329
|
+
.execute()
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** What waits for the merge: each page's latest move, reorder and delete, and each marker. */
|
|
333
|
+
async pending(id: number): Promise<WorkspacePending[]> {
|
|
334
|
+
const rows = await this.#db.q
|
|
335
|
+
.selectFrom('workspace_pending as p')
|
|
336
|
+
.leftJoin('node as n', 'n.id', 'p.node_id')
|
|
337
|
+
.select([
|
|
338
|
+
'n.unique_id',
|
|
339
|
+
'n.text',
|
|
340
|
+
'p.node_id',
|
|
341
|
+
'p.name',
|
|
342
|
+
'p.kind',
|
|
343
|
+
'p.target_parent_id',
|
|
344
|
+
'p.sort_order',
|
|
345
|
+
'p.base_parent_id',
|
|
346
|
+
'p.base_sort_order',
|
|
347
|
+
'p.base_event_id',
|
|
348
|
+
])
|
|
349
|
+
.where('p.workspace_id', '=', id)
|
|
350
|
+
.orderBy('p.id')
|
|
351
|
+
.execute()
|
|
352
|
+
const num = (v: unknown) => (v === null || v === undefined ? null : Number(v))
|
|
353
|
+
return rows.map((row) => ({
|
|
354
|
+
key: row.unique_id ? normaliseUuid(String(row.unique_id)) : null,
|
|
355
|
+
nodeId: Number(row.node_id),
|
|
356
|
+
name: String(row.text ?? row.name ?? ''),
|
|
357
|
+
kind: String(row.kind) as WorkspacePending['kind'],
|
|
358
|
+
targetParentId: num(row.target_parent_id),
|
|
359
|
+
sortOrder: num(row.sort_order),
|
|
360
|
+
baseParentId: num(row.base_parent_id),
|
|
361
|
+
baseSortOrder: num(row.base_sort_order),
|
|
362
|
+
baseEventId: num(row.base_event_id),
|
|
363
|
+
}))
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Drops what waits for the merge for one page, or one marker: the change is not to go. */
|
|
367
|
+
async dropPending(id: number, nodeId: number): Promise<void> {
|
|
368
|
+
await this.#db.q
|
|
369
|
+
.deleteFrom('workspace_pending')
|
|
370
|
+
.where('workspace_id', '=', id)
|
|
371
|
+
.where('node_id', '=', nodeId)
|
|
372
|
+
.execute()
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** A page merged alone from a person's own workspace (story 380): its rows there go. */
|
|
376
|
+
async dropPage(id: number, nodeId: number): Promise<void> {
|
|
377
|
+
const versions = () =>
|
|
378
|
+
this.#db.q
|
|
379
|
+
.selectFrom('content_version')
|
|
380
|
+
.select('id')
|
|
381
|
+
.where('workspace_id', '=', id)
|
|
382
|
+
.where('node_id', '=', nodeId)
|
|
383
|
+
await this.#db.q
|
|
384
|
+
.deleteFrom('property_value')
|
|
385
|
+
.where('workspace_id', '=', id)
|
|
386
|
+
.where('node_id', '=', nodeId)
|
|
387
|
+
.execute()
|
|
388
|
+
await this.#db.q
|
|
389
|
+
.deleteFrom('content_version_culture_variation')
|
|
390
|
+
.where('version_id', 'in', versions())
|
|
391
|
+
.execute()
|
|
392
|
+
await this.#db.q.deleteFrom('document_version').where('id', 'in', versions()).execute()
|
|
393
|
+
await this.#db.q
|
|
394
|
+
.deleteFrom('content_version')
|
|
395
|
+
.where('workspace_id', '=', id)
|
|
396
|
+
.where('node_id', '=', nodeId)
|
|
397
|
+
.execute()
|
|
398
|
+
for (const table of ['workspace_document_culture', 'workspace_document'] as const)
|
|
399
|
+
await this.#db.q
|
|
400
|
+
.deleteFrom(table)
|
|
401
|
+
.where('workspace_id', '=', id)
|
|
402
|
+
.where('node_id', '=', nodeId)
|
|
403
|
+
.execute()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Closes a merged workspace (story 380): every row carrying it goes, as a
|
|
408
|
+
* discard removes them, and the workspace stays, marked merged, by whom and
|
|
409
|
+
* when. Called inside the merge's transaction, holding the tree's lock.
|
|
410
|
+
*/
|
|
411
|
+
async closeMerged(id: number, by: string): Promise<void> {
|
|
412
|
+
const versions = () =>
|
|
413
|
+
this.#db.q.selectFrom('content_version').select('id').where('workspace_id', '=', id)
|
|
414
|
+
await this.#db.q.deleteFrom('property_value').where('workspace_id', '=', id).execute()
|
|
415
|
+
await this.#db.q
|
|
416
|
+
.deleteFrom('content_version_culture_variation')
|
|
417
|
+
.where('version_id', 'in', versions())
|
|
418
|
+
.execute()
|
|
419
|
+
await this.#db.q.deleteFrom('document_version').where('id', 'in', versions()).execute()
|
|
420
|
+
await this.#db.q.deleteFrom('content_version').where('workspace_id', '=', id).execute()
|
|
421
|
+
await this.#db.q.deleteFrom('content_schedule').where('workspace_id', '=', id).execute()
|
|
422
|
+
for (const table of [
|
|
423
|
+
'workspace_pending',
|
|
424
|
+
'workspace_document_culture',
|
|
425
|
+
'workspace_document',
|
|
426
|
+
] as const)
|
|
427
|
+
await this.#db.q.deleteFrom(table).where('workspace_id', '=', id).execute()
|
|
428
|
+
await dropWorkspaceSchema(this.#db, id)
|
|
429
|
+
const now = DbDate.toDb(new Date())
|
|
430
|
+
await this.#db.q
|
|
431
|
+
.updateTable('workspace')
|
|
432
|
+
.set({ state: 'merged', merged_at: now, merged_by: normaliseUuid(by), updated_at: now })
|
|
433
|
+
.where('id', '=', id)
|
|
434
|
+
.execute()
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async #withMembers(
|
|
438
|
+
rows: ReadonlyArray<{
|
|
439
|
+
id: number
|
|
440
|
+
key: string
|
|
441
|
+
name: string
|
|
442
|
+
owner_key: string
|
|
443
|
+
personal?: unknown
|
|
444
|
+
state?: string | null
|
|
445
|
+
merged_at?: unknown
|
|
446
|
+
merged_by?: string | null
|
|
447
|
+
created_at: unknown
|
|
448
|
+
updated_at: unknown
|
|
449
|
+
staged_changes?: string | null
|
|
450
|
+
visibility?: string | null
|
|
451
|
+
tell_others?: unknown
|
|
452
|
+
}>,
|
|
453
|
+
): Promise<Workspace[]> {
|
|
454
|
+
if (rows.length === 0) return []
|
|
455
|
+
const members = await this.#db.q
|
|
456
|
+
.selectFrom('workspace_member')
|
|
457
|
+
.select(['workspace_id', 'user_key'])
|
|
458
|
+
.where(
|
|
459
|
+
'workspace_id',
|
|
460
|
+
'in',
|
|
461
|
+
rows.map((row) => Number(row.id)),
|
|
462
|
+
)
|
|
463
|
+
.orderBy('added_at')
|
|
464
|
+
.execute()
|
|
465
|
+
return rows.map((row) => ({
|
|
466
|
+
id: Number(row.id),
|
|
467
|
+
key: normaliseUuid(String(row.key)),
|
|
468
|
+
name: String(row.name),
|
|
469
|
+
ownerKey: normaliseUuid(String(row.owner_key)),
|
|
470
|
+
memberKeys: members
|
|
471
|
+
.filter((m) => Number(m.workspace_id) === Number(row.id))
|
|
472
|
+
.map((m) => normaliseUuid(String(m.user_key))),
|
|
473
|
+
personal: fromDbBool(row.personal),
|
|
474
|
+
state: row.state === 'merged' ? 'merged' : 'open',
|
|
475
|
+
mergedAt: DbDate.fromDb(row.merged_at) ?? null,
|
|
476
|
+
mergedBy: row.merged_by ? normaliseUuid(String(row.merged_by)) : null,
|
|
477
|
+
createdAt: DbDate.fromDb(row.created_at) ?? new Date(0),
|
|
478
|
+
updatedAt: DbDate.fromDb(row.updated_at) ?? new Date(0),
|
|
479
|
+
...stagedOf(row.staged_changes),
|
|
480
|
+
visibility: row.visibility === 'editors' ? 'editors' : 'members',
|
|
481
|
+
tellOthers: row.tell_others === undefined ? true : fromDbBool(row.tell_others),
|
|
482
|
+
}))
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Removes a workspace's own schema (story 376): its copies of types and data types,
|
|
488
|
+
* with their properties, groups and relations, then its file list and every text
|
|
489
|
+
* nothing else keeps. Its pages must be gone, or moved to Shared's types, first.
|
|
490
|
+
*/
|
|
491
|
+
export async function dropWorkspaceSchema(db: Db, id: number): Promise<void> {
|
|
492
|
+
const types = (
|
|
493
|
+
await db.q.selectFrom('content_type').select('node_id').where('workspace_id', '=', id).execute()
|
|
494
|
+
).map((row) => Number(row.node_id))
|
|
495
|
+
const dataTypes = (
|
|
496
|
+
await db.q.selectFrom('data_type').select('node_id').where('workspace_id', '=', id).execute()
|
|
497
|
+
).map((row) => Number(row.node_id))
|
|
498
|
+
if (types.length > 0) {
|
|
499
|
+
const properties = () =>
|
|
500
|
+
db.q.selectFrom('property_type').select('id').where('content_type_id', 'in', types)
|
|
501
|
+
await db.q.deleteFrom('property_value').where('property_type_id', 'in', properties()).execute()
|
|
502
|
+
await db.q
|
|
503
|
+
.deleteFrom('member_property_type')
|
|
504
|
+
.where('property_type_id', 'in', properties())
|
|
505
|
+
.execute()
|
|
506
|
+
await db.q.deleteFrom('property_type').where('content_type_id', 'in', types).execute()
|
|
507
|
+
await db.q
|
|
508
|
+
.deleteFrom('property_type_group')
|
|
509
|
+
.where('content_type_node_id', 'in', types)
|
|
510
|
+
.execute()
|
|
511
|
+
await db.q
|
|
512
|
+
.deleteFrom('content_type_composition')
|
|
513
|
+
.where((eb) =>
|
|
514
|
+
eb.or([
|
|
515
|
+
eb('child_content_type_id', 'in', types),
|
|
516
|
+
eb('parent_content_type_id', 'in', types),
|
|
517
|
+
]),
|
|
518
|
+
)
|
|
519
|
+
.execute()
|
|
520
|
+
await db.q
|
|
521
|
+
.deleteFrom('content_type_allowed_child')
|
|
522
|
+
.where((eb) =>
|
|
523
|
+
eb.or([eb('content_type_id', 'in', types), eb('allowed_content_type_id', 'in', types)]),
|
|
524
|
+
)
|
|
525
|
+
.execute()
|
|
526
|
+
await db.q
|
|
527
|
+
.deleteFrom('content_type_template')
|
|
528
|
+
.where('content_type_node_id', 'in', types)
|
|
529
|
+
.execute()
|
|
530
|
+
await db.q.deleteFrom('content_type').where('workspace_id', '=', id).execute()
|
|
531
|
+
}
|
|
532
|
+
await db.q.deleteFrom('data_type').where('workspace_id', '=', id).execute()
|
|
533
|
+
const nodes = [...types, ...dataTypes]
|
|
534
|
+
if (nodes.length > 0) await db.q.deleteFrom('node').where('id', 'in', nodes).execute()
|
|
535
|
+
await new WorkspaceFileRepository(db).discard(id)
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function stagedOf(
|
|
539
|
+
text: string | null | undefined,
|
|
540
|
+
): Pick<Workspace, 'stagedChanges' | 'stagedReadOnly'> {
|
|
541
|
+
if (!text) return { stagedChanges: null, stagedReadOnly: false }
|
|
542
|
+
const parsed = JSON.parse(String(text)) as {
|
|
543
|
+
readOnly: boolean
|
|
544
|
+
changes: Array<{ key: string; name: string | null }>
|
|
545
|
+
}
|
|
546
|
+
return { stagedChanges: parsed.changes, stagedReadOnly: parsed.readOnly }
|
|
547
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 088 — changes moved between sites (story 383): each change applied
|
|
3
|
+
* here, by the identity it carried, so one that comes back by another route is
|
|
4
|
+
* known; and the changes a staged workspace holds, which nobody edits.
|
|
5
|
+
*/
|
|
6
|
+
import type { Db } from '../database.ts'
|
|
7
|
+
import type { Migration } from '../migrations.ts'
|
|
8
|
+
import { sql } from '../query.ts'
|
|
9
|
+
import { STATE_WORKSPACE_LINKS } from './workspace-links.ts'
|
|
10
|
+
|
|
11
|
+
export const STATE_CHANGE_TRANSFER = '{4c7e1b92-0088-4d3a-9f25-7b1e6a3c0088}'
|
|
12
|
+
|
|
13
|
+
export const changeTransferMigration: Migration = {
|
|
14
|
+
from: STATE_WORKSPACE_LINKS,
|
|
15
|
+
to: STATE_CHANGE_TRANSFER,
|
|
16
|
+
name: 'AddChangeTransfer',
|
|
17
|
+
kind: 'expand',
|
|
18
|
+
release: '0.70.0',
|
|
19
|
+
async up(db: Db) {
|
|
20
|
+
const t = db.dialect.types
|
|
21
|
+
await db.q.schema
|
|
22
|
+
.createTable('applied_change')
|
|
23
|
+
.addColumn('change_key', sql.raw(t.varchar(36)), (c) => c.primaryKey())
|
|
24
|
+
.addColumn('run_id', sql.raw(t.varchar(64)), (c) => c.notNull())
|
|
25
|
+
.addColumn('name', sql.raw(t.varchar(255)))
|
|
26
|
+
.addColumn('applied_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
27
|
+
.execute()
|
|
28
|
+
await db.q.schema.alterTable('workspace').addColumn('staged_changes', sql.raw(t.text)).execute()
|
|
29
|
+
},
|
|
30
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 086 — every change is recorded (story 381). A change is a run, as an
|
|
3
|
+
* applied bundle's is: a merged workspace, a direct edit of the definitions or
|
|
4
|
+
* files, or a revert, with its name, its identity, who made it and where it came
|
|
5
|
+
* from. Each file it touched, a type's file included, is a `change_item` with its
|
|
6
|
+
* text before and after, kept by hash in `file_blob`; its pages are the run's
|
|
7
|
+
* `content_transfer_change` rows, as they are for a bundle.
|
|
8
|
+
*/
|
|
9
|
+
import type { Db } from '../database.ts'
|
|
10
|
+
import type { Migration } from '../migrations.ts'
|
|
11
|
+
import { sql } from '../query.ts'
|
|
12
|
+
import { STATE_WORKSPACE_SCHEMA } from './workspace-schema.ts'
|
|
13
|
+
|
|
14
|
+
export const STATE_CHANGES = '{4c8a2e19-0086-4d7f-9b31-6e0c5a8d0086}'
|
|
15
|
+
|
|
16
|
+
/** The columns the migration adds to `content_transfer_run`, for a rewind to drop. */
|
|
17
|
+
export const CHANGE_RUN_COLUMNS = [
|
|
18
|
+
'change_kind',
|
|
19
|
+
'name',
|
|
20
|
+
'change_key',
|
|
21
|
+
'made_by',
|
|
22
|
+
'workspace_key',
|
|
23
|
+
'closed_at',
|
|
24
|
+
] as const
|
|
25
|
+
|
|
26
|
+
export const changesMigration: Migration = {
|
|
27
|
+
from: STATE_WORKSPACE_SCHEMA,
|
|
28
|
+
to: STATE_CHANGES,
|
|
29
|
+
name: 'AddChanges',
|
|
30
|
+
kind: 'expand',
|
|
31
|
+
release: '0.70.0',
|
|
32
|
+
async up(db: Db) {
|
|
33
|
+
const t = db.dialect.types
|
|
34
|
+
const add = async (column: string, type: string, notNullDefault?: string) => {
|
|
35
|
+
await db.q.schema
|
|
36
|
+
.alterTable('content_transfer_run')
|
|
37
|
+
.addColumn(column, sql.raw(type), (c) =>
|
|
38
|
+
notNullDefault === undefined ? c : c.notNull().defaultTo(sql.raw(notNullDefault)),
|
|
39
|
+
)
|
|
40
|
+
.execute()
|
|
41
|
+
}
|
|
42
|
+
// `bundle` for every run before this; `merge`, `edit` and `revert` from here.
|
|
43
|
+
await add('change_kind', t.varchar(16), "'bundle'")
|
|
44
|
+
await add('name', t.varchar(255))
|
|
45
|
+
await add('change_key', t.varchar(64))
|
|
46
|
+
await add('made_by', t.varchar(255))
|
|
47
|
+
await add('workspace_key', t.varchar(64))
|
|
48
|
+
// A direct edit is open while its author keeps editing; then it closes.
|
|
49
|
+
await add('closed_at', t.timestamp)
|
|
50
|
+
await db.q.schema
|
|
51
|
+
.createTable('change_item')
|
|
52
|
+
.addColumn('id', sql.raw(t.identity))
|
|
53
|
+
.addColumn('run_id', sql.raw(t.varchar(64)), (c) => c.notNull())
|
|
54
|
+
.addColumn('path', sql.raw(t.varchar(512)), (c) => c.notNull())
|
|
55
|
+
.addColumn('kind', sql.raw(t.varchar(32)), (c) => c.notNull())
|
|
56
|
+
.addColumn('class', sql.raw(t.varchar(16)), (c) => c.notNull())
|
|
57
|
+
.addColumn('before_hash', sql.raw(t.varchar(64)))
|
|
58
|
+
.addColumn('after_hash', sql.raw(t.varchar(64)))
|
|
59
|
+
.execute()
|
|
60
|
+
await db.q.schema
|
|
61
|
+
.createIndex('ix_change_item_path')
|
|
62
|
+
.on('change_item')
|
|
63
|
+
.columns(['path', 'run_id'])
|
|
64
|
+
.execute()
|
|
65
|
+
await db.q.schema.createIndex('ix_change_item_run').on('change_item').column('run_id').execute()
|
|
66
|
+
},
|
|
67
|
+
}
|