| 1 | /** |
| 2 | * Moves every write-ahead log from the old `repos/<username>/<slug>` keyspace to |
| 3 | * `repos/<repositoryId>`, which is what the transport reads since repository ids |
| 4 | * replaced name pairs. |
| 5 | * |
| 6 | * A log left behind is invisible to the API: `readIndex` returns null, the cache |
| 7 | * replays an empty index, and the repository serves as freshly initialised. |
| 8 | * |
| 9 | * Entries are copied before the index that names them, so an interrupted run |
| 10 | * leaves the new prefix either absent or complete, never pointing at packs that |
| 11 | * have not landed. Old keys are kept unless `--prune` is passed. |
| 12 | * |
| 13 | * bun scripts/migrate-wal-keys.ts [--apply] [--prune] |
| 14 | */ |
| 15 | import { S3 } |
| 16 | import { createDatabase, schema |
| 17 | import { eq, isNotNull |
| 18 | |
| 19 | const apply = process |
| 20 | const prune = process |
| 21 | |
| 22 | const bucket = required |
| 23 | const s3 = new |
| 24 | forcePathStyle: true, |
| 25 | endpoint: required('S3_ENDPOINT |
| 26 | region: 'auto', |
| 27 | credentials: { |
| 28 | accessKeyId: required('S3_ACCESS_KEY_ID |
| 29 | secretAccessKey: required('S3_SECRET_ACCESS_KEY |
| 30 | }, |
| 31 | }); |
| 32 | |
| 33 | const { db, |
| 34 | connectionString: required('DATABASE_URL |
| 35 | }); |
| 36 | |
| 37 | const rows = await |
| 38 | .select({ |
| 39 | id: schema.repository |
| 40 | username: schema.user |
| 41 | slug: schema.repository |
| 42 | }) |
| 43 | .from(schema.repository |
| 44 | .innerJoin(schema.user |
| 45 | .where(isNotNull(schema |
| 46 | |
| 47 | let moved = 0 |
| 48 | let skipped = 0 |
| 49 | |
| 50 | for (const { id |
| 51 | const from = |
| 52 | const label = |
| 53 | |
| 54 | if (await exists |
| 55 | skipped++; |
| 56 | continue; |
| 57 | } |
| 58 | if (!(await |
| 59 | console.log(`no log |
| 60 | skipped++; |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | const keys = |
| 65 | const entries = |
| 66 | |
| 67 | console.log(`${ |
| 68 | if (!apply) |
| 69 | moved++; |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | for (const key |
| 74 | await s3.copyObject |
| 75 | Bucket: bucket, |
| 76 | Key: key.replace |
| 77 | CopySource: `${bucket} |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | await s3.copyObject |
| 82 | Bucket: bucket, |
| 83 | Key: `repos/${id |
| 84 | CopySource: `${bucket} |
| 85 | }); |
| 86 | |
| 87 | if (prune) { |
| 88 | for (const key |
| 89 | await s3.deleteObject |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | moved++; |
| 94 | } |
| 95 | |
| 96 | console.log( |
| 97 | `\n${apply ? |
| 98 | (apply ? '' |
| 99 | ); |
| 100 | |
| 101 | await pool.end() |
| 102 | |
| 103 | async function exists( |
| 104 | try { |
| 105 | await s3.headObject |
| 106 | return true; |
| 107 | } catch { |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | async function listPrefix( |
| 113 | const keys: |
| 114 | let token: |
| 115 | |
| 116 | do { |
| 117 | const page = |
| 118 | Bucket: bucket, |
| 119 | Prefix: prefix, |
| 120 | ContinuationToken: token, |
| 121 | }); |
| 122 | for (const object |
| 123 | if (object.Key |
| 124 | } |
| 125 | token = page. |
| 126 | } while (token) |
| 127 | |
| 128 | return keys; |
| 129 | } |
| 130 | |
| 131 | function required(name: |
| 132 | const value = |
| 133 | if (!value) |
| 134 | return value; |
| 135 | } |