Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable createMigrate to handle async migrations #866

Open
ssorallen opened this issue Jul 12, 2018 · 4 comments · May be fixed by #1429
Open

Enable createMigrate to handle async migrations #866

ssorallen opened this issue Jul 12, 2018 · 4 comments · May be fixed by #1429

Comments

@ssorallen
Copy link
Contributor

ssorallen commented Jul 12, 2018

The function passed to migrate can already handle a Promise, but createMigrate expects each of the integer migrations to be simple, synchronous transforms of PersistedState => PersistedState. It would be great to enable serial, asynchronous migrations in createMigrate. The change could be localized to createMigrate since the rest of of redux-persist already expects migrate to be asynchronous.

@aguynamedben
Copy link
Collaborator

To deal with this, I'm copying this project's createMigrate.js and modifying it in my project to this: https://gist.github.com/aguynamedben/ee5ef358856f1543129265fd3cf8b732

See the tests for how to use it...

      const asyncMigrations = {
        1: async (state) => {
          return {
            ...state,
            foo: 'different',
          };
        },
        2: async (state) => {
          return {
            ...state,
            baz: 'bang',
          };
        },
        3: async (state) => {
          return {
            ...state,
            cool: 'beans',
          };
        },
      };

configureStore.dev.js

  const persistConfig = {
    key: 'root',
    storage: storage,
    debug: true,

    migrate: customCreateMigrate(asyncMigrations, {
      debug: true,
      asyncMigrations: true,
    }),
    version: 8,
  };

We use the same logic in customCreate.js to figure out which migrations to run. For async migrations, we use this technique to dynamically build a promise chain using Array.reduce(). This means the async migrations are run in sequence, one after another.

With the current code, you must provide the option { asyncMigrations: true } or else the old logic is used, which will screw up if you have async migrations.

If #929 gets resolved I'd happily submit a PR to add this. I think we could even detect if the migration function is async and resolve automatically so that specifying the asyncMigrations option isn't necessary.

@aguynamedben
Copy link
Collaborator

I need to submit my gist as a PR.

@aguynamedben
Copy link
Collaborator

I'm not sure if this is also related to feature requests for async transforms. #303 I need to figure that out. It seems like they might be separate per this comment: #426 (comment)

Also maybe related: #426

retyui added a commit to retyui/redux-persist that referenced this issue Jul 1, 2020
retyui added a commit to retyui/redux-persist that referenced this issue Jul 1, 2020
@retyui
Copy link

retyui commented Jul 1, 2020

Fixed in #1219

@qkudev qkudev linked a pull request Jan 22, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants