|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Action } from '../../proxy/actions'; |
| 18 | +import { Repo, User } from '../types'; |
| 19 | + |
| 20 | +/** |
| 21 | + * A read-only view over a backend (mongo or fs) that data is migrated *from*. |
| 22 | + * Implementations own their own connection and must be closed by the caller. |
| 23 | + */ |
| 24 | +export const DEFAULT_PUSH_BATCH_SIZE = 500; |
| 25 | + |
| 26 | +export interface MigrationSource { |
| 27 | + getUsers(): Promise<User[]>; |
| 28 | + getRepos(): Promise<Repo[]>; |
| 29 | + /** |
| 30 | + * Stream pushes in batches of at most `batchSize`. Production datasets can |
| 31 | + * hold tens of thousands of large push documents, so sources must not |
| 32 | + * require the whole table in memory at once. |
| 33 | + */ |
| 34 | + getPushBatches(batchSize: number): AsyncIterable<Action[]>; |
| 35 | + close(): Promise<void>; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * The subset of the Postgres adapter used to write migrated records. The |
| 40 | + * adapter module satisfies this shape directly, so the CLI can pass it as-is. |
| 41 | + */ |
| 42 | +export interface MigrationDestination { |
| 43 | + findUser(username: string): Promise<User | null>; |
| 44 | + findUserByEmail(email: string): Promise<User | null>; |
| 45 | + createUser(user: User): Promise<void>; |
| 46 | + getRepoByUrl(url: string): Promise<Repo | null>; |
| 47 | + createRepo(repo: Repo): Promise<Repo>; |
| 48 | + writeAudit(action: Action): Promise<void>; |
| 49 | +} |
| 50 | + |
| 51 | +export interface MigrationSummary { |
| 52 | + users: { imported: number; skipped: number }; |
| 53 | + repos: { imported: number; skipped: number }; |
| 54 | + pushes: { imported: number }; |
| 55 | +} |
| 56 | + |
| 57 | +export interface MigrateOptions { |
| 58 | + /** Receives human-readable progress lines. Defaults to a no-op. */ |
| 59 | + log?: (message: string) => void; |
| 60 | + /** Maximum pushes fetched and written per batch. Defaults to 500. */ |
| 61 | + pushBatchSize?: number; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Copy users, repos and pushes from `source` into the Postgres `destination`. |
| 66 | + * |
| 67 | + * Idempotent and re-runnable: users and repos that already exist (matched by |
| 68 | + * username/email and URL respectively) are skipped, and pushes are upserted by |
| 69 | + * their stable string id. Record `_id`s are intentionally NOT carried over — |
| 70 | + * Postgres assigns fresh UUIDs; push ids (TEXT) are preserved by the upsert. |
| 71 | + */ |
| 72 | +export const migrate = async ( |
| 73 | + source: MigrationSource, |
| 74 | + destination: MigrationDestination, |
| 75 | + options: MigrateOptions = {}, |
| 76 | +): Promise<MigrationSummary> => { |
| 77 | + const log = options.log ?? (() => undefined); |
| 78 | + const summary: MigrationSummary = { |
| 79 | + users: { imported: 0, skipped: 0 }, |
| 80 | + repos: { imported: 0, skipped: 0 }, |
| 81 | + pushes: { imported: 0 }, |
| 82 | + }; |
| 83 | + |
| 84 | + const users = await source.getUsers(); |
| 85 | + log(`Migrating ${users.length} user(s)...`); |
| 86 | + for (const user of users) { |
| 87 | + const existing = |
| 88 | + (await destination.findUser(user.username)) || |
| 89 | + (user.email ? await destination.findUserByEmail(user.email) : null); |
| 90 | + if (existing) { |
| 91 | + summary.users.skipped++; |
| 92 | + continue; |
| 93 | + } |
| 94 | + // Legacy documents can lack optional fields the writers dereference: |
| 95 | + // users synced from AD may have no email (the mail attribute is |
| 96 | + // optional) or gitAccount. Default them like the mongo upsert path does. |
| 97 | + await destination.createUser({ |
| 98 | + ...user, |
| 99 | + email: user.email ?? '', |
| 100 | + gitAccount: user.gitAccount ?? '', |
| 101 | + }); |
| 102 | + summary.users.imported++; |
| 103 | + } |
| 104 | + |
| 105 | + const repos = await source.getRepos(); |
| 106 | + log(`Migrating ${repos.length} repo(s)...`); |
| 107 | + for (const repo of repos) { |
| 108 | + if (await destination.getRepoByUrl(repo.url)) { |
| 109 | + summary.repos.skipped++; |
| 110 | + continue; |
| 111 | + } |
| 112 | + await destination.createRepo(repo); |
| 113 | + summary.repos.imported++; |
| 114 | + } |
| 115 | + |
| 116 | + const batchSize = options.pushBatchSize ?? DEFAULT_PUSH_BATCH_SIZE; |
| 117 | + log('Migrating pushes...'); |
| 118 | + for await (const batch of source.getPushBatches(batchSize)) { |
| 119 | + for (const push of batch) { |
| 120 | + await destination.writeAudit(push); |
| 121 | + summary.pushes.imported++; |
| 122 | + } |
| 123 | + log(` ${summary.pushes.imported} push(es) migrated`); |
| 124 | + } |
| 125 | + |
| 126 | + return summary; |
| 127 | +}; |
0 commit comments