-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS roadmap_sync_identities (
    id                      BIGSERIAL    PRIMARY KEY,
    shape                   TEXT         NOT NULL DEFAULT 'milestone',
    roadmap_milestone_path  TEXT         NOT NULL,
    roadmap_item_id         TEXT         NOT NULL DEFAULT '',
    provider                TEXT         NOT NULL,
    tenant                  TEXT         NOT NULL DEFAULT '',
    project                 TEXT         NOT NULL DEFAULT '',
    work_item_id            TEXT         NOT NULL,
    parent_work_item_id     TEXT         NOT NULL DEFAULT '',
    provider_revision       TEXT,
    roadmap_revision        TEXT,
    created_at              TIMESTAMPTZ  NOT NULL DEFAULT now(),
    updated_at              TIMESTAMPTZ  NOT NULL DEFAULT now(),
    CONSTRAINT roadmap_sync_identities_shape_check
        CHECK (shape IN ('milestone', 'task'))
);

-- The same provider work item maps to exactly one identity row, so re-processing
-- the same Plane ticket never creates a duplicate Milestone identity.
CREATE UNIQUE INDEX ux_roadmap_sync_identities_provider_work_item
    ON roadmap_sync_identities (provider, tenant, project, work_item_id);
-- The same Milestone path maps to exactly one identity row, so re-processing the
-- same Milestone path never creates a duplicate identity.
CREATE UNIQUE INDEX ux_roadmap_sync_identities_milestone_path
    ON roadmap_sync_identities (roadmap_milestone_path);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS roadmap_sync_identities;
-- +goose StatementEnd
