Skip to content

Commit

Permalink
fix: transaction ...: in use: for query
Browse files Browse the repository at this point in the history
- Fixes unkeyed#2197
- The error is caused by the cache revalidation happening in the background, so we have a racecondition with the other queries we are running in the insertGenericAuditLogs function
  • Loading branch information
Flo4604 committed Oct 8, 2024
1 parent 10749fe commit 13371ce
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion apps/api/src/pkg/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,21 @@ export async function insertGenericAuditLogs(
let { val: bucket, err } = await cache.auditLogBucketByWorkspaceIdAndName.swr(
cacheKey,
async () => {
const bucket = await (tx ?? db.primary).query.auditLogBucket.findFirst({
const bucket = await db.readonly.query.auditLogBucket.findFirst({
where: (table, { eq, and }) =>
and(eq(table.workspaceId, log.workspaceId), eq(table.name, log.bucket)),
});

if (!bucket) {
return undefined;
}

return {
id: bucket.id,
};
},
);

if (err) {
logger.error("Could not find audit log bucket for workspace", {
workspaceId: log.workspaceId,
Expand Down Expand Up @@ -96,6 +99,7 @@ export async function insertGenericAuditLogs(
actorName: log.actor.name,
actorMeta: log.actor.meta,
});

await (tx ?? db.primary).insert(schema.auditLogTarget).values(
log.resources.map((r) => ({
workspaceId: log.workspaceId,
Expand Down

0 comments on commit 13371ce

Please sign in to comment.