diff --git a/lib/utils/migration-utils.js b/lib/utils/migration-utils.js --- a/lib/utils/migration-utils.js +++ b/lib/utils/migration-utils.js @@ -247,57 +247,75 @@ return state; } - const migrationKeys = [ - ...Object.keys(legacyMigrations), - ...Object.keys(migrations), - ] - .map(ver => parseInt(ver)) - .filter(key => currentVersion >= key && key > inboundVersion); - const sortedMigrationKeys = migrationKeys.sort((a, b) => a - b); + return await runMigrations( + legacyMigrations, + migrations, + state, + inboundVersion, + currentVersion, + debug, + ); + }; +} + +async function runMigrations( + legacyMigrations: LegacyMigrationManifest, + migrations: MigrationManifest, + state: PersistedState, + inboundVersion: number, + currentVersion: number, + debug: boolean, +): Promise { + const migrationKeys = [ + ...Object.keys(legacyMigrations), + ...Object.keys(migrations), + ] + .map(ver => parseInt(ver)) + .filter(key => currentVersion >= key && key > inboundVersion); + const sortedMigrationKeys = migrationKeys.sort((a, b) => a - b); + + if (debug) { + console.log('redux-persist: migrationKeys', sortedMigrationKeys); + } + let migratedState: PersistedState = state; + for (const versionKey of sortedMigrationKeys) { if (debug) { - console.log('redux-persist: migrationKeys', sortedMigrationKeys); + console.log( + 'redux-persist: running migration for versionKey', + versionKey, + ); } - let migratedState: PersistedState = state; - for (const versionKey of sortedMigrationKeys) { - if (debug) { - console.log( - 'redux-persist: running migration for versionKey', - versionKey, - ); - } - - if (!versionKey) { - continue; - } + if (!versionKey) { + continue; + } - if (legacyMigrations[versionKey]) { - migratedState = await legacyMigrations[versionKey](migratedState); - } else { - const { state: newState, ops } = - await migrations[versionKey](migratedState); - migratedState = newState; - const versionUpdateOp = { - type: 'replace_synced_metadata_entry', - payload: { - name: syncedMetadataNames.DB_VERSION, - data: `${versionKey}`, - }, - }; - const dbOps = { - ...ops, - syncedMetadataStoreOperations: [ - ...(ops.syncedMetadataStoreOperations ?? []), - versionUpdateOp, - ], - }; - await getConfig().sqliteAPI.processDBStoreOperations(dbOps); - } + if (legacyMigrations[versionKey]) { + migratedState = await legacyMigrations[versionKey](migratedState); + } else { + const { state: newState, ops } = + await migrations[versionKey](migratedState); + migratedState = newState; + const versionUpdateOp = { + type: 'replace_synced_metadata_entry', + payload: { + name: syncedMetadataNames.DB_VERSION, + data: `${versionKey}`, + }, + }; + const dbOps = { + ...ops, + syncedMetadataStoreOperations: [ + ...(ops.syncedMetadataStoreOperations ?? []), + versionUpdateOp, + ], + }; + await getConfig().sqliteAPI.processDBStoreOperations(dbOps); } + } - return migratedState; - }; + return migratedState; } export { @@ -311,4 +329,5 @@ convertNotificationMessageInfoToNewIDSchema, permissionsToRemoveInMigration, createAsyncMigrate, + runMigrations, };