diff --git a/document-service/doc-api/Dockerfile b/document-service/doc-api/Dockerfile index 3c4c34d9..1e70b133 100644 --- a/document-service/doc-api/Dockerfile +++ b/document-service/doc-api/Dockerfile @@ -85,6 +85,8 @@ USER web FROM development_build AS production_build COPY --chown=web:web . /code +RUN chmod +x /code/update_db.sh + # ENV PYTHONPATH=/opt/app-root/src # CMD gunicorn --bind 0.0.0.0:${PORT} --config /code/gunicorn_config.py wsgi:app diff --git a/document-service/doc-api/src/doc_api/__init__.py b/document-service/doc-api/src/doc_api/__init__.py index e7c4ec1d..8c7c3926 100644 --- a/document-service/doc-api/src/doc_api/__init__.py +++ b/document-service/doc-api/src/doc_api/__init__.py @@ -45,7 +45,10 @@ def create_app(service_environment=APP_RUNNING_ENVIRONMENT, **kwargs): db.init_app(app) Migrate(app, db) - if app.config.get("DEPLOYMENT_ENV", "") == "testing": # CI only run upgrade for unit testing. + + deployment_env = app.config.get("DEPLOYMENT_ENV", "") + + if deployment_env == "testing": # CI only run upgrade for unit testing. logger.info("Running migration upgrade.") with app.app_context(): upgrade(directory="migrations", revision="head", sql=False, tag=None) @@ -54,14 +57,16 @@ def create_app(service_environment=APP_RUNNING_ENVIRONMENT, **kwargs): logger.info("Finished migration upgrade.") else: logger.info("Logging, migrate set up.") - auth_service.init_app(app) - storage_service.init_app(app) - meta_endpoint.init_app(app) - ops_endpoint.init_app(app) - v1_endpoint.init_app(app) - queue_service.init_app(app) - - setup_jwt_manager(app, jwt) + + # Skip service initialization for migration jobs + if deployment_env != "migration": + auth_service.init_app(app) + storage_service.init_app(app) + meta_endpoint.init_app(app) + ops_endpoint.init_app(app) + v1_endpoint.init_app(app) + queue_service.init_app(app) + setup_jwt_manager(app, jwt) with app.app_context(): if app.config.get("CLOUDSQL_INSTANCE_CONNECTION_NAME"): # pragma: no cover diff --git a/document-service/doc-api/update_db.sh b/document-service/doc-api/update_db.sh index eae5ad29..3ffb87e5 100644 --- a/document-service/doc-api/update_db.sh +++ b/document-service/doc-api/update_db.sh @@ -3,6 +3,8 @@ COMMAND=${1:-upgrade} REVISION=${2:-} echo starting $COMMAND $REVISION export DEPLOYMENT_ENV=migration +cd /code export PYTHONPATH="$(pwd)/src:$PYTHONPATH" +export FLASK_APP=wsgi:app flask db $COMMAND $REVISION echo 'upgrade completed'