Add deploy startup seed guard
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function runSeed() {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const child = spawn("npx", ["tsx", "prisma/seed.ts"], {
|
||||
stdio: "inherit",
|
||||
shell: true
|
||||
});
|
||||
child.on("exit", (code) => {
|
||||
if (code === 0) resolve();
|
||||
else reject(new Error(`Seed failed with exit code ${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [plans, users, ads] = await Promise.all([
|
||||
prisma.plan.count(),
|
||||
prisma.user.count(),
|
||||
prisma.ad.count()
|
||||
]);
|
||||
|
||||
if (plans > 0 && users > 0 && ads > 0) {
|
||||
console.log("✅ Database already seeded. Skipping demo seed.");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("🌱 Empty database detected. Running demo seed...");
|
||||
await runSeed();
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user