Vectrum
Sign In
31 lines · 1.5 KB · JavaScript
// Forks: a repository can be a copy of another one (`fork_of`), and pull requests can come from a
// fork's branch. Such a pull request lives in the repository it proposes changes to; its head commit
// is fetched there as refs/pull/<number>/head (like on GitHub), so its changes stay readable even
// after the fork is deleted. `head_repo_name` keeps the fork's name for that case.

migrate(
  (app) => {
    const repos = app.findCollectionByNameOrId("repos");
    repos.fields.add(new RelationField({ name: "fork_of", collectionId: repos.id, maxSelect: 1, cascadeDelete: false }));
    repos.indexes.push("CREATE INDEX idx_repos_fork_of ON repos (fork_of)");
    app.save(repos);

    const pulls = app.findCollectionByNameOrId("pull_requests");
    pulls.fields.add(new RelationField({ name: "head_repo", collectionId: repos.id, maxSelect: 1, cascadeDelete: false }));
    pulls.fields.add(new TextField({ name: "head_repo_name", max: 200 }));
    pulls.indexes.push("CREATE INDEX idx_pull_requests_head_repo ON pull_requests (head_repo, head, state)");
    app.save(pulls);
  },
  (app) => {
    const pulls = app.findCollectionByNameOrId("pull_requests");
    pulls.indexes = pulls.indexes.filter((i) => !i.includes("idx_pull_requests_head_repo"));
    pulls.fields.removeByName("head_repo");
    pulls.fields.removeByName("head_repo_name");
    app.save(pulls);

    const repos = app.findCollectionByNameOrId("repos");
    repos.indexes = repos.indexes.filter((i) => !i.includes("idx_repos_fork_of"));
    repos.fields.removeByName("fork_of");
    app.save(repos);
  },
);