from sqlalchemy import Column, BigInteger, String, DateTime, Enum as SQLEnum, func, Index from enum import Enum from app.db.session import Base class MovementType(str, Enum): TAKEOFF = "TAKEOFF" # Aircraft becomes airborne LANDING = "LANDING" # Aircraft touches down OVERFLIGHT = "OVERFLIGHT" # Aircraft passes through airspace (e.g., on call or QSY) GO_AROUND = "GO_AROUND" # Aircraft aborts landing and goes around TOUCH_AND_GO = "TOUCH_AND_GO" # Aircraft lands and immediately takes off again class Movement(Base): __tablename__ = "movements" id = Column(BigInteger, primary_key=True, autoincrement=True) movement_type = Column(SQLEnum(MovementType), nullable=False, index=True) aircraft_registration = Column(String(16), nullable=False, index=True) aircraft_type = Column(String(32), nullable=True) callsign = Column(String(16), nullable=True) timestamp = Column(DateTime, nullable=False, index=True) # Exact time of movement entity_type = Column(String(50), nullable=False, index=True) # PPR, LOCAL_FLIGHT, ARRIVAL, DEPARTURE, OVERFLIGHT entity_id = Column(BigInteger, nullable=False, index=True) # ID of the associated flight record to_location = Column(String(64), nullable=True) # Destination (TO) - populated based on movement type from_location = Column(String(64), nullable=True) # Origin (FROM) - populated based on movement type runway = Column(String(10), nullable=True) # Runway used (e.g., "10", "28", "04", "22") wind = Column(String(20), nullable=True) # Wind speed/direction (e.g., "280/25") pressure_setting = Column(String(20), nullable=True) # Pressure setting (e.g., "QNH1024", "QFE1013") created_by = Column(String(16), nullable=True, index=True) # User who triggered the movement ip_address = Column(String(45), nullable=True) # For audit notes = Column(String(255), nullable=True) # Optional context (e.g., runway used) created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp()) # Composite index for efficient queries __table_args__ = ( Index('idx_movement_lookup', 'entity_type', 'entity_id'), Index('idx_movement_time', 'timestamp', 'movement_type'), )