Refactor database configuration update and migration logic in scheduler scripts

This commit is contained in:
Théophile Diot 2024-12-23 18:07:39 +01:00
parent 8d2a95b696
commit 4b2a853318
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
2 changed files with 86 additions and 90 deletions

View file

@ -73,21 +73,13 @@ function start() {
esac
# Update configuration files
if ! sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then
log "SYSTEMCTL" "❌" "Failed to update database URL in configuration"
exit 1
fi
if ! sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then
log "SYSTEMCTL" "❌" "Failed to update version locations in configuration"
exit 1
fi
# Check current version and stamp
log "SYSTEMCTL" "" "Checking database version..."
installed_version=$(cat /usr/share/bunkerweb/VERSION)
# Create temporary Python script
cat > /tmp/version_check.py << EOL
if sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then
if sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then
# Check current version and stamp
log "SYSTEMCTL" "" "Checking database version..."
installed_version=$(cat /usr/share/bunkerweb/VERSION)
# Create temporary Python script
cat > /tmp/version_check.py << EOL
import sqlalchemy as sa
from os import getenv
@ -102,45 +94,51 @@ with engine.connect() as conn:
result = conn.execute(sa.text('SELECT version FROM bw_metadata WHERE id = 1'))
print(next(result)[0])
except BaseException as e:
if 'doesn\'t exist' not in str(e) and 'no such table' not in str(e) and 'relation \"bw_metadata\" does not exist' not in str(e):
print('none')
else:
print('${installed_version}')
if 'doesn\'t exist' not in str(e) and 'no such table' not in str(e) and 'relation \"bw_metadata\" does not exist' not in str(e):
print('none')
else:
print('${installed_version}')
EOL
current_version=$(sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 /tmp/version_check.py")
rm -f /tmp/version_check.py
current_version=$(sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 /tmp/version_check.py")
rm -f /tmp/version_check.py
if [ "$current_version" == "none" ]; then
log "SYSTEMCTL" "❌" "Failed to retrieve database version"
exit 1
fi
if [ "$current_version" == "none" ]; then
log "SYSTEMCTL" "❌" "Failed to retrieve database version"
exit 1
fi
if [ "$current_version" != "$installed_version" ]; then
# Find the corresponding Alembic revision by scanning migration files
MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions"
NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_')
REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}')
if [ "$current_version" != "$installed_version" ]; then
# Find the corresponding Alembic revision by scanning migration files
MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions"
NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_')
REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}')
if [ -z "$REVISION" ]; then
log "SYSTEMCTL" "❌" "No migration file found for database version: $current_version"
exit 1
if [ -z "$REVISION" ]; then
log "SYSTEMCTL" "❌" "No migration file found for database version: $current_version"
exit 1
fi
# Stamp the database with the determined revision
if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic stamp \"$REVISION\""; then
log "SYSTEMCTL" "❌" "Failed to stamp database with revision: $REVISION"
exit 1
fi
# Run database migration
log "SYSTEMCTL" "" "Running database migration..."
if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic upgrade head"; then
log "SYSTEMCTL" "❌" "Database migration failed"
exit 1
fi
log "SYSTEMCTL" "✅" "Database migration completed successfully"
fi
else
log "ENTRYPOINT" "❌" "Failed to update version locations in configuration, migration aborted"
fi
# Stamp the database with the determined revision
if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic stamp \"$REVISION\""; then
log "SYSTEMCTL" "❌" "Failed to stamp database with revision: $REVISION"
exit 1
fi
# Run database migration
log "SYSTEMCTL" "" "Running database migration..."
if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic upgrade head"; then
log "SYSTEMCTL" "❌" "Database migration failed"
exit 1
fi
log "SYSTEMCTL" "✅" "Database migration completed successfully"
else
log "ENTRYPOINT" "❌" "Failed to update database URL in configuration, migration aborted"
fi
cd - > /dev/null || exit 1

View file

@ -59,20 +59,12 @@ case "$db_type" in
esac
# Update configuration files
if ! sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then
log "ENTRYPOINT" "❌" "Failed to update database URL in configuration"
exit 1
fi
if ! sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then
log "ENTRYPOINT" "❌" "Failed to update version locations in configuration"
exit 1
fi
# Check current version and stamp
log "ENTRYPOINT" "" "Checking database version..."
installed_version=$(cat /usr/share/bunkerweb/VERSION)
current_version=$(python3 -c "
if sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then
if sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then
# Check current version and stamp
log "ENTRYPOINT" "" "Checking database version..."
installed_version=$(cat /usr/share/bunkerweb/VERSION)
current_version=$(python3 -c "
import sqlalchemy as sa
from os import getenv
@ -91,38 +83,44 @@ with engine.connect() as conn:
print('none')
else:
print('${installed_version}')
")
")
if [ "$current_version" == "none" ]; then
log "ENTRYPOINT" "❌" "Failed to retrieve database version"
exit 1
fi
if [ "$current_version" != "$installed_version" ]; then
# Find the corresponding Alembic revision by scanning migration files
MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions"
NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_')
REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}')
if [ -z "$REVISION" ]; then
log "ENTRYPOINT" "❌" "No migration file found for database version: $current_version"
exit 1
fi
# Stamp the database with the determined revision
if ! python3 -m alembic stamp "$REVISION"; then
log "ENTRYPOINT" "❌" "Failed to stamp database with revision: $REVISION"
if [ "$current_version" == "none" ]; then
log "ENTRYPOINT" "❌" "Failed to retrieve database version"
exit 1
fi
fi
# Run database migration
log "ENTRYPOINT" "" "Running database migration..."
if ! python3 -m alembic upgrade head; then
log "ENTRYPOINT" "❌" "Database migration failed"
exit 1
fi
if [ "$current_version" != "$installed_version" ]; then
# Find the corresponding Alembic revision by scanning migration files
MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions"
NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_')
REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}')
log "ENTRYPOINT" "✅" "Database migration completed successfully"
if [ -z "$REVISION" ]; then
log "ENTRYPOINT" "❌" "No migration file found for database version: $current_version"
exit 1
fi
# Stamp the database with the determined revision
if ! python3 -m alembic stamp "$REVISION"; then
log "ENTRYPOINT" "❌" "Failed to stamp database with revision: $REVISION"
exit 1
fi
# Run database migration
log "ENTRYPOINT" "" "Running database migration..."
if ! python3 -m alembic upgrade head; then
log "ENTRYPOINT" "❌" "Database migration failed"
exit 1
fi
log "ENTRYPOINT" "✅" "Database migration completed successfully"
fi
else
log "ENTRYPOINT" "❌" "Failed to update version locations in configuration, migration aborted"
fi
else
log "ENTRYPOINT" "❌" "Failed to update database URL in configuration, migration aborted"
fi
cd - > /dev/null || exit 1