| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { eq } |
| 3 | |
| 4 | import { RepositoryImportingError } |
| 5 | |
| 6 | /** An import writes GitHub's issue numbers, so nothing else may take one until it is done. Call inside the issue-inserting transaction: the shared lock on the import row serializes it against a retry's `FOR UPDATE`, so each sees the other's write. */ |
| 7 | export async function assertNotImporting |
| 8 | tx: Pick |
| 9 | repositoryId: string, |
| 10 | ) { |
| 11 | // Filtering by status in SQL would skip locking a failed row, which is the one a retry is about to flip. |
| 12 | const [row] |
| 13 | .select({ status: |
| 14 | .from(schema.repositoryImport |
| 15 | .where(eq(schema |
| 16 | .for('share') |
| 17 | if (row?.status |
| 18 | throw new RepositoryImportingError() |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | export const MAX_IMPORT_ATTEMPTS = |