ALTER TABLE generation_tasks
    ADD COLUMN lease_token VARCHAR(64),
    ADD COLUMN lease_owner VARCHAR(128),
    ADD COLUMN lease_expires_at TIMESTAMPTZ,
    ADD COLUMN attempt_count INT NOT NULL DEFAULT 0,
    ADD COLUMN last_heartbeat_at TIMESTAMPTZ;

CREATE INDEX idx_generation_tasks_running_lease
    ON generation_tasks(lease_expires_at, id)
    WHERE status = 'running';

CREATE INDEX idx_generation_tasks_queued_stale
    ON generation_tasks(updated_at, id)
    WHERE status = 'queued';
