From 7cdf842cbb89b0e6bbaca81ad6b3774a3964950f Mon Sep 17 00:00:00 2001 From: Pedro Lunet Date: Fri, 2 Oct 2026 10:22:31 +0100 Subject: [PATCH 1/2] chore: make seed script idempotent and update env --- .env.example | 2 +- src/prisma/seed.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 65c1ed2..0330527 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,4 @@ # Connection string for PostgreSQL. # Requires PostgreSQL >= 15. -DATABASE_URL="postgresql://neet:neet@localhost:5432/neet" +DATABASE_URL="postgresql://neet:neet@127.0.0.1:5432/neet" diff --git a/src/prisma/seed.ts b/src/prisma/seed.ts index 94938d9..6bd9b2b 100644 --- a/src/prisma/seed.ts +++ b/src/prisma/seed.ts @@ -5,6 +5,19 @@ import { db } from "./db.ts"; async function main() { console.log("Seeding database... ๐ŸŒฑ"); + // Forcefully wipe the database tables before inserting using Prisma's raw SQL execution builder + await db + .runtime() + .execute( + db.raw.sql`DELETE FROM "public"."Availability"`.affectedCount().build(), + ); + await db + .runtime() + .execute(db.raw.sql`DELETE FROM "public"."Event"`.affectedCount().build()); + await db + .runtime() + .execute(db.raw.sql`DELETE FROM "public"."User"`.affectedCount().build()); + // Create some users const alice = await db.orm.public.User.create({ name: "Alice Silva", @@ -29,7 +42,6 @@ async function main() { }); // Add availability for Alice (she is free today 14:00 - 15:30) - // We use ZonedDateTime to get an accurate Instant for Lisbon time const aliceStart = today .toZonedDateTime({ timeZone: "Europe/Lisbon", plainTime: "14:00" }) .toInstant(); From 434f1da8fdbb70c99efa38fe96385f417f02526e Mon Sep 17 00:00:00 2001 From: Pedro Lunet Date: Fri, 2 Oct 2026 10:53:23 +0100 Subject: [PATCH 2/2] feat: use dynamic postgres sql block to automatically find and wipe all tables --- src/prisma/seed.ts | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/prisma/seed.ts b/src/prisma/seed.ts index 6bd9b2b..c50bcec 100644 --- a/src/prisma/seed.ts +++ b/src/prisma/seed.ts @@ -5,18 +5,30 @@ import { db } from "./db.ts"; async function main() { console.log("Seeding database... ๐ŸŒฑ"); - // Forcefully wipe the database tables before inserting using Prisma's raw SQL execution builder - await db - .runtime() - .execute( - db.raw.sql`DELETE FROM "public"."Availability"`.affectedCount().build(), - ); - await db - .runtime() - .execute(db.raw.sql`DELETE FROM "public"."Event"`.affectedCount().build()); - await db - .runtime() - .execute(db.raw.sql`DELETE FROM "public"."User"`.affectedCount().build()); + // Forcefully wipe ALL tables intelligently using a dynamic Postgres query! + // This automatically finds all your tables (even if you add new ones later) + // and TRUNCATES them, while ignoring Prisma's internal state tables. + const wipePlan = db.raw.sql` + DO $$ + DECLARE + r RECORD; + BEGIN + FOR r IN ( + SELECT tablename + FROM pg_tables + WHERE schemaname = 'public' + AND tablename NOT LIKE 'prisma_%' + AND tablename != '_prisma_migrations' + ) LOOP + EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE'; + END LOOP; + END $$; + ` + .affectedCount() + .build(); + + await db.runtime().execute(wipePlan); + console.log("Database wiped perfectly! ๐Ÿงน"); // Create some users const alice = await db.orm.public.User.create({