Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

misc: added outer try catch block

Sheen Capadngan4d830f1d1a49
diff --git a/backend/src/ee/services/secret-snapshot/snapshot-dal.ts b/backend/src/ee/services/secret-snapshot/snapshot-dal.ts
index b49e520b8a..7c17c4f072 100644
--- a/backend/src/ee/services/secret-snapshot/snapshot-dal.ts
+++ b/backend/src/ee/services/secret-snapshot/snapshot-dal.ts
@@ -331,49 +331,53 @@ export const snapshotDALFactory = (db: TDbClient) => {
const PRUNE_FOLDER_BATCH_SIZE = 10000;
let uuidOffset = "00000000-0000-0000-0000-000000000000";
- // eslint-disable-next-line no-constant-condition, no-unreachable-loop
- while (true) {
- const folderBatch = await db(TableName.SecretFolder)
- .where("id", ">", uuidOffset)
- .orderBy("id", "asc")
- .limit(PRUNE_FOLDER_BATCH_SIZE)
- .select("id");
+ try {
+ // eslint-disable-next-line no-constant-condition, no-unreachable-loop
+ while (true) {
+ const folderBatch = await db(TableName.SecretFolder)
+ .where("id", ">", uuidOffset)
+ .orderBy("id", "asc")
+ .limit(PRUNE_FOLDER_BATCH_SIZE)
+ .select("id");
- const batchEntries = folderBatch.map((folder) => folder.id);
- logger.info("UUID offset:", uuidOffset);
+ const batchEntries = folderBatch.map((folder) => folder.id);
+ logger.info("UUID offset:", uuidOffset);
- if (folderBatch.length) {
- try {
- logger.info(`Pruning snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`);
- await db(TableName.Snapshot)
- .with("snapshot_cte", (qb) => {
- void qb
- .from(TableName.Snapshot)
- .whereIn(`${TableName.Snapshot}.folderId`, batchEntries)
- .select(
- "folderId",
- `${TableName.Snapshot}.id as id`,
- db.raw(
- `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.Environment, `${TableName.Environment}.id`, `${TableName.SecretFolder}.envId`)
- .join(TableName.Project, `${TableName.Project}.id`, `${TableName.Environment}.projectId`)
- .join("snapshot_cte", "snapshot_cte.id", `${TableName.Snapshot}.id`)
- .whereRaw(`snapshot_cte.row_num > ${TableName.Project}."pitVersionLimit"`)
- .delete();
- } catch (err) {
- logger.error(
- `Failed to prune snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`
- );
- } finally {
- uuidOffset = batchEntries[batchEntries.length - 1];
+ if (folderBatch.length) {
+ try {
+ logger.info(`Pruning snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`);
+ await db(TableName.Snapshot)
+ .with("snapshot_cte", (qb) => {
+ void qb
+ .from(TableName.Snapshot)
+ .whereIn(`${TableName.Snapshot}.folderId`, batchEntries)
+ .select(
+ "folderId",
+ `${TableName.Snapshot}.id as id`,
+ db.raw(
+ `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.Environment, `${TableName.Environment}.id`, `${TableName.SecretFolder}.envId`)
+ .join(TableName.Project, `${TableName.Project}.id`, `${TableName.Environment}.projectId`)
+ .join("snapshot_cte", "snapshot_cte.id", `${TableName.Snapshot}.id`)
+ .whereRaw(`snapshot_cte.row_num > ${TableName.Project}."pitVersionLimit"`)
+ .delete();
+ } catch (err) {
+ logger.error(
+ `Failed to prune snapshots in range ${batchEntries[0]}:${batchEntries[batchEntries.length - 1]}`
+ );
+ } finally {
+ uuidOffset = batchEntries[batchEntries.length - 1];
+ }
+ } else {
+ return;
}
- } else {
- return;
}
+ } catch (error) {
+ throw new DatabaseError({ error, name: "SnapshotPrune" });
}
};