From 44b64eca37e885aa3cac78cec98b6adae624d0c2 Mon Sep 17 00:00:00 2001 From: Melvin PETIT Date: Tue, 16 Jun 2026 11:00:28 +0200 Subject: [PATCH] chore(db): add db:migrate script and pending-migration warning Decouple app boot from schema migrations. postinstall keeps the Prisma client in sync; predev warns (non-blocking) when migrations are pending instead of applying them. Document the after-pull workflow. --- README.md | 18 ++++++++++++++++++ package.json | 2 ++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 687a802..d5c25ed 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,29 @@ Open http://localhost:3000 and sign in with the seeded admin account: admin@datashield.local / ChangeMe123! ``` +### Switching machines / after `git pull` + +The Prisma client is regenerated automatically on `npm install` (`postinstall`). +The database is **not** migrated automatically: `npm run dev` only *warns* if +migrations are pending (it never applies them on boot). After pulling changes +that add a migration, apply it explicitly before running the app: + +```bash +npm run db:migrate # prisma migrate deploy, applies pending migrations +``` + +When you edit `prisma/schema.prisma` yourself, create the migration instead: + +```bash +npx prisma migrate dev --name +``` + ### Database commands - `npm run db:init` starts a Postgres container (`compose.yml`), applies all migrations and seeds demo data (breaches, employees, alerts). - `npm run db:up` / `npm run db:down` start and stop the container. +- `npm run db:migrate` applies pending migrations to the current database. - `npm run seed:dev` reseeds the demo data; `npm run seed` seeds only the admin. No Docker? Point `DATABASE_URL` at your own PostgreSQL, then run diff --git a/package.json b/package.json index d67a8b1..2a588d5 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "author": "Melvin PETIT (WhiteMuush)", "type": "module", "scripts": { + "predev": "prisma migrate status || echo '\\n[!] Pending migrations or DB unreachable. If the app errors on a missing table, run: npm run db:migrate\\n'", "dev": "next dev", "build": "next build", "start": "next start", @@ -23,6 +24,7 @@ "db:up": "docker compose up -d db", "db:down": "docker compose down", "db:init": "sh scripts/db-init.sh", + "db:migrate": "prisma migrate deploy", "postinstall": "prisma generate", "prepare": "sh .githooks/install.sh", "seed:dev": "dotenv -e .env.local -- tsx prisma/seed.dev.ts"