[web-db] implement asynchronous redux-persist migrations
Summary:
This is createMigrate code allowing to async migrations.
This could potentially be patched, but this code will have more responsibilities in near future (D7668), so it's better to maintain this that way.
Depends on D7289
Test Plan:
- Run tests
- Add one more async migration and check if it works
Tests could not be added directly, this function will read something from local storage (D7668) - that being said this test will fail, but code I used:
// @flow import { creatAsyncMigrate } from './create-async-migrate.js'; describe('async redux migrations', () => { it('should run migrations with async functions', async () => { const persistedState = { oldKey: 'oldKeyValue', _persist: { version: 1, rehydrated: false, }, }; const asyncMigrations = { [1]: async state => { return { ...state, oldKey: 'oldKeyValue', }; }, // needs to run [2]: async state => { return { ...state, newKey: 'newKeyValue', }; }, // needs to run [3]: async state => { return { ...state, oldKey: 'oldKeyUpdated', }; }, }; const migrate = creatAsyncMigrate(asyncMigrations, { debug: true, }); const currentVersion = 3; const migratedState = await migrate(persistedState, currentVersion); expect(migratedState).toEqual({ oldKey: 'oldKeyUpdated', newKey: 'newKeyValue', _persist: { version: 1, rehydrated: false, }, }); }); it(`should do nothing when inboundVersion and currentVersion match`, async () => { const persistedState = { oldKey: 'oldKeyValue', _persist: { version: 3, rehydrated: false, }, }; const asyncMigrations = { [1]: async state => { return { ...state, oldKey: 'oldKeyValue', }; }, [2]: async state => { return { ...state, newKey: 'newKeyValue', }; }, [3]: async state => { return { ...state, oldKey: 'oldKeyUpdated', }; }, }; const migrate = creatAsyncMigrate(asyncMigrations, { debug: true, }); const currentVersion = 3; const migratedState = await migrate(persistedState, currentVersion); expect(migratedState).toEqual(persistedState); }); it(`should return undefined for undefined persisted state`, async () => { const persistedState = undefined; const asyncMigrations = { [1]: async state => { return { ...state, oldKey: 'oldKeyValue', }; }, }; const migrate = creatAsyncMigrate(asyncMigrations, { debug: true, }); const currentVersion = 1; const migratedState = await migrate(persistedState, currentVersion); expect(migratedState).toBeUndefined(); }); });
Reviewers: tomek, marcin, michal, atul
Reviewed By: tomek
Subscribers: ashoat, atul
Differential Revision: https://phab.comm.dev/D7658