"""Add stable governance references to PostgreSQL records."""

from alembic import op


revision = "20260716_02"
down_revision = "20260716_01"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute(
        """
        ALTER TABLE public.data_orders
            ADD COLUMN IF NOT EXISTS result_dataflow_uid UUID,
            ADD COLUMN IF NOT EXISTS data_source_uid UUID;
        ALTER TABLE public.data_products
            ADD COLUMN IF NOT EXISTS source_dataflow_uid UUID;
        ALTER TABLE public.metadata_review_records
            ADD COLUMN IF NOT EXISTS business_domain_uid UUID;
        ALTER TABLE public.metadata_version_history
            ADD COLUMN IF NOT EXISTS meta_uid UUID;

        CREATE INDEX IF NOT EXISTS idx_data_orders_result_dataflow_uid
            ON public.data_orders(result_dataflow_uid);
        CREATE INDEX IF NOT EXISTS idx_data_products_source_dataflow_uid
            ON public.data_products(source_dataflow_uid);
        CREATE INDEX IF NOT EXISTS idx_metadata_review_business_domain_uid
            ON public.metadata_review_records(business_domain_uid);
        CREATE INDEX IF NOT EXISTS idx_metadata_version_meta_uid
            ON public.metadata_version_history(meta_uid);
        """
    )


def downgrade() -> None:
    # Preserve additive compatibility fields during application rollback.
    pass
