| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | bigint, |
| 4 | date, |
| 5 | integer, |
| 6 | pgEnum, |
| 7 | pgTable, |
| 8 | primaryKey, |
| 9 | text, |
| 10 | timestamp, |
| 11 | uniqueIndex, |
| 12 | type AnyPgColumn, |
| 13 | } from "drizzle-orm/pg-core"; |
| 14 | import { organization, user |
| 15 | import { isNotNull, isNull |
| 16 | |
| 17 | export const repositoryVisiblity = |
| 18 | "public", |
| 19 | "private", |
| 20 | ]); |
| 21 | |
| 22 | export const repository = |
| 23 | "repository", |
| 24 | { |
| 25 | id: text() |
| 26 | .primaryKey() |
| 27 | .unique() |
| 28 | .notNull() |
| 29 | .$defaultFn(() => |
| 30 | name: text().notNull |
| 31 | slug: text().notNull |
| 32 | description: text(), |
| 33 | // nullable cus a repo can also belong to just a user |
| 34 | organizationId: text().references |
| 35 | onDelete: "cascade", |
| 36 | }), |
| 37 | // a repo will always have an owner. will also allow them to transfer the ownership incase they wanna preserve the repo but delete the account |
| 38 | ownerId: text() |
| 39 | .references(() => |
| 40 | .notNull(), |
| 41 | |
| 42 | visibility: repositoryVisiblity().notNull |
| 43 | lastPushedAt: timestamp({ |
| 44 | // AnyPgColumn breaks the circular inference a self-reference would otherwise cause |
| 45 | parentRepositoryId: text().references |
| 46 | onDelete: "set null", |
| 47 | }), |
| 48 | |
| 49 | createdAt: timestamp({ |
| 50 | updatedAt: timestamp({ |
| 51 | .notNull() |
| 52 | .defaultNow() |
| 53 | .$onUpdateFn(() => |
| 54 | }, |
| 55 | (t) => |
| 56 | uniqueIndex("repository_org_slug_idx") |
| 57 | .on(t.organizationId |
| 58 | .where(isNotNull(t |
| 59 | |
| 60 | uniqueIndex("repository_owner_slug_idx") |
| 61 | .on(t.ownerId |
| 62 | .where(isNull(t |
| 63 | ], |
| 64 | ); |
| 65 | |
| 66 | /** |
| 67 | * Denormalized "which commit last touched this path" for a single ref, so a |
| 68 | * directory listing costs one indexed query instead of one `git log` per entry. |
| 69 | * |
| 70 | * Rows are keyed by path, and every ancestor directory of a changed file gets a |
| 71 | * row too, so `src` carries the newest commit under `src/`. Paths deleted from |
| 72 | * the tree keep their row; the read path joins against `ls-tree`, so they are |
| 73 | * invisible until something prunes them. |
| 74 | */ |
| 75 | export const repositoryPathCommit = |
| 76 | "repository_path_commit", |
| 77 | { |
| 78 | repositoryId: text() |
| 79 | .references(() => |
| 80 | .notNull(), |
| 81 | ref: text().notNull |
| 82 | // "" is the repository root, so its row is the tip commit of the ref |
| 83 | path: text().notNull |
| 84 | commitSha: text().notNull |
| 85 | committedAt: timestamp({ |
| 86 | subject: text().notNull |
| 87 | }, |
| 88 | (t) => |
| 89 | ); |
| 90 | |
| 91 | /** |
| 92 | * How far `repository_path_commit` has been walked for a ref. A stored sha that |
| 93 | * is still an ancestor of the tip means the next walk only has to cover the new |
| 94 | * commits; anything else (force push, dropped objects) forces a full rebuild. |
| 95 | */ |
| 96 | export const repositoryRefIndex = |
| 97 | "repository_ref_index", |
| 98 | { |
| 99 | repositoryId: text() |
| 100 | .references(() => |
| 101 | .notNull(), |
| 102 | ref: text().notNull |
| 103 | indexedCommitSha: text().notNull |
| 104 | updatedAt: timestamp({ |
| 105 | .notNull() |
| 106 | .defaultNow() |
| 107 | .$onUpdateFn(() => |
| 108 | }, |
| 109 | (t) => |
| 110 | ); |
| 111 | |
| 112 | export const repositoryLanguageStat = |
| 113 | "repository_language_stat", |
| 114 | { |
| 115 | repositoryId: text() |
| 116 | .references(() => |
| 117 | .notNull(), |
| 118 | ref: text().notNull |
| 119 | language: text().notNull |
| 120 | bytes: bigint({ |
| 121 | }, |
| 122 | (t) => |
| 123 | ); |
| 124 | |
| 125 | /** |
| 126 | * How far the language stats have been computed for a ref. A stored sha that is |
| 127 | * still an ancestor of the tip lets the next sync apply only the changed blobs; |
| 128 | * anything else forces a full `ls-tree` recount. |
| 129 | */ |
| 130 | export const repositoryLanguageIndex = |
| 131 | "repository_language_index", |
| 132 | { |
| 133 | repositoryId: text() |
| 134 | .references(() => |
| 135 | .notNull(), |
| 136 | ref: text().notNull |
| 137 | indexedCommitSha: text().notNull |
| 138 | updatedAt: timestamp({ |
| 139 | .notNull() |
| 140 | .defaultNow() |
| 141 | .$onUpdateFn(() => |
| 142 | }, |
| 143 | (t) => |
| 144 | ); |
| 145 | |
| 146 | /** |
| 147 | * Daily commit counts per author on a repository's default branch, so the |
| 148 | * profile contribution graph costs one indexed query instead of materializing |
| 149 | * and walking every repository the user owns. |
| 150 | * |
| 151 | * The grain is a git fact: emails are stored lowercased (`Alice@x.com` and |
| 152 | * `alice@x.com` count as one author). `authorId` links that fact to an |
| 153 | * account, resolved at sync time and nullable when the author has no account |
| 154 | * (yet) - accounts and their emails change, history does not. Readers match on |
| 155 | * both, so adding an email to an account never needs a reindex. |
| 156 | */ |
| 157 | export const repositoryContribution = |
| 158 | "repository_contribution", |
| 159 | { |
| 160 | repositoryId: text("repository_id |
| 161 | .references(() => |
| 162 | .notNull(), |
| 163 | authorEmail: text("author_email |
| 164 | // UTC calendar day, YYYY-MM-DD. |
| 165 | day: date("day |
| 166 | // Name from the author's newest indexed commit. |
| 167 | authorName: text("author_name |
| 168 | // Linked account, if the author email matches one. Null for |
| 169 | // not-yet-registered authors; backfilled once they register. |
| 170 | authorId: text("author_id |
| 171 | onDelete: "set null", |
| 172 | }), |
| 173 | commits: integer("commits |
| 174 | }, |
| 175 | (t) => |
| 176 | ); |
| 177 | |
| 178 | /** |
| 179 | * How far `repository_contribution` has been walked. One cursor per repository, |
| 180 | * since only the default branch is indexed. A stored sha that is still an |
| 181 | * ancestor of the default tip means the next sync only covers the new commits; |
| 182 | * anything else (force push, dropped objects) forces a full rebuild. |
| 183 | */ |
| 184 | export const repositoryContributionIndex = |
| 185 | "repository_contribution_index", |
| 186 | { |
| 187 | repositoryId: text("repository_id |
| 188 | .references(() => |
| 189 | .notNull(), |
| 190 | indexedCommitSha: text("indexed_commit_sha |
| 191 | updatedAt: timestamp("updated_at |
| 192 | .notNull() |
| 193 | .defaultNow() |
| 194 | .$onUpdateFn(() => |
| 195 | }, |
| 196 | (t) => |
| 197 | ); |