| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | bigint, |
| 4 | boolean, |
| 5 | index, |
| 6 | integer, |
| 7 | pgEnum, |
| 8 | pgTable, |
| 9 | primaryKey, |
| 10 | text, |
| 11 | timestamp, |
| 12 | uniqueIndex, |
| 13 | } from "drizzle-orm/pg-core"; |
| 14 | import { user } |
| 15 | import { repository } |
| 16 | |
| 17 | export const issueState = |
| 18 | |
| 19 | export const issueEventType = |
| 20 | "opened", |
| 21 | "closed", |
| 22 | "reopened", |
| 23 | "renamed", |
| 24 | "edited", |
| 25 | "labeled", |
| 26 | "unlabeled", |
| 27 | "assigned", |
| 28 | "unassigned", |
| 29 | "merged", |
| 30 | "ready_for_review", |
| 31 | "converted_to_draft", |
| 32 | ]); |
| 33 | |
| 34 | export const issueReferenceSource = |
| 35 | "issue", |
| 36 | "comment", |
| 37 | "commit", |
| 38 | ]); |
| 39 | |
| 40 | /** |
| 41 | * GitHub-style issue. `number` is per repository, 1-based, and what the URL carries. A pull request is an issue with a `pull_request` row attached, so the two share one number sequence, one comment thread and one timeline. |
| 42 | * |
| 43 | * `commentCount` is denormalized so `sort=comments` is one indexed query instead of a join + group-by on every list call. Writers must bump it on comment create/delete. |
| 44 | */ |
| 45 | export const issue = |
| 46 | "issue", |
| 47 | { |
| 48 | id: text() |
| 49 | .primaryKey() |
| 50 | .unique() |
| 51 | .notNull() |
| 52 | .$defaultFn(() => |
| 53 | repositoryId: text() |
| 54 | .references(() => |
| 55 | .notNull(), |
| 56 | // per repository, 1-based, and what the URL carries |
| 57 | number: integer().notNull |
| 58 | title: text().notNull |
| 59 | body: text(), |
| 60 | state: issueState().notNull |
| 61 | isPullRequest: boolean().notNull |
| 62 | |
| 63 | authorId: text() |
| 64 | .references(() => |
| 65 | .notNull(), |
| 66 | closedById: text().references |
| 67 | |
| 68 | commentCount: integer().notNull |
| 69 | |
| 70 | closedAt: timestamp({ |
| 71 | createdAt: timestamp({ |
| 72 | updatedAt: timestamp({ |
| 73 | .notNull() |
| 74 | .defaultNow() |
| 75 | .$onUpdateFn(() => |
| 76 | }, |
| 77 | (t) => |
| 78 | uniqueIndex("issue_repo_number_idx"). |
| 79 | index("issue_repo_kind_state_idx"). |
| 80 | t.repositoryId, |
| 81 | t.isPullRequest, |
| 82 | t.state, |
| 83 | ), |
| 84 | index("issue_repo_updated_idx"). |
| 85 | ], |
| 86 | ); |
| 87 | |
| 88 | /** Timeline comments on an issue, oldest first. */ |
| 89 | export const issueComment = |
| 90 | "issue_comment", |
| 91 | { |
| 92 | id: text() |
| 93 | .primaryKey() |
| 94 | .unique() |
| 95 | .notNull() |
| 96 | .$defaultFn(() => |
| 97 | issueId: text() |
| 98 | .references(() => |
| 99 | .notNull(), |
| 100 | authorId: text() |
| 101 | .references(() => |
| 102 | .notNull(), |
| 103 | body: text().notNull |
| 104 | githubId: bigint({ |
| 105 | |
| 106 | createdAt: timestamp({ |
| 107 | updatedAt: timestamp({ |
| 108 | .notNull() |
| 109 | .defaultNow() |
| 110 | .$onUpdateFn(() => |
| 111 | }, |
| 112 | (t) => |
| 113 | index("issue_comment_issue_idx"). |
| 114 | uniqueIndex("issue_comment_issue_github_idx"). |
| 115 | ], |
| 116 | ); |
| 117 | |
| 118 | /** Repository-scoped labels, exactly like GitHub: a name, an optional description, and a 6-char hex color (stored without `#`). */ |
| 119 | export const label = |
| 120 | "label", |
| 121 | { |
| 122 | id: text() |
| 123 | .primaryKey() |
| 124 | .unique() |
| 125 | .notNull() |
| 126 | .$defaultFn(() => |
| 127 | repositoryId: text() |
| 128 | .references(() => |
| 129 | .notNull(), |
| 130 | name: text().notNull |
| 131 | description: text(), |
| 132 | // 6 hex chars, lowercase, no `#` — the client adds `#` when rendering |
| 133 | color: text().notNull |
| 134 | |
| 135 | createdAt: timestamp({ |
| 136 | updatedAt: timestamp({ |
| 137 | .notNull() |
| 138 | .defaultNow() |
| 139 | .$onUpdateFn(() => |
| 140 | }, |
| 141 | (t) => |
| 142 | uniqueIndex("label_repo_name_idx"). |
| 143 | index("label_repo_idx"). |
| 144 | ], |
| 145 | ); |
| 146 | |
| 147 | export const issueLabel = |
| 148 | "issue_label", |
| 149 | { |
| 150 | issueId: text() |
| 151 | .references(() => |
| 152 | .notNull(), |
| 153 | labelId: text() |
| 154 | .references(() => |
| 155 | .notNull(), |
| 156 | }, |
| 157 | (t) => |
| 158 | ); |
| 159 | |
| 160 | export const issueAssignee = |
| 161 | "issue_assignee", |
| 162 | { |
| 163 | issueId: text() |
| 164 | .references(() => |
| 165 | .notNull(), |
| 166 | userId: text() |
| 167 | .references(() => |
| 168 | .notNull(), |
| 169 | }, |
| 170 | (t) => |
| 171 | ); |
| 172 | |
| 173 | /** Audit timeline for an issue — what GitHub renders between comments. Only the columns relevant to `type` are set; a `closed` event carries `sourceIssueId` or `commitSha` when a pull request or commit closed it. */ |
| 174 | export const issueEvent = |
| 175 | "issue_event", |
| 176 | { |
| 177 | id: text() |
| 178 | .primaryKey() |
| 179 | .unique() |
| 180 | .notNull() |
| 181 | .$defaultFn(() => |
| 182 | issueId: text() |
| 183 | .references(() => |
| 184 | .notNull(), |
| 185 | actorId: text().references |
| 186 | type: issueEventType().notNull |
| 187 | |
| 188 | labelName: text(), |
| 189 | assigneeUsername: text(), |
| 190 | oldTitle: text(), |
| 191 | newTitle: text(), |
| 192 | sourceIssueId: text().references |
| 193 | commitSha: text(), |
| 194 | |
| 195 | createdAt: timestamp({ |
| 196 | }, |
| 197 | (t) => |
| 198 | ); |
| 199 | |
| 200 | /** |
| 201 | * One row per `#N` written somewhere, resolved when the text is saved so a timeline read is one indexed lookup on `targetIssueId`. |
| 202 | * |
| 203 | * `sourceId` is the issue id, comment id or commit sha the text lives in; an edit replaces every row with that `sourceId`. `sourceIssueId` is the issue or pull request the text belongs to, null for a commit. |
| 204 | */ |
| 205 | export const issueReference = |
| 206 | "issue_reference", |
| 207 | { |
| 208 | id: text() |
| 209 | .primaryKey() |
| 210 | .unique() |
| 211 | .notNull() |
| 212 | .$defaultFn(() => |
| 213 | sourceType: issueReferenceSource().notNull |
| 214 | sourceId: text().notNull |
| 215 | sourceRepositoryId: text() |
| 216 | .references(() => |
| 217 | .notNull(), |
| 218 | sourceIssueId: text().references |
| 219 | targetIssueId: text() |
| 220 | .references(() => |
| 221 | .notNull(), |
| 222 | closing: boolean().notNull |
| 223 | actorId: text().references |
| 224 | |
| 225 | createdAt: timestamp({ |
| 226 | }, |
| 227 | (t) => |
| 228 | uniqueIndex("issue_reference_source_target_idx"). |
| 229 | t.sourceType, |
| 230 | t.sourceId, |
| 231 | t.targetIssueId, |
| 232 | ), |
| 233 | index("issue_reference_target_idx"). |
| 234 | index("issue_reference_source_issue_idx"). |
| 235 | ], |
| 236 | ); |