| 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 } from "./auth.js"; |
| 15 | import { isNotNull, isNull } from "drizzle-orm"; |
| 16 | |
| 17 | export const repositoryVisiblity = pgEnum("repository_visibility", [ |
| 18 | "public", |
| 19 | "private", |
| 20 | ]); |
| 21 | |
| 22 | export const repository = pgTable( |
| 23 | "repository", |
| 24 | { |
| 25 | id: text() |
| 26 | .primaryKey() |
| 27 | .unique() |
| 28 | .notNull() |
| 29 | .$defaultFn(() => `repo_${createId()}`), |
| 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(() => organization.id, { |
| 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(() => user.id, { onDelete: "cascade" }) |
| 40 | .notNull(), |
| 41 | |
| 42 | visibility: repositoryVisiblity().notNull().default("private"), |
| 43 | // Branch name without `refs/heads/`. Null lets the materializer pick main, master or the first branch. |
| 44 | defaultBranch: text(), |
| 45 | lastPushedAt: timestamp({ withTimezone: true }).notNull().defaultNow(), |
| 46 | // AnyPgColumn breaks the circular inference a self-reference would otherwise cause |
| 47 | parentRepositoryId: text().references((): AnyPgColumn => repository.id, { |
| 48 | onDelete: "set null", |
| 49 | }), |
| 50 | |
| 51 | createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(), |
| 52 | updatedAt: timestamp({ withTimezone: true }) |
| 53 | .notNull() |
| 54 | .defaultNow() |
| 55 | .$onUpdateFn(() => new Date()), |
| 56 | }, |
| 57 | (t) => [ |
| 58 | uniqueIndex("repository_org_slug_idx") |
| 59 | .on(t.organizationId, t.slug) |
| 60 | .where(isNotNull(t.organizationId)), |
| 61 | |
| 62 | uniqueIndex("repository_owner_slug_idx") |
| 63 | .on(t.ownerId, t.slug) |
| 64 | .where(isNull(t.organizationId)), |
| 65 | ], |
| 66 | ); |
| 67 | |
| 68 | /** |
| 69 | * Denormalized "which commit last touched this path" for a single ref, so a directory listing costs one indexed query instead of one `git log` per entry. |
| 70 | * |
| 71 | * Keyed by path, with a row for every ancestor directory, so `src` carries the newest commit under `src/`. Deleted paths keep their row and are hidden by the join against `ls-tree`. |
| 72 | */ |
| 73 | export const repositoryPathCommit = pgTable( |
| 74 | "repository_path_commit", |
| 75 | { |
| 76 | repositoryId: text() |
| 77 | .references(() => repository.id, { onDelete: "cascade" }) |
| 78 | .notNull(), |
| 79 | ref: text().notNull(), |
| 80 | // "" is the repository root, so its row is the tip commit of the ref |
| 81 | path: text().notNull(), |
| 82 | commitSha: text().notNull(), |
| 83 | committedAt: timestamp({ withTimezone: true }).notNull(), |
| 84 | subject: text().notNull(), |
| 85 | }, |
| 86 | (t) => [primaryKey({ columns: [t.repositoryId, t.ref, t.path] })], |
| 87 | ); |
| 88 | |
| 89 | /** How far `repository_path_commit` has been walked for a ref. A stored sha still reachable from the tip means the next walk covers only new commits; anything else forces a rebuild. */ |
| 90 | export const repositoryRefIndex = pgTable( |
| 91 | "repository_ref_index", |
| 92 | { |
| 93 | repositoryId: text() |
| 94 | .references(() => repository.id, { onDelete: "cascade" }) |
| 95 | .notNull(), |
| 96 | ref: text().notNull(), |
| 97 | indexedCommitSha: text().notNull(), |
| 98 | updatedAt: timestamp({ withTimezone: true }) |
| 99 | .notNull() |
| 100 | .defaultNow() |
| 101 | .$onUpdateFn(() => new Date()), |
| 102 | }, |
| 103 | (t) => [primaryKey({ columns: [t.repositoryId, t.ref] })], |
| 104 | ); |
| 105 | |
| 106 | export const repositoryLanguageStat = pgTable( |
| 107 | "repository_language_stat", |
| 108 | { |
| 109 | repositoryId: text() |
| 110 | .references(() => repository.id, { onDelete: "cascade" }) |
| 111 | .notNull(), |
| 112 | ref: text().notNull(), |
| 113 | language: text().notNull(), |
| 114 | bytes: bigint({ mode: "number" }).notNull(), |
| 115 | }, |
| 116 | (t) => [primaryKey({ columns: [t.repositoryId, t.ref, t.language] })], |
| 117 | ); |
| 118 | |
| 119 | /** How far the language stats have been computed for a ref. A stored sha that is still an ancestor of the tip lets the next sync apply only the changed blobs; anything else forces a full `ls-tree` recount. */ |
| 120 | export const repositoryLanguageIndex = pgTable( |
| 121 | "repository_language_index", |
| 122 | { |
| 123 | repositoryId: text() |
| 124 | .references(() => repository.id, { onDelete: "cascade" }) |
| 125 | .notNull(), |
| 126 | ref: text().notNull(), |
| 127 | indexedCommitSha: text().notNull(), |
| 128 | updatedAt: timestamp({ withTimezone: true }) |
| 129 | .notNull() |
| 130 | .defaultNow() |
| 131 | .$onUpdateFn(() => new Date()), |
| 132 | }, |
| 133 | (t) => [primaryKey({ columns: [t.repositoryId, t.ref] })], |
| 134 | ); |
| 135 | |
| 136 | /** |
| 137 | * Daily commit counts per author on a repository's default branch, so the profile contribution graph costs one indexed query instead of materializing and walking every repository the user owns. |
| 138 | * |
| 139 | * One row per author email, lowercased. `authorId` is resolved at sync time and stays nullable, and readers match on both, so adding an email to an account never needs a reindex. |
| 140 | */ |
| 141 | export const repositoryContribution = pgTable( |
| 142 | "repository_contribution", |
| 143 | { |
| 144 | repositoryId: text("repository_id") |
| 145 | .references(() => repository.id, { onDelete: "cascade" }) |
| 146 | .notNull(), |
| 147 | authorEmail: text("author_email").notNull(), |
| 148 | // UTC calendar day, YYYY-MM-DD. |
| 149 | day: date("day").notNull(), |
| 150 | // Name from the author's newest indexed commit. |
| 151 | authorName: text("author_name").notNull(), |
| 152 | // Linked account, if the author email matches one. Null for not-yet-registered authors; backfilled once they register. |
| 153 | authorId: text("author_id").references(() => user.id, { |
| 154 | onDelete: "set null", |
| 155 | }), |
| 156 | commits: integer("commits").notNull().default(0), |
| 157 | }, |
| 158 | (t) => [primaryKey({ columns: [t.repositoryId, t.authorEmail, t.day] })], |
| 159 | ); |
| 160 | |
| 161 | /** How far `repository_contribution` has been walked. One cursor per repository, since only the default branch is indexed; an unreachable sha forces a rebuild. */ |
| 162 | export const repositoryContributionIndex = pgTable( |
| 163 | "repository_contribution_index", |
| 164 | { |
| 165 | repositoryId: text("repository_id") |
| 166 | .references(() => repository.id, { onDelete: "cascade" }) |
| 167 | .notNull(), |
| 168 | indexedCommitSha: text("indexed_commit_sha").notNull(), |
| 169 | updatedAt: timestamp("updated_at", { withTimezone: true }) |
| 170 | .notNull() |
| 171 | .defaultNow() |
| 172 | .$onUpdateFn(() => new Date()), |
| 173 | }, |
| 174 | (t) => [primaryKey({ columns: [t.repositoryId] })], |
| 175 | ); |