Skip to content

Commit 19ef2a9

Browse files
committed
Give seeded notifications stable IDs so overlapping seeds don't duplicate them
From review: the seed reads a user's existing notifications and then inserts the missing ones, so two seeds running at the same time could both insert the same notification under different random IDs. Derive each ID from the org, user, and template, so the second insert hits onConflictDoNothing. Two concurrent seeds on a local database now insert 280 notifications instead of 490. The text check stays for rows that older seeds created with random IDs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EsE8hW2Sa81QXuqHAStNNa
1 parent 90ac485 commit 19ef2a9

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

‎scripts/seed.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import "dotenv/config"; //must be first to load environment variables
2-
import { randomUUID } from "node:crypto";
32
import { EventVisibility, JoinRequestStatus, schema } from "../lib/schema";
43
import db from "../lib/db";
54
import { auth } from "@/lib/auth";
@@ -581,8 +580,8 @@ export async function main() {
581580
for (const email of memberEmails) {
582581
for (const org of ORGS) {
583582
const userId = userIdsByOrganization.get(org.id)!.get(email)!;
584-
// Skip the ones an earlier run created. Their IDs are random, so
585-
// onConflictDoNothing alone would insert a second copy.
583+
// Skip the ones the user already has. Seeds before the IDs below were
584+
// stable gave them random IDs, which onConflictDoNothing can't match.
586585
const existing = await db
587586
.select({ text: schema.notifications.text })
588587
.from(schema.notifications)
@@ -594,16 +593,18 @@ export async function main() {
594593
);
595594
const existingTexts = new Set(existing.map((row) => row.text));
596595
const notificationValues = notificationTemplates
597-
.filter((tmpl) => !existingTexts.has(tmpl.text))
598-
.map((tmpl) => ({
599-
id: randomUUID(),
596+
.map((tmpl, index) => ({
597+
// Stable, so a seed running at the same time hits
598+
// onConflictDoNothing instead of inserting a second copy.
599+
id: `notif_${org.id}_${userId}_${index}`,
600600
userId,
601601
organizationId: org.id,
602602
type: tmpl.type,
603603
text: tmpl.text,
604604
read: tmpl.read,
605605
createdAt: new Date(Date.now() - tmpl.minutesAgo * 60 * 1000),
606-
}));
606+
}))
607+
.filter((notification) => !existingTexts.has(notification.text));
607608
if (notificationValues.length === 0) continue;
608609

609610
await db

0 commit comments

Comments
 (0)