"""Extend audit action/replay checks without editing applied ingestion revisions.""" from alembic import op revision = "0003_extraction_review" down_revision = "0002_worker_artifact" branch_labels = None depends_on = None def upgrade(**kwargs): op.execute("ALTER TABLE audit_events DROP CONSTRAINT audit_events_action_check") op.execute(""" ALTER TABLE audit_events ADD CONSTRAINT audit_events_action_check CHECK ( action IN ('upload','selection_changed','document_deleted','ingestion_retry', 'extraction_review','configuration_changed','session_revoked') ), ADD CONSTRAINT audit_events_extraction_review_replay CHECK ( action <> 'extraction_review' OR ( actor_user_id IS NOT NULL AND target_id IS NOT NULL AND idempotency_key IS NOT NULL AND request_hash IS NOT NULL AND response_json IS NOT NULL ) ) """) def downgrade(**kwargs): op.execute("ALTER TABLE audit_events DROP CONSTRAINT audit_events_extraction_review_replay") op.execute("ALTER TABLE audit_events DROP CONSTRAINT audit_events_action_check") op.execute(""" ALTER TABLE audit_events ADD CONSTRAINT audit_events_action_check CHECK ( action IN ('upload','selection_changed','document_deleted','ingestion_retry', 'configuration_changed','session_revoked') ) """)