Initial commit - production deployment
This commit is contained in:
141
services/external/migrations/env.py
vendored
Normal file
141
services/external/migrations/env.py
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
"""Alembic environment configuration for external service"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
from alembic import context
|
||||
|
||||
# Add the service directory to the Python path
|
||||
service_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
if service_path not in sys.path:
|
||||
sys.path.insert(0, service_path)
|
||||
|
||||
# Add shared modules to path
|
||||
shared_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "shared"))
|
||||
if shared_path not in sys.path:
|
||||
sys.path.insert(0, shared_path)
|
||||
|
||||
try:
|
||||
from app.core.config import settings
|
||||
from shared.database.base import Base
|
||||
|
||||
# Import all models to ensure they are registered with Base.metadata
|
||||
from app.models import * # noqa: F401, F403
|
||||
|
||||
except ImportError as e:
|
||||
print(f"Import error in migrations env.py: {e}")
|
||||
print(f"Current Python path: {sys.path}")
|
||||
raise
|
||||
|
||||
# this is the Alembic Config object
|
||||
config = context.config
|
||||
|
||||
# Determine service name from file path
|
||||
service_name = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
|
||||
service_name_upper = service_name.upper().replace('-', '_')
|
||||
|
||||
# Set database URL from environment variables with multiple fallback strategies
|
||||
database_url = (
|
||||
os.getenv(f'{service_name_upper}_DATABASE_URL') or # Service-specific
|
||||
os.getenv('DATABASE_URL') # Generic fallback
|
||||
)
|
||||
|
||||
# If DATABASE_URL is not set, construct from individual components
|
||||
if not database_url:
|
||||
# Try generic PostgreSQL environment variables first
|
||||
postgres_host = os.getenv('POSTGRES_HOST')
|
||||
postgres_port = os.getenv('POSTGRES_PORT', '5432')
|
||||
postgres_db = os.getenv('POSTGRES_DB')
|
||||
postgres_user = os.getenv('POSTGRES_USER')
|
||||
postgres_password = os.getenv('POSTGRES_PASSWORD')
|
||||
|
||||
if all([postgres_host, postgres_db, postgres_user, postgres_password]):
|
||||
database_url = f"postgresql+asyncpg://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
|
||||
else:
|
||||
# Try service-specific environment variables
|
||||
db_host = os.getenv(f'{service_name_upper}_DB_HOST', f'{service_name}-db-service')
|
||||
db_port = os.getenv(f'{service_name_upper}_DB_PORT', '5432')
|
||||
db_name = os.getenv(f'{service_name_upper}_DB_NAME', f'{service_name.replace("-", "_")}_db')
|
||||
db_user = os.getenv(f'{service_name_upper}_DB_USER', f'{service_name.replace("-", "_")}_user')
|
||||
db_password = os.getenv(f'{service_name_upper}_DB_PASSWORD')
|
||||
|
||||
if db_password:
|
||||
database_url = f"postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
|
||||
else:
|
||||
# Final fallback: try to get from settings object
|
||||
try:
|
||||
database_url = getattr(settings, 'DATABASE_URL', None)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not database_url:
|
||||
error_msg = f"ERROR: No database URL configured for {service_name} service"
|
||||
print(error_msg)
|
||||
raise Exception(error_msg)
|
||||
|
||||
config.set_main_option("sqlalchemy.url", database_url)
|
||||
|
||||
# Interpret the config file for Python logging
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Set target metadata
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode."""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
compare_type=True,
|
||||
compare_server_default=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
"""Execute migrations with the given connection."""
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
compare_server_default=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
"""Run migrations in 'online' mode with async support."""
|
||||
connectable = async_engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
26
services/external/migrations/script.py.mako
vendored
Normal file
26
services/external/migrations/script.py.mako
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
464
services/external/migrations/versions/20251110_1900_unified_initial_schema.py
vendored
Normal file
464
services/external/migrations/versions/20251110_1900_unified_initial_schema.py
vendored
Normal file
@@ -0,0 +1,464 @@
|
||||
"""unified_initial_schema
|
||||
|
||||
Revision ID: 00001
|
||||
Revises:
|
||||
Create Date: 2025-11-10 19:00:00.000000+01:00
|
||||
|
||||
Complete unified initial schema for External Service including:
|
||||
- Weather data collection (weather_data, weather_forecasts, city_weather_data)
|
||||
- Traffic data collection (traffic_data, traffic_measurement_points, traffic_background_jobs, city_traffic_data)
|
||||
- School calendars and location context (school_calendars, tenant_location_contexts)
|
||||
- POI detection system (tenant_poi_contexts, poi_refresh_jobs)
|
||||
- Audit logging (audit_logs)
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '00001'
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Create all tables for External Service"""
|
||||
|
||||
# ============================================================================
|
||||
# AUDIT LOGS
|
||||
# ============================================================================
|
||||
op.create_table(
|
||||
'audit_logs',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('user_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('action', sa.String(length=100), nullable=False),
|
||||
sa.Column('resource_type', sa.String(length=100), nullable=False),
|
||||
sa.Column('resource_id', sa.String(length=255), nullable=True),
|
||||
sa.Column('severity', sa.String(length=20), nullable=False),
|
||||
sa.Column('service_name', sa.String(length=100), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('changes', JSONB, nullable=True),
|
||||
sa.Column('audit_metadata', JSONB, nullable=True),
|
||||
sa.Column('ip_address', sa.String(length=45), nullable=True),
|
||||
sa.Column('user_agent', sa.Text(), nullable=True),
|
||||
sa.Column('endpoint', sa.String(length=255), nullable=True),
|
||||
sa.Column('method', sa.String(length=10), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_audit_resource_type_action', 'audit_logs', ['resource_type', 'action'])
|
||||
op.create_index('idx_audit_service_created', 'audit_logs', ['service_name', 'created_at'])
|
||||
op.create_index('idx_audit_severity_created', 'audit_logs', ['severity', 'created_at'])
|
||||
op.create_index('idx_audit_tenant_created', 'audit_logs', ['tenant_id', 'created_at'])
|
||||
op.create_index('idx_audit_user_created', 'audit_logs', ['user_id', 'created_at'])
|
||||
op.create_index(op.f('ix_audit_logs_action'), 'audit_logs', ['action'])
|
||||
op.create_index(op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'])
|
||||
op.create_index(op.f('ix_audit_logs_resource_id'), 'audit_logs', ['resource_id'])
|
||||
op.create_index(op.f('ix_audit_logs_resource_type'), 'audit_logs', ['resource_type'])
|
||||
op.create_index(op.f('ix_audit_logs_service_name'), 'audit_logs', ['service_name'])
|
||||
op.create_index(op.f('ix_audit_logs_severity'), 'audit_logs', ['severity'])
|
||||
op.create_index(op.f('ix_audit_logs_tenant_id'), 'audit_logs', ['tenant_id'])
|
||||
op.create_index(op.f('ix_audit_logs_user_id'), 'audit_logs', ['user_id'])
|
||||
|
||||
# ============================================================================
|
||||
# WEATHER DATA
|
||||
# ============================================================================
|
||||
op.create_table(
|
||||
'city_weather_data',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('city_id', sa.String(length=50), nullable=False),
|
||||
sa.Column('date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('temperature', sa.Float(), nullable=True),
|
||||
sa.Column('precipitation', sa.Float(), nullable=True),
|
||||
sa.Column('humidity', sa.Float(), nullable=True),
|
||||
sa.Column('wind_speed', sa.Float(), nullable=True),
|
||||
sa.Column('pressure', sa.Float(), nullable=True),
|
||||
sa.Column('description', sa.String(length=200), nullable=True),
|
||||
sa.Column('source', sa.String(length=50), nullable=False),
|
||||
sa.Column('raw_data', JSONB, nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_city_weather_lookup', 'city_weather_data', ['city_id', 'date'])
|
||||
op.create_index(op.f('ix_city_weather_data_city_id'), 'city_weather_data', ['city_id'])
|
||||
op.create_index(op.f('ix_city_weather_data_date'), 'city_weather_data', ['date'])
|
||||
|
||||
op.create_table(
|
||||
'weather_data',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('location_id', sa.String(length=100), nullable=False),
|
||||
sa.Column('city', sa.String(length=50), nullable=False),
|
||||
sa.Column('station_name', sa.String(length=200), nullable=True),
|
||||
sa.Column('latitude', sa.Float(), nullable=True),
|
||||
sa.Column('longitude', sa.Float(), nullable=True),
|
||||
sa.Column('date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('forecast_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('temperature', sa.Float(), nullable=True),
|
||||
sa.Column('temperature_min', sa.Float(), nullable=True),
|
||||
sa.Column('temperature_max', sa.Float(), nullable=True),
|
||||
sa.Column('feels_like', sa.Float(), nullable=True),
|
||||
sa.Column('precipitation', sa.Float(), nullable=True),
|
||||
sa.Column('precipitation_probability', sa.Float(), nullable=True),
|
||||
sa.Column('humidity', sa.Float(), nullable=True),
|
||||
sa.Column('wind_speed', sa.Float(), nullable=True),
|
||||
sa.Column('wind_direction', sa.Float(), nullable=True),
|
||||
sa.Column('wind_gust', sa.Float(), nullable=True),
|
||||
sa.Column('pressure', sa.Float(), nullable=True),
|
||||
sa.Column('visibility', sa.Float(), nullable=True),
|
||||
sa.Column('uv_index', sa.Float(), nullable=True),
|
||||
sa.Column('cloud_cover', sa.Float(), nullable=True),
|
||||
sa.Column('condition', sa.String(length=100), nullable=True),
|
||||
sa.Column('description', sa.String(length=200), nullable=True),
|
||||
sa.Column('weather_code', sa.String(length=20), nullable=True),
|
||||
sa.Column('source', sa.String(length=50), nullable=False),
|
||||
sa.Column('data_type', sa.String(length=20), nullable=False),
|
||||
sa.Column('is_forecast', sa.Boolean(), nullable=True),
|
||||
sa.Column('data_quality_score', sa.Float(), nullable=True),
|
||||
sa.Column('raw_data', JSONB, nullable=True),
|
||||
sa.Column('processed_data', JSONB, nullable=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_weather_location_date', 'weather_data', ['location_id', 'date'])
|
||||
op.create_index(op.f('ix_weather_data_date'), 'weather_data', ['date'])
|
||||
op.create_index(op.f('ix_weather_data_location_id'), 'weather_data', ['location_id'])
|
||||
op.create_index(op.f('ix_weather_data_tenant_id'), 'weather_data', ['tenant_id'])
|
||||
|
||||
op.create_table(
|
||||
'weather_forecasts',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('location_id', sa.String(length=100), nullable=False),
|
||||
sa.Column('forecast_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('generated_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('temperature', sa.Float(), nullable=True),
|
||||
sa.Column('precipitation', sa.Float(), nullable=True),
|
||||
sa.Column('humidity', sa.Float(), nullable=True),
|
||||
sa.Column('wind_speed', sa.Float(), nullable=True),
|
||||
sa.Column('description', sa.String(length=200), nullable=True),
|
||||
sa.Column('source', sa.String(length=50), nullable=False),
|
||||
sa.Column('raw_data', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_forecast_location_date', 'weather_forecasts', ['location_id', 'forecast_date'])
|
||||
op.create_index(op.f('ix_weather_forecasts_location_id'), 'weather_forecasts', ['location_id'])
|
||||
|
||||
# ============================================================================
|
||||
# TRAFFIC DATA
|
||||
# ============================================================================
|
||||
op.create_table(
|
||||
'city_traffic_data',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('city_id', sa.String(length=50), nullable=False),
|
||||
sa.Column('date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('traffic_volume', sa.Integer(), nullable=True),
|
||||
sa.Column('pedestrian_count', sa.Integer(), nullable=True),
|
||||
sa.Column('congestion_level', sa.String(length=20), nullable=True),
|
||||
sa.Column('average_speed', sa.Float(), nullable=True),
|
||||
sa.Column('source', sa.String(length=50), nullable=False),
|
||||
sa.Column('raw_data', JSONB, nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_city_traffic_lookup', 'city_traffic_data', ['city_id', 'date'])
|
||||
op.create_index(op.f('ix_city_traffic_data_city_id'), 'city_traffic_data', ['city_id'])
|
||||
op.create_index(op.f('ix_city_traffic_data_date'), 'city_traffic_data', ['date'])
|
||||
|
||||
op.create_table(
|
||||
'traffic_measurement_points',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('city', sa.String(length=50), nullable=False),
|
||||
sa.Column('measurement_point_id', sa.String(length=100), nullable=False),
|
||||
sa.Column('name', sa.String(length=500), nullable=True),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('latitude', sa.Float(), nullable=False),
|
||||
sa.Column('longitude', sa.Float(), nullable=False),
|
||||
sa.Column('district', sa.String(length=100), nullable=True),
|
||||
sa.Column('zone', sa.String(length=100), nullable=True),
|
||||
sa.Column('road_type', sa.String(length=50), nullable=True),
|
||||
sa.Column('measurement_type', sa.String(length=50), nullable=True),
|
||||
sa.Column('point_category', sa.String(length=50), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||||
sa.Column('installation_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('last_data_received', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('data_quality_rating', sa.Float(), nullable=True),
|
||||
sa.Column('city_specific_metadata', JSONB, nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_points_active', 'traffic_measurement_points', ['city', 'is_active', 'last_data_received'])
|
||||
op.create_index('idx_points_city_location', 'traffic_measurement_points', ['city', 'latitude', 'longitude'])
|
||||
op.create_index('idx_points_district', 'traffic_measurement_points', ['city', 'district'])
|
||||
op.create_index('idx_points_road_type', 'traffic_measurement_points', ['city', 'road_type'])
|
||||
op.create_index('idx_unique_city_point', 'traffic_measurement_points', ['city', 'measurement_point_id'], unique=True)
|
||||
op.create_index(op.f('ix_traffic_measurement_points_city'), 'traffic_measurement_points', ['city'])
|
||||
op.create_index(op.f('ix_traffic_measurement_points_measurement_point_id'), 'traffic_measurement_points', ['measurement_point_id'])
|
||||
|
||||
op.create_table(
|
||||
'traffic_data',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('location_id', sa.String(length=100), nullable=False),
|
||||
sa.Column('city', sa.String(length=50), nullable=False),
|
||||
sa.Column('date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('traffic_volume', sa.Integer(), nullable=True),
|
||||
sa.Column('congestion_level', sa.String(length=20), nullable=True),
|
||||
sa.Column('average_speed', sa.Float(), nullable=True),
|
||||
sa.Column('occupation_percentage', sa.Float(), nullable=True),
|
||||
sa.Column('load_percentage', sa.Float(), nullable=True),
|
||||
sa.Column('pedestrian_count', sa.Integer(), nullable=True),
|
||||
sa.Column('measurement_point_id', sa.String(length=100), nullable=True),
|
||||
sa.Column('measurement_point_name', sa.String(length=500), nullable=True),
|
||||
sa.Column('measurement_point_type', sa.String(length=50), nullable=True),
|
||||
sa.Column('latitude', sa.Float(), nullable=True),
|
||||
sa.Column('longitude', sa.Float(), nullable=True),
|
||||
sa.Column('district', sa.String(length=100), nullable=True),
|
||||
sa.Column('zone', sa.String(length=100), nullable=True),
|
||||
sa.Column('source', sa.String(length=50), nullable=False),
|
||||
sa.Column('data_quality_score', sa.Float(), nullable=True),
|
||||
sa.Column('is_synthetic', sa.Boolean(), nullable=True),
|
||||
sa.Column('has_pedestrian_inference', sa.Boolean(), nullable=True),
|
||||
sa.Column('city_specific_data', JSONB, nullable=True),
|
||||
sa.Column('raw_data', sa.Text(), nullable=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_traffic_city_date', 'traffic_data', ['city', 'date'])
|
||||
op.create_index('idx_traffic_city_location', 'traffic_data', ['city', 'location_id'])
|
||||
op.create_index('idx_traffic_district_date', 'traffic_data', ['city', 'district', 'date'])
|
||||
op.create_index('idx_traffic_location_date', 'traffic_data', ['location_id', 'date'])
|
||||
op.create_index('idx_traffic_measurement_point', 'traffic_data', ['city', 'measurement_point_id'])
|
||||
op.create_index('idx_traffic_quality', 'traffic_data', ['city', 'data_quality_score', 'date'])
|
||||
op.create_index('idx_traffic_tenant_date', 'traffic_data', ['tenant_id', 'date'])
|
||||
op.create_index('idx_traffic_training', 'traffic_data', ['tenant_id', 'city', 'date', 'is_synthetic'])
|
||||
op.create_index(op.f('ix_traffic_data_city'), 'traffic_data', ['city'])
|
||||
op.create_index(op.f('ix_traffic_data_date'), 'traffic_data', ['date'])
|
||||
op.create_index(op.f('ix_traffic_data_location_id'), 'traffic_data', ['location_id'])
|
||||
op.create_index(op.f('ix_traffic_data_measurement_point_id'), 'traffic_data', ['measurement_point_id'])
|
||||
op.create_index(op.f('ix_traffic_data_tenant_id'), 'traffic_data', ['tenant_id'])
|
||||
|
||||
op.create_table(
|
||||
'traffic_background_jobs',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('job_type', sa.String(length=50), nullable=False),
|
||||
sa.Column('city', sa.String(length=50), nullable=False),
|
||||
sa.Column('location_pattern', sa.String(length=200), nullable=True),
|
||||
sa.Column('scheduled_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('status', sa.String(length=20), nullable=False),
|
||||
sa.Column('progress_percentage', sa.Float(), nullable=True),
|
||||
sa.Column('records_processed', sa.Integer(), nullable=True),
|
||||
sa.Column('records_stored', sa.Integer(), nullable=True),
|
||||
sa.Column('data_start_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('data_end_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('success_count', sa.Integer(), nullable=True),
|
||||
sa.Column('error_count', sa.Integer(), nullable=True),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('job_metadata', JSONB, nullable=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_jobs_city_status', 'traffic_background_jobs', ['city', 'status', 'scheduled_at'])
|
||||
op.create_index('idx_jobs_completed', 'traffic_background_jobs', ['status', 'completed_at'])
|
||||
op.create_index('idx_jobs_tenant_status', 'traffic_background_jobs', ['tenant_id', 'status', 'scheduled_at'])
|
||||
op.create_index('idx_jobs_type_city', 'traffic_background_jobs', ['job_type', 'city', 'scheduled_at'])
|
||||
op.create_index(op.f('ix_traffic_background_jobs_city'), 'traffic_background_jobs', ['city'])
|
||||
op.create_index(op.f('ix_traffic_background_jobs_tenant_id'), 'traffic_background_jobs', ['tenant_id'])
|
||||
|
||||
# ============================================================================
|
||||
# SCHOOL CALENDARS & LOCATION CONTEXT
|
||||
# ============================================================================
|
||||
op.create_table(
|
||||
'school_calendars',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('city_id', sa.String(length=50), nullable=False),
|
||||
sa.Column('calendar_name', sa.String(length=100), nullable=False),
|
||||
sa.Column('school_type', sa.String(length=20), nullable=False),
|
||||
sa.Column('academic_year', sa.String(length=10), nullable=False),
|
||||
sa.Column('holiday_periods', JSONB, nullable=False),
|
||||
sa.Column('school_hours', JSONB, nullable=False),
|
||||
sa.Column('source', sa.String(length=100), nullable=True),
|
||||
sa.Column('enabled', sa.Boolean(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_school_calendar_city_year', 'school_calendars', ['city_id', 'academic_year'])
|
||||
op.create_index('idx_school_calendar_city_type', 'school_calendars', ['city_id', 'school_type'])
|
||||
op.create_index(op.f('ix_school_calendars_city_id'), 'school_calendars', ['city_id'])
|
||||
|
||||
op.create_table(
|
||||
'tenant_location_contexts',
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('city_id', sa.String(length=50), nullable=False),
|
||||
sa.Column('school_calendar_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('neighborhood', sa.String(length=100), nullable=True),
|
||||
sa.Column('local_events', JSONB, nullable=True),
|
||||
sa.Column('notes', sa.String(length=500), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('tenant_id')
|
||||
)
|
||||
op.create_index('idx_tenant_location_calendar', 'tenant_location_contexts', ['school_calendar_id'])
|
||||
op.create_index(op.f('ix_tenant_location_contexts_city_id'), 'tenant_location_contexts', ['city_id'])
|
||||
|
||||
# ============================================================================
|
||||
# POI DETECTION SYSTEM
|
||||
# ============================================================================
|
||||
op.create_table(
|
||||
'tenant_poi_contexts',
|
||||
sa.Column('id', UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False, unique=True, index=True),
|
||||
sa.Column('latitude', sa.Float(), nullable=False),
|
||||
sa.Column('longitude', sa.Float(), nullable=False),
|
||||
sa.Column('poi_detection_results', JSONB, nullable=False, server_default='{}'),
|
||||
sa.Column('ml_features', JSONB, nullable=False, server_default='{}'),
|
||||
sa.Column('total_pois_detected', sa.Integer(), default=0),
|
||||
sa.Column('high_impact_categories', JSONB, server_default='[]'),
|
||||
sa.Column('relevant_categories', JSONB, server_default='[]'),
|
||||
sa.Column('detection_timestamp', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('detection_source', sa.String(50), default='overpass_api'),
|
||||
sa.Column('detection_status', sa.String(20), default='completed'),
|
||||
sa.Column('detection_error', sa.String(500), nullable=True),
|
||||
sa.Column('next_refresh_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('refresh_interval_days', sa.Integer(), default=180),
|
||||
sa.Column('last_refreshed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now())
|
||||
)
|
||||
op.create_index('idx_tenant_poi_location', 'tenant_poi_contexts', ['latitude', 'longitude'])
|
||||
op.create_index('idx_tenant_poi_refresh', 'tenant_poi_contexts', ['next_refresh_date'])
|
||||
op.create_index('idx_tenant_poi_status', 'tenant_poi_contexts', ['detection_status'])
|
||||
|
||||
op.create_table(
|
||||
'poi_refresh_jobs',
|
||||
sa.Column('id', UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False, index=True),
|
||||
sa.Column('scheduled_at', sa.DateTime(timezone=True), nullable=False, index=True),
|
||||
sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('status', sa.String(50), nullable=False, default='pending', index=True),
|
||||
sa.Column('attempt_count', sa.Integer, nullable=False, default=0),
|
||||
sa.Column('max_attempts', sa.Integer, nullable=False, default=3),
|
||||
sa.Column('latitude', sa.Float, nullable=False),
|
||||
sa.Column('longitude', sa.Float, nullable=False),
|
||||
sa.Column('pois_detected', sa.Integer, nullable=True),
|
||||
sa.Column('changes_detected', sa.Boolean, default=False),
|
||||
sa.Column('change_summary', JSONB, nullable=True),
|
||||
sa.Column('error_message', sa.Text, nullable=True),
|
||||
sa.Column('error_details', JSONB, nullable=True),
|
||||
sa.Column('next_scheduled_at', sa.DateTime(timezone=True), nullable=True, index=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now(), onupdate=sa.func.now())
|
||||
)
|
||||
op.create_index('idx_poi_refresh_jobs_tenant_status', 'poi_refresh_jobs', ['tenant_id', 'status'])
|
||||
op.create_index('idx_poi_refresh_jobs_status_scheduled', 'poi_refresh_jobs', ['status', 'scheduled_at'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Drop all tables"""
|
||||
|
||||
# POI Detection System
|
||||
op.drop_index('idx_poi_refresh_jobs_status_scheduled', table_name='poi_refresh_jobs')
|
||||
op.drop_index('idx_poi_refresh_jobs_tenant_status', table_name='poi_refresh_jobs')
|
||||
op.drop_table('poi_refresh_jobs')
|
||||
|
||||
op.drop_index('idx_tenant_poi_status', table_name='tenant_poi_contexts')
|
||||
op.drop_index('idx_tenant_poi_refresh', table_name='tenant_poi_contexts')
|
||||
op.drop_index('idx_tenant_poi_location', table_name='tenant_poi_contexts')
|
||||
op.drop_table('tenant_poi_contexts')
|
||||
|
||||
# School Calendars & Location Context
|
||||
op.drop_index(op.f('ix_tenant_location_contexts_city_id'), table_name='tenant_location_contexts')
|
||||
op.drop_index('idx_tenant_location_calendar', table_name='tenant_location_contexts')
|
||||
op.drop_table('tenant_location_contexts')
|
||||
|
||||
op.drop_index(op.f('ix_school_calendars_city_id'), table_name='school_calendars')
|
||||
op.drop_index('idx_school_calendar_city_type', table_name='school_calendars')
|
||||
op.drop_index('idx_school_calendar_city_year', table_name='school_calendars')
|
||||
op.drop_table('school_calendars')
|
||||
|
||||
# Traffic Data
|
||||
op.drop_index(op.f('ix_traffic_background_jobs_tenant_id'), table_name='traffic_background_jobs')
|
||||
op.drop_index(op.f('ix_traffic_background_jobs_city'), table_name='traffic_background_jobs')
|
||||
op.drop_index('idx_jobs_type_city', table_name='traffic_background_jobs')
|
||||
op.drop_index('idx_jobs_tenant_status', table_name='traffic_background_jobs')
|
||||
op.drop_index('idx_jobs_completed', table_name='traffic_background_jobs')
|
||||
op.drop_index('idx_jobs_city_status', table_name='traffic_background_jobs')
|
||||
op.drop_table('traffic_background_jobs')
|
||||
|
||||
op.drop_index(op.f('ix_traffic_data_tenant_id'), table_name='traffic_data')
|
||||
op.drop_index(op.f('ix_traffic_data_measurement_point_id'), table_name='traffic_data')
|
||||
op.drop_index(op.f('ix_traffic_data_location_id'), table_name='traffic_data')
|
||||
op.drop_index(op.f('ix_traffic_data_date'), table_name='traffic_data')
|
||||
op.drop_index(op.f('ix_traffic_data_city'), table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_training', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_tenant_date', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_quality', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_measurement_point', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_location_date', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_district_date', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_city_location', table_name='traffic_data')
|
||||
op.drop_index('idx_traffic_city_date', table_name='traffic_data')
|
||||
op.drop_table('traffic_data')
|
||||
|
||||
op.drop_index(op.f('ix_traffic_measurement_points_measurement_point_id'), table_name='traffic_measurement_points')
|
||||
op.drop_index(op.f('ix_traffic_measurement_points_city'), table_name='traffic_measurement_points')
|
||||
op.drop_index('idx_unique_city_point', table_name='traffic_measurement_points')
|
||||
op.drop_index('idx_points_road_type', table_name='traffic_measurement_points')
|
||||
op.drop_index('idx_points_district', table_name='traffic_measurement_points')
|
||||
op.drop_index('idx_points_city_location', table_name='traffic_measurement_points')
|
||||
op.drop_index('idx_points_active', table_name='traffic_measurement_points')
|
||||
op.drop_table('traffic_measurement_points')
|
||||
|
||||
op.drop_index(op.f('ix_city_traffic_data_date'), table_name='city_traffic_data')
|
||||
op.drop_index(op.f('ix_city_traffic_data_city_id'), table_name='city_traffic_data')
|
||||
op.drop_index('idx_city_traffic_lookup', table_name='city_traffic_data')
|
||||
op.drop_table('city_traffic_data')
|
||||
|
||||
# Weather Data
|
||||
op.drop_index(op.f('ix_weather_forecasts_location_id'), table_name='weather_forecasts')
|
||||
op.drop_index('idx_forecast_location_date', table_name='weather_forecasts')
|
||||
op.drop_table('weather_forecasts')
|
||||
|
||||
op.drop_index(op.f('ix_weather_data_tenant_id'), table_name='weather_data')
|
||||
op.drop_index(op.f('ix_weather_data_location_id'), table_name='weather_data')
|
||||
op.drop_index(op.f('ix_weather_data_date'), table_name='weather_data')
|
||||
op.drop_index('idx_weather_location_date', table_name='weather_data')
|
||||
op.drop_table('weather_data')
|
||||
|
||||
op.drop_index(op.f('ix_city_weather_data_date'), table_name='city_weather_data')
|
||||
op.drop_index(op.f('ix_city_weather_data_city_id'), table_name='city_weather_data')
|
||||
op.drop_index('idx_city_weather_lookup', table_name='city_weather_data')
|
||||
op.drop_table('city_weather_data')
|
||||
|
||||
# Audit Logs
|
||||
op.drop_index(op.f('ix_audit_logs_user_id'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_tenant_id'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_severity'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_service_name'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_resource_type'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_resource_id'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_created_at'), table_name='audit_logs')
|
||||
op.drop_index(op.f('ix_audit_logs_action'), table_name='audit_logs')
|
||||
op.drop_index('idx_audit_user_created', table_name='audit_logs')
|
||||
op.drop_index('idx_audit_tenant_created', table_name='audit_logs')
|
||||
op.drop_index('idx_audit_severity_created', table_name='audit_logs')
|
||||
op.drop_index('idx_audit_service_created', table_name='audit_logs')
|
||||
op.drop_index('idx_audit_resource_type_action', table_name='audit_logs')
|
||||
op.drop_table('audit_logs')
|
||||
Reference in New Issue
Block a user