-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS workspace_slots (
    id                      BIGSERIAL    PRIMARY KEY,
    project_sync_setting_id BIGINT       NOT NULL REFERENCES project_sync_settings(id) ON DELETE CASCADE,
    slot_index              INTEGER      NOT NULL,
    state                   TEXT         NOT NULL DEFAULT 'available',
    path                    TEXT         NOT NULL,
    created_at              TIMESTAMPTZ  NOT NULL DEFAULT now(),
    updated_at              TIMESTAMPTZ  NOT NULL DEFAULT now(),
    CONSTRAINT workspace_slots_state_check
        CHECK (state IN ('available', 'in_use', 'dirty', 'error')),
    CONSTRAINT workspace_slots_index_range
        CHECK (slot_index >= 0 AND slot_index <= 999)
);

-- Each project sync setting owns at most one slot per index.
CREATE UNIQUE INDEX ux_workspace_slots_project_index
    ON workspace_slots (project_sync_setting_id, slot_index);
-- +goose StatementEnd

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