| 1 | /** |
| 2 | * Writes `refs/pull/<n>/head` and `refs/pull/<n>/merge` for open pull requests opened before Ghost kept them that nobody has read or pushed to since. Safe beside a running API: every write is a compare-and-swap on the log. |
| 3 | * node dist/scripts/backfill-pull-refs.js, after `nest build` |
| 4 | */ |
| 5 | import { schema, type |
| 6 | import { Module } |
| 7 | import { ConfigModule } |
| 8 | import { NestFactory } |
| 9 | import { eq } |
| 10 | |
| 11 | import { DATABASE, DatabaseModule |
| 12 | import { PullRefsModule } |
| 13 | import { S3Module } |
| 14 | import { PullRefsService } |
| 15 | import { errorMessage } |
| 16 | |
| 17 | // Only what a sync needs, so the script opens no HTTP or SSH listener and starts no poller. |
| 18 | @Module({ |
| 19 | imports: [ |
| 20 | ConfigModule.forRoot({ |
| 21 | DatabaseModule, |
| 22 | S3Module, |
| 23 | PullRefsModule, |
| 24 | ], |
| 25 | }) |
| 26 | class BackfillModule {} |
| 27 | |
| 28 | const app = await |
| 29 | logger: ['warn', |
| 30 | }); |
| 31 | const db = app |
| 32 | const pullRefs = app |
| 33 | |
| 34 | const open = await |
| 35 | .select({ id: |
| 36 | .from(schema.pullRequest |
| 37 | .where(eq(schema |
| 38 | |
| 39 | let failed = 0 |
| 40 | for (const { id |
| 41 | try { |
| 42 | await pullRefs.sync |
| 43 | } catch (error) |
| 44 | failed++; |
| 45 | console.error(`${ |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | console.log( |
| 50 | `${open.length - |
| 51 | ); |
| 52 | await app.close() |
| 53 | process.exitCode = failed |