Merge db-migrations: Add Flask-Migrate support and clean up old migration system

This commit is contained in:
2025-07-13 12:17:20 +02:00
parent 7140aeba41
commit 1500b2cf88
65 changed files with 2153 additions and 7881 deletions

View File

@@ -11,26 +11,66 @@ while ! pg_isready -h db -p 5432 -U "$POSTGRES_USER" > /dev/null 2>&1; do
done
echo "PostgreSQL is ready!"
# Initialize database tables if they don't exist
echo "Ensuring database tables exist..."
python -c "
# Run Flask-Migrate migrations
echo ""
echo "=== Running Database Migrations ==="
export FLASK_APP=app.py
# Check if migrations directory exists
if [ -d "migrations" ]; then
echo "Applying database migrations..."
flask db upgrade
if [ $? -ne 0 ]; then
echo "❌ Migration failed! Check the logs above."
exit 1
fi
echo "✅ Database migrations completed successfully"
else
echo "⚠️ No migrations directory found. Initializing Flask-Migrate..."
# Use Docker-friendly initialization (no Git required)
python docker_migrate_init.py
if [ $? -ne 0 ]; then
echo "❌ Migration initialization failed!"
exit 1
fi
# Check if database has existing tables
python -c "
from app import app, db
with app.app_context():
db.create_all()
print('Database tables created/verified')
"
# Run PostgreSQL-only migrations
echo ""
echo "=== Running PostgreSQL Migrations ==="
if [ -f "migrations/run_postgres_migrations.py" ]; then
echo "Applying PostgreSQL schema updates..."
python migrations/run_postgres_migrations.py
if [ $? -ne 0 ]; then
echo "⚠️ Some migrations failed, but continuing..."
inspector = db.inspect(db.engine)
tables = [t for t in inspector.get_table_names() if t != 'alembic_version']
if tables:
print('has_tables')
" > /tmp/db_check.txt
if grep -q "has_tables" /tmp/db_check.txt 2>/dev/null; then
echo "📊 Existing database detected. Marking as current..."
flask db stamp head
echo "✅ Database marked as current"
else
echo "🆕 Empty database detected. Creating tables..."
flask db upgrade
echo "✅ Database tables created"
fi
else
echo "PostgreSQL migration runner not found, skipping..."
rm -f /tmp/db_check.txt
fi
# Sync PostgreSQL enums with Python models
echo ""
echo "=== Syncing PostgreSQL Enums ==="
python sync_postgres_enums.py
if [ $? -ne 0 ]; then
echo "⚠️ Enum sync failed, but continuing..."
fi
# Legacy migration support (can be removed after full transition)
if [ -f "migrations_old/run_postgres_migrations.py" ]; then
echo ""
echo "=== Checking Legacy Migrations ==="
echo "Found old migration system. Consider removing after confirming Flask-Migrate is working."
fi
# Start the Flask application with gunicorn