Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: transaction ...: in use: for query #2278

Merged
8 changes: 7 additions & 1 deletion apps/api/src/pkg/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ export async function insertGenericAuditLogs(
let { val: bucket, err } = await cache.auditLogBucketByWorkspaceIdAndName.swr(
cacheKey,
async () => {
const bucket = await (tx ?? db.primary).query.auditLogBucket.findFirst({
// do not use the transaction here, otherwise we may run into race conditions
// https://github.com/unkeyed/unkey/pull/2278
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 +101,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
Loading