Skip to content

Add command "check-if-head" #1705

@sscherfke

Description

@sscherfke

Describe the use case
When multiple services/container use a database, only one of them should run the migrations. Assuming, that all services/containers are deployed at the same time, other services should not start, until the datatabase has been migrated to the head version. Currently, I use alembic current 2>/dev/null | grep --quiet '\(head\)' to perform this check. A dedicated command alembic check-if-main (or something like this) would make this easier and improve readability. The command would exit with 0 if the DB is at head or with 1 otherwise.

Optionally, this command could have a --retry SECONDS options.

Databases / Backends / Drivers targeted

all / independent of backend

Example Use

if [[ $RUN_MIGRATIONS = "1" ]] || [[ $RUN_MIGRATIONS = "true" ]]; then
    alembic upgrade head
else
    SLEEP=5
    until alembic current 2>/dev/null | grep --quiet '\(head\)'; do
        echo "Database migration version is not HEAD"
        sleep $SLEEP
    done
    echo "Database migrations are up-to-date"
fi

Additional context

Have a nice day!

Thanks. You, too! And thanks for this library. :-)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions