@@ -54,6 +54,8 @@ interface PruneLargeValueMetadataOptions {
5454 tombstonesDeletedBefore : Date
5555 batchSize ?: number
5656 maxRowsPerTable ?: number
57+ /** Client the prune DELETEs run on. Defaults to the global pool; cleanup jobs pass `dbFor('cleanup')`. */
58+ dbClient ?: LargeValueMetadataClient
5759}
5860
5961function parseLargeValueStorageKey ( key : string ) : LargeValueStorageKeyParts | null {
@@ -368,19 +370,26 @@ export async function replaceLargeValueReferences(
368370 } )
369371}
370372
371- export async function markLargeValuesDeleted ( keys : string [ ] ) : Promise < void > {
373+ export async function markLargeValuesDeleted (
374+ keys : string [ ] ,
375+ dbClient : LargeValueMetadataClient = db
376+ ) : Promise < void > {
372377 if ( keys . length === 0 ) {
373378 return
374379 }
375380
376- await db
381+ await dbClient
377382 . update ( executionLargeValues )
378383 . set ( { deletedAt : new Date ( ) } )
379384 . where ( inArray ( executionLargeValues . key , keys ) )
380385}
381386
382- async function pruneStaleReferences ( workspaceIds : string [ ] , batchSize : number ) : Promise < number > {
383- const rows = await db . execute < { count : number } > ( sql `
387+ async function pruneStaleReferences (
388+ workspaceIds : string [ ] ,
389+ batchSize : number ,
390+ dbClient : LargeValueMetadataClient
391+ ) : Promise < number > {
392+ const rows = await dbClient . execute < { count : number } > ( sql `
384393 WITH deleted AS (
385394 DELETE FROM ${ executionLargeValueReferences } AS ref
386395 WHERE ref.ctid IN (
@@ -418,9 +427,10 @@ async function pruneStaleReferences(workspaceIds: string[], batchSize: number):
418427
419428async function pruneDeletedParentDependencies (
420429 workspaceIds : string [ ] ,
421- batchSize : number
430+ batchSize : number ,
431+ dbClient : LargeValueMetadataClient
422432) : Promise < number > {
423- const rows = await db . execute < { count : number } > ( sql `
433+ const rows = await dbClient . execute < { count : number } > ( sql `
424434 WITH deleted AS (
425435 DELETE FROM ${ executionLargeValueDependencies } AS dependency
426436 WHERE dependency.ctid IN (
@@ -452,9 +462,10 @@ async function pruneDeletedParentDependencies(
452462async function pruneDeletedLargeValueTombstones (
453463 workspaceIds : string [ ] ,
454464 deletedBefore : Date ,
455- batchSize : number
465+ batchSize : number ,
466+ dbClient : LargeValueMetadataClient
456467) : Promise < number > {
457- const rows = await db . execute < { count : number } > ( sql `
468+ const rows = await dbClient . execute < { count : number } > ( sql `
458469 WITH deleted AS (
459470 DELETE FROM ${ executionLargeValues } AS value
460471 WHERE value.ctid IN (
@@ -482,6 +493,7 @@ export async function pruneLargeValueMetadata({
482493 tombstonesDeletedBefore,
483494 batchSize = LARGE_VALUE_METADATA_PRUNE_BATCH_SIZE ,
484495 maxRowsPerTable = LARGE_VALUE_METADATA_PRUNE_MAX_ROWS_PER_TABLE ,
496+ dbClient = db ,
485497} : PruneLargeValueMetadataOptions ) : Promise < LargeValueMetadataPruneResult > {
486498 const result : LargeValueMetadataPruneResult = {
487499 referencesDeleted : 0 ,
@@ -498,15 +510,17 @@ export async function pruneLargeValueMetadata({
498510 if ( referencesRemaining > 0 ) {
499511 result . referencesDeleted += await pruneStaleReferences (
500512 workspaceChunk ,
501- Math . min ( batchSize , referencesRemaining )
513+ Math . min ( batchSize , referencesRemaining ) ,
514+ dbClient
502515 )
503516 }
504517
505518 const dependenciesRemaining = maxRowsPerTable - result . dependenciesDeleted
506519 if ( dependenciesRemaining > 0 ) {
507520 result . dependenciesDeleted += await pruneDeletedParentDependencies (
508521 workspaceChunk ,
509- Math . min ( batchSize , dependenciesRemaining )
522+ Math . min ( batchSize , dependenciesRemaining ) ,
523+ dbClient
510524 )
511525 }
512526
@@ -515,7 +529,8 @@ export async function pruneLargeValueMetadata({
515529 result . tombstonesDeleted += await pruneDeletedLargeValueTombstones (
516530 workspaceChunk ,
517531 tombstonesDeletedBefore ,
518- Math . min ( batchSize , tombstonesRemaining )
532+ Math . min ( batchSize , tombstonesRemaining ) ,
533+ dbClient
519534 )
520535 }
521536
0 commit comments