local-flights #5
@@ -11,7 +11,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.db.session import Base
|
from app.db.session import Base
|
||||||
# Import all models to ensure they are registered with Base
|
# Import all models to ensure they are registered with Base
|
||||||
from app.models.ppr import PPRRecord, User, Journal, Airport, Aircraft
|
from app.models.ppr import PPRRecord, User, Airport, Aircraft
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
|
|||||||
@@ -27,27 +27,39 @@ def upgrade() -> None:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Modify existing journal table to support all entity types
|
# Modify existing journal table to support all entity types
|
||||||
# First add new columns
|
# First add new columns (check if they don't already exist)
|
||||||
op.add_column('journal', sa.Column('entity_type', sa.String(50), nullable=True))
|
from sqlalchemy import inspect, text
|
||||||
op.add_column('journal', sa.Column('entity_id', sa.BigInteger(), nullable=True))
|
from alembic import op
|
||||||
|
|
||||||
|
# Get table columns to check if entity_type and entity_id already exist
|
||||||
|
connection = op.get_context().bind
|
||||||
|
inspector = inspect(connection)
|
||||||
|
columns = [col['name'] for col in inspector.get_columns('journal')]
|
||||||
|
|
||||||
|
if 'entity_type' not in columns:
|
||||||
|
op.add_column('journal', sa.Column('entity_type', sa.String(50), nullable=True))
|
||||||
|
if 'entity_id' not in columns:
|
||||||
|
op.add_column('journal', sa.Column('entity_id', sa.BigInteger(), nullable=True))
|
||||||
|
|
||||||
# Migrate existing PPR journal entries: backfill entity_type and entity_id
|
# Migrate existing PPR journal entries: backfill entity_type and entity_id
|
||||||
op.execute("""
|
op.execute("""
|
||||||
UPDATE journal SET
|
UPDATE journal SET
|
||||||
entity_type = 'PPR',
|
entity_type = 'PPR',
|
||||||
entity_id = ppr_id
|
entity_id = ppr_id
|
||||||
WHERE entity_type IS NULL
|
WHERE entity_type IS NULL AND ppr_id IS NOT NULL
|
||||||
""")
|
""")
|
||||||
|
|
||||||
# Make new columns NOT NULL after migration
|
# Make new columns NOT NULL after migration
|
||||||
op.alter_column('journal', 'entity_type', nullable=False)
|
op.alter_column('journal', 'entity_type', existing_type=sa.String(50), nullable=False)
|
||||||
op.alter_column('journal', 'entity_id', nullable=False)
|
op.alter_column('journal', 'entity_id', existing_type=sa.BigInteger(), nullable=False)
|
||||||
|
|
||||||
# Make ip column nullable (new entries won't always have it)
|
# Make ip column nullable (new entries won't always have it)
|
||||||
op.alter_column('journal', 'ip', existing_type=sa.String(45), nullable=True)
|
op.alter_column('journal', 'ip', existing_type=sa.String(45), nullable=True)
|
||||||
|
|
||||||
# Drop the old ppr_id column
|
# Drop the foreign key constraint before dropping the column
|
||||||
op.drop_column('journal', 'ppr_id')
|
if 'ppr_id' in columns:
|
||||||
|
op.drop_constraint('journal_ibfk_1', 'journal', type_='foreignkey')
|
||||||
|
op.drop_column('journal', 'ppr_id')
|
||||||
|
|
||||||
# Add composite index for efficient queries
|
# Add composite index for efficient queries
|
||||||
op.create_index('idx_entity_lookup', 'journal', ['entity_type', 'entity_id'])
|
op.create_index('idx_entity_lookup', 'journal', ['entity_type', 'entity_id'])
|
||||||
|
|||||||
@@ -125,17 +125,23 @@ elif [ $DB_STATE -eq 0 ]; then
|
|||||||
echo "Checking for pending migrations..."
|
echo "Checking for pending migrations..."
|
||||||
cd /app
|
cd /app
|
||||||
|
|
||||||
# Get current and head revisions
|
# Get current and head revisions (handle both hash and named revisions)
|
||||||
CURRENT=$(alembic current 2>/dev/null | grep -o '[a-f0-9]\{12\}' | head -1 || echo "none")
|
CURRENT=$(alembic current 2>/dev/null | tail -1 | awk '{print $NF}' || echo "none")
|
||||||
HEAD=$(alembic heads 2>/dev/null | grep -o '[a-f0-9]\{12\}' | head -1 || echo "none")
|
HEAD=$(alembic heads 2>/dev/null | tail -1 | awk '{print $NF}' || echo "none")
|
||||||
|
|
||||||
|
echo " Current: $CURRENT"
|
||||||
|
echo " Target: $HEAD"
|
||||||
|
|
||||||
if [ "$CURRENT" != "$HEAD" ] && [ "$HEAD" != "none" ]; then
|
if [ "$CURRENT" != "$HEAD" ] && [ "$HEAD" != "none" ]; then
|
||||||
echo "✓ Pending migrations detected"
|
echo "✓ Pending migrations detected"
|
||||||
echo " Current: $CURRENT"
|
|
||||||
echo " Target: $HEAD"
|
|
||||||
echo "Applying migrations..."
|
echo "Applying migrations..."
|
||||||
alembic upgrade head
|
alembic upgrade head
|
||||||
echo "✓ Migrations applied"
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "✓ Migrations applied successfully"
|
||||||
|
else
|
||||||
|
echo "✗ Migration failed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
echo "✓ Database is up to date"
|
echo "✓ Database is up to date"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user