AkurAI Build
Menu

AkurAI-Build

public

Latest change 0f97f2565890fc5015cf9cc035532809caec56cb - feat(pr): close, reopen, edit and comment on pull requests; delete branches by Ólafur Búi Ólafsson

-- Two gaps that made a pull request a one-way object: it could be opened,
-- reviewed and merged, but never closed without merging and never discussed
-- outside a review body.
--
-- 1. Comments. `issue_comments` is keyed to `issues`, so there was no way to
--    attach a note to a pull request at all. Review bodies were the only
--    channel, and those require an individual identity and a state verdict --
--    a machine identity could not leave so much as a "superseded by #29".
--
-- 2. Closability. `UNIQUE(repository_id, source_ref, target_ref, state)` means
--    at most ONE row per (source, target, state) tuple, so a second pull
--    request between the same two refs cannot enter 'closed' once another
--    already sits there -- exactly the defect migration 013 fixed for
--    merge_queue_entries. Republishing a branch pair after an abandoned
--    attempt (the common case: v4..v9 of one fix) therefore left PRs
--    permanently unclosable. The invariant that actually matters is "at most
--    one ACTIVE pull request per branch pair"; terminal states must repeat
--    freely. Replaced with a partial unique index over the active states.
CREATE TABLE pull_request_comments (
    id INTEGER PRIMARY KEY,
    pull_request_id INTEGER NOT NULL REFERENCES pull_requests(id) ON DELETE CASCADE,
    body TEXT NOT NULL CHECK (length(body) BETWEEN 1 AND 65536),
    author_sub TEXT NOT NULL CHECK (length(author_sub) BETWEEN 1 AND 256),
    author_display TEXT NOT NULL CHECK (length(author_display) BETWEEN 1 AND 256),
    created_at INTEGER NOT NULL DEFAULT (unixepoch()),
    updated_at INTEGER NOT NULL DEFAULT (unixepoch())
) STRICT;

CREATE INDEX pull_request_comments_pr_created
    ON pull_request_comments(pull_request_id, created_at, id);

CREATE TABLE pull_requests_new (
    id INTEGER PRIMARY KEY,
    repository_id INTEGER NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
    number INTEGER NOT NULL CHECK (number > 0),
    title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 256),
    body TEXT NOT NULL CHECK (length(body) <= 65536),
    source_ref TEXT NOT NULL CHECK (length(source_ref) BETWEEN 1 AND 200),
    target_ref TEXT NOT NULL CHECK (length(target_ref) BETWEEN 1 AND 200),
    head_sha TEXT NOT NULL CHECK (length(head_sha) IN (40, 64)),
    base_sha TEXT NOT NULL CHECK (length(base_sha) IN (40, 64)),
    state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'closed', 'merged', 'queued')),
    author_sub TEXT NOT NULL CHECK (length(author_sub) BETWEEN 1 AND 256),
    author_display TEXT NOT NULL CHECK (length(author_display) BETWEEN 1 AND 256),
    merge_commit TEXT CHECK (merge_commit IS NULL OR length(merge_commit) IN (40, 64)),
    created_at INTEGER NOT NULL DEFAULT (unixepoch()),
    updated_at INTEGER NOT NULL DEFAULT (unixepoch()),
    closed_at INTEGER,
    merged_at INTEGER,
    UNIQUE(repository_id, number)
) STRICT;

INSERT INTO pull_requests_new
    SELECT id, repository_id, number, title, body, source_ref, target_ref,
           head_sha, base_sha, state, author_sub, author_display, merge_commit,
           created_at, updated_at, closed_at, merged_at
    FROM pull_requests;

DROP TABLE pull_requests;
ALTER TABLE pull_requests_new RENAME TO pull_requests;

CREATE INDEX pull_requests_repository_state ON pull_requests(repository_id, state, updated_at DESC);
CREATE INDEX pull_requests_repository_number ON pull_requests(repository_id, number);
CREATE UNIQUE INDEX pull_requests_one_active_per_branch_pair
    ON pull_requests(repository_id, source_ref, target_ref)
    WHERE state IN ('open', 'queued');

PRAGMA user_version = 14;