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..c50bcec 100644 --- a/src/prisma/seed.ts +++ b/src/prisma/seed.ts @@ -5,6 +5,31 @@ import { db } from "./db.ts"; async function main() { console.log("Seeding database... 🌱"); + // 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({ name: "Alice Silva", @@ -29,7 +54,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();