fix: harden container startup and user admin migration
This commit is contained in:
+17
-11
@@ -1,23 +1,29 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Start the Flask application with better error reporting
|
# Start the Flask application with better error reporting.
|
||||||
echo "Starting Flask application..."
|
echo "Starting Flask application..."
|
||||||
export PYTHONUNBUFFERED=1
|
export PYTHONUNBUFFERED=1
|
||||||
export FLASK_DEBUG=1
|
: "${FLASK_DEBUG:=0}"
|
||||||
echo "Flask environment: $FLASK_ENV"
|
echo "Flask environment: $FLASK_ENV"
|
||||||
echo "Database URI: $SQLALCHEMY_DATABASE_URI"
|
echo "Database URI: $SQLALCHEMY_DATABASE_URI"
|
||||||
echo "Database path: $DATABASE_PATH"
|
echo "Database path: $DATABASE_PATH"
|
||||||
echo "Available environment variables:"
|
|
||||||
env | grep -v PASSWORD | grep -v SECRET
|
|
||||||
|
|
||||||
# Use flask run with explicit reload for better hot reloading
|
if [ "${LOG_ENV:-0}" = "1" ]; then
|
||||||
|
echo "Available non-sensitive environment variables:"
|
||||||
|
env | grep -Evi '(PASSWORD|PASS|SECRET|TOKEN|KEY|AUTH|CREDENTIAL|PRIVATE)'
|
||||||
|
fi
|
||||||
|
|
||||||
export PYTHONFAULTHANDLER=1
|
export PYTHONFAULTHANDLER=1
|
||||||
export PYTHONDONTWRITEBYTECODE=1
|
export PYTHONDONTWRITEBYTECODE=1
|
||||||
|
|
||||||
echo "Starting Flask development server with hot reload..."
|
case "${FLASK_DEBUG,,}" in
|
||||||
exec python -m flask run --host=0.0.0.0 --port=5000 --reload --debug || {
|
1|true|yes|on)
|
||||||
echo "Flask application failed to start. Error details:"
|
echo "Starting Flask development server with hot reload..."
|
||||||
python -c "import traceback; traceback.print_exc()"
|
exec python -m flask run --host=0.0.0.0 --port=5000 --reload --debug
|
||||||
exit 1
|
;;
|
||||||
}
|
*)
|
||||||
|
echo "Starting Flask application server..."
|
||||||
|
exec python run.py
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
"""
|
||||||
|
Migration script to add the is_admin flag expected by the current User model.
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from sqlalchemy import inspect, text
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def run_migration():
|
||||||
|
"""
|
||||||
|
Add user.is_admin and backfill it from the existing admin role assignment.
|
||||||
|
Returns:
|
||||||
|
- True: if changes were made successfully
|
||||||
|
- None: if no changes were needed
|
||||||
|
- False: if errors occurred
|
||||||
|
"""
|
||||||
|
from musicround import db
|
||||||
|
|
||||||
|
try:
|
||||||
|
inspector = inspect(db.engine)
|
||||||
|
existing_columns = [column["name"] for column in inspector.get_columns("user")]
|
||||||
|
|
||||||
|
if "is_admin" in existing_columns:
|
||||||
|
logger.info("is_admin column already exists")
|
||||||
|
return None
|
||||||
|
|
||||||
|
with db.engine.connect() as conn:
|
||||||
|
logger.info("Adding is_admin column")
|
||||||
|
conn.execute(text('ALTER TABLE "user" ADD COLUMN is_admin BOOLEAN DEFAULT 0'))
|
||||||
|
conn.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
UPDATE "user"
|
||||||
|
SET is_admin = 1
|
||||||
|
WHERE id IN (
|
||||||
|
SELECT ur.user_id
|
||||||
|
FROM user_roles ur
|
||||||
|
JOIN role r ON r.id = ur.role_id
|
||||||
|
WHERE lower(r.name) = 'admin'
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
logger.info("Added is_admin column")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Migration add_user_is_admin failed: {str(e)}")
|
||||||
|
return False
|
||||||
Reference in New Issue
Block a user