@@ -709,9 +709,12 @@ function getLegacyFlaggedAccountsPath(): string {
709709 ) ;
710710}
711711
712- async function migrateLegacyProjectStorageIfNeeded (
713- persist : ( storage : AccountStorageV3 ) => Promise < void > = saveAccounts ,
714- ) : Promise < AccountStorageV3 | null > {
712+ async function migrateLegacyProjectStorageIfNeeded ( options ?: {
713+ persist ?: ( storage : AccountStorageV3 ) => Promise < void > ;
714+ commit ?: boolean ;
715+ } ) : Promise < AccountStorageV3 | null > {
716+ const persist = options ?. persist ?? saveAccounts ;
717+ const commit = options ?. commit ?? true ;
715718 const state = getStoragePathState ( ) ;
716719 if ( ! state . currentStoragePath ) {
717720 return null ;
@@ -782,43 +785,49 @@ async function migrateLegacyProjectStorageIfNeeded(
782785 ) ;
783786 const fallbackStorage = targetStorage ?? legacyStorage ;
784787
785- try {
786- await persist ( mergedStorage ) ;
787- targetStorage = mergedStorage ;
788- migrated = true ;
789- } catch ( error ) {
790- targetStorage = fallbackStorage ;
791- log . warn ( "Failed to persist migrated account storage" , {
788+ if ( commit ) {
789+ try {
790+ await persist ( mergedStorage ) ;
791+ targetStorage = mergedStorage ;
792+ migrated = true ;
793+ } catch ( error ) {
794+ targetStorage = fallbackStorage ;
795+ log . warn ( "Failed to persist migrated account storage" , {
796+ from : legacyPath ,
797+ to : state . currentStoragePath ,
798+ error : String ( error ) ,
799+ } ) ;
800+ continue ;
801+ }
802+
803+ try {
804+ await fs . unlink ( legacyPath ) ;
805+ log . info ( "Removed legacy account storage file after migration" , {
806+ path : legacyPath ,
807+ } ) ;
808+ } catch ( unlinkError ) {
809+ const code = ( unlinkError as NodeJS . ErrnoException ) . code ;
810+ if ( code !== "ENOENT" ) {
811+ log . warn (
812+ "Failed to remove legacy account storage file after migration" ,
813+ {
814+ path : legacyPath ,
815+ error : String ( unlinkError ) ,
816+ } ,
817+ ) ;
818+ }
819+ }
820+
821+ log . info ( "Migrated legacy project account storage" , {
792822 from : legacyPath ,
793823 to : state . currentStoragePath ,
794- error : String ( error ) ,
824+ accounts : mergedStorage . accounts . length ,
795825 } ) ;
796826 continue ;
797827 }
798828
799- try {
800- await fs . unlink ( legacyPath ) ;
801- log . info ( "Removed legacy account storage file after migration" , {
802- path : legacyPath ,
803- } ) ;
804- } catch ( unlinkError ) {
805- const code = ( unlinkError as NodeJS . ErrnoException ) . code ;
806- if ( code !== "ENOENT" ) {
807- log . warn (
808- "Failed to remove legacy account storage file after migration" ,
809- {
810- path : legacyPath ,
811- error : String ( unlinkError ) ,
812- } ,
813- ) ;
814- }
815- }
816-
817- log . info ( "Migrated legacy project account storage" , {
818- from : legacyPath ,
819- to : state . currentStoragePath ,
820- accounts : mergedStorage . accounts . length ,
821- } ) ;
829+ targetStorage = mergedStorage ;
830+ migrated = true ;
822831 }
823832
824833 if ( migrated ) {
@@ -1263,7 +1272,7 @@ async function loadAccountsInternal(
12631272 const resetMarkerPath = getIntentionalResetMarkerPath ( path ) ;
12641273 await cleanupStaleRotatingBackupArtifacts ( path ) ;
12651274 const migratedLegacyStorage = persistMigration
1266- ? await migrateLegacyProjectStorageIfNeeded ( persistMigration )
1275+ ? await migrateLegacyProjectStorageIfNeeded ( { persist : persistMigration } )
12671276 : null ;
12681277
12691278 try {
@@ -1432,11 +1441,17 @@ async function loadAccountsInternal(
14321441 }
14331442}
14341443
1435- async function loadAccountsForExport ( ) : Promise < AccountStorageV3 | null > {
1444+ async function loadAccountsForExport ( options ?: {
1445+ persistMigrations ?: boolean ;
1446+ } ) : Promise < AccountStorageV3 | null > {
1447+ const persistMigrations = options ?. persistMigrations ?? true ;
14361448 const path = getStoragePath ( ) ;
14371449 const resetMarkerPath = getIntentionalResetMarkerPath ( path ) ;
1438- const migratedLegacyStorage =
1439- await migrateLegacyProjectStorageIfNeeded ( saveAccountsUnlocked ) ;
1450+ const migratedLegacyStorage = await migrateLegacyProjectStorageIfNeeded (
1451+ persistMigrations
1452+ ? { persist : saveAccountsUnlocked }
1453+ : { commit : false } ,
1454+ ) ;
14401455
14411456 if ( existsSync ( resetMarkerPath ) ) {
14421457 return createEmptyStorageWithMetadata ( false , "intentional-reset" ) ;
@@ -1455,7 +1470,7 @@ async function loadAccountsForExport(): Promise<AccountStorageV3 | null> {
14551470 errors : schemaErrors . slice ( 0 , 5 ) ,
14561471 } ) ;
14571472 }
1458- if ( normalized && storedVersion !== normalized . version ) {
1473+ if ( persistMigrations && normalized && storedVersion !== normalized . version ) {
14591474 log . info ( "Migrating account storage to v3" , {
14601475 from : storedVersion ,
14611476 to : normalized . version ,
@@ -1840,8 +1855,9 @@ export async function exportAccounts(
18401855 force,
18411856 currentStoragePath,
18421857 transactionState : getTransactionSnapshotState ( ) ,
1843- readCurrentStorageUnlocked : loadAccountsForExport ,
1844- readCurrentStorage : ( ) => withStorageLock ( loadAccountsForExport ) ,
1858+ readCurrentStorageUnlocked : ( ) =>
1859+ loadAccountsForExport ( { persistMigrations : false } ) ,
1860+ readCurrentStorage : ( ) => withStorageLock ( ( ) => loadAccountsForExport ( ) ) ,
18451861 exportAccountsToFile,
18461862 beforeCommit,
18471863 logInfo : ( message , details ) => {
0 commit comments