HomePhabricator
Diffusion Comm 61453af0f8bb

[web-db] implement asynchronous `redux-persist` migrations

Description

[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:

  1. Run tests
  2. 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

Details

Provenance
kamilAuthored on Apr 24 2023, 6:34 AM
Reviewer
tomek
Differential Revision
D7658: [web-db] implement asynchronous `redux-persist` migrations
Parents
rCOMM85ad52f0ed44: [web-db] improve persisting lifecycle
Branches
Unknown
Tags
Unknown