ALTER TABLE gateway_tasks
  ADD COLUMN IF NOT EXISTS compatibility_protocol text,
  ADD COLUMN IF NOT EXISTS compatibility_public_id text,
  ADD COLUMN IF NOT EXISTS compatibility_source_protocol text,
  ADD COLUMN IF NOT EXISTS compatibility_submit_http_status integer,
  ADD COLUMN IF NOT EXISTS compatibility_submit_headers jsonb,
  ADD COLUMN IF NOT EXISTS compatibility_submit_body jsonb;

CREATE UNIQUE INDEX IF NOT EXISTS gateway_tasks_compatibility_public_id_unique
  ON gateway_tasks (compatibility_protocol, compatibility_public_id)
  WHERE compatibility_protocol IS NOT NULL
    AND compatibility_protocol <> ''
    AND compatibility_public_id IS NOT NULL
    AND compatibility_public_id <> '';

CREATE INDEX IF NOT EXISTS gateway_tasks_remote_task_compatibility_lookup
  ON gateway_tasks (remote_task_id)
  WHERE remote_task_id IS NOT NULL AND remote_task_id <> '';
