From 56f055c0b6722df2907215d8d03d713d2185ee63 Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Mon, 23 Jan 2023 20:26:17 -0500 Subject: [PATCH] Add ability to auto-generate alembic migrations We move to a slightly more general model where we can support specific Alembic operations passed as arguments. This was very useful to easily run the auto-generated migrations. --- jenkins/Pipeline.gy | 2 +- ...igrations-check => run-alembic-migrations} | 2 +- lib/pbench/server/database/alembic.check | 10 ---------- lib/pbench/server/database/alembic.migration | 19 +++++++++++++++++++ tox.ini | 4 ++-- 5 files changed, 23 insertions(+), 14 deletions(-) rename jenkins/{run-alembic-migrations-check => run-alembic-migrations} (96%) delete mode 100755 lib/pbench/server/database/alembic.check create mode 100755 lib/pbench/server/database/alembic.migration diff --git a/jenkins/Pipeline.gy b/jenkins/Pipeline.gy index 328a56ce7d..d4d7b7805e 100644 --- a/jenkins/Pipeline.gy +++ b/jenkins/Pipeline.gy @@ -28,7 +28,7 @@ pipeline { stage('Server Alembic Migrations Check') { steps { echo 'Verify alembic migrations cover latest database schema' - sh 'jenkins/run-alembic-migrations-check' + sh 'jenkins/run-alembic-migrations check' } } stage('Linting, Unit Tests, RPM builds') { diff --git a/jenkins/run-alembic-migrations-check b/jenkins/run-alembic-migrations similarity index 96% rename from jenkins/run-alembic-migrations-check rename to jenkins/run-alembic-migrations index 38df42e846..8da070c771 100755 --- a/jenkins/run-alembic-migrations-check +++ b/jenkins/run-alembic-migrations @@ -34,4 +34,4 @@ timeout 60s bash -c 'until nc -z localhost 5432; do done' echo "Starting tox" >&2 -EXTRA_PODMAN_SWITCHES="${EXTRA_PODMAN_SWITCHES} --network host" jenkins/run tox -e alembic-check +EXTRA_PODMAN_SWITCHES="${EXTRA_PODMAN_SWITCHES} --network host" jenkins/run tox -e alembic-migration -- ${1:-check} diff --git a/lib/pbench/server/database/alembic.check b/lib/pbench/server/database/alembic.check deleted file mode 100755 index f59f6455dd..0000000000 --- a/lib/pbench/server/database/alembic.check +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -e - -cd lib/pbench/server/database - -# First we run all our migrations to bring the blank database up to speed. -alembic upgrade head - -# Then we check to see if we have any model changes not captured in existing -# migrations. -alembic check diff --git a/lib/pbench/server/database/alembic.migration b/lib/pbench/server/database/alembic.migration new file mode 100755 index 0000000000..cd0891306d --- /dev/null +++ b/lib/pbench/server/database/alembic.migration @@ -0,0 +1,19 @@ +#!/bin/bash -e + +cd lib/pbench/server/database + +# First we run all our migrations to bring the blank database up to speed. +alembic upgrade head + +if [[ "${1}" == "check" ]]; then + # We have been asked to check to see if there are any model changes not + # captured in existing migrations. + alembic check +elif [[ "${1}" == "create" ]]; then + # We have been asked to auto-generate a migration based on the existing + # model compared against the most recent migration "head". + alembic revision --autogenerate +else + printf "Unsupported operation requested, '%s'\n" "${1}" >&2 + exit 1 +fi diff --git a/tox.ini b/tox.ini index 1c71353255..fe242a78a3 100644 --- a/tox.ini +++ b/tox.ini @@ -41,9 +41,9 @@ deps = -r{toxinidir}/agent/requirements.txt -r{toxinidir}/agent/test-requirements.txt -[testenv:alembic-check] +[testenv:alembic-migration] description = Verify alembic migrations cover latest database schema deps = -r{toxinidir}/server/requirements.txt commands = - bash -c "{toxinidir}/lib/pbench/server/database/alembic.check" + bash -c "{toxinidir}/lib/pbench/server/database/alembic.migration {posargs}"