From e204ba4b2b5b6e253973147a6cc4d27e3b981594 Mon Sep 17 00:00:00 2001 From: AlexeerCT Date: Fri, 24 Apr 2026 14:21:26 -0500 Subject: [PATCH] Refactor Alembic upgrade command in Jenkinsfile for improved execution - Updated the Alembic upgrade command to use a Python script for execution, addressing issues with configuration handling in Docker. - Enhanced comments to clarify the purpose of the changes and their implications for E2E testing. --- Jenkinsfile | 5 ++--- backend/e2e_alembic_upgrade.py | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 backend/e2e_alembic_upgrade.py diff --git a/Jenkinsfile b/Jenkinsfile index 8315282e..5ff65cbd 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -284,9 +284,8 @@ pipeline { e2e_compose_fail_logs exit 1 fi - # Binario /usr/local/bin/alembic: sin python3 -m (el -c rompe; log con -c = build sin el último commit) - # --config=/ruta evita un token que empiece con - (algunos compose run lo mezclan con sus flags) - if ! e2e_compose run --rm --no-deps --entrypoint /usr/local/bin/alembic backend --config=/app/alembic.ini upgrade head; then + # e2e_alembic_upgrade.py: carga /app/alembic.ini en la API; la CLI a veces queda con config vacío bajo compose (No 'script_location') + if ! e2e_compose run --rm --no-deps --entrypoint /usr/local/bin/python3 backend /app/e2e_alembic_upgrade.py; then echo "ERROR: falló alembic upgrade head (job previo al stack completo)" e2e_compose_fail_logs exit 1 diff --git a/backend/e2e_alembic_upgrade.py b/backend/e2e_alembic_upgrade.py new file mode 100644 index 00000000..3aa2e58e --- /dev/null +++ b/backend/e2e_alembic_upgrade.py @@ -0,0 +1,38 @@ +""" +E2E/Jenkins: aplica migraciones sin la CLI de Alembic. + +docker compose run … alembic --config=… a veces deja un Config vacío (No 'script_location'); +aquí se carga explícitamente /app/alembic.ini y se llama a command.upgrade(). +""" +from __future__ import annotations + +import os +import sys +from pathlib import Path + + +def main() -> int: + root = Path("/app") + if not root.is_dir(): + print("ERROR: /app no es un directorio (volumen backend).", file=sys.stderr) + return 1 + os.chdir(root) + sys.path.insert(0, str(root)) + + ini = root / "alembic.ini" + if not ini.is_file(): + print( + f"ERROR: {ini} no existe. Comprueba el bind ./backend:./app en el agente.", + file=sys.stderr, + ) + return 1 + + from alembic.config import Config + from alembic import command + + command.upgrade(Config(str(ini)), "head") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())