| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | bigint, |
| 4 | check, |
| 5 | index, |
| 6 | primaryKey, |
| 7 | uniqueIndex, |
| 8 | pgTable, |
| 9 | text, |
| 10 | timestamp, |
| 11 | } from "drizzle-orm/pg-core"; |
| 12 | import { sql } |
| 13 | |
| 14 | import { organization, user |
| 15 | import { repository } |
| 16 | |
| 17 | /** Per-account overrides of the instance's storage limits. A null limit falls back to the environment's. */ |
| 18 | export const storageLimit = |
| 19 | "storage_limit", |
| 20 | { |
| 21 | id: text() |
| 22 | .primaryKey() |
| 23 | .$defaultFn(() => |
| 24 | userId: text() |
| 25 | .unique() |
| 26 | .references(() => |
| 27 | organizationId: text() |
| 28 | .unique() |
| 29 | .references(() => |
| 30 | repositoryBytes: bigint({ |
| 31 | forkBytes: bigint({ |
| 32 | lfsBytes: bigint({ |
| 33 | assetBytes: bigint({ |
| 34 | updatedAt: timestamp({ |
| 35 | .notNull() |
| 36 | .defaultNow() |
| 37 | .$onUpdateFn(() => |
| 38 | }, |
| 39 | () => [ |
| 40 | check( |
| 41 | "storage_limit_owner", |
| 42 | sql`num_nonnulls(user_id, organization_id) = 1`, |
| 43 | ), |
| 44 | ], |
| 45 | ); |
| 46 | |
| 47 | /** Bytes a repository's log holds that its account is billed for, one row per WAL entry. A fork has rows for the entries it copied from its parent, under the same ulids. */ |
| 48 | export const repositoryLogEntry = |
| 49 | "repository_log_entry", |
| 50 | { |
| 51 | repositoryId: text() |
| 52 | .references(() => |
| 53 | .notNull(), |
| 54 | ulid: text().notNull |
| 55 | size: bigint({ |
| 56 | createdAt: timestamp({ |
| 57 | }, |
| 58 | (t) => |
| 59 | ); |
| 60 | |
| 61 | /** A Git LFS object a repository holds, stored at `lfs/<repositoryId>/<oid>`. A null `uploadedAt` is a reservation for an upload still in flight. */ |
| 62 | export const lfsObject = |
| 63 | "lfs_object", |
| 64 | { |
| 65 | repositoryId: text() |
| 66 | .references(() => |
| 67 | .notNull(), |
| 68 | oid: text().notNull |
| 69 | size: bigint({ |
| 70 | uploadedAt: timestamp({ |
| 71 | // Which upload holds the reservation, so one whose reservation lapsed and was taken cannot clean up after its successor. Null for rows a fork or merge copied. |
| 72 | uploadId: text(), |
| 73 | createdAt: timestamp({ |
| 74 | }, |
| 75 | (t) => |
| 76 | ); |
| 77 | |
| 78 | /** A Git LFS lock: one person's claim on a path, which git-lfs checks before it pushes. */ |
| 79 | export const lfsLock = |
| 80 | "lfs_lock", |
| 81 | { |
| 82 | id: text() |
| 83 | .primaryKey() |
| 84 | .$defaultFn(() => |
| 85 | repositoryId: text() |
| 86 | .references(() => |
| 87 | .notNull(), |
| 88 | path: text().notNull |
| 89 | ownerId: text() |
| 90 | .references(() => |
| 91 | .notNull(), |
| 92 | createdAt: timestamp({ |
| 93 | }, |
| 94 | (t) => |
| 95 | uniqueIndex("lfs_lock_repository_path_idx"). |
| 96 | ], |
| 97 | ); |
| 98 | |
| 99 | /** A file attached to an issue, pull request, comment or release notes, stored at `attachments/<repositoryId>/<id>`. A null `uploadedAt` is a reservation for an upload still in flight. Nothing links it to the text that embeds it: rows no text in the repository mentions are swept away. */ |
| 100 | export const attachment = |
| 101 | "attachment", |
| 102 | { |
| 103 | id: text() |
| 104 | .primaryKey() |
| 105 | .$defaultFn(() => |
| 106 | repositoryId: text() |
| 107 | .references(() => |
| 108 | .notNull(), |
| 109 | name: text().notNull |
| 110 | contentType: text().notNull |
| 111 | size: bigint({ |
| 112 | uploaderId: text().references |
| 113 | uploadedAt: timestamp({ |
| 114 | createdAt: timestamp({ |
| 115 | }, |
| 116 | (t) => |
| 117 | index("attachment_repository_idx"). |
| 118 | index("attachment_created_idx"). |
| 119 | ], |
| 120 | ); |