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';