fix(seed): never run the demo seed as part of a deploy
CI / build (push) Has been cancelled

The migrate task chained db:seed, so every production deploy inserted demo
users, duplicate Sunday templates and 45 fake assignments, and its unconditional
assignment delete wiped real schedule data. Migrations now run alone, and the
seed aborts when the database already holds non-seed users unless SEED_FORCE=1
is set for a throwaway database.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-22 21:34:02 +00:00
parent ade21425d4
commit f71952f838
3 changed files with 35 additions and 7 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ RUN pnpm build
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
FROM builder AS tools FROM builder AS tools
ENV NODE_ENV=development ENV NODE_ENV=development
CMD ["sh", "-c", "pnpm db:migrate && pnpm db:seed"] CMD ["pnpm", "db:migrate"]
FROM base AS runner FROM base AS runner
WORKDIR /app WORKDIR /app
+5 -2
View File
@@ -159,14 +159,17 @@ services:
retries: 10 retries: 10
restart: unless-stopped restart: unless-stopped
# One-shot migrations + seed. Run on demand: # One-shot migrations. Run on demand:
# docker compose --profile tools run --rm migrate # 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: migrate:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
target: tools target: tools
command: ["sh", "-c", "pnpm db:migrate && pnpm db:seed"] command: ["pnpm", "db:migrate"]
environment: environment:
DATABASE_URL: postgres://${POSTGRES_USER:-media}:${POSTGRES_PASSWORD:-media}@db:5432/${POSTGRES_DB:-media} DATABASE_URL: postgres://${POSTGRES_USER:-media}:${POSTGRES_PASSWORD:-media}@db:5432/${POSTGRES_DB:-media}
depends_on: depends_on:
+29 -4
View File
@@ -1,10 +1,15 @@
/** /**
* Seed script for Media Team Hub (Phase A2). * Seed script for Media Team Hub (Phase A2).
* *
* Idempotent: every insert uses ON CONFLICT DO NOTHING, so it is safe to run * Populates the core scheduling tables with a small, browsable dataset — 3
* repeatedly. Populates the core scheduling tables with a small, browsable * roles, 2 services, the next 2 Sundays of instances, their slots, and a few
* dataset — 3 roles, 2 services, the next 2 Sundays of instances, their slots, * volunteers assigned to 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) * 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"; 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 // Tables the seed owns. Truncating these (and cascading) gives a clean slate
// for E2E resets without touching schema/migration tables. // for E2E resets without touching schema/migration tables.
const RESET_TABLES = [ const RESET_TABLES = [
@@ -60,6 +75,16 @@ const RESET_TABLES = [
/** Populate the core scheduling tables (idempotent: ON CONFLICT DO NOTHING). */ /** Populate the core scheduling tables (idempotent: ON CONFLICT DO NOTHING). */
export async function seed() { 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) */ /* 1. Roles — production positions (audio, livestream, slides) */
const roleRows: (typeof roles.$inferInsert)[] = [ const roleRows: (typeof roles.$inferInsert)[] = [
{ {