Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Fail the seed on errors and refuse a database that has data

Luke Vella5289f2d9bb7a
diff --git a/packages/database/prisma/seed.ts b/packages/database/prisma/seed.ts
index 7f8023054..123e90a85 100644
--- a/packages/database/prisma/seed.ts
+++ b/packages/database/prisma/seed.ts
@@ -22,7 +22,15 @@ function nextId() {
return `seed-${(++idCounter).toString().padStart(4, "0")}`;
}
+class DatabaseNotEmptyError extends Error {}
+
async function main() {
+ if (await prisma.user.findFirst({ select: { id: true } })) {
+ throw new DatabaseNotEmptyError(
+ "The database already has data. Run `pnpm db:reset` to reset and seed it.",
+ );
+ }
+
// 1. Users
await prisma.user.createMany({ data: users.map((u) => ({ ...u })) });
console.info(`✓ ${users.length} users`);
@@ -229,7 +237,10 @@ async function main() {
}
main()
- .catch((e) => console.error(e))
+ .catch((e) => {
+ console.error(e instanceof DatabaseNotEmptyError ? e.message : e);
+ process.exitCode = 1;
+ })
.finally(async () => {
await prisma.$disconnect();
});