Fix Alembic issue
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""Alembic environment configuration for alert-processor 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('ALERT_PROCESSOR_DATABASE_URL') or os.getenv('DATABASE_URL')
|
||||
# Determine service name from file path
|
||||
service_name = "alert-processor"
|
||||
service_name_upper = "ALERT_PROCESSOR"
|
||||
|
||||
# Set database URL from environment variables with multiple fallback strategies
|
||||
database_url = (
|
||||
os.getenv('ALERT_PROCESSOR_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,22 +56,22 @@ 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:
|
||||
# As a last resort, construct the database URL manually from individual environment variables
|
||||
# that are likely to be set in the Kubernetes environment
|
||||
db_user = os.getenv("ALERT_PROCESSOR_DB_USER", "alert_processor_user")
|
||||
db_password = os.getenv("ALERT_PROCESSOR_DB_PASSWORD", "alert_processor_pass123")
|
||||
db_host = os.getenv("ALERT_PROCESSOR_DB_HOST", "alert-processor-db-service")
|
||||
db_port = os.getenv("ALERT_PROCESSOR_DB_PORT", "5432")
|
||||
db_name = os.getenv("ALERT_PROCESSOR_DB_NAME", "alert_processor_db")
|
||||
|
||||
database_url = f"postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
|
||||
# Try service-specific environment variables (alert-processor specific pattern)
|
||||
db_host = os.getenv('ALERT_PROCESSOR_DB_HOST', 'alert-processor-db-service')
|
||||
db_port = os.getenv('ALERT_PROCESSOR_DB_PORT', '5432')
|
||||
db_name = os.getenv('ALERT_PROCESSOR_DB_NAME', 'alert_processor_db')
|
||||
db_user = os.getenv('ALERT_PROCESSOR_DB_USER', 'alert_processor_user')
|
||||
db_password = os.getenv('ALERT_PROCESSOR_DB_PASSWORD')
|
||||
|
||||
if database_url:
|
||||
print(f"Using database URL: {database_url}")
|
||||
config.set_main_option("sqlalchemy.url", database_url)
|
||||
else:
|
||||
print("ERROR: No database URL configured!")
|
||||
raise Exception("No database URL found after all fallback methods")
|
||||
if db_password:
|
||||
database_url = f"postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
|
||||
|
||||
if not database_url:
|
||||
error_msg = "ERROR: No database URL configured for alert-processor 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:
|
||||
@@ -74,6 +80,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")
|
||||
@@ -89,7 +96,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,
|
||||
@@ -100,8 +109,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.",
|
||||
@@ -113,10 +123,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:
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
"""Initial schema for alert processor
|
||||
|
||||
Revision ID: 00000001
|
||||
Revises:
|
||||
Create Date: 2025-09-30 18:00:00.000000
|
||||
|
||||
"""
|
||||
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 alerts table (ENUMs will be created automatically)
|
||||
op.create_table('alerts',
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
sa.Column('tenant_id', sa.UUID(), nullable=False),
|
||||
sa.Column('item_type', sa.String(length=50), nullable=False),
|
||||
sa.Column('alert_type', sa.String(length=100), nullable=False),
|
||||
sa.Column('severity', sa.Enum('LOW', 'MEDIUM', 'HIGH', 'URGENT', name='alertseverity'), nullable=False),
|
||||
sa.Column('status', sa.Enum('ACTIVE', 'RESOLVED', 'ACKNOWLEDGED', 'IGNORED', name='alertstatus'), nullable=False),
|
||||
sa.Column('service', sa.String(length=100), nullable=False),
|
||||
sa.Column('title', sa.String(length=255), nullable=False),
|
||||
sa.Column('message', sa.Text(), nullable=False),
|
||||
sa.Column('actions', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('alert_metadata', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('resolved_at', sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_alerts_tenant_id'), 'alerts', ['tenant_id'], unique=False)
|
||||
op.create_index(op.f('ix_alerts_severity'), 'alerts', ['severity'], unique=False)
|
||||
op.create_index(op.f('ix_alerts_status'), 'alerts', ['status'], unique=False)
|
||||
op.create_index(op.f('ix_alerts_created_at'), 'alerts', ['created_at'], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(op.f('ix_alerts_created_at'), table_name='alerts')
|
||||
op.drop_index(op.f('ix_alerts_status'), table_name='alerts')
|
||||
op.drop_index(op.f('ix_alerts_severity'), table_name='alerts')
|
||||
op.drop_index(op.f('ix_alerts_tenant_id'), table_name='alerts')
|
||||
op.drop_table('alerts')
|
||||
|
||||
# Drop enums (will be dropped automatically with table, but explicit for clarity)
|
||||
sa.Enum(name='alertseverity').drop(op.get_bind(), checkfirst=True)
|
||||
sa.Enum(name='alertstatus').drop(op.get_bind(), checkfirst=True)
|
||||
@@ -0,0 +1,54 @@
|
||||
"""initial_schema_20251001_1119
|
||||
|
||||
Revision ID: 7114c42d4b31
|
||||
Revises:
|
||||
Create Date: 2025-10-01 11:19:33.701313+02:00
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '7114c42d4b31'
|
||||
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('alerts',
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
sa.Column('tenant_id', sa.UUID(), nullable=False),
|
||||
sa.Column('item_type', sa.String(length=50), nullable=False),
|
||||
sa.Column('alert_type', sa.String(length=100), nullable=False),
|
||||
sa.Column('severity', sa.Enum('low', 'medium', 'high', 'urgent', name='alertseverity'), nullable=False),
|
||||
sa.Column('status', sa.Enum('active', 'resolved', 'acknowledged', 'ignored', name='alertstatus'), nullable=True),
|
||||
sa.Column('service', sa.String(length=100), nullable=False),
|
||||
sa.Column('title', sa.String(length=255), nullable=False),
|
||||
sa.Column('message', sa.Text(), nullable=False),
|
||||
sa.Column('actions', sa.JSON(), nullable=True),
|
||||
sa.Column('alert_metadata', sa.JSON(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('resolved_at', sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_alerts_created_at'), 'alerts', ['created_at'], unique=False)
|
||||
op.create_index(op.f('ix_alerts_severity'), 'alerts', ['severity'], unique=False)
|
||||
op.create_index(op.f('ix_alerts_status'), 'alerts', ['status'], unique=False)
|
||||
op.create_index(op.f('ix_alerts_tenant_id'), 'alerts', ['tenant_id'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_alerts_tenant_id'), table_name='alerts')
|
||||
op.drop_index(op.f('ix_alerts_status'), table_name='alerts')
|
||||
op.drop_index(op.f('ix_alerts_severity'), table_name='alerts')
|
||||
op.drop_index(op.f('ix_alerts_created_at'), table_name='alerts')
|
||||
op.drop_table('alerts')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user