Fix Alembic issue

This commit is contained in:
Urtzi Alfaro
2025-10-01 11:24:06 +02:00
parent 7cc4b957a5
commit 2eeebfc1e0
62 changed files with 6114 additions and 3676 deletions

View File

@@ -1,7 +1,6 @@
"""Alembic environment configuration for external service"""
import asyncio
import logging
import os
import sys
from logging.config import fileConfig
@@ -25,7 +24,7 @@ try:
from shared.database.base import Base
# Import all models to ensure they are registered with Base.metadata
from app.models import * # Import all models
from app.models import * # noqa: F401, F403
except ImportError as e:
print(f"Import error in migrations env.py: {e}")
@@ -35,12 +34,19 @@ except ImportError as e:
# this is the Alembic Config object
config = context.config
# Set database URL from environment variables or settings
# Try service-specific DATABASE_URL first, then fall back to generic
database_url = os.getenv('EXTERNAL_DATABASE_URL') or os.getenv('DATABASE_URL')
# 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')
@@ -50,11 +56,28 @@ if not database_url:
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:
# Fallback to settings
database_url = getattr(settings, 'DATABASE_URL', None)
# 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 database_url:
config.set_main_option("sqlalchemy.url", database_url)
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:
@@ -63,6 +86,7 @@ if config.config_file_name is not None:
# Set target metadata
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = config.get_main_option("sqlalchemy.url")
@@ -78,7 +102,9 @@ def run_migrations_offline() -> None:
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,
@@ -89,8 +115,9 @@ def do_run_migrations(connection: Connection) -> None:
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
"""Run migrations in 'online' mode."""
"""Run migrations in 'online' mode with async support."""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
@@ -102,10 +129,12 @@ async def run_async_migrations() -> None:
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:

View File

