From 33ff6473b620ceb42ad0783d8ee5f2291947fe22 Mon Sep 17 00:00:00 2001 From: Hamed Valiollahi Bayeki Date: Mon, 2 Dec 2024 15:55:59 -0800 Subject: [PATCH] feat: prevent backend startup on Alembic migration conflicts --- backend/lcfs/prestart.sh | 12 +++++++++--- backend/lcfs/start.sh | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/lcfs/prestart.sh b/backend/lcfs/prestart.sh index 952f220c6..3fe3f9cda 100755 --- a/backend/lcfs/prestart.sh +++ b/backend/lcfs/prestart.sh @@ -1,6 +1,13 @@ #!/usr/bin/env bash -echo "running prestart.sh from $(pwd)" +echo "Running prestart.sh from $(pwd)" + +# Check for Alembic head conflicts +HEAD_COUNT=$(poetry run alembic heads | wc -l) +if [ "$HEAD_COUNT" -gt 1 ]; then + echo "Alembic head conflict detected: Multiple migration heads present." + exit 1 +fi # Apply base database migrations echo "Applying base migrations." @@ -23,5 +30,4 @@ if [ $? -ne 0 ]; then fi echo "Migrations and seeding completed successfully." - -echo "done running prestart.sh from $(pwd)" +echo "Done running prestart.sh from $(pwd)" diff --git a/backend/lcfs/start.sh b/backend/lcfs/start.sh index b84880611..7e19eb1ef 100755 --- a/backend/lcfs/start.sh +++ b/backend/lcfs/start.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# Enable strict error handling +set -e + # Wait for the database to be ready ./wait-for-it.sh $LCFS_DB_HOST:5432 --timeout=30