| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { sql } |
| 3 | import { |
| 4 | index, |
| 5 | integer, |
| 6 | pgEnum, |
| 7 | pgTable, |
| 8 | text, |
| 9 | timestamp, |
| 10 | uuid, |
| 11 | } from "drizzle-orm/pg-core"; |
| 12 | import { user } |
| 13 | import { repository } |
| 14 | |
| 15 | /** Seeded by migration 0038. Its username has a hyphen, which Better Auth's username validator refuses, so no person can ever register it. */ |
| 16 | export const IMPORTER_USER_ID = |
| 17 | |
| 18 | export const repositoryImportStatus = |
| 19 | "pending", |
| 20 | "running", |
| 21 | "succeeded", |
| 22 | "failed", |
| 23 | ]); |
| 24 | |
| 25 | /** |
| 26 | * One GitHub import per repository, driven by the API and run by `apps/importer`. |
| 27 | * |
| 28 | * `claimToken` changes on every attempt and every importer callback must carry it, so an attempt the API gave up on cannot write after its retry started. `leaseUntil` is pushed forward by each callback; a running row past it is an importer that went silent. |
| 29 | */ |
| 30 | export const repositoryImport = |
| 31 | "repository_import", |
| 32 | { |
| 33 | id: text() |
| 34 | .primaryKey() |
| 35 | .$defaultFn(() => |
| 36 | repositoryId: text() |
| 37 | .references(() => |
| 38 | .notNull() |
| 39 | .unique(), |
| 40 | requestedById: text() |
| 41 | .references(() => |
| 42 | .notNull(), |
| 43 | // `owner/name` on github.com. |
| 44 | source: text().notNull |
| 45 | status: repositoryImportStatus().notNull |
| 46 | attempts: integer().notNull |
| 47 | nextAttemptAt: timestamp({ |
| 48 | leaseUntil: timestamp({ |
| 49 | claimToken: uuid().notNull |
| 50 | // The push key minted for the current attempt, deleted when the attempt ends. |
| 51 | apiKeyId: text(), |
| 52 | lastError: text(), |
| 53 | createdAt: timestamp({ |
| 54 | updatedAt: timestamp({ |
| 55 | .notNull() |
| 56 | .defaultNow() |
| 57 | .$onUpdateFn(() => |
| 58 | }, |
| 59 | (t) => |
| 60 | index("repository_import_active_idx") |
| 61 | .on(t.nextAttemptAt |
| 62 | .where(sql`${ |
| 63 | ], |
| 64 | ); |