diff --git a/Dockerfile b/Dockerfile index ab922ba..f077075 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,7 +29,7 @@ RUN pnpm build # --------------------------------------------------------------------------- FROM builder AS tools ENV NODE_ENV=development -CMD ["sh", "-c", "pnpm db:migrate && pnpm db:seed"] +CMD ["pnpm", "db:migrate"] FROM base AS runner WORKDIR /app diff --git a/docker-compose.yml b/docker-compose.yml index ab1b335..353e8e3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -159,14 +159,17 @@ services: retries: 10 restart: unless-stopped - # One-shot migrations + seed. Run on demand: + # One-shot migrations. Run on demand: # docker compose --profile tools run --rm migrate + # Seed is intentionally separate: it inserts demo users/services and rewrites + # assignments, so it must never run as part of a production deploy. On a + # fresh install run: docker compose --profile tools run --rm migrate pnpm db:seed migrate: build: context: . dockerfile: Dockerfile target: tools - command: ["sh", "-c", "pnpm db:migrate && pnpm db:seed"] + command: ["pnpm", "db:migrate"] environment: DATABASE_URL: postgres://${POSTGRES_USER:-media}:${POSTGRES_PASSWORD:-media}@db:5432/${POSTGRES_DB:-media} depends_on: diff --git a/src/db/seed.ts b/src/db/seed.ts index f1f4111..f64ca7b 100644 --- a/src/db/seed.ts +++ b/src/db/seed.ts @@ -1,10 +1,15 @@ /** * Seed script for Media Team Hub (Phase A2). * - * Idempotent: every insert uses ON CONFLICT DO NOTHING, so it is safe to run - * repeatedly. Populates the core scheduling tables with a small, browsable - * dataset — 3 roles, 2 services, the next 2 Sundays of instances, their slots, - * and a few volunteers assigned to slots. + * Populates the core scheduling tables with a small, browsable dataset — 3 + * roles, 2 services, the next 2 Sundays of instances, their slots, and a few + * volunteers assigned to slots. + * + * NOT safe on a populated database: step 6 deletes every assignment before + * inserting the demo ones, and the demo services collide with real Sunday + * templates. It therefore refuses to run when non-seed users already exist + * unless SEED_FORCE=1 is set. The dev reset route truncates first, so it is + * unaffected. * * Run: pnpm db:seed (DATABASE_URL must point at the target DB) */ @@ -31,6 +36,16 @@ import { localToUtc, nextSundaysInTz } from "../lib/tz"; const CHURCH_TZ = "America/New_York"; +/** Accounts this script owns; anything else means the DB has real data. */ +const SEED_EMAILS = new Set([ + "admin@glenloch.local", + "coordinator@glenloch.local", + "jordan@glenloch.local", + "avery@glenloch.local", + "sam@glenloch.local", + "riley@glenloch.local", +]); + // Tables the seed owns. Truncating these (and cascading) gives a clean slate // for E2E resets without touching schema/migration tables. const RESET_TABLES = [ @@ -60,6 +75,16 @@ const RESET_TABLES = [ /** Populate the core scheduling tables (idempotent: ON CONFLICT DO NOTHING). */ export async function seed() { + const existingUsers = await db.select({ email: users.email }).from(users); + const foreignUsers = existingUsers.filter((u) => !SEED_EMAILS.has(u.email)); + if (foreignUsers.length > 0 && process.env.SEED_FORCE !== "1") { + throw new Error( + `seed: refusing to run on a database with ${foreignUsers.length} non-seed user(s) ` + + `(${foreignUsers.map((u) => u.email).join(", ")}). ` + + "This script rewrites demo services and assignments; set SEED_FORCE=1 only on a throwaway database.", + ); + } + /* 1. Roles — production positions (audio, livestream, slides) */ const roleRows: (typeof roles.$inferInsert)[] = [ {