ALTER TABLE tasks
  ADD COLUMN IF NOT EXISTS processing_attempt BIGINT NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS lease_owner UUID,
  ADD COLUMN IF NOT EXISTS lease_until TIMESTAMPTZ;

ALTER TABLE task_files
  ADD COLUMN IF NOT EXISTS processing_attempt BIGINT NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS lease_owner UUID,
  ADD COLUMN IF NOT EXISTS lease_until TIMESTAMPTZ;

CREATE INDEX IF NOT EXISTS idx_tasks_processing_lease
  ON tasks(lease_until)
  WHERE status = 'processing';

CREATE INDEX IF NOT EXISTS idx_task_files_processing_lease
  ON task_files(task_id, lease_until)
  WHERE status = 'processing';
