Skip to content

Commit b83a01f

Browse files
committed
refactor: extract storage migration helpers
1 parent df358e8 commit b83a01f

2 files changed

Lines changed: 69 additions & 39 deletions

File tree

lib/storage.ts

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ import {
4545
type AccountIdentityRef,
4646
toAccountIdentityRef,
4747
} from "./storage/identity.js";
48+
import {
49+
loadNormalizedStorageFromPathOrNull,
50+
mergeStorageForMigration,
51+
} from "./storage/migration-helpers.js";
4852
import {
4953
type AccountMetadataV1,
5054
type AccountMetadataV3,
@@ -747,9 +751,13 @@ async function migrateLegacyProjectStorageIfNeeded(
747751
let migrated = false;
748752

749753
for (const legacyPath of existingCandidatePaths) {
750-
const legacyStorage = await loadNormalizedStorageFromPath(
754+
const legacyStorage = await loadNormalizedStorageFromPathOrNull(
751755
legacyPath,
752756
"legacy account storage",
757+
{
758+
loadAccountsFromPath,
759+
logWarn: (message, meta) => log.warn(message, meta),
760+
},
753761
);
754762
if (!legacyStorage) {
755763
continue;
@@ -758,6 +766,7 @@ async function migrateLegacyProjectStorageIfNeeded(
758766
const mergedStorage = mergeStorageForMigration(
759767
targetStorage,
760768
legacyStorage,
769+
{ normalizeAccountStorage },
761770
);
762771
const fallbackStorage = targetStorage ?? legacyStorage;
763772

@@ -813,45 +822,10 @@ async function loadNormalizedStorageFromPath(
813822
path: string,
814823
label: string,
815824
): Promise<AccountStorageV3 | null> {
816-
try {
817-
const { normalized, schemaErrors } = await loadAccountsFromPath(path);
818-
if (schemaErrors.length > 0) {
819-
log.warn(`${label} schema validation warnings`, {
820-
path,
821-
errors: schemaErrors.slice(0, 5),
822-
});
823-
}
824-
return normalized;
825-
} catch (error) {
826-
const code = (error as NodeJS.ErrnoException).code;
827-
if (code !== "ENOENT") {
828-
log.warn(`Failed to load ${label}`, {
829-
path,
830-
error: String(error),
831-
});
832-
}
833-
return null;
834-
}
835-
}
836-
837-
function mergeStorageForMigration(
838-
current: AccountStorageV3 | null,
839-
incoming: AccountStorageV3,
840-
): AccountStorageV3 {
841-
if (!current) {
842-
return incoming;
843-
}
844-
845-
const merged = normalizeAccountStorage({
846-
version: 3,
847-
activeIndex: current.activeIndex,
848-
activeIndexByFamily: current.activeIndexByFamily,
849-
accounts: [...current.accounts, ...incoming.accounts],
825+
return loadNormalizedStorageFromPathOrNull(path, label, {
826+
loadAccountsFromPath,
827+
logWarn: (message, meta) => log.warn(message, meta),
850828
});
851-
if (!merged) {
852-
return current;
853-
}
854-
return merged;
855829
}
856830

857831
function selectNewestAccount<T extends AccountLike>(

lib/storage/migration-helpers.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { AccountStorageV3 } from "../storage.js";
2+
3+
export async function loadNormalizedStorageFromPathOrNull(
4+
path: string,
5+
label: string,
6+
deps: {
7+
loadAccountsFromPath: (path: string) => Promise<{
8+
normalized: AccountStorageV3 | null;
9+
schemaErrors: string[];
10+
}>;
11+
logWarn: (message: string, meta: Record<string, unknown>) => void;
12+
},
13+
): Promise<AccountStorageV3 | null> {
14+
try {
15+
const { normalized, schemaErrors } = await deps.loadAccountsFromPath(path);
16+
if (schemaErrors.length > 0) {
17+
deps.logWarn(`${label} schema validation warnings`, {
18+
path,
19+
errors: schemaErrors.slice(0, 5),
20+
});
21+
}
22+
return normalized;
23+
} catch (error) {
24+
const code = (error as NodeJS.ErrnoException).code;
25+
if (code !== "ENOENT") {
26+
deps.logWarn(`Failed to load ${label}`, {
27+
path,
28+
error: String(error),
29+
});
30+
}
31+
return null;
32+
}
33+
}
34+
35+
export function mergeStorageForMigration(
36+
current: AccountStorageV3 | null,
37+
incoming: AccountStorageV3,
38+
deps: {
39+
normalizeAccountStorage: (data: unknown) => AccountStorageV3 | null;
40+
},
41+
): AccountStorageV3 {
42+
if (!current) {
43+
return incoming;
44+
}
45+
46+
const merged = deps.normalizeAccountStorage({
47+
version: 3,
48+
activeIndex: current.activeIndex,
49+
activeIndexByFamily: current.activeIndexByFamily,
50+
accounts: [...current.accounts, ...incoming.accounts],
51+
});
52+
if (!merged) {
53+
return current;
54+
}
55+
return merged;
56+
}

0 commit comments

Comments
 (0)