local-flights #5

Merged
jamesp merged 37 commits from local-flights into main 2025-12-20 12:29:32 -05:00
3 changed files with 33 additions and 15 deletions
Showing only changes of commit 8513a7bb0f - Show all commits

View File

@@ -11,7 +11,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from app.core.config import settings
from app.db.session import 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
# access to the values within the .ini file in use.

View File

@@ -27,8 +27,18 @@ def upgrade() -> None:
"""
# Modify existing journal table to support all entity types
# First add new columns
# First add new columns (check if they don't already exist)
from sqlalchemy import inspect, text
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
@@ -36,17 +46,19 @@ def upgrade() -> None:
UPDATE journal SET
entity_type = 'PPR',
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
op.alter_column('journal', 'entity_type', nullable=False)
op.alter_column('journal', 'entity_id', nullable=False)
op.alter_column('journal', 'entity_type', existing_type=sa.String(50), 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)
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
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

View File

@@ -125,17 +125,23 @@ elif [ $DB_STATE -eq 0 ]; then
echo "Checking for pending migrations..."
cd /app
# Get current and head revisions
CURRENT=$(alembic current 2>/dev/null | grep -o '[a-f0-9]\{12\}' | head -1 || echo "none")
HEAD=$(alembic heads 2>/dev/null | grep -o '[a-f0-9]\{12\}' | head -1 || echo "none")
# Get current and head revisions (handle both hash and named revisions)
CURRENT=$(alembic current 2>/dev/null | tail -1 | awk '{print $NF}' || 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
echo "✓ Pending migrations detected"
echo " Current: $CURRENT"
echo " Target: $HEAD"
echo "Applying migrations..."
alembic upgrade head
echo "✓ Migrations applied"
if [ $? -eq 0 ]; then
echo "✓ Migrations applied successfully"
else
echo "✗ Migration failed"
exit 1
fi
else
echo "✓ Database is up to date"
fi