Skip to content

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

remove FK and optmize queries

Thiago Araujo da Silvada7c5a825cca
diff --git a/backend/src/db/migrations/20260522191822_audit-log-stream-outbox.ts b/backend/src/db/migrations/20260522191822_audit-log-stream-outbox.ts
index 8be98d654c..1cf2d10655 100644
--- a/backend/src/db/migrations/20260522191822_audit-log-stream-outbox.ts
+++ b/backend/src/db/migrations/20260522191822_audit-log-stream-outbox.ts
@@ -18,9 +18,6 @@ export async function up(knex: Knex): Promise<void> {
t.timestamp("lockedAt", { useTz: true });
t.timestamps(true, true, true);
- t.foreign("streamId").references("id").inTable(TableName.AuditLogStream).onDelete("CASCADE");
- t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");
-
t.unique(["streamId", "auditLogId"]);
});
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
index a0d5225d14..04156701f3 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
@@ -54,7 +54,10 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
.where({ streamId })
.whereIn("status", [AuditLogStreamOutboxStatus.Pending, AuditLogStreamOutboxStatus.Retry])
.andWhere("nextRetryAt", "<=", tx.fn.now())
- .orderBy("id", "asc")
+ .orderBy([
+ { column: "nextRetryAt", order: "asc" },
+ { column: "id", order: "asc" }
+ ])
.limit(limit)
.forUpdate()
.skipLocked()
@@ -254,13 +257,21 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
const findStreamsWithOverdueRows = async (): Promise<{ streamId: string; orgId: string; provider: string }[]> => {
try {
- return await db(`${TableName.AuditLogStreamOutbox} as o`)
- .join(`${TableName.AuditLogStream} as s`, "s.id", "o.streamId")
- .whereIn("o.status", [AuditLogStreamOutboxStatus.Pending, AuditLogStreamOutboxStatus.Retry])
- .andWhere("o.nextRetryAt", "<=", db.fn.now())
- .distinct<
- { streamId: string; orgId: string; provider: string }[]
- >("o.streamId as streamId", "o.orgId as orgId", "s.provider as provider");
+ const rows = await db(`${TableName.AuditLogStream} as s`)
+ .whereExists((qb) => {
+ void qb
+ .select(db.raw("1"))
+ .from(`${TableName.AuditLogStreamOutbox} as o`)
+ .whereRaw('o."streamId" = s.id')
+ .whereIn("o.status", [AuditLogStreamOutboxStatus.Pending, AuditLogStreamOutboxStatus.Retry])
+ .andWhere("o.nextRetryAt", "<=", db.fn.now());
+ })
+ .select<{ streamId: string; orgId: string; provider: string }[]>(
+ "s.id as streamId",
+ "s.orgId as orgId",
+ "s.provider as provider"
+ );
+ return rows;
} catch (error) {
throw new DatabaseError({ error, name: "AuditLogStreamOutbox: findStreamsWithOverdueRows" });
}