feat: implement entrypoint script for backend to handle migrations and server startup

This commit is contained in:
Jose Selesan
2026-09-28 19:41:50 -03:00
parent 88c8b2ec90
commit 54f1c7ab34
6 changed files with 68 additions and 38 deletions

View File

@@ -0,0 +1,42 @@
#!/bin/sh
# Entrypoint de producción del backend: aplica las migraciones y levanta el
# servidor en el mismo contenedor.
#
# Es POSIX sh, no bash: la imagen es alpine y no trae bash instalado (agregarlo
# serían ~10MB por un shebang).
#
# Setear RUN_MIGRATIONS=false para arrancar sin tocar la base (por ejemplo, una
# base ya migrada a mano o una que se gestiona fuera de este despliegue).
set -e
: "${DATABASE_URL:?falta DATABASE_URL}"
if [ "${RUN_MIGRATIONS:-true}" = "false" ]; then
echo "migrate: omitido (RUN_MIGRATIONS=false)"
else
attempts="${MIGRATE_ATTEMPTS:-30}"
delay="${MIGRATE_RETRY_DELAY:-5}"
i=1
# La base puede seguir arrancando cuando este contenedor ya empezó, y un
# deploy suele convivir con un reinicio de Postgres.
while [ "$i" -le "$attempts" ]; do
if bun run db:deploy; then
echo "migrate: listo"
break
fi
if [ "$i" -eq "$attempts" ]; then
echo "migrate: fallo tras $attempts intentos; no se levanta el servidor" >&2
exit 1
fi
echo "migrate: intento $i/$attempts fallo; reintento en ${delay}s" >&2
i=$((i + 1))
sleep "$delay"
done
fi
# exec para que Bun quede como PID 1 y reciba el SIGTERM de `docker stop`
# (el servidor lo usa para cerrar Bun.serve y desconectar Prisma).
exec "$@"

View File

@@ -24,11 +24,13 @@
"@prisma/adapter-pg": "^7.10.0",
"@prisma/client": "^7.10.0",
"better-auth": "1.7.2",
"dotenv": "^16.4.5",
"hono": "^4.13.3",
"nanoid": "^3.3.19",
"nodemailer": "^9.0.6",
"pino": "^9.5.0",
"pino-pretty": "^13.0.0",
"prisma": "^7.10.0",
"zod": "3.24.2"
},
"devDependencies": {
@@ -37,8 +39,6 @@
"@types/bun": "^1.4.0",
"@types/node": "^20.17.0",
"@types/nodemailer": "^8.0.1",
"dotenv": "^16.4.5",
"prisma": "^7.10.0",
"typescript": "^5.7.0"
}
}