| 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 | `${apply ? ' |
| 69 | ); |
| 70 | if (!apply) |
| 71 | moved++; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | for (const key |
| 76 | await s3.copyObject |
| 77 | Bucket: bucket, |
| 78 | Key: key.replace |
| 79 | CopySource: `${bucket} |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | await s3.copyObject |
| 84 | Bucket: bucket, |
| 85 | Key: `repos/${id |
| 86 | CopySource: `${bucket} |
| 87 | }); |
| 88 | |
| 89 | if (prune) { |
| 90 | for (const key |
| 91 | await s3.deleteObject |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | moved++; |
| 96 | } |
| 97 | |
| 98 | console.log( |
| 99 | `\n${apply ? |
| 100 | (apply |
| 101 | ? '' |
| 102 | : '\nre-run with --apply to write, and --prune to drop old keys'), |
| 103 | ); |
| 104 | |
| 105 | await pool.end() |
| 106 | |
| 107 | async function exists( |
| 108 | try { |
| 109 | await s3.headObject |
| 110 | return true; |
| 111 | } catch { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | async function listPrefix( |
| 117 | const keys: |
| 118 | let token: |
| 119 | |
| 120 | do { |
| 121 | const page = |
| 122 | Bucket: bucket, |
| 123 | Prefix: prefix, |
| 124 | ContinuationToken: token, |
| 125 | }); |
| 126 | for (const object |
| 127 | if (object.Key |
| 128 | } |
| 129 | token = page. |
| 130 | } while (token) |
| 131 | |
| 132 | return keys; |
| 133 | } |
| 134 | |
| 135 | function required(name: |
| 136 | const value = |
| 137 | if (!value) |
| 138 | return value; |
| 139 | } |