"""Create ai_decision_records table for oversight. Revision ID: 0128 Revises: 0127 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.postgresql import UUID, JSONB revision = "0128" down_revision = "0127" branch_labels = None depends_on = None def upgrade() -> None: # Use IF NOT EXISTS to avoid DuplicateTableError if table was already # created by Base.metadata.create_all() in prestart.sh op.execute(""" CREATE TABLE IF NOT EXISTS ai_decision_records ( id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY, tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE, owner_id UUID, agent_run_id UUID NOT NULL, recommendation TEXT NOT NULL, evidence JSONB NOT NULL DEFAULT '{}', reviewer_id UUID, decision VARCHAR(20), decision_timestamp VARCHAR(40), deviation_note TEXT, created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL, updated_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL, deleted_at TIMESTAMP WITH TIME ZONE ) """) op.execute("CREATE INDEX IF NOT EXISTS ix_ai_decision_records_tenant_id ON ai_decision_records(tenant_id)") op.execute("CREATE INDEX IF NOT EXISTS ix_ai_decision_records_agent_run_id ON ai_decision_records(agent_run_id)") def downgrade() -> None: op.drop_table("ai_decision_records")