misc: added outer try catch block

This commit is contained in:
Sheen Capadngan
2024-06-03 17:58:39 +08:00
parent cd6caab508
commit 4d830f1d1a
@@ -331,49 +331,53 @@ export const snapshotDALFactory = (db: TDbClient) => {
const PRUNE_FOLDER_BATCH_SIZE = 10000; const PRUNE_FOLDER_BATCH_SIZE = 10000;
let uuidOffset = "00000000-0000-0000-0000-000000000000"; let uuidOffset = "00000000-0000-0000-0000-000000000000";
// eslint-disable-next-line no-constant-condition, no-unreachable-loop try {
while (true) { // eslint-disable-next-line no-constant-condition, no-unreachable-loop
const folderBatch = await db(TableName.SecretFolder) while (true) {
.where("id", ">", uuidOffset) const folderBatch = await db(TableName.SecretFolder)
.orderBy("id", "asc") .where("id", ">", uuidOffset)
.limit(PRUNE_FOLDER_BATCH_SIZE) .orderBy("id", "asc")
.select("id"); .limit(PRUNE_FOLDER_BATCH_SIZE)
.select("id");
const batchEntries = folderBatch.map((folder) => folder.id); const batchEntries = folderBatch.map((folder) => folder.id);
logger.info("UUID offset:", uuidOffset); logger.info("UUID offset:", uuidOffset);
if (folderBatch.length) { if (folderBatch.length) {
try { try {
logger.info(`Pruning snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`); logger.info(`Pruning snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`);
await db(TableName.Snapshot) await db(TableName.Snapshot)
.with("snapshot_cte", (qb) => { .with("snapshot_cte", (qb) => {
void qb void qb
.from(TableName.Snapshot) .from(TableName.Snapshot)
.whereIn(`${TableName.Snapshot}.folderId`, batchEntries) .whereIn(`${TableName.Snapshot}.folderId`, batchEntries)
.select( .select(
"folderId", "folderId",
`${TableName.Snapshot}.id as id`, `${TableName.Snapshot}.id as id`,
db.raw( db.raw(
`ROW_NUMBER() OVER (PARTITION BY ${TableName.Snapshot}."folderId" ORDER BY ${TableName.Snapshot}."createdAt" DESC) AS row_num` `ROW_NUMBER() OVER (PARTITION BY ${TableName.Snapshot}."folderId" ORDER BY ${TableName.Snapshot}."createdAt" DESC) AS row_num`
) )
); );
}) })
.join(TableName.SecretFolder, `${TableName.SecretFolder}.id`, `${TableName.Snapshot}.folderId`) .join(TableName.SecretFolder, `${TableName.SecretFolder}.id`, `${TableName.Snapshot}.folderId`)
.join(TableName.Environment, `${TableName.Environment}.id`, `${TableName.SecretFolder}.envId`) .join(TableName.Environment, `${TableName.Environment}.id`, `${TableName.SecretFolder}.envId`)
.join(TableName.Project, `${TableName.Project}.id`, `${TableName.Environment}.projectId`) .join(TableName.Project, `${TableName.Project}.id`, `${TableName.Environment}.projectId`)
.join("snapshot_cte", "snapshot_cte.id", `${TableName.Snapshot}.id`) .join("snapshot_cte", "snapshot_cte.id", `${TableName.Snapshot}.id`)
.whereRaw(`snapshot_cte.row_num > ${TableName.Project}."pitVersionLimit"`) .whereRaw(`snapshot_cte.row_num > ${TableName.Project}."pitVersionLimit"`)
.delete(); .delete();
} catch (err) { } catch (err) {
logger.error( logger.error(
`Failed to prune snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}` `Failed to prune snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`
); );
} finally { } finally {
uuidOffset = batchEntries[batchEntries.length - 1]; uuidOffset = batchEntries[batchEntries.length - 1];
}
} else {
return;
} }
} else {
return;
} }
} catch (error) {
throw new DatabaseError({ error, name: "SnapshotPrune" });
} }
}; };