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 "$@"