From ad7a3b052d454d5c966db9d2b43276515a63d0ee Mon Sep 17 00:00:00 2001 From: dholms Date: Wed, 8 Nov 2023 15:53:23 -0600 Subject: [PATCH] automate repair prefs --- .../pds/src/migrate-script/migrate-all.ts | 2 ++ .../pds/src/migrate-script/repair-prefs.ts | 5 ++- packages/pds/src/migrate-script/util.ts | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/pds/src/migrate-script/migrate-all.ts b/packages/pds/src/migrate-script/migrate-all.ts index 523384af8f8..bc3d4feeb7e 100644 --- a/packages/pds/src/migrate-script/migrate-all.ts +++ b/packages/pds/src/migrate-script/migrate-all.ts @@ -14,6 +14,7 @@ import { PdsInfo, makeAdminHeaders, repairBlob, + repairFailedPrefs, transferPreferences, } from './util' @@ -73,6 +74,7 @@ export const runScript = async () => { .execute() completed++ await repairFailedBlobs(ctx, db, pdsInfo, status.did, adminHeaders) + await repairFailedPrefs(ctx, db, pdsInfo, status.did) } catch (err) { // @ts-ignore const errmsg: string = err?.message ?? null diff --git a/packages/pds/src/migrate-script/repair-prefs.ts b/packages/pds/src/migrate-script/repair-prefs.ts index 3b69d3bd17c..4ef53bbc7da 100644 --- a/packages/pds/src/migrate-script/repair-prefs.ts +++ b/packages/pds/src/migrate-script/repair-prefs.ts @@ -3,7 +3,7 @@ import AtpAgent from '@atproto/api' import { envToCfg, envToSecrets, readEnv } from '../config' import AppContext from '../context' import { getDb } from './db' -import { transferPreferences } from './util' +import { repairPrefs } from './util' dotenv.config() @@ -32,8 +32,7 @@ export const runScript = async () => { throw new Error(`could not find pds with id: ${pref.pdsId}`) } try { - await transferPreferences(ctx, pdsInfo, pref.did) - await db.deleteFrom('failed_pref').where('did', '=', pref.did).execute() + await repairPrefs(ctx, db, pdsInfo, pref.did) } catch (err) { console.log(err) } diff --git a/packages/pds/src/migrate-script/util.ts b/packages/pds/src/migrate-script/util.ts index cb6fe5c1931..7c5b9712fe2 100644 --- a/packages/pds/src/migrate-script/util.ts +++ b/packages/pds/src/migrate-script/util.ts @@ -27,6 +27,40 @@ export const makeAdminHeaders = (secrets: ServerSecrets): AdminHeaders => { } } +export const repairFailedPrefs = async ( + ctx: AppContext, + db: MigrateDb, + pds: PdsInfo, + did: string, +) => { + const hasFailure = await db + .selectFrom('failed_pref') + .selectAll() + .where('did', '=', did) + .executeTakeFirst() + if (hasFailure) { + await repairPrefs(ctx, db, pds, did) + } +} + +export const repairPrefs = async ( + ctx: AppContext, + db: MigrateDb, + pds: PdsInfo, + did: string, +) => { + const hasFailure = await db + .selectFrom('failed_pref') + .selectAll() + .where('did', '=', did) + .executeTakeFirst() + if (!hasFailure) { + return + } + await transferPreferences(ctx, pds, did) + await db.deleteFrom('failed_pref').where('did', '=', did).execute() +} + export const transferPreferences = async ( ctx: AppContext, pds: PdsInfo,