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:
+1
-1
@@ -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
|
||||
|
||||
+5
-2
@@ -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:
|
||||
|
||||
+29
-4
@@ -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)[] = [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user