From d9f507c2402ce7900a1ada89a93741e35993cf4c Mon Sep 17 00:00:00 2001 From: umamaheswar <40281735+Skyrider3@users.noreply.github.com> Date: Wed, 17 Jun 2026 22:48:10 -0400 Subject: [PATCH] fix: make documented local-dev setup work without Docker The README "Local development (without Docker)" flow could not complete on a fresh checkout: - migrate.ts resolved migrations from process.cwd()/db/migrations, but the SQL files live at src/db/migrations and `npm run migrate` runs from server/, so readdirSync threw ENOENT and no migrations applied. Resolve relative to this module first, falling back to the cwd layout the Docker image uses (unchanged: tsc does not emit .sql into dist/, so the container still uses /app/db/migrations). - .env.example defined no DATABASE_URL, which config.ts hard-requires (process.exit(1)), so `npm run dev`/`npm run migrate` exited FATAL on a copied .env. Add it; Docker Compose injects its own value, so this line only affects bare-metal runs. - seed.ts usage comment pointed at port 5433; docker-compose publishes Postgres on host port 5434. --- .env.example | 3 +++ server/src/db/migrate.ts | 14 +++++++++++--- server/src/scripts/seed.ts | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 6fa3bc1..87b7ce4 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,7 @@ POSTGRES_PASSWORD=host39-local +# Used for bare-metal local dev (npm run dev / npm run migrate). Docker Compose +# injects its own DATABASE_URL (@db:5432), so this value is ignored under Docker. +DATABASE_URL=postgresql://host39:host39-local@localhost:5434/host39 JWT_SECRET=host39-dev-secret JWT_EXPIRES_IN=7d FRONTEND_URL=http://localhost:3002 diff --git a/server/src/db/migrate.ts b/server/src/db/migrate.ts index 84e47bc..d7bff46 100644 --- a/server/src/db/migrate.ts +++ b/server/src/db/migrate.ts @@ -1,10 +1,11 @@ -import { readdirSync } from 'node:fs'; +import { readdirSync, existsSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; import path from 'node:path'; import postgres from 'postgres'; import { buildConfig } from '../config.js'; /** - * Applies pending *.sql files from db/migrations in lexical order. + * Applies pending *.sql files from the migrations directory in lexical order. * Tracks applied files in schema_migrations. Idempotent across runs. * Each file runs in its own transaction. */ @@ -20,7 +21,14 @@ async function migrate(): Promise { ) `; - const dir = path.resolve(process.cwd(), 'db/migrations'); + // Prefer migrations sitting next to this module (tsx running + // src/db/migrate.ts locally finds src/db/migrations). Fall back to the + // cwd-relative layout used by the Docker image, where the SQL files are + // copied to /app/db/migrations and the process runs from /app. + const moduleMigrations = path.join(path.dirname(fileURLToPath(import.meta.url)), 'migrations'); + const dir = existsSync(moduleMigrations) + ? moduleMigrations + : path.resolve(process.cwd(), 'db/migrations'); const files = readdirSync(dir) .filter((f) => f.endsWith('.sql')) .sort(); diff --git a/server/src/scripts/seed.ts b/server/src/scripts/seed.ts index 7c67be5..5717c45 100644 --- a/server/src/scripts/seed.ts +++ b/server/src/scripts/seed.ts @@ -3,7 +3,7 @@ * Wipes users + agent_cards and inserts 2 SMB + 2 personal test accounts. * * Usage: - * DATABASE_URL=postgresql://host39:host39-local@localhost:5433/host39 npx tsx src/scripts/seed.ts + * DATABASE_URL=postgresql://host39:host39-local@localhost:5434/host39 npx tsx src/scripts/seed.ts */ import bcrypt from 'bcryptjs';