diff --git a/lib/shared/create-async-migrate.js b/lib/shared/create-async-migrate.js --- a/lib/shared/create-async-migrate.js +++ b/lib/shared/create-async-migrate.js @@ -3,7 +3,11 @@ import { DEFAULT_VERSION } from 'redux-persist/es/constants.js'; import type { PersistState } from 'redux-persist/es/types.js'; -type MigrationManifest = { +import type { StoreOperations } from '../types/store-ops-types.js'; +import { syncedMetadataNames } from '../types/synced-metadata-types.js'; +import { getConfig } from '../utils/config.js'; + +type LegacyMigrationManifest = { +[number | string]: (PersistedState) => Promise, }; type PersistedState = { @@ -18,9 +22,17 @@ debug: boolean, ) => Promise; +type MigrationManifest = { + +[number | string]: (PersistedState) => Promise<{ + +state: PersistedState, + +ops: StoreOperations, + }>, +}; + function createAsyncMigrate( - migrations: MigrationManifest, + legacyMigrations: LegacyMigrationManifest, config: ConfigType, + migrations: MigrationManifest, storageMigration: ?StorageMigrationFunction, ): (state: PersistedState, currentVersion: number) => Promise { const debug = process.env.NODE_ENV !== 'production' && !!config?.debug; @@ -53,10 +65,13 @@ return state; } - const newMigrationKeys = Object.keys(migrations) + const migrationKeys = [ + ...Object.keys(legacyMigrations), + ...Object.keys(migrations), + ] .map(ver => parseInt(ver)) .filter(key => currentVersion >= key && key > inboundVersion); - const sortedMigrationKeys = newMigrationKeys.sort((a, b) => a - b); + const sortedMigrationKeys = migrationKeys.sort((a, b) => a - b); if (debug) { console.log('redux-persist: migrationKeys', sortedMigrationKeys); @@ -70,8 +85,32 @@ versionKey, ); } - if (versionKey) { - migratedState = await migrations[versionKey](migratedState); + + 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); } } diff --git a/native/redux/persist.js b/native/redux/persist.js --- a/native/redux/persist.js +++ b/native/redux/persist.js @@ -137,7 +137,7 @@ import { isTaskCancelledError } from '../utils/error-handling.js'; import { defaultURLPrefix } from '../utils/url-utils.js'; -const migrations = { +const legacyMigrations = { [1]: (state: AppState) => ({ ...state, notifPermissionAlertInfo: defaultAlertInfo, @@ -1301,18 +1301,29 @@ { whitelist: ['reportStore'] }, ); +const migrations = { + [75]: (state: AppState) => ({ + state, + ops: [], + }), +}; + const persistConfig = { key: 'root', storage: AsyncStorage, blacklist: persistBlacklist, debug: __DEV__, - version: 74, + version: 75, transforms: [ messageStoreMessagesBlocklistTransform, reportStoreTransform, keyserverStoreTransform, ], - migrate: (createAsyncMigrate(migrations, { debug: __DEV__ }): any), + migrate: (createAsyncMigrate( + legacyMigrations, + { debug: __DEV__ }, + migrations, + ): any), timeout: ((__DEV__ ? 0 : undefined): number | void), }; diff --git a/web/redux/persist.js b/web/redux/persist.js --- a/web/redux/persist.js +++ b/web/redux/persist.js @@ -57,7 +57,7 @@ declare var keyserverURL: string; -const migrations = { +const legacyMigrations = { [1]: async (state: any) => { const { primaryIdentityPublicKey, @@ -503,6 +503,13 @@ return newStorage; }; +const migrations = { + [75]: (state: AppState) => ({ + state, + ops: [], + }), +}; + const persistConfig: PersistConfig = { keyPrefix: rootKeyPrefix, key: rootKey, @@ -511,11 +518,12 @@ ? persistWhitelist : [...persistWhitelist, 'draftStore'], migrate: (createAsyncMigrate( - migrations, + legacyMigrations, { debug: isDev }, + migrations, migrateStorageToSQLite, ): any), - version: 17, + version: 75, transforms: [messageStoreMessagesBlocklistTransform, keyserverStoreTransform], };