@crombolo/data 0.37.0 → 0.39.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 +3 -2
- package/src/audit.ts +164 -134
- package/src/change-report.ts +66 -43
- package/src/cluster.ts +119 -59
- package/src/content-query.ts +205 -200
- package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
- package/src/custom-tables/chain.ts +150 -0
- package/src/custom-tables/ddl.ts +159 -0
- package/src/custom-tables/handle.ts +141 -0
- package/src/{bundle-tables → custom-tables}/index.ts +3 -2
- package/src/custom-tables/names.ts +94 -0
- package/src/custom-tables/owners.ts +299 -0
- package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
- package/src/custom-tables/values.ts +181 -0
- package/src/database.ts +40 -5
- package/src/dialect.ts +43 -16
- package/src/index.ts +9 -1
- package/src/migrations.ts +130 -88
- package/src/query.ts +158 -0
- package/src/relations.ts +49 -28
- package/src/repositories/approvals.ts +130 -89
- package/src/repositories/bundle-grants.ts +105 -0
- package/src/repositories/bundles.ts +85 -76
- package/src/repositories/content-applied.ts +47 -28
- package/src/repositories/content-events.ts +114 -67
- package/src/repositories/content-notifications.ts +102 -74
- package/src/repositories/content-types.ts +449 -320
- package/src/repositories/data-types.ts +105 -56
- package/src/repositories/delivery-member-tokens.ts +37 -28
- package/src/repositories/dictionary.ts +77 -58
- package/src/repositories/documents.ts +956 -580
- package/src/repositories/domains.ts +28 -16
- package/src/repositories/dynamic-roots.ts +32 -17
- package/src/repositories/external-logins.ts +38 -24
- package/src/repositories/folders.ts +7 -4
- package/src/repositories/form-entries.ts +120 -103
- package/src/repositories/form-workflows.ts +87 -62
- package/src/repositories/jobs.ts +296 -0
- package/src/repositories/languages.ts +51 -38
- package/src/repositories/log-searches.ts +7 -5
- package/src/repositories/media-access.ts +173 -123
- package/src/repositories/media-listing.ts +155 -92
- package/src/repositories/members.ts +189 -162
- package/src/repositories/nodes.ts +227 -181
- package/src/repositories/notifications.ts +22 -17
- package/src/repositories/public-access.ts +77 -61
- package/src/repositories/redirects.ts +113 -70
- package/src/repositories/references.ts +55 -44
- package/src/repositories/remote-jobs.ts +45 -35
- package/src/repositories/tags.ts +11 -10
- package/src/repositories/templates.ts +24 -14
- package/src/repositories/transfer-answers.ts +58 -50
- package/src/repositories/transfer-runs.ts +93 -75
- package/src/repositories/two-factor.ts +74 -55
- package/src/repositories/user-data.ts +54 -36
- package/src/repositories/users.ts +345 -300
- package/src/repositories/webhooks.ts +196 -137
- package/src/schema/bundle-grants.ts +32 -0
- package/src/schema/bundles-disabled.ts +29 -0
- package/src/schema/jobs.ts +56 -0
- package/src/schema/plan.ts +6 -0
- package/src/schema-ops.ts +15 -12
- package/src/schema-state.ts +53 -38
- package/src/search.ts +175 -129
- package/src/setup.ts +17 -13
- package/src/site-search.ts +134 -83
- package/src/table-types.ts +152 -0
- package/src/tables.gen.ts +861 -0
- package/src/tree-invariants.ts +21 -12
- package/src/url-options.ts +17 -13
- package/src/workflow-store.ts +17 -13
- package/src/bundle-tables/capability.ts +0 -377
- package/src/bundle-tables/chain.ts +0 -105
- package/src/bundle-tables/ddl.ts +0 -124
- package/src/bundle-tables/guard.ts +0 -116
- package/src/bundle-tables/names.ts +0 -58
|
@@ -37,9 +37,21 @@ export class WebhookRepository {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
async all(): Promise<WebhookRow[]> {
|
|
40
|
-
const rows = await this.#db.
|
|
41
|
-
'
|
|
42
|
-
|
|
40
|
+
const rows = await this.#db.q
|
|
41
|
+
.selectFrom('webhook')
|
|
42
|
+
.select([
|
|
43
|
+
'key',
|
|
44
|
+
'name',
|
|
45
|
+
'description',
|
|
46
|
+
'url',
|
|
47
|
+
'enabled',
|
|
48
|
+
'events',
|
|
49
|
+
'content_type_keys',
|
|
50
|
+
'headers',
|
|
51
|
+
])
|
|
52
|
+
.orderBy('create_date')
|
|
53
|
+
.orderBy('key')
|
|
54
|
+
.execute()
|
|
43
55
|
return rows.map((row) => ({
|
|
44
56
|
key: String(row.key),
|
|
45
57
|
name: (row.name as string | null) ?? null,
|
|
@@ -58,35 +70,35 @@ export class WebhookRepository {
|
|
|
58
70
|
|
|
59
71
|
async save(webhook: WebhookRow): Promise<void> {
|
|
60
72
|
const now = DbDate.toDb(new Date())
|
|
61
|
-
const values =
|
|
62
|
-
webhook.name,
|
|
63
|
-
webhook.description,
|
|
64
|
-
webhook.url,
|
|
65
|
-
this.#db.dialect.boolValue(webhook.enabled),
|
|
66
|
-
JSON.stringify(webhook.events),
|
|
67
|
-
JSON.stringify(webhook.contentTypeKeys),
|
|
68
|
-
webhook.sealedHeaders,
|
|
69
|
-
now,
|
|
70
|
-
|
|
71
|
-
const updated = await this.#db.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
const values = {
|
|
74
|
+
name: webhook.name,
|
|
75
|
+
description: webhook.description,
|
|
76
|
+
url: webhook.url,
|
|
77
|
+
enabled: this.#db.dialect.boolValue(webhook.enabled),
|
|
78
|
+
events: JSON.stringify(webhook.events),
|
|
79
|
+
content_type_keys: JSON.stringify(webhook.contentTypeKeys),
|
|
80
|
+
headers: webhook.sealedHeaders,
|
|
81
|
+
update_date: now,
|
|
82
|
+
}
|
|
83
|
+
const updated = await this.#db.q
|
|
84
|
+
.updateTable('webhook')
|
|
85
|
+
.set(values)
|
|
86
|
+
.where('key', '=', webhook.key.toLowerCase())
|
|
87
|
+
.returning('key')
|
|
88
|
+
.execute()
|
|
77
89
|
if (updated.length > 0) return
|
|
78
|
-
await this.#db.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
[...values, webhook.key.toLowerCase(), now],
|
|
83
|
-
)
|
|
90
|
+
await this.#db.q
|
|
91
|
+
.insertInto('webhook')
|
|
92
|
+
.values({ ...values, key: webhook.key.toLowerCase(), create_date: now })
|
|
93
|
+
.execute()
|
|
84
94
|
}
|
|
85
95
|
|
|
86
96
|
async remove(key: string): Promise<boolean> {
|
|
87
|
-
const rows = await this.#db.
|
|
88
|
-
|
|
89
|
-
|
|
97
|
+
const rows = await this.#db.q
|
|
98
|
+
.deleteFrom('webhook')
|
|
99
|
+
.where('key', '=', key.toLowerCase())
|
|
100
|
+
.returning('key')
|
|
101
|
+
.execute()
|
|
90
102
|
return rows.length > 0
|
|
91
103
|
}
|
|
92
104
|
}
|
|
@@ -125,8 +137,18 @@ export interface WebhookLogEntry {
|
|
|
125
137
|
date: Date
|
|
126
138
|
}
|
|
127
139
|
|
|
128
|
-
const DELIVERY_COLUMNS =
|
|
129
|
-
'id,
|
|
140
|
+
const DELIVERY_COLUMNS = [
|
|
141
|
+
'id',
|
|
142
|
+
'subscriber',
|
|
143
|
+
'event_alias',
|
|
144
|
+
'payload',
|
|
145
|
+
'state',
|
|
146
|
+
'attempts',
|
|
147
|
+
'last_error',
|
|
148
|
+
'run_after',
|
|
149
|
+
'create_date',
|
|
150
|
+
'event_id',
|
|
151
|
+
] as const
|
|
130
152
|
|
|
131
153
|
const hydrateDelivery = (row: Record<string, unknown>): WebhookDelivery => {
|
|
132
154
|
let payload: unknown = null
|
|
@@ -174,24 +196,24 @@ export class WebhookDeliveryRepository {
|
|
|
174
196
|
const ids: string[] = []
|
|
175
197
|
for (const delivery of deliveries) {
|
|
176
198
|
const id = randomUUID()
|
|
177
|
-
const rows = await this.#db.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
ON CONFLICT DO NOTHING RETURNING id`,
|
|
181
|
-
[
|
|
199
|
+
const rows = await this.#db.q
|
|
200
|
+
.insertInto('webhook_delivery')
|
|
201
|
+
.values({
|
|
182
202
|
id,
|
|
183
|
-
delivery.subscriber,
|
|
184
|
-
delivery.eventAlias,
|
|
185
|
-
JSON.stringify(delivery.payload),
|
|
186
|
-
'pending',
|
|
187
|
-
0,
|
|
188
|
-
null,
|
|
189
|
-
DbDate.toDb(now),
|
|
190
|
-
DbDate.toDb(now),
|
|
191
|
-
delivery.eventId ?? null,
|
|
192
|
-
DbDate.toDb(now),
|
|
193
|
-
|
|
194
|
-
|
|
203
|
+
subscriber: delivery.subscriber,
|
|
204
|
+
event_alias: delivery.eventAlias,
|
|
205
|
+
payload: JSON.stringify(delivery.payload),
|
|
206
|
+
state: 'pending',
|
|
207
|
+
attempts: 0,
|
|
208
|
+
last_error: null,
|
|
209
|
+
run_after: DbDate.toDb(now),
|
|
210
|
+
create_date: DbDate.toDb(now),
|
|
211
|
+
event_id: delivery.eventId ?? null,
|
|
212
|
+
update_date: DbDate.toDb(now),
|
|
213
|
+
})
|
|
214
|
+
.onConflict((oc) => oc.doNothing())
|
|
215
|
+
.returning('id')
|
|
216
|
+
.execute()
|
|
195
217
|
if (rows.length > 0) ids.push(id)
|
|
196
218
|
}
|
|
197
219
|
return ids
|
|
@@ -199,124 +221,145 @@ export class WebhookDeliveryRepository {
|
|
|
199
221
|
|
|
200
222
|
/** Claims older than `olderThan` belong to a node that died: they go back in the queue. */
|
|
201
223
|
async releaseStale(olderThan: Date): Promise<number> {
|
|
202
|
-
const rows = await this.#db.
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
224
|
+
const rows = await this.#db.q
|
|
225
|
+
.updateTable('webhook_delivery')
|
|
226
|
+
.set({ state: 'pending', update_date: DbDate.toDb(new Date()) })
|
|
227
|
+
.where('state', '=', 'running')
|
|
228
|
+
.where('update_date', '<', DbDate.toDb(olderThan))
|
|
229
|
+
.returning('id')
|
|
230
|
+
.execute()
|
|
207
231
|
return rows.length
|
|
208
232
|
}
|
|
209
233
|
|
|
210
234
|
/** Puts failed deliveries back, all or one; answers how many. */
|
|
211
235
|
async retry(id?: string): Promise<number> {
|
|
212
236
|
const now = DbDate.toDb(new Date())
|
|
213
|
-
const rows = await this.#db.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
237
|
+
const rows = await this.#db.q
|
|
238
|
+
.updateTable('webhook_delivery')
|
|
239
|
+
.set({ state: 'pending', attempts: 0, run_after: now, update_date: now })
|
|
240
|
+
.where('state', '=', 'failed')
|
|
241
|
+
.$if(Boolean(id), (q) => q.where('id', '=', id as string))
|
|
242
|
+
.returning('id')
|
|
243
|
+
.execute()
|
|
218
244
|
return rows.length
|
|
219
245
|
}
|
|
220
246
|
|
|
221
247
|
/** Delivered rows and log entries older than `before` go; failed ones stay until retried. */
|
|
222
248
|
async prune(before: Date): Promise<number> {
|
|
223
|
-
const done = await this.#db.
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
249
|
+
const done = await this.#db.q
|
|
250
|
+
.deleteFrom('webhook_delivery')
|
|
251
|
+
.where('state', '=', 'done')
|
|
252
|
+
.where('update_date', '<', DbDate.toDb(before))
|
|
253
|
+
.returning('id')
|
|
254
|
+
.execute()
|
|
255
|
+
const logged = await this.#db.q
|
|
256
|
+
.deleteFrom('webhook_log')
|
|
257
|
+
.where('log_date', '<', DbDate.toDb(before))
|
|
258
|
+
.returning('key')
|
|
259
|
+
.execute()
|
|
231
260
|
return done.length + logged.length
|
|
232
261
|
}
|
|
233
262
|
|
|
234
263
|
async failed(limit = 100): Promise<WebhookDelivery[]> {
|
|
235
|
-
const rows = await this.#db.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
264
|
+
const rows = await this.#db.q
|
|
265
|
+
.selectFrom('webhook_delivery')
|
|
266
|
+
.select(DELIVERY_COLUMNS)
|
|
267
|
+
.where('state', '=', 'failed')
|
|
268
|
+
.orderBy('update_date', 'desc')
|
|
269
|
+
.limit(limit)
|
|
270
|
+
.execute()
|
|
240
271
|
return rows.map(hydrateDelivery)
|
|
241
272
|
}
|
|
242
273
|
|
|
243
274
|
async all(): Promise<WebhookDelivery[]> {
|
|
244
|
-
const rows = await this.#db.
|
|
245
|
-
|
|
246
|
-
|
|
275
|
+
const rows = await this.#db.q
|
|
276
|
+
.selectFrom('webhook_delivery')
|
|
277
|
+
.select(DELIVERY_COLUMNS)
|
|
278
|
+
.orderBy('create_date')
|
|
279
|
+
.execute()
|
|
247
280
|
return rows.map(hydrateDelivery)
|
|
248
281
|
}
|
|
249
282
|
|
|
250
283
|
async due(now = new Date(), limit = 25): Promise<WebhookDelivery[]> {
|
|
251
|
-
const rows = await this.#db.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
284
|
+
const rows = await this.#db.q
|
|
285
|
+
.selectFrom('webhook_delivery')
|
|
286
|
+
.select(DELIVERY_COLUMNS)
|
|
287
|
+
.where('state', '=', 'pending')
|
|
288
|
+
.where('run_after', '<=', DbDate.toDb(now))
|
|
289
|
+
.orderBy('run_after')
|
|
290
|
+
.orderBy('create_date')
|
|
291
|
+
.limit(limit)
|
|
292
|
+
.execute()
|
|
256
293
|
return rows.map(hydrateDelivery)
|
|
257
294
|
}
|
|
258
295
|
|
|
259
296
|
/** Takes a delivery, or finds another node already has. */
|
|
260
297
|
async claim(id: string): Promise<WebhookDelivery | undefined> {
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
298
|
+
const row = await this.#db.q
|
|
299
|
+
.updateTable('webhook_delivery')
|
|
300
|
+
.set((eb) => ({
|
|
301
|
+
state: 'running',
|
|
302
|
+
attempts: eb('attempts', '+', 1),
|
|
303
|
+
update_date: DbDate.toDb(new Date()),
|
|
304
|
+
}))
|
|
305
|
+
.where('id', '=', id)
|
|
306
|
+
.where('state', '=', 'pending')
|
|
307
|
+
.returning(DELIVERY_COLUMNS)
|
|
308
|
+
.executeTakeFirst()
|
|
309
|
+
return row ? hydrateDelivery(row) : undefined
|
|
267
310
|
}
|
|
268
311
|
|
|
269
312
|
async succeed(id: string): Promise<void> {
|
|
270
|
-
await this.#db.
|
|
271
|
-
'
|
|
272
|
-
|
|
273
|
-
|
|
313
|
+
await this.#db.q
|
|
314
|
+
.updateTable('webhook_delivery')
|
|
315
|
+
.set({ state: 'done', last_error: null, update_date: DbDate.toDb(new Date()) })
|
|
316
|
+
.where('id', '=', id)
|
|
317
|
+
.execute()
|
|
274
318
|
}
|
|
275
319
|
|
|
276
320
|
/** `retryAt` puts it back in the queue; without one it has failed for good. */
|
|
277
321
|
async fail(id: string, error: string, retryAt?: Date): Promise<void> {
|
|
278
|
-
await this.#db.
|
|
279
|
-
'
|
|
280
|
-
|
|
281
|
-
retryAt ? 'pending' : 'failed',
|
|
282
|
-
error.slice(0, 2000),
|
|
283
|
-
DbDate.toDb(retryAt ?? new Date()),
|
|
284
|
-
DbDate.toDb(new Date()),
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
322
|
+
await this.#db.q
|
|
323
|
+
.updateTable('webhook_delivery')
|
|
324
|
+
.set({
|
|
325
|
+
state: retryAt ? 'pending' : 'failed',
|
|
326
|
+
last_error: error.slice(0, 2000),
|
|
327
|
+
run_after: DbDate.toDb(retryAt ?? new Date()),
|
|
328
|
+
update_date: DbDate.toDb(new Date()),
|
|
329
|
+
})
|
|
330
|
+
.where('id', '=', id)
|
|
331
|
+
.execute()
|
|
288
332
|
}
|
|
289
333
|
|
|
290
334
|
async byId(id: string): Promise<WebhookDelivery | undefined> {
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
335
|
+
const row = await this.#db.q
|
|
336
|
+
.selectFrom('webhook_delivery')
|
|
337
|
+
.select(DELIVERY_COLUMNS)
|
|
338
|
+
.where('id', '=', id)
|
|
339
|
+
.executeTakeFirst()
|
|
340
|
+
return row ? hydrateDelivery(row) : undefined
|
|
296
341
|
}
|
|
297
342
|
|
|
298
343
|
async log(entry: Omit<WebhookLogEntry, 'key' | 'date'>, date = new Date()): Promise<void> {
|
|
299
|
-
await this.#db.
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
entry.
|
|
306
|
-
entry.
|
|
307
|
-
entry.
|
|
308
|
-
entry.
|
|
309
|
-
entry.
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
entry.
|
|
313
|
-
clip(entry.
|
|
314
|
-
clip(entry.
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
],
|
|
319
|
-
)
|
|
344
|
+
await this.#db.q
|
|
345
|
+
.insertInto('webhook_log')
|
|
346
|
+
.values({
|
|
347
|
+
key: randomUUID(),
|
|
348
|
+
delivery_id: entry.deliveryId,
|
|
349
|
+
subscriber: entry.subscriber,
|
|
350
|
+
event_alias: entry.eventAlias,
|
|
351
|
+
url: entry.url,
|
|
352
|
+
status_code: entry.statusCode,
|
|
353
|
+
success: this.#db.dialect.boolValue(entry.success),
|
|
354
|
+
exception: this.#db.dialect.boolValue(entry.exception),
|
|
355
|
+
retry_count: entry.retryCount,
|
|
356
|
+
request_headers: clip(entry.requestHeaders),
|
|
357
|
+
request_body: clip(entry.requestBody),
|
|
358
|
+
response_headers: clip(entry.responseHeaders),
|
|
359
|
+
response_body: clip(entry.responseBody),
|
|
360
|
+
log_date: DbDate.toDb(date),
|
|
361
|
+
})
|
|
362
|
+
.execute()
|
|
320
363
|
}
|
|
321
364
|
|
|
322
365
|
/** The log, newest first; one subscriber's when named. */
|
|
@@ -325,20 +368,36 @@ export class WebhookDeliveryRepository {
|
|
|
325
368
|
skip: number
|
|
326
369
|
take: number
|
|
327
370
|
}): Promise<{ total: number; items: WebhookLogEntry[] }> {
|
|
328
|
-
const
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
371
|
+
const { subscriber } = options
|
|
372
|
+
const log = this.#db.q
|
|
373
|
+
.selectFrom('webhook_log')
|
|
374
|
+
.$if(Boolean(subscriber), (q) => q.where('subscriber', '=', subscriber as string))
|
|
375
|
+
const total = await log.select((eb) => eb.fn.countAll().as('n')).executeTakeFirst()
|
|
376
|
+
const rows = await log
|
|
377
|
+
.select([
|
|
378
|
+
'key',
|
|
379
|
+
'delivery_id',
|
|
380
|
+
'subscriber',
|
|
381
|
+
'event_alias',
|
|
382
|
+
'url',
|
|
383
|
+
'status_code',
|
|
384
|
+
'success',
|
|
385
|
+
'exception',
|
|
386
|
+
'retry_count',
|
|
387
|
+
'request_headers',
|
|
388
|
+
'request_body',
|
|
389
|
+
'response_headers',
|
|
390
|
+
'response_body',
|
|
391
|
+
'log_date',
|
|
392
|
+
])
|
|
393
|
+
.orderBy('log_date', 'desc')
|
|
394
|
+
.orderBy('retry_count', 'desc')
|
|
395
|
+
.orderBy('key')
|
|
396
|
+
.limit(options.take)
|
|
397
|
+
.offset(options.skip)
|
|
398
|
+
.execute()
|
|
340
399
|
return {
|
|
341
|
-
total: Number(total
|
|
400
|
+
total: Number(total?.n ?? 0),
|
|
342
401
|
items: rows.map((row) => ({
|
|
343
402
|
key: String(row.key),
|
|
344
403
|
deliveryId: String(row.delivery_id),
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 054: what an administrator granted each bundle (story 222). One row
|
|
3
|
+
* per bundle, replaced on each grant: the package and version it was granted
|
|
4
|
+
* for, the permissions, who granted them and when. A bundle with no row, or
|
|
5
|
+
* declaring more than its row holds, is off.
|
|
6
|
+
*/
|
|
7
|
+
import { sql } from 'kysely'
|
|
8
|
+
import type { Db } from '../database.ts'
|
|
9
|
+
import type { Migration } from '../migrations.ts'
|
|
10
|
+
import { STATE_MEDIA_MEMBER_GRANTS } from './media-member-grants.ts'
|
|
11
|
+
|
|
12
|
+
export const STATE_BUNDLE_GRANTS = '{4b198f00-0054-4ca8-bc39-97d92fca0054}'
|
|
13
|
+
|
|
14
|
+
export const bundleGrantsMigration: Migration = {
|
|
15
|
+
from: STATE_MEDIA_MEMBER_GRANTS,
|
|
16
|
+
to: STATE_BUNDLE_GRANTS,
|
|
17
|
+
name: 'AddBundleGrants',
|
|
18
|
+
kind: 'expand',
|
|
19
|
+
release: '0.38.1',
|
|
20
|
+
async up(db: Db) {
|
|
21
|
+
const t = db.dialect.types
|
|
22
|
+
await db.q.schema
|
|
23
|
+
.createTable('bundle_grant')
|
|
24
|
+
.addColumn('bundle_id', sql.raw(t.varchar(64)), (c) => c.primaryKey())
|
|
25
|
+
.addColumn('package_name', sql.raw(t.varchar(214)), (c) => c.notNull())
|
|
26
|
+
.addColumn('version', sql.raw(t.varchar(64)), (c) => c.notNull())
|
|
27
|
+
.addColumn('permissions', sql.raw(t.json), (c) => c.notNull())
|
|
28
|
+
.addColumn('granted_by', sql.raw(t.varchar(255)))
|
|
29
|
+
.addColumn('granted_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
30
|
+
.execute()
|
|
31
|
+
},
|
|
32
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 056: an administrator can disable a bundle (story 225). Its row in
|
|
3
|
+
* `bundle_grant` says who disabled it and when; a bundle with neither runs.
|
|
4
|
+
*/
|
|
5
|
+
import { sql } from 'kysely'
|
|
6
|
+
import type { Db } from '../database.ts'
|
|
7
|
+
import type { Migration } from '../migrations.ts'
|
|
8
|
+
import { STATE_JOBS } from './jobs.ts'
|
|
9
|
+
|
|
10
|
+
export const STATE_BUNDLES_DISABLED = '{4b198f00-0056-4ca8-bc39-97d92fca0056}'
|
|
11
|
+
|
|
12
|
+
export const bundlesDisabledMigration: Migration = {
|
|
13
|
+
from: STATE_JOBS,
|
|
14
|
+
to: STATE_BUNDLES_DISABLED,
|
|
15
|
+
name: 'DisableBundles',
|
|
16
|
+
kind: 'expand',
|
|
17
|
+
release: '0.39.0',
|
|
18
|
+
async up(db: Db) {
|
|
19
|
+
const t = db.dialect.types
|
|
20
|
+
await db.q.schema
|
|
21
|
+
.alterTable('bundle_grant')
|
|
22
|
+
.addColumn('disabled_by', sql.raw(t.varchar(255)))
|
|
23
|
+
.execute()
|
|
24
|
+
await db.q.schema
|
|
25
|
+
.alterTable('bundle_grant')
|
|
26
|
+
.addColumn('disabled_at', sql.raw(t.timestamp))
|
|
27
|
+
.execute()
|
|
28
|
+
},
|
|
29
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 055: background jobs for the site and its bundles (story 187). One
|
|
3
|
+
* queue for every owner, claimed atomically and retried with back-off, and each
|
|
4
|
+
* schedule's last occurrence, compared and set so exactly one node runs it.
|
|
5
|
+
*/
|
|
6
|
+
import { sql } from 'kysely'
|
|
7
|
+
import type { Db } from '../database.ts'
|
|
8
|
+
import type { Migration } from '../migrations.ts'
|
|
9
|
+
import { STATE_BUNDLE_GRANTS } from './bundle-grants.ts'
|
|
10
|
+
|
|
11
|
+
export const STATE_JOBS = '{4b198f00-0055-4ca8-bc39-97d92fca0055}'
|
|
12
|
+
|
|
13
|
+
export const jobsMigration: Migration = {
|
|
14
|
+
from: STATE_BUNDLE_GRANTS,
|
|
15
|
+
to: STATE_JOBS,
|
|
16
|
+
name: 'AddJobs',
|
|
17
|
+
kind: 'expand',
|
|
18
|
+
release: '0.38.1',
|
|
19
|
+
async up(db: Db) {
|
|
20
|
+
const t = db.dialect.types
|
|
21
|
+
const type = (name: string) => sql.raw(name)
|
|
22
|
+
await db.q.schema
|
|
23
|
+
.createTable('job')
|
|
24
|
+
.addColumn('id', type(t.uuid), (c) => c.primaryKey())
|
|
25
|
+
.addColumn('owner', type(t.varchar(64)), (c) => c.notNull())
|
|
26
|
+
.addColumn('name', type(t.varchar(128)), (c) => c.notNull())
|
|
27
|
+
.addColumn('payload', type(t.json), (c) => c.notNull())
|
|
28
|
+
.addColumn('state', type(t.varchar(16)), (c) => c.notNull())
|
|
29
|
+
.addColumn('attempts', type(t.integer), (c) => c.notNull())
|
|
30
|
+
.addColumn('max_attempts', type(t.integer), (c) => c.notNull())
|
|
31
|
+
.addColumn('next_attempt_at', type(t.timestamp), (c) => c.notNull())
|
|
32
|
+
.addColumn('claimed_by', type(t.varchar(255)))
|
|
33
|
+
.addColumn('claimed_at', type(t.timestamp))
|
|
34
|
+
.addColumn('progress', type(t.json))
|
|
35
|
+
.addColumn('last_error', type(t.text))
|
|
36
|
+
.addColumn('queued_by', type(t.varchar(255)))
|
|
37
|
+
.addColumn('created_at', type(t.timestamp), (c) => c.notNull())
|
|
38
|
+
.addColumn('finished_at', type(t.timestamp))
|
|
39
|
+
.execute()
|
|
40
|
+
await db.q.schema
|
|
41
|
+
.createIndex('ix_job_due')
|
|
42
|
+
.on('job')
|
|
43
|
+
.columns(['state', 'next_attempt_at'])
|
|
44
|
+
.execute()
|
|
45
|
+
await db.q.schema
|
|
46
|
+
.createIndex('ix_job_owner')
|
|
47
|
+
.on('job')
|
|
48
|
+
.columns(['owner', 'created_at'])
|
|
49
|
+
.execute()
|
|
50
|
+
await db.q.schema
|
|
51
|
+
.createTable('job_schedule')
|
|
52
|
+
.addColumn('schedule_key', type(t.varchar(200)), (c) => c.primaryKey())
|
|
53
|
+
.addColumn('last_occurrence', type(t.timestamp), (c) => c.notNull())
|
|
54
|
+
.execute()
|
|
55
|
+
},
|
|
56
|
+
}
|
package/src/schema/plan.ts
CHANGED
|
@@ -18,7 +18,9 @@ import { approvalsMigration } from './approvals.ts'
|
|
|
18
18
|
import { assistantMigration } from './assistant.ts'
|
|
19
19
|
import { auditLogMigration } from './audit-log.ts'
|
|
20
20
|
import { bundleComponentsMigration } from './bundle-components.ts'
|
|
21
|
+
import { bundleGrantsMigration } from './bundle-grants.ts'
|
|
21
22
|
import { bundlePackageMigration } from './bundle-package.ts'
|
|
23
|
+
import { bundlesDisabledMigration } from './bundles-disabled.ts'
|
|
22
24
|
import { bundlesRenameMigration } from './bundles-rename.ts'
|
|
23
25
|
import { changeReportMigration } from './change-report.ts'
|
|
24
26
|
import { clientIdPrefixMigration } from './client-id-prefix.ts'
|
|
@@ -39,6 +41,7 @@ import { formWorkflowsMigration } from './form-workflows.ts'
|
|
|
39
41
|
import { formsMigration } from './forms.ts'
|
|
40
42
|
import { groupPermissionsMigration } from './group-permissions.ts'
|
|
41
43
|
import { identityMigration } from './identity.ts'
|
|
44
|
+
import { jobsMigration } from './jobs.ts'
|
|
42
45
|
import { logViewerMigration } from './log-viewer.ts'
|
|
43
46
|
import { mediaAccessMigration } from './media-access.ts'
|
|
44
47
|
import { mediaCacheMigration } from './media-cache.ts'
|
|
@@ -121,4 +124,7 @@ export const cromboloPlan = new MigrationPlan([
|
|
|
121
124
|
mediaCacheMigration,
|
|
122
125
|
mediaMemberUploadsMigration,
|
|
123
126
|
mediaMemberGrantsMigration,
|
|
127
|
+
bundleGrantsMigration,
|
|
128
|
+
jobsMigration,
|
|
129
|
+
bundlesDisabledMigration,
|
|
124
130
|
])
|
package/src/schema-ops.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* `RENAME COLUMN`, so nothing here needs the copy-table dance.
|
|
5
5
|
*/
|
|
6
6
|
import type { Db } from './database.ts'
|
|
7
|
+
import { sql } from './query.ts'
|
|
7
8
|
|
|
8
9
|
export interface SchemaOps {
|
|
9
10
|
addColumn(table: string, column: string, definition: string): Promise<void>
|
|
@@ -16,20 +17,22 @@ export interface SchemaOps {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export function schemaOps(db: Db): SchemaOps {
|
|
19
|
-
const
|
|
20
|
+
const schema = db.q.schema
|
|
20
21
|
return {
|
|
21
22
|
addColumn: (table, column, definition) =>
|
|
22
|
-
|
|
23
|
-
dropColumn: (table, column) =>
|
|
24
|
-
renameColumn: (table, from, to) =>
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
schema.alterTable(table).addColumn(column, sql.raw(definition)).execute(),
|
|
24
|
+
dropColumn: (table, column) => schema.alterTable(table).dropColumn(column).execute(),
|
|
25
|
+
renameColumn: (table, from, to) => schema.alterTable(table).renameColumn(from, to).execute(),
|
|
26
|
+
renameTable: (from, to) => schema.alterTable(from).renameTo(to).execute(),
|
|
27
|
+
addIndex: (name, table, columns, unique = false) => {
|
|
28
|
+
const index = schema
|
|
29
|
+
.createIndex(name)
|
|
30
|
+
.on(table)
|
|
31
|
+
.columns([...columns])
|
|
32
|
+
return (unique ? index.unique() : index).execute()
|
|
33
|
+
},
|
|
31
34
|
// Postgres drops an index by name alone; SQLite too. `table` documents intent.
|
|
32
|
-
dropIndex: (name) =>
|
|
33
|
-
dropTable: (table) =>
|
|
35
|
+
dropIndex: (name) => schema.dropIndex(name).execute(),
|
|
36
|
+
dropTable: (table) => schema.dropTable(table).execute(),
|
|
34
37
|
}
|
|
35
38
|
}
|