@@ -36,6 +36,7 @@ import {
3636 PrebuildWithWorkspace ,
3737 PrebuildWithWorkspaceAndInstances ,
3838 PrebuiltUpdatableAndWorkspace ,
39+ WorkspaceAndOwner ,
3940 WorkspaceDB ,
4041 WorkspaceOwnerAndContentDeletedTime ,
4142 WorkspaceOwnerAndDeletionEligibility ,
@@ -492,39 +493,25 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
492493 throw new Error ( "cutOffDate must not be in the future, was: " + cutOffDate . toISOString ( ) ) ;
493494 }
494495 const workspaceRepo = await this . getWorkspaceRepo ( ) ;
495- const qb = workspaceRepo
496- . createQueryBuilder ( "ws" )
497- . leftJoinAndMapOne (
498- "ws.latestInstance" ,
499- DBWorkspaceInstance ,
500- "wsi" ,
501- `wsi.id = (
502- SELECT i.id
503- FROM d_b_workspace_instance AS i
504- WHERE i.workspaceId = ws.id
505- ORDER BY i.creationTime DESC
506- LIMIT 1
507- )` ,
508- )
509- . select ( [ "ws.id" , "ws.ownerId" , "ws.deletionEligibilityTime" , "wsi.id" , "wsi.status" ] )
510- . where ( "ws.deleted = :deleted" , { deleted : 0 } )
511- . andWhere ( "ws.type = :type" , { type } )
512- . andWhere ( "ws.softDeleted IS NULL" )
513- . andWhere ( "ws.softDeletedTime = ''" )
514- . andWhere ( "ws.pinned = :pinned" , { pinned : 0 } )
515- . andWhere ( "ws.deletionEligibilityTime != ''" )
516- . andWhere ( "ws.deletionEligibilityTime < :cutOffDate" , { cutOffDate : cutOffDate . toISOString ( ) } )
517- // we don't want to delete workspaces that are active
518- . andWhere (
519- new Brackets ( ( qb ) => {
520- qb . where ( "wsi.id IS NULL" ) . orWhere ( "JSON_UNQUOTE(wsi.status->>'$.phase') = :stoppedStatus" , {
521- stoppedStatus : "stopped" ,
522- } ) ;
523- } ) ,
524- )
525- . limit ( limit ) ;
496+ const dbResults = await workspaceRepo . query (
497+ `
498+ SELECT ws.id AS id,
499+ ws.ownerId AS ownerId,
500+ ws.deletionEligibilityTime AS deletionEligibilityTime
501+ FROM d_b_workspace AS ws
502+ WHERE ws.deleted = 0
503+ AND ws.type = ?
504+ AND ws.softDeleted IS NULL
505+ AND ws.softDeletedTime = ''
506+ AND ws.pinned = 0
507+ AND ws.deletionEligibilityTime != ''
508+ AND ws.deletionEligibilityTime < ?
509+ LIMIT ?;
510+ ` ,
511+ [ type , cutOffDate . toISOString ( ) , limit ] ,
512+ ) ;
526513
527- return qb . getMany ( ) ;
514+ return dbResults as WorkspaceAndOwner [ ] ;
528515 }
529516
530517 public async findWorkspacesForPurging (
0 commit comments