-- +goose Up -- A workflow.handoff ends its run and starts a linked run of the next workflow -- on the same work item. Only the current run of an item is unique; a run -- that handed off points at its successor and leaves the key. ALTER TABLE tasks ADD COLUMN handoff_from_task_id bigint REFERENCES tasks (id) ON DELETE SET NULL, ADD COLUMN handoff_to_task_id bigint; ALTER TABLE tasks DROP CONSTRAINT tasks_org_identity_owner_repo_number_key; CREATE UNIQUE INDEX tasks_current_item_key ON tasks (org_id, identity, owner, repo, issue_number) WHERE handoff_to_task_id IS NULL; -- +goose Down DELETE FROM tasks WHERE handoff_to_task_id IS NOT NULL; DROP INDEX tasks_current_item_key; ALTER TABLE tasks ADD CONSTRAINT tasks_org_identity_owner_repo_number_key UNIQUE (org_id, identity, owner, repo, issue_number); ALTER TABLE tasks DROP COLUMN handoff_to_task_id, DROP COLUMN handoff_from_task_id;