Skip to content

Commit a8c94c7

Browse files
committed
fix: race condition in getExternalDbSyncFusebox
getExternalDbFusebox used to lazy init the settings singleton. However, this caused issues. Prisma's upsert isn't safe from race conditions. So this could cause collisions/ unique key issues. Get doesn't need to update anything. Let's just return default values and have the update handle the init. Tested on local that fusebox updates work fine.
1 parent c7e1198 commit a8c94c7

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

apps/backend/src/lib/external-db-sync-metadata.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ const fuseboxSelect = {
1111
pollerEnabled: true,
1212
};
1313

14+
// Default values match the Prisma schema defaults
15+
const defaultFusebox: ExternalDbSyncFusebox = {
16+
sequencerEnabled: true,
17+
pollerEnabled: true,
18+
};
19+
1420
export async function getExternalDbSyncFusebox(): Promise<ExternalDbSyncFusebox> {
15-
return await globalPrismaClient.externalDbSyncMetadata.upsert({
21+
const result = await globalPrismaClient.externalDbSyncMetadata.findFirst({
1622
where: { singleton: BooleanTrue.TRUE },
17-
create: { singleton: BooleanTrue.TRUE },
18-
update: {},
1923
select: fuseboxSelect,
2024
});
25+
// Return defaults if row doesn't exist yet (row is created on first update)
26+
return result ?? defaultFusebox;
2127
}
2228

2329
export async function updateExternalDbSyncFusebox(
2430
updates: ExternalDbSyncFusebox,
2531
): Promise<ExternalDbSyncFusebox> {
26-
return await globalPrismaClient.externalDbSyncMetadata.upsert({
32+
// Upsert is fine here - updates are infrequent and typically manual/admin actions
33+
return globalPrismaClient.externalDbSyncMetadata.upsert({
2734
where: { singleton: BooleanTrue.TRUE },
2835
create: { singleton: BooleanTrue.TRUE, ...updates },
2936
update: updates,

0 commit comments

Comments
 (0)