| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { sql } |
| 3 | import { |
| 4 | index, |
| 5 | integer, |
| 6 | pgEnum, |
| 7 | pgTable, |
| 8 | text, |
| 9 | timestamp, |
| 10 | uniqueIndex, |
| 11 | } from "drizzle-orm/pg-core"; |
| 12 | import { user } |
| 13 | import { repository } |
| 14 | |
| 15 | export const pullRequestState = |
| 16 | "open", |
| 17 | "closed", |
| 18 | "merged", |
| 19 | ]); |
| 20 | |
| 21 | /** |
| 22 | * Base and head are stored as separate repository ids because they are only the same row for a branch-to-branch request; a fork request spans two write-ahead logs that never learn about each other. |
| 23 | * |
| 24 | * `headSha` is the tip as of the last time the request was refreshed, so the diff a reviewer read stays addressable after the branch moves on. |
| 25 | */ |
| 26 | export const pullRequest = |
| 27 | "pull_request", |
| 28 | { |
| 29 | id: text() |
| 30 | .primaryKey() |
| 31 | .unique() |
| 32 | .notNull() |
| 33 | .$defaultFn(() => |
| 34 | // per base repository, 1-based, and what the URL carries |
| 35 | number: integer().notNull |
| 36 | title: text().notNull |
| 37 | body: text(), |
| 38 | state: pullRequestState().notNull |
| 39 | |
| 40 | baseRepositoryId: text() |
| 41 | .references(() => |
| 42 | .notNull(), |
| 43 | baseRef: text().notNull |
| 44 | headRepositoryId: text() |
| 45 | .references(() => |
| 46 | .notNull(), |
| 47 | headRef: text().notNull |
| 48 | headSha: text().notNull |
| 49 | |
| 50 | mergeCommitSha: text(), |
| 51 | authorId: text() |
| 52 | .references(() => |
| 53 | .notNull(), |
| 54 | |
| 55 | closedAt: timestamp({ |
| 56 | mergedAt: timestamp({ |
| 57 | createdAt: timestamp({ |
| 58 | updatedAt: timestamp({ |
| 59 | .notNull() |
| 60 | .defaultNow() |
| 61 | .$onUpdateFn(() => |
| 62 | }, |
| 63 | (t) => |
| 64 | uniqueIndex("pull_request_base_number_idx"). |
| 65 | t.baseRepositoryId, |
| 66 | t.number, |
| 67 | ), |
| 68 | |
| 69 | // One open request per branch pair. Closed and merged rows are history, so the constraint has to be partial or reopening the same branch is blocked. |
| 70 | uniqueIndex("pull_request_open_branch_idx") |
| 71 | .on(t.baseRepositoryId |
| 72 | .where(sql`${ |
| 73 | |
| 74 | index("pull_request_head_idx"). |
| 75 | index("pull_request_base_state_idx"). |
| 76 | ], |
| 77 | ); |
| 78 | |
| 79 | /** Timeline comments on a pull request, oldest first. */ |
| 80 | export const pullRequestComment = |
| 81 | "pull_request_comment", |
| 82 | { |
| 83 | id: text() |
| 84 | .primaryKey() |
| 85 | .unique() |
| 86 | .notNull() |
| 87 | .$defaultFn(() => |
| 88 | pullRequestId: text() |
| 89 | .references(() => |
| 90 | .notNull(), |
| 91 | authorId: text() |
| 92 | .references(() => |
| 93 | .notNull(), |
| 94 | body: text().notNull |
| 95 | |
| 96 | createdAt: timestamp({ |
| 97 | updatedAt: timestamp({ |
| 98 | .notNull() |
| 99 | .defaultNow() |
| 100 | .$onUpdateFn(() => |
| 101 | }, |
| 102 | (t) => |
| 103 | index("pull_request_comment_pull_request_idx"). |
| 104 | t.pullRequestId, |
| 105 | t.createdAt, |
| 106 | ), |
| 107 | ], |
| 108 | ); |