-- 0063 was released before its idempotency table was added. Installations that
-- already recorded 0063 need a new immutable migration to repair that drift.
CREATE TABLE IF NOT EXISTS gateway_security_event_connection_idempotency (
  operation text NOT NULL,
  idempotency_key text NOT NULL,
  request_hash text NOT NULL,
  response jsonb NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now(),
  PRIMARY KEY (operation, idempotency_key),
  CONSTRAINT gateway_security_event_connection_idempotency_operation
    CHECK (operation IN ('connect','verify','rotate','disconnect'))
);

CREATE INDEX IF NOT EXISTS idx_gateway_security_event_connection_idempotency_created
  ON gateway_security_event_connection_idempotency(created_at);