@@ -1,236 +0,0 @@
"""Initial schema for external service
Revision ID: 000001
Revises:
Create Date: 2025-09-30 18:00:00.00000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '000001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create traffic_data table
op.create_table('traffic_data',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=True),
sa.Column('location_id', sa.String(100), nullable=False),
sa.Column('city', sa.String(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(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(100), nullable=True),
sa.Column('measurement_point_name', sa.String(500), nullable=True),
sa.Column('measurement_point_type', sa.String(50), nullable=True),
sa.Column('latitude', sa.Float(), nullable=True),
sa.Column('longitude', sa.Float(), nullable=True),
sa.Column('district', sa.String(100), nullable=True),
sa.Column('zone', sa.String(100), nullable=True),
sa.Column('source', sa.String(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', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('raw_data', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_traffic_data_tenant_id'), 'traffic_data', ['tenant_id'], unique=False)
op.create_index(op.f('ix_traffic_data_location_id'), 'traffic_data', ['location_id'], unique=False)
op.create_index(op.f('ix_traffic_data_city'), 'traffic_data', ['city'], unique=False)
op.create_index(op.f('ix_traffic_data_date'), 'traffic_data', ['date'], unique=False)
op.create_index('idx_traffic_location_date', 'traffic_data', ['location_id', 'date'], unique=False)
op.create_index('idx_traffic_city_date', 'traffic_data', ['city', 'date'], unique=False)
op.create_index('idx_traffic_tenant_date', 'traffic_data', ['tenant_id', 'date'], unique=False)
op.create_index('idx_traffic_city_location', 'traffic_data', ['city', 'location_id'], unique=False)
op.create_index('idx_traffic_measurement_point', 'traffic_data', ['city', 'measurement_point_id'], unique=False)
op.create_index('idx_traffic_district_date', 'traffic_data', ['city', 'district', 'date'], unique=False)
op.create_index('idx_traffic_training', 'traffic_data', ['tenant_id', 'city', 'date', 'is_synthetic'], unique=False)
op.create_index('idx_traffic_quality', 'traffic_data', ['city', 'data_quality_score', 'date'], unique=False)
# Create traffic_measurement_points table
op.create_table('traffic_measurement_points',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('city', sa.String(50), nullable=False),
sa.Column('measurement_point_id', sa.String(100), nullable=False),
sa.Column('name', sa.String(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(100), nullable=True),
sa.Column('zone', sa.String(100), nullable=True),
sa.Column('road_type', sa.String(50), nullable=True),
sa.Column('measurement_type', sa.String(50), nullable=True),
sa.Column('point_category', sa.String(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', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
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'], unique=False)
op.create_index(op.f('ix_traffic_measurement_points_measurement_point_id'), 'traffic_measurement_points', ['measurement_point_id'], unique=False)
op.create_index('idx_points_city_location', 'traffic_measurement_points', ['city', 'latitude', 'longitude'], unique=False)
op.create_index('idx_points_district', 'traffic_measurement_points', ['city', 'district'], unique=False)
op.create_index('idx_points_road_type', 'traffic_measurement_points', ['city', 'road_type'], unique=False)
op.create_index('idx_points_active', 'traffic_measurement_points', ['city', 'is_active', 'last_data_received'], unique=False)
# Create traffic_background_jobs table
op.create_table('traffic_background_jobs',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('job_type', sa.String(50), nullable=False),
sa.Column('city', sa.String(50), nullable=False),
sa.Column('location_pattern', sa.String(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(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', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('tenant_id', sa.UUID(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_traffic_background_jobs_city'), 'traffic_background_jobs', ['city'], unique=False)
op.create_index(op.f('ix_traffic_background_jobs_tenant_id'), 'traffic_background_jobs', ['tenant_id'], unique=False)
op.create_index('idx_jobs_city_status', 'traffic_background_jobs', ['city', 'status', 'scheduled_at'], unique=False)
op.create_index('idx_jobs_tenant_status', 'traffic_background_jobs', ['tenant_id', 'status', 'scheduled_at'], unique=False)
op.create_index('idx_jobs_type_city', 'traffic_background_jobs', ['job_type', 'city', 'scheduled_at'], unique=False)
op.create_index('idx_jobs_completed', 'traffic_background_jobs', ['status', 'completed_at'], unique=False)
# Create weather_data table
op.create_table('weather_data',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('location_id', sa.String(100), nullable=False),
sa.Column('city', sa.String(50), nullable=False),
sa.Column('station_name', sa.String(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(100), nullable=True),
sa.Column('description', sa.String(200), nullable=True),
sa.Column('weather_code', sa.String(20), nullable=True),
sa.Column('source', sa.String(50), nullable=False),
sa.Column('data_type', sa.String(20), nullable=False),
sa.Column('is_forecast', sa.Boolean(), nullable=True),
sa.Column('data_quality_score', sa.Float(), nullable=True),
sa.Column('raw_data', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('processed_data', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('tenant_id', sa.UUID(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_weather_data_location_id'), 'weather_data', ['location_id'], unique=False)
op.create_index(op.f('ix_weather_data_city'), 'weather_data', ['city'], unique=False)
op.create_index(op.f('ix_weather_data_date'), 'weather_data', ['date'], unique=False)
op.create_index(op.f('ix_weather_data_tenant_id'), 'weather_data', ['tenant_id'], unique=False)
op.create_index('idx_weather_location_date', 'weather_data', ['location_id', 'date'], unique=False)
# Create weather_forecasts table
op.create_table('weather_forecasts',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('location_id', sa.String(100), nullable=False),
sa.Column('forecast_date', sa.DateTime(timezone=True), nullable=False),
sa.Column('generated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), 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(200), nullable=True),
sa.Column('source', sa.String(50), nullable=False),
sa.Column('raw_data', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_weather_forecasts_location_id'), 'weather_forecasts', ['location_id'], unique=False)
op.create_index('idx_forecast_location_date', 'weather_forecasts', ['location_id', 'forecast_date'], unique=False)
def downgrade() -> None:
# Drop weather_forecasts table
op.drop_index('idx_forecast_location_date', table_name='weather_forecasts')
op.drop_index(op.f('ix_weather_forecasts_location_id'), table_name='weather_forecasts')
op.drop_table('weather_forecasts')
# Drop weather_data table
op.drop_index('idx_weather_location_date', table_name='weather_data')
op.drop_index(op.f('ix_weather_data_tenant_id'), table_name='weather_data')
op.drop_index(op.f('ix_weather_data_date'), table_name='weather_data')
op.drop_index(op.f('ix_weather_data_city'), table_name='weather_data')
op.drop_index(op.f('ix_weather_data_location_id'), table_name='weather_data')
op.drop_table('weather_data')
# Drop traffic_background_jobs table
op.drop_index('idx_jobs_completed', 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_city_status', table_name='traffic_background_jobs')
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_table('traffic_background_jobs')
# Drop traffic_measurement_points table
op.drop_index('idx_points_active', 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_unique_city_point', table_name='traffic_measurement_points')
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_table('traffic_measurement_points')
# Drop traffic_data table
op.drop_index('idx_traffic_quality', table_name='traffic_data')
op.drop_index('idx_traffic_training', table_name='traffic_data')
op.drop_index('idx_traffic_district_date', table_name='traffic_data')
op.drop_index('idx_traffic_measurement_point', table_name='traffic_data')
op.drop_index('idx_traffic_city_location', table_name='traffic_data')
op.drop_index('idx_traffic_tenant_date', table_name='traffic_data')
op.drop_index('idx_traffic_city_date', table_name='traffic_data')
op.drop_index('idx_traffic_location_date', 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(op.f('ix_traffic_data_location_id'), table_name='traffic_data')
op.drop_index(op.f('ix_traffic_data_tenant_id'), table_name='traffic_data')
op.drop_table('traffic_data')

View File

@@ -0,0 +1,224 @@
"""initial_schema_20251001_1119
Revision ID: 374752db316e
Revises:
Create Date: 2025-10-01 11:19:50.472480+02:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '374752db316e'
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:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('traffic_background_jobs',
sa.Column('id', sa.UUID(), 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', sa.JSON(), nullable=True),
sa.Column('tenant_id', sa.UUID(), 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'], unique=False)
op.create_index('idx_jobs_completed', 'traffic_background_jobs', ['status', 'completed_at'], unique=False)
op.create_index('idx_jobs_tenant_status', 'traffic_background_jobs', ['tenant_id', 'status', 'scheduled_at'], unique=False)
op.create_index('idx_jobs_type_city', 'traffic_background_jobs', ['job_type', 'city', 'scheduled_at'], unique=False)
op.create_index(op.f('ix_traffic_background_jobs_city'), 'traffic_background_jobs', ['city'], unique=False)
op.create_index(op.f('ix_traffic_background_jobs_tenant_id'), 'traffic_background_jobs', ['tenant_id'], unique=False)
op.create_table('traffic_data',
sa.Column('id', sa.UUID(), 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', sa.JSON(), nullable=True),
sa.Column('raw_data', sa.Text(), nullable=True),
sa.Column('tenant_id', sa.UUID(), 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'], unique=False)
op.create_index('idx_traffic_city_location', 'traffic_data', ['city', 'location_id'], unique=False)
op.create_index('idx_traffic_district_date', 'traffic_data', ['city', 'district', 'date'], unique=False)
op.create_index('idx_traffic_location_date', 'traffic_data', ['location_id', 'date'], unique=False)
op.create_index('idx_traffic_measurement_point', 'traffic_data', ['city', 'measurement_point_id'], unique=False)
op.create_index('idx_traffic_quality', 'traffic_data', ['city', 'data_quality_score', 'date'], unique=False)
op.create_index('idx_traffic_tenant_date', 'traffic_data', ['tenant_id', 'date'], unique=False)
op.create_index('idx_traffic_training', 'traffic_data', ['tenant_id', 'city', 'date', 'is_synthetic'], unique=False)
op.create_index(op.f('ix_traffic_data_city'), 'traffic_data', ['city'], unique=False)
op.create_index(op.f('ix_traffic_data_date'), 'traffic_data', ['date'], unique=False)
op.create_index(op.f('ix_traffic_data_location_id'), 'traffic_data', ['location_id'], unique=False)
op.create_index(op.f('ix_traffic_data_measurement_point_id'), 'traffic_data', ['measurement_point_id'], unique=False)
op.create_index(op.f('ix_traffic_data_tenant_id'), 'traffic_data', ['tenant_id'], unique=False)
op.create_table('traffic_measurement_points',
sa.Column('id', sa.UUID(), 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', sa.JSON(), 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'], unique=False)
op.create_index('idx_points_city_location', 'traffic_measurement_points', ['city', 'latitude', 'longitude'], unique=False)
op.create_index('idx_points_district', 'traffic_measurement_points', ['city', 'district'], unique=False)
op.create_index('idx_points_road_type', 'traffic_measurement_points', ['city', 'road_type'], unique=False)
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'], unique=False)
op.create_index(op.f('ix_traffic_measurement_points_measurement_point_id'), 'traffic_measurement_points', ['measurement_point_id'], unique=False)
op.create_table('weather_data',
sa.Column('id', sa.UUID(), 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', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('processed_data', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('tenant_id', sa.UUID(), 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'], unique=False)
op.create_index(op.f('ix_weather_data_date'), 'weather_data', ['date'], unique=False)
op.create_index(op.f('ix_weather_data_location_id'), 'weather_data', ['location_id'], unique=False)
op.create_index(op.f('ix_weather_data_tenant_id'), 'weather_data', ['tenant_id'], unique=False)
op.create_table('weather_forecasts',
sa.Column('id', sa.UUID(), 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'], unique=False)
op.create_index(op.f('ix_weather_forecasts_location_id'), 'weather_forecasts', ['location_id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
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_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_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_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')
# ### end Alembic commands ###