import Inject from "@entity-access/entity-access/dist/di/di.js"; import Workflow, { UniqueActivity } from "@entity-access/entity-access/dist/workflows/Workflow.js"; import SocialMailContext from "../../../model/SocialMailContext.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; export default class RepairPathWorkflow extends Workflow { async run() { await this.fixRootPath(); for(let i=0;i<10000;i++) { const changed = await this.fixPath(i); if (changed === 0) { break; } } } @UniqueActivity async fixRootPath( @Inject db?: SocialMailContext ) { await db.appFiles.where((x) => x.parentID === null && x.isDeleted === false && x.path !== `${x.parent.path}${Sql.cast.asText(x.appFileID)}/` ) .update((x) => ({ path: `${x.parent.path}${Sql.cast.asText(x.appFileID)}/` })); } @UniqueActivity async fixPath( _i: any, @Inject db?: SocialMailContext ) { const n = await db.appFiles.where((x) => x.parentID !== null && x.isDeleted === false && x.path !== `${x.parent.path}${Sql.cast.asText(x.appFileID)}/` ) .orderBy((x) => x.appFileID) .limit(10000) .update((x) => ({ path: `${x.parent.path}${Sql.cast.asText(x.appFileID)}/` })); if(n > 0) { console.log(JSON.stringify({ action: "Path fixed", total: n })) } return n; } @UniqueActivity async queueAll( @Inject db?: SocialMailContext ) { let start = 0; for(;;) { const roots = await db.appFiles .where( { start }, (x, p) => x.parentID === null && x.isFolder === true && x.appFileID > p.start) .orderBy((x) => x.appFileID) .limit(100) .toArray(); if (!roots.length) { break; } for (const iterator of roots) { start = iterator.appFileID; await this.context.queue(RepairPathWorkflow, iterator.appFileID); } } } }