| 1 | import { |
| 2 | boolean, |
| 3 | index, |
| 4 | integer, |
| 5 | pgTable, |
| 6 | primaryKey, |
| 7 | text, |
| 8 | timestamp, |
| 9 | uniqueIndex, |
| 10 | } from "drizzle-orm/pg-core"; |
| 11 | import { sql } from "drizzle-orm"; |
| 12 | |
| 13 | import { organization, team, user } from "./auth.js"; |
| 14 | import { repositoryRole } from "./collaborators.js"; |
| 15 | import { repository } from "./repository.js"; |
| 16 | |
| 17 | /** What Ghost knows about an organization beyond Better Auth's row: its policies and its public profile. One row per organization, written when it is created. */ |
| 18 | export const organizationSettings = pgTable("organization_settings", { |
| 19 | organizationId: text() |
| 20 | .primaryKey() |
| 21 | .references(() => organization.id, { onDelete: "cascade" }), |
| 22 | // Every member's floor on every repository. Null is no access beyond teams and collaborations. |
| 23 | basePermission: repositoryRole().default("read"), |
| 24 | membersCanCreatePublicRepositories: boolean().notNull().default(true), |
| 25 | membersCanCreatePrivateRepositories: boolean().notNull().default(true), |
| 26 | allowPrivateForks: boolean().notNull().default(false), |
| 27 | // Recorded on each new repository, and followed once a branch by that name is pushed. |
| 28 | defaultBranch: text(), |
| 29 | description: text(), |
| 30 | website: text(), |
| 31 | location: text(), |
| 32 | email: text(), |
| 33 | updatedAt: timestamp({ withTimezone: true }) |
| 34 | .notNull() |
| 35 | .defaultNow() |
| 36 | .$onUpdateFn(() => new Date()), |
| 37 | }); |
| 38 | |
| 39 | /** Members who chose to show their membership. Membership is private otherwise. */ |
| 40 | export const organizationPublicMember = pgTable( |
| 41 | "organization_public_member", |
| 42 | { |
| 43 | organizationId: text() |
| 44 | .references(() => organization.id, { onDelete: "cascade" }) |
| 45 | .notNull(), |
| 46 | userId: text() |
| 47 | .references(() => user.id, { onDelete: "cascade" }) |
| 48 | .notNull(), |
| 49 | }, |
| 50 | (t) => [primaryKey({ columns: [t.organizationId, t.userId] })], |
| 51 | ); |
| 52 | |
| 53 | export const organizationPinnedRepository = pgTable( |
| 54 | "organization_pinned_repository", |
| 55 | { |
| 56 | organizationId: text() |
| 57 | .references(() => organization.id, { onDelete: "cascade" }) |
| 58 | .notNull(), |
| 59 | repositoryId: text() |
| 60 | .references(() => repository.id, { onDelete: "cascade" }) |
| 61 | .notNull(), |
| 62 | position: integer().notNull(), |
| 63 | }, |
| 64 | (t) => [primaryKey({ columns: [t.organizationId, t.repositoryId] })], |
| 65 | ); |
| 66 | |
| 67 | /** Members who may manage one team's membership without administering the organization. */ |
| 68 | export const teamMaintainer = pgTable( |
| 69 | "team_maintainer", |
| 70 | { |
| 71 | teamId: text() |
| 72 | .references(() => team.id, { onDelete: "cascade" }) |
| 73 | .notNull(), |
| 74 | userId: text() |
| 75 | .references(() => user.id, { onDelete: "cascade" }) |
| 76 | .notNull(), |
| 77 | }, |
| 78 | (t) => [primaryKey({ columns: [t.teamId, t.userId] })], |
| 79 | ); |
| 80 | |
| 81 | /** Where a repository used to be, so links and git remotes survive a rename or a transfer. A repository created at the old name takes precedence. */ |
| 82 | export const repositoryRedirect = pgTable( |
| 83 | "repository_redirect", |
| 84 | { |
| 85 | ownerName: text().notNull(), |
| 86 | slug: text().notNull(), |
| 87 | repositoryId: text() |
| 88 | .references(() => repository.id, { onDelete: "cascade" }) |
| 89 | .notNull(), |
| 90 | createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(), |
| 91 | }, |
| 92 | (t) => [ |
| 93 | uniqueIndex("repository_redirect_name_idx").on( |
| 94 | sql`lower(${t.ownerName})`, |
| 95 | t.slug, |
| 96 | ), |
| 97 | index("repository_redirect_repository_idx").on(t.repositoryId), |
| 98 | ], |
| 99 | ); |
| 100 | |
| 101 | /** A transfer waiting for its recipient: a user accepts for themselves, an organization's admins for it. Exactly one of the two targets is set. */ |
| 102 | export const repositoryTransfer = pgTable("repository_transfer", { |
| 103 | repositoryId: text() |
| 104 | .primaryKey() |
| 105 | .references(() => repository.id, { onDelete: "cascade" }), |
| 106 | toUserId: text().references(() => user.id, { onDelete: "cascade" }), |
| 107 | toOrganizationId: text().references(() => organization.id, { |
| 108 | onDelete: "cascade", |
| 109 | }), |
| 110 | requestedById: text().references(() => user.id, { onDelete: "set null" }), |
| 111 | createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(), |
| 112 | }); |