#!/bin/sh set -e echo "[entrypoint] waiting for database..." node <<'NODE' const { Pool } = require('pg'); const url = process.env.DATABASE_URL; if (!url) { console.error('DATABASE_URL is required'); process.exit(1); } (async () => { for (let i = 0; i < 60; i++) { const pool = new Pool({ connectionString: url }); try { await pool.query('SELECT 1'); await pool.end(); process.exit(0); } catch { await pool.end().catch(() => undefined); await new Promise((r) => setTimeout(r, 2000)); } } console.error('database not ready after 120s'); process.exit(1); })(); NODE # 默认不灌演示/E2E 测试数据(migrate.js 判断值是否 === '1')。 # 如需在本地/演示环境灌数据,显式设置 SKIP_DEMO_SEED=0 SKIP_E2E_SEED=0 覆盖。 export SKIP_DEMO_SEED="${SKIP_DEMO_SEED:-1}" export SKIP_E2E_SEED="${SKIP_E2E_SEED:-1}" # OPS-6:迁移可开关(默认 1 保持现状);0 = 跳过(多副本部署时只让一个实例跑迁移) RUN_MIGRATIONS="${RUN_MIGRATIONS:-1}" if [ "$RUN_MIGRATIONS" = "1" ]; then echo "[entrypoint] running migrations..." if ! node dist/migrate.js; then echo "[entrypoint] ERROR: 数据库迁移失败,API 不会启动。" >&2 echo "[entrypoint] 请先备份数据库(pg_dump)并检查 migrations/ 与 schema_migrations 后重试;" >&2 echo "[entrypoint] 本脚本不自动备份/自动回滚(避免给出假的安全感)。" >&2 exit 1 fi else echo "[entrypoint] skip migrations (RUN_MIGRATIONS=$RUN_MIGRATIONS)" fi echo "[entrypoint] starting API..." exec "$@"