"""Grafana topology cache: org, folders, alert rules per instance slug Revision ID: 004_grafana_catalog Revises: 003_ingress_org Create Date: 2026-04-03 """ from typing import Sequence, Union from alembic import op revision: str = "004_grafana_catalog" down_revision: Union[str, None] = "003_ingress_org" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.execute( """ CREATE TABLE IF NOT EXISTS grafana_catalog_meta ( instance_slug text NOT NULL, grafana_org_id int NOT NULL, org_name text NOT NULL DEFAULT '', synced_at timestamptz NOT NULL DEFAULT now(), folder_count int NOT NULL DEFAULT 0, rule_count int NOT NULL DEFAULT 0, error_text text, PRIMARY KEY (instance_slug, grafana_org_id) ); """ ) op.execute( """ CREATE TABLE IF NOT EXISTS grafana_catalog_folders ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), instance_slug text NOT NULL, grafana_org_id int NOT NULL, folder_uid text NOT NULL, title text NOT NULL DEFAULT '', parent_uid text, updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (instance_slug, grafana_org_id, folder_uid) ); """ ) op.execute( """ CREATE INDEX IF NOT EXISTS grafana_cat_folders_inst_org_idx ON grafana_catalog_folders (instance_slug, grafana_org_id); """ ) op.execute( """ CREATE TABLE IF NOT EXISTS grafana_catalog_rules ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), instance_slug text NOT NULL, grafana_org_id int NOT NULL, namespace_uid text NOT NULL, rule_group_name text NOT NULL, rule_uid text NOT NULL, title text NOT NULL DEFAULT '', rule_group_interval text, labels jsonb NOT NULL DEFAULT '{}'::jsonb, updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (instance_slug, grafana_org_id, rule_uid) ); """ ) op.execute( """ CREATE INDEX IF NOT EXISTS grafana_cat_rules_inst_org_idx ON grafana_catalog_rules (instance_slug, grafana_org_id); """ ) op.execute( """ CREATE INDEX IF NOT EXISTS grafana_cat_rules_ns_idx ON grafana_catalog_rules (instance_slug, grafana_org_id, namespace_uid); """ ) def downgrade() -> None: op.execute("DROP TABLE IF EXISTS grafana_catalog_rules;") op.execute("DROP TABLE IF EXISTS grafana_catalog_folders;") op.execute("DROP TABLE IF EXISTS grafana_catalog_meta;")