#!/bin/sh # Production entrypoint for the backend: applies the migrations and starts the # server in the same container. # # This is POSIX sh, not bash: the image is alpine and does not ship bash # installed (adding it would be ~10MB for a shebang). # # Set RUN_MIGRATIONS=false to start without touching the database (for example, # a database already migrated by hand, or one managed outside this deployment). 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 # The database may still be starting up when this container is already # running, and a deploy usually overlaps a Postgres restart. 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 so that Bun becomes PID 1 and receives the SIGTERM from `docker stop` # (the server uses it to close Bun.serve and disconnect Prisma). exec "$@"