@crombolo/data 0.69.0 → 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.70.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.
|
|
32
|
+
"@crombolo/core": "0.70.0",
|
|
33
33
|
"kysely": "0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environments a site's values may vary for (story 387): the copies of the site
|
|
3
|
+
* that exist, as `[environments] names` in `schema/schema.toml` lists them. Synced
|
|
4
|
+
* with the schema, so every copy edits the same set. docs/environments.md.
|
|
5
|
+
*/
|
|
6
|
+
import type { Db } from './database.ts'
|
|
7
|
+
import { now } from './query.ts'
|
|
8
|
+
|
|
9
|
+
export const ENVIRONMENTS_KEY = 'crombolo:environments'
|
|
10
|
+
|
|
11
|
+
/** The environments the schema names, in its order; none when it names none. */
|
|
12
|
+
export async function readEnvironments(db: Db): Promise<string[]> {
|
|
13
|
+
const rows = await db.q
|
|
14
|
+
.selectFrom('key_value')
|
|
15
|
+
.select('value')
|
|
16
|
+
.where('key', '=', ENVIRONMENTS_KEY)
|
|
17
|
+
.execute()
|
|
18
|
+
try {
|
|
19
|
+
const parsed = JSON.parse(String(rows[0]?.value ?? '[]')) as unknown
|
|
20
|
+
return Array.isArray(parsed) ? parsed.filter((n): n is string => typeof n === 'string') : []
|
|
21
|
+
} catch {
|
|
22
|
+
return []
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Stores the list; answers what it was before. */
|
|
27
|
+
export async function writeEnvironments(db: Db, names: readonly string[]): Promise<string[]> {
|
|
28
|
+
return db.transaction(async (tx) => {
|
|
29
|
+
const before = await readEnvironments(tx)
|
|
30
|
+
const value = JSON.stringify([...names])
|
|
31
|
+
const updated = await tx.q
|
|
32
|
+
.updateTable('key_value')
|
|
33
|
+
.set({ value, update_date: now(tx) })
|
|
34
|
+
.where('key', '=', ENVIRONMENTS_KEY)
|
|
35
|
+
.returning('key')
|
|
36
|
+
.execute()
|
|
37
|
+
if (updated.length === 0)
|
|
38
|
+
await tx.q
|
|
39
|
+
.insertInto('key_value')
|
|
40
|
+
.values({ key: ENVIRONMENTS_KEY, value, update_date: now(tx) })
|
|
41
|
+
.execute()
|
|
42
|
+
return before
|
|
43
|
+
})
|
|
44
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './content-query.ts'
|
|
|
7
7
|
export * from './custom-tables/index.ts'
|
|
8
8
|
export * from './database.ts'
|
|
9
9
|
export * from './dialect.ts'
|
|
10
|
+
export * from './environments.ts'
|
|
10
11
|
export * from './extension-activity.ts'
|
|
11
12
|
export * from './locks.ts'
|
|
12
13
|
export * from './maintenance.ts'
|
|
@@ -19,6 +20,7 @@ export * from './relations.ts'
|
|
|
19
20
|
export * from './repositories/approvals.ts'
|
|
20
21
|
export * from './repositories/bundle-grants.ts'
|
|
21
22
|
export * from './repositories/bundles.ts'
|
|
23
|
+
export * from './repositories/changes.ts'
|
|
22
24
|
export * from './repositories/content-applied.ts'
|
|
23
25
|
export * from './repositories/content-events.ts'
|
|
24
26
|
export * from './repositories/content-notifications.ts'
|
|
@@ -59,6 +61,9 @@ export * from './repositories/two-factor.ts'
|
|
|
59
61
|
export * from './repositories/user-data.ts'
|
|
60
62
|
export * from './repositories/users.ts'
|
|
61
63
|
export * from './repositories/webhooks.ts'
|
|
64
|
+
export * from './repositories/workspace-files.ts'
|
|
65
|
+
export * from './repositories/workspace-links.ts'
|
|
66
|
+
export * from './repositories/workspaces.ts'
|
|
62
67
|
export * from './schema/approval-schedules.ts'
|
|
63
68
|
export * from './schema/approvals.ts'
|
|
64
69
|
export * from './schema/assistant.ts'
|
|
@@ -76,6 +81,8 @@ export * from './schema/bundles-disabled.ts'
|
|
|
76
81
|
export * from './schema/bundles-in-settings.ts'
|
|
77
82
|
export * from './schema/bundles-rename.ts'
|
|
78
83
|
export * from './schema/change-report.ts'
|
|
84
|
+
export * from './schema/change-transfer.ts'
|
|
85
|
+
export * from './schema/changes.ts'
|
|
79
86
|
export * from './schema/cli-tokens.ts'
|
|
80
87
|
export * from './schema/client-id-prefix.ts'
|
|
81
88
|
export * from './schema/content.ts'
|
|
@@ -83,6 +90,7 @@ export * from './schema/content-applied.ts'
|
|
|
83
90
|
export * from './schema/content-checkpoint.ts'
|
|
84
91
|
export * from './schema/content-events.ts'
|
|
85
92
|
export * from './schema/content-notifications.ts'
|
|
93
|
+
export * from './schema/content-places.ts'
|
|
86
94
|
export * from './schema/content-seed.ts'
|
|
87
95
|
export * from './schema/content-type-seo.ts'
|
|
88
96
|
export * from './schema/created-packages.ts'
|
|
@@ -90,6 +98,7 @@ export * from './schema/delivery-member-tokens.ts'
|
|
|
90
98
|
export * from './schema/diagnostics.ts'
|
|
91
99
|
export * from './schema/drop-assistant.ts'
|
|
92
100
|
export * from './schema/elements.ts'
|
|
101
|
+
export * from './schema/environment-values.ts'
|
|
93
102
|
export * from './schema/exchange-actor.ts'
|
|
94
103
|
export * from './schema/extension-activity.ts'
|
|
95
104
|
export * from './schema/extension-settings.ts'
|
|
@@ -140,6 +149,11 @@ export * from './schema/user-start-nodes-dropped.ts'
|
|
|
140
149
|
export * from './schema/users-localisation.ts'
|
|
141
150
|
export * from './schema/values.ts'
|
|
142
151
|
export * from './schema/webhooks.ts'
|
|
152
|
+
export * from './schema/workspace-links.ts'
|
|
153
|
+
export * from './schema/workspace-merge.ts'
|
|
154
|
+
export * from './schema/workspace-privacy.ts'
|
|
155
|
+
export * from './schema/workspace-schema.ts'
|
|
156
|
+
export * from './schema/workspaces.ts'
|
|
143
157
|
export * from './schema-ops.ts'
|
|
144
158
|
export * from './schema-state.ts'
|
|
145
159
|
export * from './search.ts'
|
|
@@ -152,3 +166,4 @@ export type { CoreTables } from './tables.gen.ts'
|
|
|
152
166
|
export * from './tree-invariants.ts'
|
|
153
167
|
export * from './url-options.ts'
|
|
154
168
|
export * from './workflow-store.ts'
|
|
169
|
+
export * from './workspace-keys.ts'
|
package/src/relations.ts
CHANGED
|
@@ -263,7 +263,8 @@ async function describeNodes(db: Db, ids: readonly number[]): Promise<Map<number
|
|
|
263
263
|
eb
|
|
264
264
|
.selectFrom('content_version as cv')
|
|
265
265
|
.select((sub) => sub.fn.max('cv.version_date').as('latest'))
|
|
266
|
-
.whereRef('cv.node_id', '=', 'n.id')
|
|
266
|
+
.whereRef('cv.node_id', '=', 'n.id')
|
|
267
|
+
.where('cv.workspace_id', 'is', null),
|
|
267
268
|
'n.create_date',
|
|
268
269
|
)
|
|
269
270
|
.as('d'),
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every change to a site, recorded (story 381). A change is a run, as an applied
|
|
3
|
+
* bundle is: a merged workspace, a direct edit of the definitions or files, or a
|
|
4
|
+
* revert. Its files are `change_item` rows, each with its text before and after,
|
|
5
|
+
* kept once by hash; its pages are the run's `content_transfer_change` rows. A
|
|
6
|
+
* person's direct edits gather in one change until ten minutes pass with none;
|
|
7
|
+
* their page edits are counted on it from the pages' own history, not listed.
|
|
8
|
+
*/
|
|
9
|
+
import { normaliseUuid } from '@crombolo/core'
|
|
10
|
+
import type { Db } from '../database.ts'
|
|
11
|
+
import { DbDate } from '../dialect.ts'
|
|
12
|
+
import { currentSchemaState } from '../schema-state.ts'
|
|
13
|
+
import { fileHash } from './workspace-files.ts'
|
|
14
|
+
|
|
15
|
+
/** `replace`: a whole site applied over one that held pages (story 383). */
|
|
16
|
+
export type ChangeSource = 'bundle' | 'replace' | 'merge' | 'edit' | 'revert'
|
|
17
|
+
|
|
18
|
+
/** How long a person's direct edits keep gathering in one change. */
|
|
19
|
+
export const EDIT_WINDOW_MS = 10 * 60 * 1000
|
|
20
|
+
|
|
21
|
+
export interface Change {
|
|
22
|
+
id: string
|
|
23
|
+
kind: ChangeSource
|
|
24
|
+
name: string | null
|
|
25
|
+
/** The identity that travels with it to another site (story 383). */
|
|
26
|
+
key: string
|
|
27
|
+
madeBy: string | null
|
|
28
|
+
appliedBy: string | null
|
|
29
|
+
workspaceKey: string | null
|
|
30
|
+
revertsId: string | null
|
|
31
|
+
status: string
|
|
32
|
+
startedAt: Date
|
|
33
|
+
finishedAt: Date | null
|
|
34
|
+
closedAt: Date | null
|
|
35
|
+
items: number
|
|
36
|
+
pages: number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ChangeItem {
|
|
40
|
+
id: number
|
|
41
|
+
changeId: string
|
|
42
|
+
path: string
|
|
43
|
+
kind: string
|
|
44
|
+
class: string
|
|
45
|
+
before: string | null
|
|
46
|
+
after: string | null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** One item as it is recorded. */
|
|
50
|
+
export interface ChangeItemInput {
|
|
51
|
+
path: string
|
|
52
|
+
kind: string
|
|
53
|
+
class: string
|
|
54
|
+
before: string | null
|
|
55
|
+
after: string | null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class ChangeRepository {
|
|
59
|
+
#db: Db
|
|
60
|
+
|
|
61
|
+
constructor(db: Db) {
|
|
62
|
+
this.#db = db
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async #put(text: string | null): Promise<string | null> {
|
|
66
|
+
if (text === null) return null
|
|
67
|
+
const hash = fileHash(text)
|
|
68
|
+
const found = await this.#db.q
|
|
69
|
+
.selectFrom('file_blob')
|
|
70
|
+
.select('hash')
|
|
71
|
+
.where('hash', '=', hash)
|
|
72
|
+
.execute()
|
|
73
|
+
if (found.length === 0)
|
|
74
|
+
await this.#db.q
|
|
75
|
+
.insertInto('file_blob')
|
|
76
|
+
.values({ hash, text, size: Buffer.byteLength(text) })
|
|
77
|
+
.execute()
|
|
78
|
+
return hash
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Starts a change; its id is its key, which travels with it. */
|
|
82
|
+
async begin(change: {
|
|
83
|
+
kind: Exclude<ChangeSource, 'bundle' | 'replace'>
|
|
84
|
+
name: string | null
|
|
85
|
+
madeBy: string | null
|
|
86
|
+
appliedBy?: string | null
|
|
87
|
+
workspaceKey?: string | null
|
|
88
|
+
revertsId?: string | null
|
|
89
|
+
key?: string
|
|
90
|
+
}): Promise<string> {
|
|
91
|
+
const id = normaliseUuid(change.key ?? crypto.randomUUID())
|
|
92
|
+
const state = await currentSchemaState(this.#db)
|
|
93
|
+
const now = DbDate.toDb(new Date())
|
|
94
|
+
await this.#db.q
|
|
95
|
+
.insertInto('content_transfer_run')
|
|
96
|
+
.values({
|
|
97
|
+
id,
|
|
98
|
+
bundle_id: id,
|
|
99
|
+
bundle_label: change.name,
|
|
100
|
+
direction: change.kind === 'revert' ? 'revert' : 'import',
|
|
101
|
+
reverts_run_id: change.revertsId ?? null,
|
|
102
|
+
schema_state_id: state.id,
|
|
103
|
+
started_at: now,
|
|
104
|
+
finished_at: now,
|
|
105
|
+
applied_by: change.appliedBy ?? change.madeBy,
|
|
106
|
+
node_count: 0,
|
|
107
|
+
status: 'applied',
|
|
108
|
+
change_kind: change.kind,
|
|
109
|
+
name: change.name,
|
|
110
|
+
change_key: id,
|
|
111
|
+
made_by: change.madeBy,
|
|
112
|
+
workspace_key: change.workspaceKey ?? null,
|
|
113
|
+
})
|
|
114
|
+
.execute()
|
|
115
|
+
return id
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Records a change brought from another site as applied here, in run `runId` (story 383). */
|
|
119
|
+
async applied(change: { key: string; name: string | null }, runId: string): Promise<void> {
|
|
120
|
+
await this.#db.q
|
|
121
|
+
.insertInto('applied_change')
|
|
122
|
+
.values({
|
|
123
|
+
change_key: normaliseUuid(change.key),
|
|
124
|
+
run_id: runId,
|
|
125
|
+
name: change.name,
|
|
126
|
+
applied_at: DbDate.toDb(new Date()),
|
|
127
|
+
})
|
|
128
|
+
.execute()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Which of these changes this site holds: applied from elsewhere, or made here. */
|
|
132
|
+
async held(keys: readonly string[]): Promise<Set<string>> {
|
|
133
|
+
const wanted = keys.map(normaliseUuid)
|
|
134
|
+
if (wanted.length === 0) return new Set()
|
|
135
|
+
const applied = await this.#db.q
|
|
136
|
+
.selectFrom('applied_change')
|
|
137
|
+
.select('change_key')
|
|
138
|
+
.where('change_key', 'in', wanted)
|
|
139
|
+
.execute()
|
|
140
|
+
// Made here, even since reverted: a change that was reverted does not come back by another route.
|
|
141
|
+
const made = await this.#db.q
|
|
142
|
+
.selectFrom('content_transfer_run')
|
|
143
|
+
.select('change_key')
|
|
144
|
+
.where('change_key', 'in', wanted)
|
|
145
|
+
.execute()
|
|
146
|
+
return new Set([
|
|
147
|
+
...applied.map((r) => String(r.change_key)),
|
|
148
|
+
...made.map((r) => String(r.change_key)),
|
|
149
|
+
])
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Whether the site holds any page, media item or Library item outside a workspace. */
|
|
153
|
+
async holdsPages(): Promise<boolean> {
|
|
154
|
+
const rows = await this.#db.q
|
|
155
|
+
.selectFrom('content')
|
|
156
|
+
.innerJoin('node', 'node.id', 'content.node_id')
|
|
157
|
+
.select('node.id')
|
|
158
|
+
.where('node.workspace_id', 'is', null)
|
|
159
|
+
.limit(1)
|
|
160
|
+
.execute()
|
|
161
|
+
return rows.length > 0
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Marks an applied whole site as replacing everything the site held (story 383). */
|
|
165
|
+
async markReplacing(runId: string): Promise<void> {
|
|
166
|
+
await this.#db.q
|
|
167
|
+
.updateTable('content_transfer_run')
|
|
168
|
+
.set({ change_kind: 'replace' })
|
|
169
|
+
.where('id', '=', runId)
|
|
170
|
+
.execute()
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Records one item of a change. */
|
|
174
|
+
async item(changeId: string, input: ChangeItemInput): Promise<void> {
|
|
175
|
+
await this.#db.q
|
|
176
|
+
.insertInto('change_item')
|
|
177
|
+
.values({
|
|
178
|
+
run_id: changeId,
|
|
179
|
+
path: input.path,
|
|
180
|
+
kind: input.kind,
|
|
181
|
+
class: input.class,
|
|
182
|
+
before_hash: await this.#put(input.before),
|
|
183
|
+
after_hash: await this.#put(input.after),
|
|
184
|
+
})
|
|
185
|
+
.execute()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* A person's direct edit of a file (story 381): it joins their open edit, unless
|
|
190
|
+
* ten minutes have passed since its last, when that one closes and another
|
|
191
|
+
* opens. An item already there keeps its first "before" and takes the new "after".
|
|
192
|
+
*/
|
|
193
|
+
async recordEdit(madeBy: string, input: ChangeItemInput, now = new Date()): Promise<string> {
|
|
194
|
+
const id = await this.#openEdit(madeBy, now)
|
|
195
|
+
const found = await this.#db.q
|
|
196
|
+
.selectFrom('change_item')
|
|
197
|
+
.select('id')
|
|
198
|
+
.where('run_id', '=', id)
|
|
199
|
+
.where('path', '=', input.path)
|
|
200
|
+
.execute()
|
|
201
|
+
if (found[0])
|
|
202
|
+
await this.#db.q
|
|
203
|
+
.updateTable('change_item')
|
|
204
|
+
.set({ after_hash: await this.#put(input.after), kind: input.kind, class: input.class })
|
|
205
|
+
.where('id', '=', Number(found[0].id))
|
|
206
|
+
.execute()
|
|
207
|
+
else await this.item(id, input)
|
|
208
|
+
await this.#touch(id, now)
|
|
209
|
+
return id
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A page a person saved straight in Shared (story 388): in their open change, as it
|
|
214
|
+
* stood before their first save there and as it is now.
|
|
215
|
+
*/
|
|
216
|
+
async pageSavedBy(madeBy: string, page: { key: string; kind: string }, now = new Date()) {
|
|
217
|
+
const id = await this.#openEdit(madeBy, now)
|
|
218
|
+
await this.#recordPage(id, page)
|
|
219
|
+
await this.#touch(id, now)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* A page a person published straight in Shared (story 388): in their open change as
|
|
224
|
+
* it is now, and the change closed, so it is finished and can be passed on. Their
|
|
225
|
+
* next edit starts another.
|
|
226
|
+
*/
|
|
227
|
+
async publishedBy(
|
|
228
|
+
madeBy: string,
|
|
229
|
+
page: { key: string; kind: string },
|
|
230
|
+
now = new Date(),
|
|
231
|
+
): Promise<{ key: string; name: string | null } | undefined> {
|
|
232
|
+
const id = await this.#openEdit(madeBy, now)
|
|
233
|
+
if (!(await this.#recordPage(id, page))) return undefined
|
|
234
|
+
await this.#touch(id, now)
|
|
235
|
+
await this.#db.q
|
|
236
|
+
.updateTable('content_transfer_run')
|
|
237
|
+
.set({ closed_at: DbDate.toDb(now) })
|
|
238
|
+
.where('id', '=', id)
|
|
239
|
+
.execute()
|
|
240
|
+
const run = await this.#db.q
|
|
241
|
+
.selectFrom('content_transfer_run')
|
|
242
|
+
.select(['change_key', 'name'])
|
|
243
|
+
.where('id', '=', id)
|
|
244
|
+
.executeTakeFirstOrThrow()
|
|
245
|
+
return { key: String(run.change_key ?? id), name: (run.name as string | null) ?? null }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* A page in a change: its event now, and, the first time, the event it stood at
|
|
250
|
+
* before: the one before its latest write, or where the last change left it.
|
|
251
|
+
*/
|
|
252
|
+
async #recordPage(id: string, page: { key: string; kind: string }): Promise<boolean> {
|
|
253
|
+
const nodeKey = normaliseUuid(page.key)
|
|
254
|
+
const node = await this.#db.q
|
|
255
|
+
.selectFrom('node')
|
|
256
|
+
.select('id')
|
|
257
|
+
.where('unique_id', '=', nodeKey)
|
|
258
|
+
.where('workspace_id', 'is', null)
|
|
259
|
+
.executeTakeFirst()
|
|
260
|
+
if (!node) return false
|
|
261
|
+
const versions = await this.#db.q
|
|
262
|
+
.selectFrom('content_version')
|
|
263
|
+
.select('id')
|
|
264
|
+
.where('node_id', '=', Number(node.id))
|
|
265
|
+
.where('workspace_id', 'is', null)
|
|
266
|
+
.orderBy('id', 'desc')
|
|
267
|
+
.limit(2)
|
|
268
|
+
.execute()
|
|
269
|
+
const head = versions[0] ? Number(versions[0].id) : null
|
|
270
|
+
const recorded = await this.#db.q
|
|
271
|
+
.selectFrom('content_transfer_change')
|
|
272
|
+
.select('id')
|
|
273
|
+
.where('run_id', '=', id)
|
|
274
|
+
.where('node_key', '=', nodeKey)
|
|
275
|
+
.execute()
|
|
276
|
+
if (recorded[0]) {
|
|
277
|
+
await this.#db.q
|
|
278
|
+
.updateTable('content_transfer_change')
|
|
279
|
+
.set({ event_id: head })
|
|
280
|
+
.where('id', '=', Number(recorded[0].id))
|
|
281
|
+
.execute()
|
|
282
|
+
return true
|
|
283
|
+
}
|
|
284
|
+
const left = await this.#db.q
|
|
285
|
+
.selectFrom('content_transfer_change')
|
|
286
|
+
.select('event_id')
|
|
287
|
+
.where('node_key', '=', nodeKey)
|
|
288
|
+
.where('run_id', '!=', id)
|
|
289
|
+
.where('event_id', 'is not', null)
|
|
290
|
+
.orderBy('event_id', 'desc')
|
|
291
|
+
.limit(1)
|
|
292
|
+
.execute()
|
|
293
|
+
const before =
|
|
294
|
+
left[0]?.event_id !== undefined && left[0]?.event_id !== null
|
|
295
|
+
? Number(left[0].event_id)
|
|
296
|
+
: versions[1]
|
|
297
|
+
? Number(versions[1].id)
|
|
298
|
+
: null
|
|
299
|
+
await this.#db.q
|
|
300
|
+
.insertInto('content_transfer_change')
|
|
301
|
+
.values({
|
|
302
|
+
run_id: id,
|
|
303
|
+
node_key: nodeKey,
|
|
304
|
+
kind: page.kind,
|
|
305
|
+
action: before === null ? 'create' : 'update',
|
|
306
|
+
before_event_id: before,
|
|
307
|
+
before_published_event_id: null,
|
|
308
|
+
event_id: head,
|
|
309
|
+
})
|
|
310
|
+
.execute()
|
|
311
|
+
return true
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async #touch(id: string, now: Date) {
|
|
315
|
+
await this.#db.q
|
|
316
|
+
.updateTable('content_transfer_run')
|
|
317
|
+
.set({ finished_at: DbDate.toDb(now) })
|
|
318
|
+
.where('id', '=', id)
|
|
319
|
+
.execute()
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** A person's open direct-edit change, closing it and opening another past the window. */
|
|
323
|
+
async #openEdit(madeBy: string, now: Date): Promise<string> {
|
|
324
|
+
const open = await this.#db.q
|
|
325
|
+
.selectFrom('content_transfer_run')
|
|
326
|
+
.select(['id', 'finished_at'])
|
|
327
|
+
.where('change_kind', '=', 'edit')
|
|
328
|
+
.where('made_by', '=', madeBy)
|
|
329
|
+
.where('closed_at', 'is', null)
|
|
330
|
+
.orderBy('started_at', 'desc')
|
|
331
|
+
.execute()
|
|
332
|
+
let id = open[0] ? String(open[0].id) : undefined
|
|
333
|
+
const last = open[0] ? DbDate.fromDb(open[0].finished_at) : null
|
|
334
|
+
if (id && last && now.getTime() - last.getTime() > EDIT_WINDOW_MS) {
|
|
335
|
+
await this.#db.q
|
|
336
|
+
.updateTable('content_transfer_run')
|
|
337
|
+
.set({ closed_at: DbDate.toDb(last) })
|
|
338
|
+
.where('id', '=', id)
|
|
339
|
+
.execute()
|
|
340
|
+
id = undefined
|
|
341
|
+
}
|
|
342
|
+
return id ?? (await this.begin({ kind: 'edit', name: null, madeBy }))
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#view(row: Record<string, unknown>, items: number, pages: number): Change {
|
|
346
|
+
return {
|
|
347
|
+
id: String(row.id),
|
|
348
|
+
kind: (row.change_kind as ChangeSource | null) ?? 'bundle',
|
|
349
|
+
name: (row.name as string | null) ?? (row.bundle_label as string | null) ?? null,
|
|
350
|
+
key: String(row.change_key ?? row.bundle_id),
|
|
351
|
+
madeBy: (row.made_by as string | null) ?? null,
|
|
352
|
+
appliedBy: (row.applied_by as string | null) ?? null,
|
|
353
|
+
workspaceKey: (row.workspace_key as string | null) ?? null,
|
|
354
|
+
revertsId: (row.reverts_run_id as string | null) ?? null,
|
|
355
|
+
status: String(row.status),
|
|
356
|
+
startedAt: DbDate.fromDb(row.started_at) ?? new Date(0),
|
|
357
|
+
finishedAt: DbDate.fromDb(row.finished_at) ?? null,
|
|
358
|
+
closedAt: DbDate.fromDb(row.closed_at) ?? null,
|
|
359
|
+
items,
|
|
360
|
+
pages,
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async #counts(ids: readonly string[]) {
|
|
365
|
+
const items = new Map<string, number>()
|
|
366
|
+
const pages = new Map<string, number>()
|
|
367
|
+
if (ids.length === 0) return { items, pages }
|
|
368
|
+
for (const row of await this.#db.q
|
|
369
|
+
.selectFrom('change_item')
|
|
370
|
+
.select(['run_id', (eb) => eb.fn.countAll<number>().as('n')])
|
|
371
|
+
.where('run_id', 'in', ids)
|
|
372
|
+
.groupBy('run_id')
|
|
373
|
+
.execute())
|
|
374
|
+
items.set(String(row.run_id), Number(row.n))
|
|
375
|
+
for (const row of await this.#db.q
|
|
376
|
+
.selectFrom('content_transfer_change')
|
|
377
|
+
.select(['run_id', (eb) => eb.fn.countAll<number>().as('n')])
|
|
378
|
+
.where('run_id', 'in', ids)
|
|
379
|
+
.groupBy('run_id')
|
|
380
|
+
.execute())
|
|
381
|
+
pages.set(String(row.run_id), Number(row.n))
|
|
382
|
+
return { items, pages }
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** The site's changes, newest first. */
|
|
386
|
+
async list(skip = 0, take = 50): Promise<{ total: number; items: Change[] }> {
|
|
387
|
+
const total = Number(
|
|
388
|
+
(
|
|
389
|
+
await this.#db.q
|
|
390
|
+
.selectFrom('content_transfer_run')
|
|
391
|
+
.select((eb) => eb.fn.countAll<number>().as('n'))
|
|
392
|
+
.execute()
|
|
393
|
+
)[0]?.n ?? 0,
|
|
394
|
+
)
|
|
395
|
+
const rows = await this.#db.q
|
|
396
|
+
.selectFrom('content_transfer_run')
|
|
397
|
+
.selectAll()
|
|
398
|
+
.orderBy('started_at', 'desc')
|
|
399
|
+
.orderBy('id')
|
|
400
|
+
.limit(take)
|
|
401
|
+
.offset(skip)
|
|
402
|
+
.execute()
|
|
403
|
+
const counts = await this.#counts(rows.map((r) => String(r.id)))
|
|
404
|
+
return {
|
|
405
|
+
total,
|
|
406
|
+
items: rows.map((r) =>
|
|
407
|
+
this.#view(r, counts.items.get(String(r.id)) ?? 0, counts.pages.get(String(r.id)) ?? 0),
|
|
408
|
+
),
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async byId(id: string): Promise<Change | undefined> {
|
|
413
|
+
const rows = await this.#db.q
|
|
414
|
+
.selectFrom('content_transfer_run')
|
|
415
|
+
.selectAll()
|
|
416
|
+
.where('id', '=', id)
|
|
417
|
+
.execute()
|
|
418
|
+
if (!rows[0]) return undefined
|
|
419
|
+
const counts = await this.#counts([id])
|
|
420
|
+
return this.#view(rows[0], counts.items.get(id) ?? 0, counts.pages.get(id) ?? 0)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async #texts(hashes: readonly (string | null)[]): Promise<Map<string, string>> {
|
|
424
|
+
const wanted = [...new Set(hashes.filter((h): h is string => Boolean(h)))]
|
|
425
|
+
if (wanted.length === 0) return new Map()
|
|
426
|
+
return new Map(
|
|
427
|
+
(
|
|
428
|
+
await this.#db.q
|
|
429
|
+
.selectFrom('file_blob')
|
|
430
|
+
.select(['hash', 'text'])
|
|
431
|
+
.where('hash', 'in', wanted)
|
|
432
|
+
.execute()
|
|
433
|
+
).map((r) => [String(r.hash), String(r.text)]),
|
|
434
|
+
)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async #items(where: { runId?: string; path?: string }): Promise<ChangeItem[]> {
|
|
438
|
+
let query = this.#db.q
|
|
439
|
+
.selectFrom('change_item as i')
|
|
440
|
+
.innerJoin('content_transfer_run as r', 'r.id', 'i.run_id')
|
|
441
|
+
.select(['i.id', 'i.run_id', 'i.path', 'i.kind', 'i.class', 'i.before_hash', 'i.after_hash'])
|
|
442
|
+
.orderBy('r.started_at')
|
|
443
|
+
.orderBy('i.id')
|
|
444
|
+
if (where.runId) query = query.where('i.run_id', '=', where.runId)
|
|
445
|
+
if (where.path) query = query.where('i.path', '=', where.path)
|
|
446
|
+
const rows = await query.execute()
|
|
447
|
+
const texts = await this.#texts(rows.flatMap((r) => [r.before_hash, r.after_hash]))
|
|
448
|
+
return rows.map((r) => ({
|
|
449
|
+
id: Number(r.id),
|
|
450
|
+
changeId: String(r.run_id),
|
|
451
|
+
path: String(r.path),
|
|
452
|
+
kind: String(r.kind),
|
|
453
|
+
class: String(r.class),
|
|
454
|
+
before: r.before_hash ? (texts.get(String(r.before_hash)) ?? null) : null,
|
|
455
|
+
after: r.after_hash ? (texts.get(String(r.after_hash)) ?? null) : null,
|
|
456
|
+
}))
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** The files a change touched, with each text before and after. */
|
|
460
|
+
/**
|
|
461
|
+
* A change's files. A page it deleted is kept among its items as it stood
|
|
462
|
+
* (story 383), for taking the change to another site: only with `pages`.
|
|
463
|
+
*/
|
|
464
|
+
async itemsOf(changeId: string, options: { pages?: boolean } = {}): Promise<ChangeItem[]> {
|
|
465
|
+
const items = await this.#items({ runId: changeId })
|
|
466
|
+
return options.pages ? items : items.filter((i) => i.kind !== 'page')
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** A file's versions: each change that touched it, oldest first. Nobody numbers anything. */
|
|
470
|
+
versions(path: string): Promise<ChangeItem[]> {
|
|
471
|
+
return this.#items({ path })
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* The paths a change touched that a later change, still applied, touched again.
|
|
476
|
+
* A later change since reverted no longer stands in the way, and neither does
|
|
477
|
+
* its revert.
|
|
478
|
+
*/
|
|
479
|
+
async laterTouching(changeId: string): Promise<Array<{ path: string; changeId: string }>> {
|
|
480
|
+
const change = await this.byId(changeId)
|
|
481
|
+
if (!change) return []
|
|
482
|
+
const own = (await this.itemsOf(changeId)).map((i) => i.path)
|
|
483
|
+
if (own.length === 0) return []
|
|
484
|
+
const rows = await this.#db.q
|
|
485
|
+
.selectFrom('change_item as i')
|
|
486
|
+
.innerJoin('content_transfer_run as r', 'r.id', 'i.run_id')
|
|
487
|
+
.leftJoin('content_transfer_run as t', 't.id', 'r.reverts_run_id')
|
|
488
|
+
.select(['i.path', 'i.run_id', 'r.change_kind', 't.started_at as reverted_started_at'])
|
|
489
|
+
.where('i.path', 'in', own)
|
|
490
|
+
.where('r.id', '<>', changeId)
|
|
491
|
+
.where('r.status', '=', 'applied')
|
|
492
|
+
.where('r.started_at', '>', DbDate.toDb(change.startedAt))
|
|
493
|
+
.execute()
|
|
494
|
+
return rows
|
|
495
|
+
.filter((r) => {
|
|
496
|
+
if (r.change_kind !== 'revert') return true
|
|
497
|
+
const reverted = DbDate.fromDb(r.reverted_started_at)
|
|
498
|
+
return !reverted || reverted.getTime() <= change.startedAt.getTime()
|
|
499
|
+
})
|
|
500
|
+
.map((r) => ({ path: String(r.path), changeId: String(r.run_id) }))
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** How many page saves a person made directly between two moments: an edit's pages, counted. */
|
|
504
|
+
async pageEditsBy(userKey: string, from: Date, to: Date): Promise<number> {
|
|
505
|
+
const rows = await this.#db.q
|
|
506
|
+
.selectFrom('content_version as v')
|
|
507
|
+
.innerJoin('user_account as u', 'u.id', 'v.user_id')
|
|
508
|
+
.select((eb) => eb.fn.countAll<number>().as('n'))
|
|
509
|
+
.where('u.key', '=', normaliseUuid(userKey))
|
|
510
|
+
.where('v.workspace_id', 'is', null)
|
|
511
|
+
.where('v.version_date', '>=', DbDate.toDb(from))
|
|
512
|
+
.where('v.version_date', '<=', DbDate.toDb(to))
|
|
513
|
+
.execute()
|
|
514
|
+
return Number(rows[0]?.n ?? 0)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** Marks a change reverted, as a run's revert does. */
|
|
518
|
+
async setStatus(id: string, status: 'applied' | 'reverted'): Promise<void> {
|
|
519
|
+
await this.#db.q
|
|
520
|
+
.updateTable('content_transfer_run')
|
|
521
|
+
.set({ status })
|
|
522
|
+
.where('id', '=', id)
|
|
523
|
+
.execute()
|
|
524
|
+
}
|
|
525
|
+
}
|