| 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 | |
| 14 | import { user } |
| 15 | import { repository } |
| 16 | |
| 17 | /** 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. */ |
| 18 | export const release = |
| 19 | "release", |
| 20 | { |
| 21 | id: text() |
| 22 | .primaryKey() |
| 23 | .$defaultFn(() => |
| 24 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 25 | numericId: bigint({ |
| 26 | .generatedAlwaysAsIdentity() |
| 27 | .notNull() |
| 28 | .unique(), |
| 29 | repositoryId: text() |
| 30 | .references(() => |
| 31 | .notNull(), |
| 32 | // Tag name without `refs/tags/`. |
| 33 | tagName: text().notNull |
| 34 | // Null reads as the tag name. |
| 35 | name: text(), |
| 36 | body: text(), |
| 37 | isDraft: boolean().notNull |
| 38 | isPrerelease: boolean().notNull |
| 39 | authorId: text().references |
| 40 | // Null while a draft; set once, when it is first published. |
| 41 | publishedAt: timestamp({ |
| 42 | createdAt: timestamp({ |
| 43 | updatedAt: timestamp({ |
| 44 | .notNull() |
| 45 | .defaultNow() |
| 46 | .$onUpdateFn(() => |
| 47 | }, |
| 48 | (t) => |
| 49 | uniqueIndex("release_repository_tag_idx"). |
| 50 | index("release_repository_created_idx"). |
| 51 | ], |
| 52 | ); |
| 53 | |
| 54 | /** `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. */ |
| 55 | export const releaseAssetState = |
| 56 | "uploading", |
| 57 | "uploaded", |
| 58 | ]); |
| 59 | |
| 60 | /** A file attached to a release. The bytes live in object storage under `release-assets/<repositoryId>/<id>`. */ |
| 61 | export const releaseAsset = |
| 62 | "release_asset", |
| 63 | { |
| 64 | id: text() |
| 65 | .primaryKey() |
| 66 | .$defaultFn(() => |
| 67 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 68 | numericId: bigint({ |
| 69 | .generatedAlwaysAsIdentity() |
| 70 | .notNull() |
| 71 | .unique(), |
| 72 | releaseId: text() |
| 73 | .references(() => |
| 74 | .notNull(), |
| 75 | // Denormalized from the release, so an account's usage is one join to `repository` and the object key survives the release row. |
| 76 | repositoryId: text() |
| 77 | .references(() => |
| 78 | .notNull(), |
| 79 | name: text().notNull |
| 80 | // GitHub's short description shown in place of the file name; null shows the name. |
| 81 | label: text(), |
| 82 | contentType: text().notNull |
| 83 | size: bigint({ |
| 84 | state: releaseAssetState().notNull |
| 85 | downloadCount: integer().notNull |
| 86 | uploaderId: text().references |
| 87 | createdAt: timestamp({ |
| 88 | updatedAt: timestamp({ |
| 89 | .notNull() |
| 90 | .defaultNow() |
| 91 | .$onUpdateFn(() => |
| 92 | }, |
| 93 | (t) => |
| 94 | uniqueIndex("release_asset_release_name_idx"). |
| 95 | index("release_asset_repository_idx"). |
| 96 | ], |
| 97 | ); |