83f48dce97
Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
"""problem correlation fields
|
|
|
|
Revision ID: 003
|
|
Revises: 002
|
|
"""
|
|
|
|
from typing import Sequence, Union
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision: str = "003"
|
|
down_revision: Union[str, None] = "002"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.add_column("problems", sa.Column("fingerprint", sa.String(length=128), nullable=True))
|
|
op.add_column(
|
|
"problems",
|
|
sa.Column("event_count", sa.Integer(), nullable=False, server_default="1"),
|
|
)
|
|
op.add_column("problems", sa.Column("last_seen_at", sa.DateTime(timezone=True), nullable=True))
|
|
op.create_index("ix_problems_fingerprint", "problems", ["fingerprint"])
|
|
op.create_index("ix_problems_last_seen_at", "problems", ["last_seen_at"])
|
|
|
|
op.execute(
|
|
sa.text(
|
|
"""
|
|
UPDATE problems p SET
|
|
fingerprint = 'h' || COALESCE(p.host_id::text, '0') || chr(58) || 'r'
|
|
|| COALESCE(p.rule_id, 'unknown'),
|
|
event_count = COALESCE(
|
|
(SELECT COUNT(*)::int FROM problem_events pe WHERE pe.problem_id = p.id),
|
|
1
|
|
),
|
|
last_seen_at = COALESCE(p.updated_at, p.created_at)
|
|
"""
|
|
)
|
|
)
|
|
|
|
op.alter_column("problems", "fingerprint", nullable=False)
|
|
op.alter_column("problems", "last_seen_at", nullable=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("ix_problems_last_seen_at", table_name="problems")
|
|
op.drop_index("ix_problems_fingerprint", table_name="problems")
|
|
op.drop_column("problems", "last_seen_at")
|
|
op.drop_column("problems", "event_count")
|
|
op.drop_column("problems", "fingerprint")
|