Skip to content

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

refactor(reminders): name the uuid pattern in the orphan alert query and explain the cast

Calvin05966c8a493c
diff --git a/backend/src/services/reminder/reminder-dal.ts b/backend/src/services/reminder/reminder-dal.ts
index 4c30d7cf76..ae39e2d4cd 100644
--- a/backend/src/services/reminder/reminder-dal.ts
+++ b/backend/src/services/reminder/reminder-dal.ts
@@ -5,6 +5,8 @@ import { TableName, TProjectEnvironments, TProjects, TSecretFolders, TSecretsV2
import { RemindersSchema } from "@app/db/schemas/reminders";
import { ormify, selectAllTableCols } from "@app/lib/knex";
+const UUID_PATTERN = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
+
export type TReminderDALFactory = ReturnType<typeof reminderDALFactory>;
export const reminderDALFactory = (db: TDbClient) => {
@@ -50,9 +52,12 @@ export const reminderDALFactory = (db: TDbClient) => {
void qb
.select(db.raw("1"))
.from(TableName.SecretV2)
- // The CASE keeps a non-uuid resourceId from failing the cast for the whole query.
+ // alerts.resourceId is text and secrets_v2.id is a uuid, so the id is cast to compare them. The
+ // cast only runs on values shaped like a uuid, so a malformed id becomes NULL (and is reaped)
+ // instead of failing the whole query.
.whereRaw(
- `"${TableName.SecretV2}"."id" = CASE WHEN "${TableName.Alert}"."resourceId" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' THEN "${TableName.Alert}"."resourceId"::uuid END`
+ `"${TableName.SecretV2}"."id" = CASE WHEN "${TableName.Alert}"."resourceId" ~* ? THEN "${TableName.Alert}"."resourceId"::uuid END`,
+ [UUID_PATTERN]
);
})
.limit(limit)