@sixb/pg 0.2.2 → 0.2.4
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/auth-storage/auth-storage.d.ts +2 -1
- package/dist/auth-storage/auth-storage.d.ts.map +1 -1
- package/dist/auth-storage/identities.d.ts +4 -0
- package/dist/auth-storage/identities.d.ts.map +1 -1
- package/dist/auth-storage/rows.d.ts +2 -1
- package/dist/auth-storage/rows.d.ts.map +1 -1
- package/dist/auth-storage/users.d.ts.map +1 -1
- package/dist/connector-connection-storage.d.ts.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1807 -279
- package/dist/index.js.map +25 -21
- package/dist/migrations.d.ts.map +1 -1
- package/dist/objects/storage.d.ts +10 -1
- package/dist/objects/storage.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-session.d.ts +5 -0
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts +14 -0
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/dist/pg-artifact-storage.d.ts +14 -2
- package/dist/pg-artifact-storage.d.ts.map +1 -1
- package/dist/pg-dataset-ingest-storage.d.ts +6 -0
- package/dist/pg-dataset-ingest-storage.d.ts.map +1 -0
- package/dist/pg-lake-retention-run-storage.d.ts +11 -0
- package/dist/pg-lake-retention-run-storage.d.ts.map +1 -0
- package/dist/pg-maintenance-heartbeat-storage.d.ts +11 -0
- package/dist/pg-maintenance-heartbeat-storage.d.ts.map +1 -0
- package/dist/pg-pipeline-run-storage.d.ts +5 -2
- package/dist/pg-pipeline-run-storage.d.ts.map +1 -1
- package/dist/pg-projection-run-storage.d.ts +5 -1
- package/dist/pg-projection-run-storage.d.ts.map +1 -1
- package/dist/pg-sync-run-storage.d.ts +6 -2
- package/dist/pg-sync-run-storage.d.ts.map +1 -1
- package/dist/pg-webhook-run-storage.d.ts +2 -1
- package/dist/pg-webhook-run-storage.d.ts.map +1 -1
- package/dist/pg-workflow-run-storage.d.ts +4 -3
- package/dist/pg-workflow-run-storage.d.ts.map +1 -1
- package/dist/run-claim-sql.d.ts +37 -0
- package/dist/run-claim-sql.d.ts.map +1 -0
- package/dist/share-session-storage.d.ts +2 -1
- package/dist/share-session-storage.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/auth-storage/auth-storage.ts +66 -0
- package/src/auth-storage/identities.ts +15 -0
- package/src/auth-storage/rows.ts +3 -0
- package/src/auth-storage/users.ts +3 -0
- package/src/connector-connection-storage.ts +33 -16
- package/src/index.ts +15 -0
- package/src/migrations/089-maintenance-heartbeats.sql +10 -0
- package/src/migrations/090-lake-retention-runs.sql +8 -0
- package/src/migrations/091-dataset-ingest-inbox.sql +38 -0
- package/src/migrations/092-run-recovery.sql +23 -0
- package/src/migrations/093-webhook-and-projection-leases.sql +10 -0
- package/src/migrations/094-artifact-board-previews.sql +14 -0
- package/src/migrations/095-artifact-open-versions.sql +101 -0
- package/src/migrations/096-artifact-documents.sql +11 -0
- package/src/migrations/097-artifact-frame-files.sql +8 -0
- package/src/migrations/098-artifact-comments.sql +36 -0
- package/src/migrations/099-object-artifact-refs.sql +12 -0
- package/src/migrations/100-shared-link-guests.sql +17 -0
- package/src/migrations/101-connector-connection-owners.sql +38 -0
- package/src/migrations/102-user-suspension-source.sql +12 -0
- package/src/migrations.ts +40 -0
- package/src/objects/storage.ts +49 -0
- package/src/ontology-storage/materialization-session.ts +4 -0
- package/src/ontology-storage/materialization-writer.ts +68 -1
- package/src/ontology-storage/materializations.ts +3 -1
- package/src/pg-artifact-storage.ts +798 -137
- package/src/pg-dataset-ingest-storage.ts +305 -0
- package/src/pg-lake-retention-run-storage.ts +40 -0
- package/src/pg-maintenance-heartbeat-storage.ts +53 -0
- package/src/pg-pipeline-run-storage.ts +85 -42
- package/src/pg-projection-run-storage.ts +61 -2
- package/src/pg-sync-run-storage.ts +100 -37
- package/src/pg-webhook-run-storage.ts +37 -11
- package/src/pg-workflow-run-storage.ts +39 -100
- package/src/run-claim-sql.ts +173 -0
- package/src/share-session-storage.ts +20 -0
|
@@ -1,29 +1,49 @@
|
|
|
1
|
+
import type { FileRef } from "@sixb/core"
|
|
2
|
+
import { ARTIFACT_COMMENT_LIMITS } from "@sixb/core/artifacts"
|
|
1
3
|
import {
|
|
4
|
+
ARTIFACT_RESTORE_POINTS,
|
|
2
5
|
artifactStateBytes,
|
|
3
6
|
artifactStorageErrors,
|
|
4
7
|
checkArtifactStateWrite,
|
|
8
|
+
othersCommented,
|
|
9
|
+
parseArtifactBoardPreviewRow,
|
|
10
|
+
parseArtifactCommentRow,
|
|
5
11
|
parseArtifactFrameGrantRow,
|
|
12
|
+
parseArtifactRestorePointRow,
|
|
6
13
|
parseArtifactRow,
|
|
7
14
|
parseArtifactStateRow,
|
|
15
|
+
parseArtifactTurnWriteRow,
|
|
8
16
|
parseArtifactVersionRow,
|
|
17
|
+
turnUndoable,
|
|
9
18
|
} from "@sixb/core/internal/storage"
|
|
10
19
|
import type {
|
|
20
|
+
ArtifactBoardPreviewRecord,
|
|
21
|
+
ArtifactCommentAnchor,
|
|
22
|
+
ArtifactCommentKey,
|
|
23
|
+
ArtifactCommentRecord,
|
|
11
24
|
ArtifactFrameGrantRecord,
|
|
12
25
|
ArtifactRecord,
|
|
26
|
+
ArtifactRestorePointReason,
|
|
27
|
+
ArtifactRestorePointRecord,
|
|
13
28
|
ArtifactStateEntry,
|
|
14
29
|
ArtifactStateKeyInput,
|
|
15
30
|
ArtifactStateScope,
|
|
16
31
|
ArtifactStorage,
|
|
32
|
+
ArtifactTurnInput,
|
|
33
|
+
ArtifactTurnWriteRecord,
|
|
17
34
|
ArtifactVersionInput,
|
|
18
35
|
ArtifactVersionRecord,
|
|
19
36
|
CopyArtifactInput,
|
|
37
|
+
CreateArtifactCommentInput,
|
|
20
38
|
CreateArtifactInput,
|
|
21
39
|
DeleteArtifactInput,
|
|
22
|
-
DeleteArtifactVersionsByOriginInput,
|
|
23
40
|
DeleteExpiredArtifactFrameGrantsInput,
|
|
24
41
|
FindArtifactFrameGrantInput,
|
|
42
|
+
FreezeArtifactVersionInput,
|
|
25
43
|
GetArtifactInput,
|
|
44
|
+
GetArtifactRestorePointInput,
|
|
26
45
|
GetArtifactVersionInput,
|
|
46
|
+
ListArtifactBoardPreviewsInput,
|
|
27
47
|
ListArtifactsInput,
|
|
28
48
|
ListArtifactsResult,
|
|
29
49
|
ListArtifactVersionsInput,
|
|
@@ -31,7 +51,12 @@ import type {
|
|
|
31
51
|
ListReferencingVersionsInput,
|
|
32
52
|
PublishArtifactVersionInput,
|
|
33
53
|
PublishedArtifactVersion,
|
|
54
|
+
PutArtifactBoardPreviewsInput,
|
|
55
|
+
ResolveArtifactCommentInput,
|
|
56
|
+
RestoreArtifactInput,
|
|
34
57
|
SetArtifactStateInput,
|
|
58
|
+
UndoArtifactTurnInput,
|
|
59
|
+
UndoneArtifactTurn,
|
|
35
60
|
} from "@sixb/core/storage"
|
|
36
61
|
import type { SQLClient, SqlParameter } from "./pg-client"
|
|
37
62
|
import { type PgStoreClient, runPgTransaction } from "./transactions"
|
|
@@ -45,89 +70,313 @@ interface PgRow {
|
|
|
45
70
|
const ARTIFACT_COLUMNS = `
|
|
46
71
|
project_id AS "projectId", id, kind, title,
|
|
47
72
|
owner_principal_type AS "ownerPrincipalType", owner_principal_id AS "ownerPrincipalId",
|
|
48
|
-
latest_version AS "latestVersion", created_at AS "createdAt",
|
|
73
|
+
latest_version AS "latestVersion", revision, created_at AS "createdAt",
|
|
74
|
+
updated_at AS "updatedAt", document, document_revision AS "documentRevision"
|
|
75
|
+
`
|
|
76
|
+
|
|
77
|
+
/** What a version and a restore point both hold, and who last wrote it from where. */
|
|
78
|
+
const CONTENT_COLUMNS = `
|
|
79
|
+
title, entry, files, outline, refs, author_principal_type AS "authorPrincipalType",
|
|
80
|
+
author_principal_id AS "authorPrincipalId", origin_thread_id AS "originThreadId",
|
|
81
|
+
origin_run_id AS "originRunId", updated_at AS "updatedAt"
|
|
49
82
|
`
|
|
50
83
|
|
|
51
84
|
const VERSION_COLUMNS = `
|
|
52
|
-
project_id AS "projectId", artifact_id AS "artifactId", version,
|
|
53
|
-
|
|
54
|
-
|
|
85
|
+
project_id AS "projectId", artifact_id AS "artifactId", version, revision, ${CONTENT_COLUMNS},
|
|
86
|
+
created_at AS "createdAt", frozen_at AS "frozenAt",
|
|
87
|
+
frozen_by_principal_type AS "frozenByPrincipalType",
|
|
88
|
+
frozen_by_principal_id AS "frozenByPrincipalId"
|
|
89
|
+
`
|
|
90
|
+
|
|
91
|
+
const RESTORE_POINT_COLUMNS = `
|
|
92
|
+
project_id AS "projectId", artifact_id AS "artifactId", revision, version, frozen,
|
|
93
|
+
${CONTENT_COLUMNS}, reason, turn_run_id AS "turnRunId", created_at AS "createdAt"
|
|
55
94
|
`
|
|
56
95
|
|
|
96
|
+
const TURN_WRITE_COLUMNS = `artifact_id AS "artifactId", created, revision, writes`
|
|
97
|
+
|
|
57
98
|
const FRAME_GRANT_COLUMNS = `
|
|
58
99
|
project_id AS "projectId", token_hash AS "tokenHash", artifact_id AS "artifactId", version,
|
|
59
|
-
targets, created_at AS "createdAt", expires_at AS "expiresAt"
|
|
100
|
+
files_digest AS "filesDigest", targets, created_at AS "createdAt", expires_at AS "expiresAt"
|
|
101
|
+
`
|
|
102
|
+
|
|
103
|
+
const COMMENT_COLUMNS = `
|
|
104
|
+
project_id AS "projectId", artifact_id AS "artifactId", id, parent_id AS "parentId", anchor,
|
|
105
|
+
body, author_principal_type AS "authorPrincipalType", author_principal_id AS "authorPrincipalId",
|
|
106
|
+
author_name AS "authorName", created_at AS "createdAt", resolved_at AS "resolvedAt",
|
|
107
|
+
resolved_by_principal_type AS "resolvedByPrincipalType",
|
|
108
|
+
resolved_by_principal_id AS "resolvedByPrincipalId",
|
|
109
|
+
origin_thread_id AS "originThreadId", origin_run_id AS "originRunId"
|
|
60
110
|
`
|
|
61
111
|
|
|
62
112
|
const STATE_COLUMNS = `key, value, size_bytes AS "sizeBytes", updated_at AS "updatedAt"`
|
|
63
113
|
|
|
114
|
+
const BOARD_PREVIEW_COLUMNS = `
|
|
115
|
+
project_id AS "projectId", render_key AS "renderKey", theme, size, file,
|
|
116
|
+
created_at AS "createdAt"
|
|
117
|
+
`
|
|
118
|
+
|
|
64
119
|
export class PgArtifactStorage implements ArtifactStorage {
|
|
65
120
|
constructor(private readonly sql: PgStoreClient) {}
|
|
66
121
|
|
|
67
122
|
async create(input: CreateArtifactInput): Promise<PublishedArtifactVersion> {
|
|
68
123
|
return runPgTransaction(this.sql, async (tx) => {
|
|
124
|
+
const content = input.version
|
|
69
125
|
const [artifact] = await tx<PgRow[]>`
|
|
70
126
|
INSERT INTO artifacts (
|
|
71
127
|
project_id, id, kind, title, owner_principal_type, owner_principal_id,
|
|
72
|
-
latest_version, next_version, created_at, updated_at
|
|
128
|
+
latest_version, next_version, revision, created_at, updated_at, document,
|
|
129
|
+
document_revision
|
|
73
130
|
) VALUES (
|
|
74
|
-
${input.projectId}, ${input.id}, ${input.kind}, ${
|
|
75
|
-
${input.owner.type}, ${input.owner.id}, 1, 2,
|
|
76
|
-
${
|
|
131
|
+
${input.projectId}, ${input.id}, ${input.kind}, ${content.title},
|
|
132
|
+
${input.owner.type}, ${input.owner.id}, 1, 2, 1,
|
|
133
|
+
${content.updatedAt}, ${content.updatedAt}, ${documentJson(input.document)}::text::jsonb,
|
|
134
|
+
${input.document ? 1 : null}
|
|
77
135
|
)
|
|
78
136
|
ON CONFLICT (project_id, id) DO NOTHING
|
|
79
137
|
RETURNING ${tx.unsafe(ARTIFACT_COLUMNS)}
|
|
80
138
|
`
|
|
81
139
|
if (!artifact) throw artifactStorageErrors.exists(input.id)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
140
|
+
|
|
141
|
+
const version = await insertVersion(tx, {
|
|
142
|
+
projectId: input.projectId,
|
|
143
|
+
artifactId: input.id,
|
|
144
|
+
version: 1,
|
|
145
|
+
revision: 1,
|
|
146
|
+
content,
|
|
147
|
+
})
|
|
148
|
+
await shareIn(tx, input.projectId, input.id, content, input.turnRunId)
|
|
149
|
+
if (input.turnRunId) {
|
|
150
|
+
await recordTurnWrite(tx, {
|
|
151
|
+
projectId: input.projectId,
|
|
152
|
+
artifactId: input.id,
|
|
153
|
+
turnRunId: input.turnRunId,
|
|
154
|
+
created: true,
|
|
155
|
+
revision: 1,
|
|
156
|
+
})
|
|
85
157
|
}
|
|
158
|
+
|
|
159
|
+
return { artifact: parseArtifactRow(artifact), version }
|
|
86
160
|
})
|
|
87
161
|
}
|
|
88
162
|
|
|
89
163
|
async publish(input: PublishArtifactVersionInput): Promise<PublishedArtifactVersion> {
|
|
90
164
|
return runPgTransaction(this.sql, async (tx) => {
|
|
91
|
-
|
|
92
|
-
const [current] = await tx<PgRow[]>`
|
|
93
|
-
SELECT kind, next_version, latest_version FROM artifacts
|
|
94
|
-
WHERE project_id = ${input.projectId} AND id = ${input.artifactId}
|
|
95
|
-
FOR UPDATE
|
|
96
|
-
`
|
|
165
|
+
const current = await lockArtifact(tx, input.projectId, input.artifactId)
|
|
97
166
|
if (!current) throw artifactStorageErrors.notFound(input.artifactId)
|
|
98
|
-
const
|
|
99
|
-
if (kind !== input.kind) {
|
|
100
|
-
throw artifactStorageErrors.kindMismatch(input.artifactId, kind, input.kind)
|
|
167
|
+
const { artifact } = current
|
|
168
|
+
if (artifact.kind !== input.kind) {
|
|
169
|
+
throw artifactStorageErrors.kindMismatch(input.artifactId, artifact.kind, input.kind)
|
|
101
170
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
171
|
+
|
|
172
|
+
// Under the row lock: a write from the same base after this one finds it moved.
|
|
173
|
+
const latest = await latestVersion(tx, artifact)
|
|
174
|
+
assertBase(artifact, latest, input.baseRevision)
|
|
175
|
+
|
|
176
|
+
// A turn's first write keeps what it replaces, so the turn can be undone.
|
|
177
|
+
const { turnRunId } = input
|
|
178
|
+
if (turnRunId && !(await turnWrote(tx, artifact, turnRunId))) {
|
|
179
|
+
await keepRestorePoint(tx, latest, {
|
|
180
|
+
reason: "turn",
|
|
181
|
+
turnRunId,
|
|
182
|
+
createdAt: input.version.updatedAt,
|
|
114
183
|
})
|
|
115
184
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
185
|
+
|
|
186
|
+
const written = await writeLatest(tx, current, latest, input.version, input.document)
|
|
187
|
+
await shareIn(tx, input.projectId, input.artifactId, input.version, turnRunId)
|
|
188
|
+
if (turnRunId) {
|
|
189
|
+
await recordTurnWrite(tx, {
|
|
190
|
+
projectId: input.projectId,
|
|
191
|
+
artifactId: input.artifactId,
|
|
192
|
+
turnRunId,
|
|
193
|
+
created: false,
|
|
194
|
+
revision: written.artifact.revision,
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return written
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async freeze(input: FreezeArtifactVersionInput): Promise<PublishedArtifactVersion> {
|
|
203
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
204
|
+
const current = await lockArtifact(tx, input.projectId, input.artifactId)
|
|
205
|
+
if (!current) throw artifactStorageErrors.notFound(input.artifactId)
|
|
206
|
+
const { artifact } = current
|
|
207
|
+
|
|
208
|
+
const latest = await latestVersion(tx, artifact)
|
|
209
|
+
if (latest.frozenAt) return { artifact, version: latest }
|
|
210
|
+
assertBase(artifact, latest, input.baseRevision)
|
|
211
|
+
|
|
212
|
+
const revision = artifact.revision + 1
|
|
213
|
+
const [version] = await tx<PgRow[]>`
|
|
214
|
+
UPDATE artifact_versions
|
|
215
|
+
SET revision = ${revision}, frozen_at = ${input.frozenAt},
|
|
216
|
+
frozen_by_principal_type = ${input.frozenBy.type},
|
|
217
|
+
frozen_by_principal_id = ${input.frozenBy.id}
|
|
218
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
219
|
+
AND version = ${latest.version}
|
|
220
|
+
RETURNING ${tx.unsafe(VERSION_COLUMNS)}
|
|
221
|
+
`
|
|
222
|
+
const [updated] = await tx<PgRow[]>`
|
|
124
223
|
UPDATE artifacts
|
|
125
|
-
SET
|
|
126
|
-
|
|
224
|
+
SET revision = ${revision}, updated_at = ${input.frozenAt},
|
|
225
|
+
-- A freeze keeps the content, so a document that held it still does.
|
|
226
|
+
document_revision = CASE WHEN document_revision = ${artifact.revision}::int
|
|
227
|
+
THEN ${revision}::int ELSE document_revision END
|
|
127
228
|
WHERE project_id = ${input.projectId} AND id = ${input.artifactId}
|
|
128
229
|
RETURNING ${tx.unsafe(ARTIFACT_COLUMNS)}
|
|
129
230
|
`
|
|
130
|
-
return {
|
|
231
|
+
return {
|
|
232
|
+
artifact: parseArtifactRow(updated as PgRow),
|
|
233
|
+
version: parseArtifactVersionRow(version as PgRow),
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async listRestorePoints(input: GetArtifactInput): Promise<ArtifactRestorePointRecord[]> {
|
|
239
|
+
const rows = await this.sql<PgRow[]>`
|
|
240
|
+
SELECT ${this.sql.unsafe(RESTORE_POINT_COLUMNS)} FROM artifact_restore_points
|
|
241
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.id}
|
|
242
|
+
ORDER BY revision DESC
|
|
243
|
+
`
|
|
244
|
+
return rows.map(parseArtifactRestorePointRow)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async getRestorePoint(
|
|
248
|
+
input: GetArtifactRestorePointInput
|
|
249
|
+
): Promise<ArtifactRestorePointRecord | null> {
|
|
250
|
+
const [row] = await this.sql<PgRow[]>`
|
|
251
|
+
SELECT ${this.sql.unsafe(RESTORE_POINT_COLUMNS)} FROM artifact_restore_points
|
|
252
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
253
|
+
AND revision = ${input.revision}
|
|
254
|
+
`
|
|
255
|
+
return row ? parseArtifactRestorePointRow(row) : null
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async restoreTo(input: RestoreArtifactInput): Promise<PublishedArtifactVersion> {
|
|
259
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
260
|
+
const current = await lockArtifact(tx, input.projectId, input.artifactId)
|
|
261
|
+
if (!current) throw artifactStorageErrors.notFound(input.artifactId)
|
|
262
|
+
const { artifact } = current
|
|
263
|
+
|
|
264
|
+
const [row] = await tx<PgRow[]>`
|
|
265
|
+
SELECT ${tx.unsafe(RESTORE_POINT_COLUMNS)} FROM artifact_restore_points
|
|
266
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
267
|
+
AND revision = ${input.revision}
|
|
268
|
+
`
|
|
269
|
+
if (!row) throw artifactStorageErrors.restorePointNotFound(input.artifactId, input.revision)
|
|
270
|
+
const point = parseArtifactRestorePointRow(row)
|
|
271
|
+
|
|
272
|
+
const latest = await latestVersion(tx, artifact)
|
|
273
|
+
assertBase(artifact, latest, input.baseRevision)
|
|
274
|
+
|
|
275
|
+
// What the restore replaces is kept first, so the restore can be undone.
|
|
276
|
+
await keepRestorePoint(tx, latest, { reason: "restore", createdAt: input.restoredAt })
|
|
277
|
+
return writeLatest(
|
|
278
|
+
tx,
|
|
279
|
+
current,
|
|
280
|
+
latest,
|
|
281
|
+
{
|
|
282
|
+
title: point.title,
|
|
283
|
+
entry: point.entry,
|
|
284
|
+
files: point.files,
|
|
285
|
+
outline: point.outline,
|
|
286
|
+
references: point.references,
|
|
287
|
+
author: input.author,
|
|
288
|
+
updatedAt: input.restoredAt,
|
|
289
|
+
},
|
|
290
|
+
input.document
|
|
291
|
+
)
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async listTurnWrites(input: ArtifactTurnInput): Promise<ArtifactTurnWriteRecord[]> {
|
|
296
|
+
const rows = await this.sql<PgRow[]>`
|
|
297
|
+
SELECT ${this.sql.unsafe(TURN_WRITE_COLUMNS)} FROM artifact_turn_writes
|
|
298
|
+
WHERE project_id = ${input.projectId} AND turn_run_id = ${input.turnRunId}
|
|
299
|
+
ORDER BY artifact_id COLLATE "C"
|
|
300
|
+
`
|
|
301
|
+
return rows.map(parseArtifactTurnWriteRow)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async undoTurnWrites(input: UndoArtifactTurnInput): Promise<UndoneArtifactTurn> {
|
|
305
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
306
|
+
const { projectId, turnRunId } = input
|
|
307
|
+
|
|
308
|
+
// Lock the turn's artifacts first, in id order so two of these never deadlock. A write
|
|
309
|
+
// holds the same lock, and under READ COMMITTED each statement below then reads a snapshot
|
|
310
|
+
// taken after any write that held it committed: a revision read here is the artifact's.
|
|
311
|
+
await tx`
|
|
312
|
+
SELECT id FROM artifacts
|
|
313
|
+
WHERE project_id = ${projectId} AND id IN (
|
|
314
|
+
SELECT artifact_id FROM artifact_turn_writes
|
|
315
|
+
WHERE project_id = ${projectId} AND turn_run_id = ${turnRunId}
|
|
316
|
+
)
|
|
317
|
+
ORDER BY id
|
|
318
|
+
FOR UPDATE
|
|
319
|
+
`
|
|
320
|
+
const rows = await tx<PgRow[]>`
|
|
321
|
+
SELECT w.artifact_id AS "artifactId", w.created, w.revision, w.writes,
|
|
322
|
+
a.revision AS "currentRevision"
|
|
323
|
+
FROM artifact_turn_writes w
|
|
324
|
+
JOIN artifacts a ON a.project_id = w.project_id AND a.id = w.artifact_id
|
|
325
|
+
WHERE w.project_id = ${projectId} AND w.turn_run_id = ${turnRunId}
|
|
326
|
+
ORDER BY w.artifact_id COLLATE "C"
|
|
327
|
+
`
|
|
328
|
+
|
|
329
|
+
const restored: PublishedArtifactVersion[] = []
|
|
330
|
+
const deleted: string[] = []
|
|
331
|
+
for (const row of rows) {
|
|
332
|
+
const write = parseArtifactTurnWriteRow(row)
|
|
333
|
+
const revision = Number(row.currentRevision)
|
|
334
|
+
|
|
335
|
+
if (write.created) {
|
|
336
|
+
if (!turnUndoable({ write, revision })) continue
|
|
337
|
+
|
|
338
|
+
const comments = await tx<PgRow[]>`
|
|
339
|
+
SELECT ${tx.unsafe(COMMENT_COLUMNS)} FROM artifact_comments
|
|
340
|
+
WHERE project_id = ${projectId} AND artifact_id = ${write.artifactId}
|
|
341
|
+
`
|
|
342
|
+
if (othersCommented(comments.map(parseArtifactCommentRow), input)) continue
|
|
343
|
+
|
|
344
|
+
await tx`DELETE FROM artifacts WHERE project_id = ${projectId} AND id = ${write.artifactId}`
|
|
345
|
+
deleted.push(write.artifactId)
|
|
346
|
+
continue
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const undone = await undoTurn(
|
|
350
|
+
tx,
|
|
351
|
+
projectId,
|
|
352
|
+
write,
|
|
353
|
+
turnRunId,
|
|
354
|
+
input.documents?.[write.artifactId]
|
|
355
|
+
)
|
|
356
|
+
if (undone) restored.push(undone)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// An artifact undone holds its restore point again, and leaves the threads only the turn
|
|
360
|
+
// shared it in; one kept stays where it was made. A deleted one took its own with it.
|
|
361
|
+
if (restored.length > 0) {
|
|
362
|
+
const undone = restored.map(({ artifact }) => artifact.id)
|
|
363
|
+
await tx`
|
|
364
|
+
DELETE FROM artifact_restore_points
|
|
365
|
+
WHERE project_id = ${projectId} AND turn_run_id = ${turnRunId}
|
|
366
|
+
AND artifact_id IN ${tx(undone)}
|
|
367
|
+
`
|
|
368
|
+
await tx`
|
|
369
|
+
DELETE FROM artifact_threads
|
|
370
|
+
WHERE project_id = ${projectId} AND linked_by_run_id = ${turnRunId} AND NOT copied
|
|
371
|
+
AND artifact_id IN ${tx(undone)}
|
|
372
|
+
`
|
|
373
|
+
}
|
|
374
|
+
await tx`
|
|
375
|
+
DELETE FROM artifact_turn_writes
|
|
376
|
+
WHERE project_id = ${projectId} AND turn_run_id = ${turnRunId}
|
|
377
|
+
`
|
|
378
|
+
|
|
379
|
+
return { restored, deleted }
|
|
131
380
|
})
|
|
132
381
|
}
|
|
133
382
|
|
|
@@ -198,8 +447,8 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
198
447
|
|
|
199
448
|
async copy(input: CopyArtifactInput): Promise<ArtifactRecord> {
|
|
200
449
|
return runPgTransaction(this.sql, async (tx) => {
|
|
201
|
-
// The share lock keeps the source from being deleted or
|
|
202
|
-
//
|
|
450
|
+
// The share lock keeps the source from being deleted or written while it is copied, so the
|
|
451
|
+
// copy holds every version it has, as it is.
|
|
203
452
|
const [source] = await tx<PgRow[]>`
|
|
204
453
|
SELECT id FROM artifacts WHERE project_id = ${input.projectId} AND id = ${input.sourceId}
|
|
205
454
|
FOR SHARE
|
|
@@ -209,10 +458,12 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
209
458
|
const [artifact] = await tx<PgRow[]>`
|
|
210
459
|
INSERT INTO artifacts (
|
|
211
460
|
project_id, id, kind, title, owner_principal_type, owner_principal_id,
|
|
212
|
-
latest_version, next_version, created_at, updated_at
|
|
461
|
+
latest_version, next_version, revision, created_at, updated_at, document,
|
|
462
|
+
document_revision
|
|
213
463
|
)
|
|
214
464
|
SELECT project_id, ${input.id}, kind, title, ${input.owner.type}, ${input.owner.id},
|
|
215
|
-
latest_version, next_version, created_at, updated_at
|
|
465
|
+
latest_version, next_version, revision, created_at, updated_at, document,
|
|
466
|
+
document_revision
|
|
216
467
|
FROM artifacts WHERE project_id = ${input.projectId} AND id = ${input.sourceId}
|
|
217
468
|
ON CONFLICT (project_id, id) DO NOTHING
|
|
218
469
|
RETURNING ${tx.unsafe(ARTIFACT_COLUMNS)}
|
|
@@ -221,14 +472,33 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
221
472
|
|
|
222
473
|
await tx`
|
|
223
474
|
INSERT INTO artifact_versions (
|
|
224
|
-
project_id, artifact_id, version, title, entry, files, outline, refs,
|
|
225
|
-
author_principal_type, author_principal_id, created_at
|
|
475
|
+
project_id, artifact_id, version, revision, title, entry, files, outline, refs,
|
|
476
|
+
author_principal_type, author_principal_id, created_at, updated_at, frozen_at,
|
|
477
|
+
frozen_by_principal_type, frozen_by_principal_id
|
|
226
478
|
)
|
|
227
|
-
SELECT project_id, ${input.id}, version, title, entry, files, outline, refs,
|
|
228
|
-
author_principal_type, author_principal_id, created_at
|
|
479
|
+
SELECT project_id, ${input.id}, version, revision, title, entry, files, outline, refs,
|
|
480
|
+
author_principal_type, author_principal_id, created_at, updated_at, frozen_at,
|
|
481
|
+
frozen_by_principal_type, frozen_by_principal_id
|
|
229
482
|
FROM artifact_versions
|
|
230
483
|
WHERE project_id = ${input.projectId} AND artifact_id = ${input.sourceId}
|
|
231
484
|
`
|
|
485
|
+
|
|
486
|
+
if (input.outlineOf) {
|
|
487
|
+
const copied = await tx<PgRow[]>`
|
|
488
|
+
SELECT ${tx.unsafe(VERSION_COLUMNS)} FROM artifact_versions
|
|
489
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.id}
|
|
490
|
+
`
|
|
491
|
+
for (const version of copied.map(parseArtifactVersionRow)) {
|
|
492
|
+
const outline = input.outlineOf(version)
|
|
493
|
+
if (!outline) continue
|
|
494
|
+
|
|
495
|
+
await tx`
|
|
496
|
+
UPDATE artifact_versions SET outline = ${JSON.stringify(outline)}::text::jsonb
|
|
497
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.id}
|
|
498
|
+
AND version = ${version.version}
|
|
499
|
+
`
|
|
500
|
+
}
|
|
501
|
+
}
|
|
232
502
|
await tx`
|
|
233
503
|
INSERT INTO artifact_threads (project_id, artifact_id, thread_id, shared_at, copied)
|
|
234
504
|
VALUES (
|
|
@@ -282,10 +552,11 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
282
552
|
async createFrameGrant(record: ArtifactFrameGrantRecord): Promise<void> {
|
|
283
553
|
const rows = await this.sql`
|
|
284
554
|
INSERT INTO artifact_frame_grants
|
|
285
|
-
(project_id, token_hash, artifact_id, version, targets, created_at,
|
|
555
|
+
(project_id, token_hash, artifact_id, version, files_digest, targets, created_at,
|
|
556
|
+
expires_at)
|
|
286
557
|
SELECT ${record.projectId}, ${record.tokenHash}, ${record.artifactId}, ${record.version},
|
|
287
|
-
${JSON.stringify(record.targets)}::text::jsonb,
|
|
288
|
-
${record.expiresAt}
|
|
558
|
+
${record.filesDigest}, ${JSON.stringify(record.targets)}::text::jsonb,
|
|
559
|
+
${record.createdAt}, ${record.expiresAt}
|
|
289
560
|
WHERE EXISTS (
|
|
290
561
|
SELECT 1 FROM artifacts WHERE project_id = ${record.projectId} AND id = ${record.artifactId}
|
|
291
562
|
)
|
|
@@ -406,116 +677,506 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
406
677
|
return rows.length
|
|
407
678
|
}
|
|
408
679
|
|
|
680
|
+
async listBoardPreviews(
|
|
681
|
+
input: ListArtifactBoardPreviewsInput
|
|
682
|
+
): Promise<ArtifactBoardPreviewRecord[]> {
|
|
683
|
+
if (input.renderKeys.length === 0) return []
|
|
684
|
+
|
|
685
|
+
const rows = await this.sql<PgRow[]>`
|
|
686
|
+
SELECT ${this.sql.unsafe(BOARD_PREVIEW_COLUMNS)} FROM artifact_board_previews
|
|
687
|
+
WHERE project_id = ${input.projectId}
|
|
688
|
+
AND render_key = ANY(${this.sql.array([...new Set(input.renderKeys)])}::text[])
|
|
689
|
+
ORDER BY render_key COLLATE "C", theme COLLATE "C", size COLLATE "C"
|
|
690
|
+
`
|
|
691
|
+
return rows.map(parseArtifactBoardPreviewRow)
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
async putBoardPreviews(input: PutArtifactBoardPreviewsInput): Promise<void> {
|
|
695
|
+
if (input.previews.length === 0) return
|
|
696
|
+
|
|
697
|
+
await runPgTransaction(this.sql, async (tx) => {
|
|
698
|
+
for (const preview of input.previews) {
|
|
699
|
+
await tx`
|
|
700
|
+
INSERT INTO artifact_board_previews (
|
|
701
|
+
project_id, render_key, theme, size, file, created_at
|
|
702
|
+
) VALUES (
|
|
703
|
+
${input.projectId}, ${preview.renderKey}, ${preview.theme}, ${preview.size},
|
|
704
|
+
${JSON.stringify(preview.file)}::text::jsonb, ${preview.createdAt}
|
|
705
|
+
)
|
|
706
|
+
ON CONFLICT (project_id, render_key, theme, size) DO NOTHING
|
|
707
|
+
`
|
|
708
|
+
}
|
|
709
|
+
})
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
async createComment(input: CreateArtifactCommentInput): Promise<ArtifactCommentRecord> {
|
|
713
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
714
|
+
// Under the artifact's lock: two comments at once cannot both pass the limit.
|
|
715
|
+
if (!(await lockArtifact(tx, input.projectId, input.artifactId))) {
|
|
716
|
+
throw artifactStorageErrors.notFound(input.artifactId)
|
|
717
|
+
}
|
|
718
|
+
if (input.parentId !== undefined) {
|
|
719
|
+
const [parent] = await tx<PgRow[]>`
|
|
720
|
+
SELECT 1 FROM artifact_comments
|
|
721
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
722
|
+
AND id = ${input.parentId} AND parent_id IS NULL
|
|
723
|
+
`
|
|
724
|
+
if (!parent) throw artifactStorageErrors.commentNotFound(input.artifactId, input.parentId)
|
|
725
|
+
}
|
|
726
|
+
const [counted] = await tx<{ total: number; guests: number }[]>`
|
|
727
|
+
SELECT COUNT(*)::int AS total,
|
|
728
|
+
(COUNT(*) FILTER (WHERE author_principal_type = 'guest'))::int AS guests
|
|
729
|
+
FROM artifact_comments
|
|
730
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
731
|
+
`
|
|
732
|
+
if ((counted?.total ?? 0) >= ARTIFACT_COMMENT_LIMITS.maxComments) {
|
|
733
|
+
throw artifactStorageErrors.commentsFull(input.artifactId)
|
|
734
|
+
}
|
|
735
|
+
if (
|
|
736
|
+
input.author.type === "guest" &&
|
|
737
|
+
(counted?.guests ?? 0) >= ARTIFACT_COMMENT_LIMITS.maxGuestComments
|
|
738
|
+
) {
|
|
739
|
+
throw artifactStorageErrors.guestCommentsFull(input.artifactId)
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
const [row] = await tx<PgRow[]>`
|
|
743
|
+
INSERT INTO artifact_comments (
|
|
744
|
+
project_id, artifact_id, id, parent_id, anchor, body, author_principal_type,
|
|
745
|
+
author_principal_id, author_name, created_at, origin_thread_id, origin_run_id
|
|
746
|
+
) VALUES (
|
|
747
|
+
${input.projectId}, ${input.artifactId}, ${input.id}, ${input.parentId ?? null},
|
|
748
|
+
${JSON.stringify(commentAnchor(input.anchor))}::text::jsonb, ${input.body},
|
|
749
|
+
${input.author.type}, ${input.author.id},
|
|
750
|
+
${input.author.type === "guest" ? input.author.name : null}, ${input.createdAt},
|
|
751
|
+
${input.origin?.threadId ?? null}, ${input.origin?.runId ?? null}
|
|
752
|
+
)
|
|
753
|
+
RETURNING ${tx.unsafe(COMMENT_COLUMNS)}
|
|
754
|
+
`
|
|
755
|
+
return parseArtifactCommentRow(row as PgRow)
|
|
756
|
+
})
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
async getComment(input: ArtifactCommentKey): Promise<ArtifactCommentRecord | null> {
|
|
760
|
+
const [row] = await this.sql<PgRow[]>`
|
|
761
|
+
SELECT ${this.sql.unsafe(COMMENT_COLUMNS)} FROM artifact_comments
|
|
762
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
763
|
+
AND id = ${input.id}
|
|
764
|
+
`
|
|
765
|
+
return row ? parseArtifactCommentRow(row) : null
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
async listComments(input: GetArtifactInput): Promise<ArtifactCommentRecord[]> {
|
|
769
|
+
const rows = await this.sql<PgRow[]>`
|
|
770
|
+
SELECT ${this.sql.unsafe(COMMENT_COLUMNS)} FROM artifact_comments
|
|
771
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.id}
|
|
772
|
+
ORDER BY created_at, seq
|
|
773
|
+
`
|
|
774
|
+
return rows.map(parseArtifactCommentRow)
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
async resolveComment(input: ResolveArtifactCommentInput): Promise<ArtifactCommentRecord | null> {
|
|
778
|
+
const [row] = await this.sql<PgRow[]>`
|
|
779
|
+
UPDATE artifact_comments
|
|
780
|
+
SET resolved_at = ${input.resolved?.at ?? null},
|
|
781
|
+
resolved_by_principal_type = ${input.resolved?.by.type ?? null},
|
|
782
|
+
resolved_by_principal_id = ${input.resolved?.by.id ?? null}
|
|
783
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
784
|
+
AND id = ${input.id}
|
|
785
|
+
RETURNING ${this.sql.unsafe(COMMENT_COLUMNS)}
|
|
786
|
+
`
|
|
787
|
+
return row ? parseArtifactCommentRow(row) : null
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
async deleteComment(input: ArtifactCommentKey): Promise<boolean> {
|
|
791
|
+
// Its replies go with it, through ON DELETE CASCADE.
|
|
792
|
+
const rows = await this.sql`
|
|
793
|
+
DELETE FROM artifact_comments
|
|
794
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
795
|
+
AND id = ${input.id}
|
|
796
|
+
RETURNING id
|
|
797
|
+
`
|
|
798
|
+
return rows.length > 0
|
|
799
|
+
}
|
|
800
|
+
|
|
409
801
|
async delete(input: DeleteArtifactInput): Promise<boolean> {
|
|
410
|
-
// Versions, frame grants, and state go with the
|
|
802
|
+
// Versions, restore points, turn writes, thread links, frame grants, and state go with the
|
|
803
|
+
// artifact through ON DELETE CASCADE.
|
|
411
804
|
const rows = await this.sql`
|
|
412
805
|
DELETE FROM artifacts WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
413
806
|
RETURNING id
|
|
414
807
|
`
|
|
415
808
|
return rows.length > 0
|
|
416
809
|
}
|
|
810
|
+
}
|
|
417
811
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
812
|
+
/** A comment's anchor as its column holds it: only what it names. */
|
|
813
|
+
function commentAnchor(anchor: ArtifactCommentAnchor): ArtifactCommentAnchor {
|
|
814
|
+
return {
|
|
815
|
+
...(anchor.board === undefined ? {} : { board: anchor.board }),
|
|
816
|
+
...(anchor.element === undefined ? {} : { element: anchor.element }),
|
|
817
|
+
...(anchor.label === undefined ? {} : { label: anchor.label }),
|
|
818
|
+
...(anchor.point === undefined ? {} : { point: { x: anchor.point.x, y: anchor.point.y } }),
|
|
819
|
+
revision: anchor.revision,
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/** A Yjs document's reference as its column holds it: null leaves the stored one. */
|
|
824
|
+
function documentJson(document: FileRef | undefined): string | null {
|
|
825
|
+
return document ? JSON.stringify(document) : null
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/** The artifact, locked for the write: writes to one artifact take turns. */
|
|
829
|
+
async function lockArtifact(
|
|
830
|
+
sql: SQLClient,
|
|
831
|
+
projectId: string,
|
|
832
|
+
artifactId: string
|
|
833
|
+
): Promise<{ readonly artifact: ArtifactRecord; readonly nextVersion: number } | null> {
|
|
834
|
+
const [row] = await sql<PgRow[]>`
|
|
835
|
+
SELECT ${sql.unsafe(ARTIFACT_COLUMNS)}, next_version AS "nextVersion" FROM artifacts
|
|
836
|
+
WHERE project_id = ${projectId} AND id = ${artifactId}
|
|
837
|
+
FOR UPDATE
|
|
838
|
+
`
|
|
839
|
+
return row ? { artifact: parseArtifactRow(row), nextVersion: Number(row.nextVersion) } : null
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
async function latestVersion(
|
|
843
|
+
sql: SQLClient,
|
|
844
|
+
artifact: ArtifactRecord
|
|
845
|
+
): Promise<ArtifactVersionRecord> {
|
|
846
|
+
const [row] = await sql<PgRow[]>`
|
|
847
|
+
SELECT ${sql.unsafe(VERSION_COLUMNS)} FROM artifact_versions
|
|
848
|
+
WHERE project_id = ${artifact.projectId} AND artifact_id = ${artifact.id}
|
|
849
|
+
AND version = ${artifact.latestVersion}
|
|
850
|
+
`
|
|
851
|
+
if (!row) throw new Error(`[SixbPg] Artifact '${artifact.id}' has no latest version.`)
|
|
852
|
+
return parseArtifactVersionRow(row)
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/** Refuses a write made from a revision the artifact has moved past. */
|
|
856
|
+
function assertBase(
|
|
857
|
+
artifact: ArtifactRecord,
|
|
858
|
+
latest: ArtifactVersionRecord,
|
|
859
|
+
baseRevision: number | undefined
|
|
860
|
+
): void {
|
|
861
|
+
if (baseRevision === undefined || baseRevision === artifact.revision) return
|
|
862
|
+
|
|
863
|
+
throw artifactStorageErrors.moved(artifact.id, baseRevision, {
|
|
864
|
+
version: latest.version,
|
|
865
|
+
revision: artifact.revision,
|
|
866
|
+
author: latest.author,
|
|
867
|
+
...(latest.frozenBy ? { frozenBy: latest.frozenBy } : {}),
|
|
868
|
+
})
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Writes content to the latest version: in place while it is open, as the next version when it is
|
|
873
|
+
* frozen. The artifact takes the next revision.
|
|
874
|
+
*/
|
|
875
|
+
async function writeLatest(
|
|
876
|
+
sql: SQLClient,
|
|
877
|
+
{ artifact, nextVersion }: { readonly artifact: ArtifactRecord; readonly nextVersion: number },
|
|
878
|
+
latest: ArtifactVersionRecord,
|
|
879
|
+
content: ArtifactVersionInput,
|
|
880
|
+
document: FileRef | undefined
|
|
881
|
+
): Promise<PublishedArtifactVersion> {
|
|
882
|
+
const { projectId, id: artifactId } = artifact
|
|
883
|
+
const revision = artifact.revision + 1
|
|
884
|
+
const state = documentJson(document)
|
|
885
|
+
|
|
886
|
+
if (latest.frozenAt) {
|
|
887
|
+
const version = await insertVersion(sql, {
|
|
888
|
+
projectId,
|
|
889
|
+
artifactId,
|
|
890
|
+
version: nextVersion,
|
|
891
|
+
revision,
|
|
892
|
+
content,
|
|
477
893
|
})
|
|
894
|
+
const [row] = await sql<PgRow[]>`
|
|
895
|
+
UPDATE artifacts
|
|
896
|
+
SET title = ${content.title}, latest_version = ${version.version},
|
|
897
|
+
next_version = ${version.version + 1}, revision = ${revision},
|
|
898
|
+
updated_at = ${content.updatedAt},
|
|
899
|
+
document = COALESCE(${state}::text::jsonb, document),
|
|
900
|
+
document_revision = CASE WHEN ${state}::text IS NULL THEN document_revision
|
|
901
|
+
ELSE ${revision}::int END
|
|
902
|
+
WHERE project_id = ${projectId} AND id = ${artifactId}
|
|
903
|
+
RETURNING ${sql.unsafe(ARTIFACT_COLUMNS)}
|
|
904
|
+
`
|
|
905
|
+
return { artifact: parseArtifactRow(row as PgRow), version }
|
|
478
906
|
}
|
|
907
|
+
|
|
908
|
+
const version = await replaceContent(sql, {
|
|
909
|
+
projectId,
|
|
910
|
+
artifactId,
|
|
911
|
+
version: latest.version,
|
|
912
|
+
revision,
|
|
913
|
+
content,
|
|
914
|
+
})
|
|
915
|
+
const [row] = await sql<PgRow[]>`
|
|
916
|
+
UPDATE artifacts
|
|
917
|
+
SET title = ${content.title}, revision = ${revision}, updated_at = ${content.updatedAt},
|
|
918
|
+
document = COALESCE(${state}::text::jsonb, document),
|
|
919
|
+
document_revision = CASE WHEN ${state}::text IS NULL THEN document_revision
|
|
920
|
+
ELSE ${revision}::int END
|
|
921
|
+
WHERE project_id = ${projectId} AND id = ${artifactId}
|
|
922
|
+
RETURNING ${sql.unsafe(ARTIFACT_COLUMNS)}
|
|
923
|
+
`
|
|
924
|
+
return { artifact: parseArtifactRow(row as PgRow), version }
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/** Replaces an open version's content, at a new revision. */
|
|
928
|
+
async function replaceContent(
|
|
929
|
+
sql: SQLClient,
|
|
930
|
+
input: {
|
|
931
|
+
readonly projectId: string
|
|
932
|
+
readonly artifactId: string
|
|
933
|
+
readonly version: number
|
|
934
|
+
readonly revision: number
|
|
935
|
+
readonly content: ArtifactVersionInput
|
|
936
|
+
}
|
|
937
|
+
): Promise<ArtifactVersionRecord> {
|
|
938
|
+
const { content } = input
|
|
939
|
+
const [row] = await sql<PgRow[]>`
|
|
940
|
+
UPDATE artifact_versions
|
|
941
|
+
SET revision = ${input.revision}, title = ${content.title}, entry = ${content.entry},
|
|
942
|
+
files = ${JSON.stringify(content.files)}::text::jsonb,
|
|
943
|
+
outline = ${JSON.stringify(content.outline)}::text::jsonb,
|
|
944
|
+
refs = ${JSON.stringify(content.references)}::text::jsonb,
|
|
945
|
+
author_principal_type = ${content.author.type},
|
|
946
|
+
author_principal_id = ${content.author.id},
|
|
947
|
+
origin_thread_id = ${content.origin?.threadId ?? null},
|
|
948
|
+
origin_run_id = ${content.origin?.runId ?? null}, updated_at = ${content.updatedAt}
|
|
949
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
950
|
+
AND version = ${input.version}
|
|
951
|
+
RETURNING ${sql.unsafe(VERSION_COLUMNS)}
|
|
952
|
+
`
|
|
953
|
+
return parseArtifactVersionRow(row as PgRow)
|
|
479
954
|
}
|
|
480
955
|
|
|
481
956
|
async function insertVersion(
|
|
482
957
|
sql: SQLClient,
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
958
|
+
input: {
|
|
959
|
+
readonly projectId: string
|
|
960
|
+
readonly artifactId: string
|
|
961
|
+
readonly version: number
|
|
962
|
+
readonly revision: number
|
|
963
|
+
readonly content: ArtifactVersionInput
|
|
964
|
+
}
|
|
487
965
|
): Promise<ArtifactVersionRecord> {
|
|
966
|
+
const { content } = input
|
|
488
967
|
const [row] = await sql<PgRow[]>`
|
|
489
968
|
INSERT INTO artifact_versions (
|
|
490
|
-
project_id, artifact_id, version, title, entry, files, outline, refs,
|
|
491
|
-
author_principal_type, author_principal_id, origin_thread_id, origin_run_id, created_at
|
|
969
|
+
project_id, artifact_id, version, revision, title, entry, files, outline, refs,
|
|
970
|
+
author_principal_type, author_principal_id, origin_thread_id, origin_run_id, created_at,
|
|
971
|
+
updated_at
|
|
492
972
|
) VALUES (
|
|
493
|
-
${projectId}, ${artifactId}, ${
|
|
494
|
-
${
|
|
495
|
-
${JSON.stringify(
|
|
496
|
-
${JSON.stringify(
|
|
497
|
-
${
|
|
498
|
-
${
|
|
973
|
+
${input.projectId}, ${input.artifactId}, ${input.version}, ${input.revision},
|
|
974
|
+
${content.title}, ${content.entry},
|
|
975
|
+
${JSON.stringify(content.files)}::text::jsonb,
|
|
976
|
+
${JSON.stringify(content.outline)}::text::jsonb,
|
|
977
|
+
${JSON.stringify(content.references)}::text::jsonb,
|
|
978
|
+
${content.author.type}, ${content.author.id},
|
|
979
|
+
${content.origin?.threadId ?? null}, ${content.origin?.runId ?? null},
|
|
980
|
+
${content.updatedAt}, ${content.updatedAt}
|
|
499
981
|
)
|
|
500
982
|
RETURNING ${sql.unsafe(VERSION_COLUMNS)}
|
|
501
983
|
`
|
|
502
|
-
|
|
503
|
-
|
|
984
|
+
return parseArtifactVersionRow(row as PgRow)
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* Keeps a restore point of the latest version, under the artifact's lock, and forgets the oldest
|
|
989
|
+
* past what an artifact keeps.
|
|
990
|
+
*/
|
|
991
|
+
async function keepRestorePoint(
|
|
992
|
+
sql: SQLClient,
|
|
993
|
+
latest: ArtifactVersionRecord,
|
|
994
|
+
input: {
|
|
995
|
+
readonly reason: ArtifactRestorePointReason
|
|
996
|
+
readonly turnRunId?: string
|
|
997
|
+
readonly createdAt: Date
|
|
504
998
|
}
|
|
505
|
-
|
|
999
|
+
): Promise<void> {
|
|
1000
|
+
const { projectId, artifactId } = latest
|
|
1001
|
+
await sql`
|
|
1002
|
+
INSERT INTO artifact_restore_points (
|
|
1003
|
+
project_id, artifact_id, revision, version, frozen, title, entry, files, outline, refs,
|
|
1004
|
+
author_principal_type, author_principal_id, origin_thread_id, origin_run_id, updated_at,
|
|
1005
|
+
reason, turn_run_id, created_at
|
|
1006
|
+
)
|
|
1007
|
+
SELECT project_id, artifact_id, revision, version, frozen_at IS NOT NULL, title, entry, files,
|
|
1008
|
+
outline, refs, author_principal_type, author_principal_id, origin_thread_id,
|
|
1009
|
+
origin_run_id, updated_at, ${input.reason}, ${input.turnRunId ?? null},
|
|
1010
|
+
${input.createdAt}
|
|
1011
|
+
FROM artifact_versions
|
|
1012
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId} AND version = ${latest.version}
|
|
1013
|
+
`
|
|
1014
|
+
await sql`
|
|
1015
|
+
DELETE FROM artifact_restore_points
|
|
1016
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId} AND revision NOT IN (
|
|
1017
|
+
SELECT revision FROM artifact_restore_points
|
|
1018
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId}
|
|
1019
|
+
ORDER BY revision DESC LIMIT ${ARTIFACT_RESTORE_POINTS}
|
|
1020
|
+
)
|
|
1021
|
+
`
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/** Whether a turn wrote the artifact already. */
|
|
1025
|
+
async function turnWrote(
|
|
1026
|
+
sql: SQLClient,
|
|
1027
|
+
artifact: ArtifactRecord,
|
|
1028
|
+
turnRunId: string
|
|
1029
|
+
): Promise<boolean> {
|
|
1030
|
+
const rows = await sql`
|
|
1031
|
+
SELECT 1 FROM artifact_turn_writes
|
|
1032
|
+
WHERE project_id = ${artifact.projectId} AND artifact_id = ${artifact.id}
|
|
1033
|
+
AND turn_run_id = ${turnRunId}
|
|
1034
|
+
`
|
|
1035
|
+
return rows.length > 0
|
|
506
1036
|
}
|
|
507
1037
|
|
|
508
|
-
/**
|
|
509
|
-
|
|
1038
|
+
/**
|
|
1039
|
+
* Remembers the revision a turn's write left and counts the write, and forgets the oldest turns
|
|
1040
|
+
* past those kept.
|
|
1041
|
+
*/
|
|
1042
|
+
async function recordTurnWrite(
|
|
1043
|
+
sql: SQLClient,
|
|
1044
|
+
input: {
|
|
1045
|
+
readonly projectId: string
|
|
1046
|
+
readonly artifactId: string
|
|
1047
|
+
readonly turnRunId: string
|
|
1048
|
+
readonly created: boolean
|
|
1049
|
+
readonly revision: number
|
|
1050
|
+
}
|
|
1051
|
+
): Promise<void> {
|
|
1052
|
+
const { projectId, artifactId } = input
|
|
1053
|
+
await sql`
|
|
1054
|
+
INSERT INTO artifact_turn_writes
|
|
1055
|
+
(project_id, artifact_id, turn_run_id, created, revision, writes)
|
|
1056
|
+
VALUES (${projectId}, ${artifactId}, ${input.turnRunId}, ${input.created}, ${input.revision}, 1)
|
|
1057
|
+
ON CONFLICT (project_id, artifact_id, turn_run_id) DO UPDATE SET
|
|
1058
|
+
revision = excluded.revision, writes = artifact_turn_writes.writes + 1
|
|
1059
|
+
`
|
|
1060
|
+
await sql`
|
|
1061
|
+
DELETE FROM artifact_turn_writes
|
|
1062
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId} AND turn_run_id NOT IN (
|
|
1063
|
+
SELECT turn_run_id FROM artifact_turn_writes
|
|
1064
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId}
|
|
1065
|
+
ORDER BY revision DESC LIMIT ${ARTIFACT_RESTORE_POINTS}
|
|
1066
|
+
)
|
|
1067
|
+
`
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* Puts a locked artifact back as the turn's restore point holds it, at a new revision: an open
|
|
1072
|
+
* version takes its content back, and a version the turn opened past a frozen one goes. `null`
|
|
1073
|
+
* when anyone else changed it since the point, as `turnUndoable` says, or its version is gone.
|
|
1074
|
+
*/
|
|
1075
|
+
async function undoTurn(
|
|
1076
|
+
sql: SQLClient,
|
|
1077
|
+
projectId: string,
|
|
1078
|
+
write: ArtifactTurnWriteRecord,
|
|
1079
|
+
turnRunId: string,
|
|
1080
|
+
document: FileRef | undefined
|
|
1081
|
+
): Promise<PublishedArtifactVersion | null> {
|
|
1082
|
+
const { artifactId } = write
|
|
1083
|
+
const [row] = await sql<PgRow[]>`
|
|
1084
|
+
SELECT ${sql.unsafe(RESTORE_POINT_COLUMNS)} FROM artifact_restore_points
|
|
1085
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId}
|
|
1086
|
+
AND turn_run_id = ${turnRunId}
|
|
1087
|
+
`
|
|
1088
|
+
if (!row) return null
|
|
1089
|
+
const point = parseArtifactRestorePointRow(row)
|
|
1090
|
+
|
|
1091
|
+
const [current] = await sql<{ revision: number }[]>`
|
|
1092
|
+
SELECT revision FROM artifacts WHERE project_id = ${projectId} AND id = ${artifactId}
|
|
1093
|
+
`
|
|
1094
|
+
const [target] = await sql<{ frozenAt: Date | null }[]>`
|
|
1095
|
+
SELECT frozen_at AS "frozenAt" FROM artifact_versions
|
|
1096
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId} AND version = ${point.version}
|
|
1097
|
+
`
|
|
1098
|
+
if (!current || !target) return null
|
|
1099
|
+
|
|
1100
|
+
const untouched = turnUndoable({
|
|
1101
|
+
write,
|
|
1102
|
+
revision: Number(current.revision),
|
|
1103
|
+
point,
|
|
1104
|
+
target: { frozen: target.frozenAt !== null },
|
|
1105
|
+
})
|
|
1106
|
+
if (!untouched) return null
|
|
1107
|
+
const revision = Number(current.revision) + 1
|
|
1108
|
+
|
|
1109
|
+
await sql`
|
|
1110
|
+
DELETE FROM artifact_versions
|
|
1111
|
+
WHERE project_id = ${projectId} AND artifact_id = ${artifactId} AND version > ${point.version}
|
|
1112
|
+
`
|
|
1113
|
+
const at = { projectId, artifactId, version: point.version, revision }
|
|
1114
|
+
const undone = point.frozen
|
|
1115
|
+
? await freshRevision(sql, at)
|
|
1116
|
+
: await replaceContent(sql, {
|
|
1117
|
+
...at,
|
|
1118
|
+
content: {
|
|
1119
|
+
title: point.title,
|
|
1120
|
+
entry: point.entry,
|
|
1121
|
+
files: point.files,
|
|
1122
|
+
outline: point.outline,
|
|
1123
|
+
references: point.references,
|
|
1124
|
+
author: point.author,
|
|
1125
|
+
...(point.origin ? { origin: point.origin } : {}),
|
|
1126
|
+
updatedAt: point.updatedAt,
|
|
1127
|
+
},
|
|
1128
|
+
})
|
|
1129
|
+
// As it was before the turn: when its content was written, or when it was frozen.
|
|
1130
|
+
const updatedAt = new Date(Math.max(undone.updatedAt.getTime(), undone.frozenAt?.getTime() ?? 0))
|
|
1131
|
+
|
|
1132
|
+
const [artifact] = await sql<PgRow[]>`
|
|
1133
|
+
UPDATE artifacts
|
|
1134
|
+
SET title = ${undone.title}, latest_version = ${undone.version}, revision = ${revision},
|
|
1135
|
+
updated_at = ${updatedAt},
|
|
1136
|
+
document = COALESCE(${documentJson(document)}::text::jsonb, document),
|
|
1137
|
+
document_revision = CASE WHEN ${documentJson(document)}::text IS NULL THEN document_revision
|
|
1138
|
+
ELSE ${revision}::int END
|
|
1139
|
+
WHERE project_id = ${projectId} AND id = ${artifactId}
|
|
1140
|
+
RETURNING ${sql.unsafe(ARTIFACT_COLUMNS)}
|
|
1141
|
+
`
|
|
1142
|
+
return { artifact: parseArtifactRow(artifact as PgRow), version: undone }
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/** A frozen version at the artifact's new revision: it is the latest again, as it was. */
|
|
1146
|
+
async function freshRevision(
|
|
1147
|
+
sql: SQLClient,
|
|
1148
|
+
input: {
|
|
1149
|
+
readonly projectId: string
|
|
1150
|
+
readonly artifactId: string
|
|
1151
|
+
readonly version: number
|
|
1152
|
+
readonly revision: number
|
|
1153
|
+
}
|
|
1154
|
+
): Promise<ArtifactVersionRecord> {
|
|
1155
|
+
const [row] = await sql<PgRow[]>`
|
|
1156
|
+
UPDATE artifact_versions SET revision = ${input.revision}
|
|
1157
|
+
WHERE project_id = ${input.projectId} AND artifact_id = ${input.artifactId}
|
|
1158
|
+
AND version = ${input.version}
|
|
1159
|
+
RETURNING ${sql.unsafe(VERSION_COLUMNS)}
|
|
1160
|
+
`
|
|
1161
|
+
return parseArtifactVersionRow(row as PgRow)
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/** Shares an artifact in its origin's thread, keeping when, and by which turn, it was first. */
|
|
1165
|
+
async function shareIn(
|
|
510
1166
|
sql: SQLClient,
|
|
511
1167
|
projectId: string,
|
|
512
1168
|
artifactId: string,
|
|
513
|
-
|
|
514
|
-
|
|
1169
|
+
content: ArtifactVersionInput,
|
|
1170
|
+
turnRunId: string | undefined
|
|
515
1171
|
): Promise<void> {
|
|
1172
|
+
if (!content.origin) return
|
|
1173
|
+
|
|
516
1174
|
await sql`
|
|
517
|
-
INSERT INTO artifact_threads (project_id, artifact_id, thread_id, shared_at)
|
|
518
|
-
VALUES (
|
|
1175
|
+
INSERT INTO artifact_threads (project_id, artifact_id, thread_id, shared_at, linked_by_run_id)
|
|
1176
|
+
VALUES (
|
|
1177
|
+
${projectId}, ${artifactId}, ${content.origin.threadId}, ${content.updatedAt},
|
|
1178
|
+
${turnRunId ?? null}
|
|
1179
|
+
)
|
|
519
1180
|
ON CONFLICT (project_id, artifact_id, thread_id) DO NOTHING
|
|
520
1181
|
`
|
|
521
1182
|
}
|