Refactor database URI handling and improve error messages in scheduler scripts

This commit is contained in:
Théophile Diot 2024-12-23 17:18:20 +01:00
parent d4603b91f2
commit d46605bf18
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
2 changed files with 11 additions and 7 deletions

View file

@ -57,6 +57,7 @@ function start() {
# Extract and validate database type
DATABASE_URI=${DATABASE_URI:-sqlite:////var/lib/bunkerweb/db.sqlite3}
export DATABASE_URI
DATABASE=$(echo "$DATABASE_URI" | awk -F: '{print $1}' | awk -F+ '{print $1}')
# Validate database type with case-insensitive comparison
@ -95,15 +96,16 @@ from logger import setup_logger
LOGGER = setup_logger('Scheduler', getenv('CUSTOM_LOG_LEVEL', getenv('LOG_LEVEL', 'INFO')))
engine = Database(LOGGER, '${DATABASE_URI}').sql_engine
engine = Database(LOGGER).sql_engine
with engine.connect() as conn:
try:
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):
print('none')
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")

View file

@ -43,6 +43,7 @@ cd /usr/share/bunkerweb/db/alembic || {
# Extract and validate database type
DATABASE_URI=${DATABASE_URI:-sqlite:////var/lib/bunkerweb/db.sqlite3}
export DATABASE_URI
DATABASE=$(echo "$DATABASE_URI" | awk -F: '{print $1}' | awk -F+ '{print $1}')
# Validate database type with case-insensitive comparison
@ -80,15 +81,16 @@ from logger import setup_logger
LOGGER = setup_logger('Scheduler', getenv('CUSTOM_LOG_LEVEL', getenv('LOG_LEVEL', 'INFO')))
engine = Database(LOGGER, '${DATABASE_URI}').sql_engine
engine = Database(LOGGER).sql_engine
with engine.connect() as conn:
try:
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):
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')
print('${installed_version}')
else:
print('${installed_version}')
")
if [ "$current_version" == "none" ]; then