| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | bigint, |
| 4 | boolean, |
| 5 | index, |
| 6 | integer, |
| 7 | pgEnum, |
| 8 | pgTable, |
| 9 | text, |
| 10 | timestamp, |
| 11 | uniqueIndex, |
| 12 | } from "drizzle-orm/pg-core"; |
| 13 | import { user } |
| 14 | import { repository } |
| 15 | |
| 16 | /** Notes attached to a tag. The tag itself lives in git, so a release outlives a deleted tag and simply stops pointing at a commit. */ |
| 17 | export const release = |
| 18 | "release", |
| 19 | { |
| 20 | id: text() |
| 21 | .primaryKey() |
| 22 | .$defaultFn(() => |
| 23 | repositoryId: text() |
| 24 | .references(() => |
| 25 | .notNull(), |
| 26 | // Tag name without `refs/tags/`. |
| 27 | tagName: text().notNull |
| 28 | // Null reads as the tag name. |
| 29 | name: text(), |
| 30 | body: text(), |
| 31 | isDraft: boolean().notNull |
| 32 | isPrerelease: boolean().notNull |
| 33 | authorId: text().references |
| 34 | // Null while a draft; set once, when it is first published. |
| 35 | publishedAt: timestamp({ |
| 36 | createdAt: timestamp({ |
| 37 | updatedAt: timestamp({ |
| 38 | .notNull() |
| 39 | .defaultNow() |
| 40 | .$onUpdateFn(() => |
| 41 | }, |
| 42 | (t) => |
| 43 | uniqueIndex("release_repository_tag_idx"). |
| 44 | index("release_repository_created_idx"). |
| 45 | ], |
| 46 | ); |
| 47 | |
| 48 | /** `uploading` rows are reservations: they count against the owner's quota while the bytes stream to object storage, so two concurrent uploads cannot both squeeze under it. */ |
| 49 | export const releaseAssetState = |
| 50 | "uploading", |
| 51 | "uploaded", |
| 52 | ]); |
| 53 | |
| 54 | /** A file attached to a release. The bytes live in object storage under `release-assets/<repositoryId>/<id>`. */ |
| 55 | export const releaseAsset = |
| 56 | "release_asset", |
| 57 | { |
| 58 | id: text() |
| 59 | .primaryKey() |
| 60 | .$defaultFn(() => |
| 61 | releaseId: text() |
| 62 | .references(() => |
| 63 | .notNull(), |
| 64 | // Denormalized from the release, so an account's usage is one join to `repository` and the object key survives the release row. |
| 65 | repositoryId: text() |
| 66 | .references(() => |
| 67 | .notNull(), |
| 68 | name: text().notNull |
| 69 | contentType: text().notNull |
| 70 | size: bigint({ |
| 71 | state: releaseAssetState().notNull |
| 72 | downloadCount: integer().notNull |
| 73 | uploaderId: text().references |
| 74 | createdAt: timestamp({ |
| 75 | }, |
| 76 | (t) => |
| 77 | uniqueIndex("release_asset_release_name_idx"). |
| 78 | index("release_asset_repository_idx"). |
| 79 | ], |
| 80 | ); |