From bc5df68ec6e6ea05d594357d09bcd3b7d612be2e Mon Sep 17 00:00:00 2001 From: Afonso Jorge Ramos Date: Thu, 16 Jul 2026 17:46:00 +0200 Subject: [PATCH] refactor(storage): remove legacy storage key after migration --- src/renderer/utils/core/storage.test.ts | 16 +++++----------- src/renderer/utils/core/storage.ts | 23 +++-------------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/renderer/utils/core/storage.test.ts b/src/renderer/utils/core/storage.test.ts index def5d35da..902038387 100644 --- a/src/renderer/utils/core/storage.test.ts +++ b/src/renderer/utils/core/storage.test.ts @@ -20,7 +20,7 @@ describe('renderer/utils/core/storage.ts', () => { expect(localStorage.getItem(Constants.STORAGE.LEGACY)).toBeNull(); }); - it('migrates legacy auth and settings into the stores and marks the key as migrated', () => { + it('migrates legacy auth and settings into the stores and removes the legacy key', () => { localStorage.setItem( Constants.STORAGE.LEGACY, JSON.stringify({ @@ -36,9 +36,7 @@ describe('renderer/utils/core/storage.ts', () => { expect(useSettingsStore.getState().theme).toBe(Theme.DARK); expect(useSettingsStore.getState().playSound).toBe(false); - const marker = JSON.parse(localStorage.getItem(Constants.STORAGE.LEGACY)!); - expect(marker.migrated).toBe(true); - expect(marker.migratedAt).toBeDefined(); + expect(localStorage.getItem(Constants.STORAGE.LEGACY)).toBeNull(); }); it('drops legacy accounts with invalid hostnames during migration', () => { @@ -68,17 +66,13 @@ describe('renderer/utils/core/storage.ts', () => { expect(useAccountsStore.getState().accounts).toHaveLength(1); }); - it('skips migration when already migrated', () => { - localStorage.setItem( - Constants.STORAGE.LEGACY, - JSON.stringify({ migrated: true, migratedAt: '2026-01-01T00:00:00Z' }), - ); + it('removes a legacy key with no migratable data', () => { + localStorage.setItem(Constants.STORAGE.LEGACY, JSON.stringify({})); migrateLegacyStoreToZustand(); expect(useAccountsStore.getState().accounts).toEqual([]); - const marker = JSON.parse(localStorage.getItem(Constants.STORAGE.LEGACY)!); - expect(marker.migratedAt).toBe('2026-01-01T00:00:00Z'); + expect(localStorage.getItem(Constants.STORAGE.LEGACY)).toBeNull(); }); it('logs an error and leaves stores untouched on malformed legacy data', () => { diff --git a/src/renderer/utils/core/storage.ts b/src/renderer/utils/core/storage.ts index 2685535e4..f3ee39285 100644 --- a/src/renderer/utils/core/storage.ts +++ b/src/renderer/utils/core/storage.ts @@ -22,18 +22,7 @@ export function migrateLegacyStoreToZustand() { } try { - const parsed = JSON.parse(existing); - - // Skip if already migrated - if (parsed.migrated) { - rendererLogInfo( - 'migrateLegacyStoreToZustand', - `Storage already migrated on ${parsed.migratedAt}`, - ); - return; - } - - const { auth, settings } = parsed; + const { auth, settings } = JSON.parse(existing); // Migrate auth to AccountsStore if it exists and store is empty if (auth?.accounts && useAccountsStore.getState().accounts.length === 0) { @@ -49,14 +38,8 @@ export function migrateLegacyStoreToZustand() { useSettingsStore.setState(knownSettings); } - // Mark old storage key as migrated instead of removing it - localStorage.setItem( - Constants.STORAGE.LEGACY, - JSON.stringify({ - migrated: true, - migratedAt: new Date().toISOString(), - }), - ); + // Remove the legacy key so subsequent launches skip migration entirely + localStorage.removeItem(Constants.STORAGE.LEGACY); rendererLogInfo( 'migrateLegacyStoreToZustand',