Improve the frontend 3
This commit is contained in:
141
services/orchestrator/migrations/env.py
Normal file
141
services/orchestrator/migrations/env.py
Normal file
@@ -0,0 +1,141 @@
|
||||
"""Alembic environment configuration for inventory 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/orchestrator/migrations/script.py.mako
Normal file
26
services/orchestrator/migrations/script.py.mako
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"}
|
||||
@@ -0,0 +1,112 @@
|
||||
"""add orchestration runs table
|
||||
|
||||
Revision ID: 20251029_1700
|
||||
Revises:
|
||||
Create Date: 2025-10-29 17:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '20251029_1700'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Create PostgreSQL enum type for orchestration status
|
||||
orchestrationstatus_enum = postgresql.ENUM(
|
||||
'pending', 'running', 'completed', 'partial_success', 'failed', 'cancelled',
|
||||
name='orchestrationstatus',
|
||||
create_type=False
|
||||
)
|
||||
orchestrationstatus_enum.create(op.get_bind(), checkfirst=True)
|
||||
|
||||
# Create orchestration_runs table
|
||||
op.create_table('orchestration_runs',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('run_number', sa.String(length=50), nullable=False),
|
||||
sa.Column('status', orchestrationstatus_enum, nullable=False, server_default='pending'),
|
||||
sa.Column('run_type', sa.String(length=50), nullable=False, server_default=sa.text("'scheduled'::character varying")),
|
||||
sa.Column('priority', sa.String(length=20), nullable=False, server_default=sa.text("'normal'::character varying")),
|
||||
sa.Column('started_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('duration_seconds', sa.Integer(), nullable=True),
|
||||
sa.Column('forecasting_started_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('forecasting_completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('forecasting_status', sa.String(length=20), nullable=True),
|
||||
sa.Column('forecasting_error', sa.Text(), nullable=True),
|
||||
sa.Column('production_started_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('production_completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('production_status', sa.String(length=20), nullable=True),
|
||||
sa.Column('production_error', sa.Text(), nullable=True),
|
||||
sa.Column('procurement_started_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('procurement_completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('procurement_status', sa.String(length=20), nullable=True),
|
||||
sa.Column('procurement_error', sa.Text(), nullable=True),
|
||||
sa.Column('notification_started_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('notification_completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('notification_status', sa.String(length=20), nullable=True),
|
||||
sa.Column('notification_error', sa.Text(), nullable=True),
|
||||
sa.Column('forecasts_generated', sa.Integer(), nullable=False, server_default=sa.text('0')),
|
||||
sa.Column('production_batches_created', sa.Integer(), nullable=False, server_default=sa.text('0')),
|
||||
sa.Column('procurement_plans_created', sa.Integer(), nullable=False, server_default=sa.text('0')),
|
||||
sa.Column('purchase_orders_created', sa.Integer(), nullable=False, server_default=sa.text('0')),
|
||||
sa.Column('notifications_sent', sa.Integer(), nullable=False, server_default=sa.text('0')),
|
||||
sa.Column('forecast_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('retry_count', sa.Integer(), nullable=False, server_default=sa.text('0')),
|
||||
sa.Column('max_retries_reached', sa.Boolean(), nullable=False, server_default=sa.text('false')),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('error_details', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('production_schedule_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('procurement_plan_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), onupdate=sa.text('now()'), nullable=False),
|
||||
sa.Column('triggered_by', sa.String(length=100), nullable=True),
|
||||
sa.Column('run_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('fulfillment_rate', sa.Integer(), nullable=True),
|
||||
sa.Column('on_time_delivery_rate', sa.Integer(), nullable=True),
|
||||
sa.Column('cost_accuracy', sa.Integer(), nullable=True),
|
||||
sa.Column('quality_score', sa.Integer(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_orchestration_runs'))
|
||||
)
|
||||
|
||||
# Create indexes
|
||||
op.create_index('ix_orchestration_runs_tenant_id', 'orchestration_runs', ['tenant_id'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_run_number', 'orchestration_runs', ['run_number'], unique=True)
|
||||
op.create_index('ix_orchestration_runs_status', 'orchestration_runs', ['status'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_started_at', 'orchestration_runs', ['started_at'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_completed_at', 'orchestration_runs', ['completed_at'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_run_type', 'orchestration_runs', ['run_type'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_trigger', 'orchestration_runs', ['triggered_by'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_tenant_status', 'orchestration_runs', ['tenant_id', 'status'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_tenant_type', 'orchestration_runs', ['tenant_id', 'run_type'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_tenant_started', 'orchestration_runs', ['tenant_id', 'started_at'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_fulfillment_rate', 'orchestration_runs', ['fulfillment_rate'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_on_time_delivery_rate', 'orchestration_runs', ['on_time_delivery_rate'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_cost_accuracy', 'orchestration_runs', ['cost_accuracy'], unique=False)
|
||||
op.create_index('ix_orchestration_runs_quality_score', 'orchestration_runs', ['quality_score'], unique=False)
|
||||
|
||||
|
||||
def downgrade():
|
||||
# Drop indexes
|
||||
op.drop_index('ix_orchestration_runs_tenant_started', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_tenant_type', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_tenant_status', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_trigger', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_run_type', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_completed_at', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_started_at', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_status', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_run_number', table_name='orchestration_runs')
|
||||
op.drop_index('ix_orchestration_runs_tenant_id', table_name='orchestration_runs')
|
||||
|
||||
# Drop table
|
||||
op.drop_table('orchestration_runs')
|
||||
|
||||
# Drop enum type
|
||||
op.execute("DROP TYPE IF EXISTS orchestrationstatus")
|
||||
Reference in New Issue
Block a user