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

Can I upgrade to v5 on an existing app using v4 and keep my data? #434

Closed
williamoliveira opened this issue Aug 31, 2017 · 10 comments
Closed
Milestone

Comments

@williamoliveira
Copy link

I tried it but rehydration comes undefined

@williamoliveira
Copy link
Author

Just checked the storage now, I see the keys are different, everything is under the root key, so is there a easy way to migrate data from v4? or should I just keep on v4?

@rt2zz
Copy link
Owner

rt2zz commented Aug 31, 2017

there is not currently a way to migrate state from v4 to v5.

I think the way to solve this would be for redux-persist to ship with a pluggable "getStoredState":

import { getStoredState } from 'redux-persist'
import { getStoredStateV4 } from 'redux-persist/lib/integration'

const v4Config = { /* whatever the old config was */ }

const config = {
  key,
  storage,
  getStoredState: async (persistConfig) => {
    let storedState = getStoredState(persistConfig)
    if (!storedState) storedState = getStoredStateV4(v4Config)
    return storedState
  }
}

persistReducer(config, baseReducer)

Its not the prettiest api, but the pattern would be generally useful, could be used to move from one storage engine to another for example.

NOTE this is not currently possible, we would need to add getStoredState option to config for this to work.

@rt2zz rt2zz modified the milestone: Priority Aug 31, 2017
@williamoliveira
Copy link
Author

I dont know how the internals work, is it easy to do? do you plan to implement it?

@rt2zz
Copy link
Owner

rt2zz commented Aug 31, 2017

the implementation would be trivial, I just would want to take some time first to make sure this is the right api. In abstract I would like to call the config option: "migrate" or something, but unfortunately "migrate" is a loaded word, we already use that for migrating state between app state versions.

@rt2zz
Copy link
Owner

rt2zz commented Sep 1, 2017

I just took a first stab at it: https://github.com/rt2zz/redux-persist/tree/v5#experimental-v4-to-v5-state-migration

I have not run this code at all so it likely requires some tweaking, but it should be generally a good approach.

@levsero
Copy link
Contributor

levsero commented Nov 7, 2017

@rt2zz I'm trying to migrate from v4 to v5 using the experimental migration. It works fine for extracting the data, but if I try and add a migration at the same time it errors because _persist does not exist. I assume this has to do with it not expecting the state to be persisted without also having an _persist but since it's pulling it from v4 that is the case.
Any thoughts or workarounds would be great.

Update
I've put up a pr with a potential fix, if there are comments or suggestions for a more desired fix can update it.

@kelset
Copy link

kelset commented Feb 26, 2018

Hi folks, can I revive this issue by asking if there is any update on how to migrate?

Because it looks like the migration guide that was present in the README and linked by @rt2zz earlier is now 404 😅

@levsero
Copy link
Contributor

levsero commented Feb 26, 2018

@kelset https://github.com/rt2zz/redux-persist/blob/master/docs/MigrationGuide-v5.md

@aguynamedben
Copy link
Collaborator

@levsero Did you get end up getting this working? Thanks for taking the time to submit that fix. I'm going to try the experimental feature in the next few days, after I implement the upgrade from v4 to v5.

FWIW my situation: I built a new feature assuming I could run async code during a redux migration, but redux-persist-migrate v4.x doesn't support async code in migrations. redux-persist v5 has first-class support for migrations and allows a Promise to be returned to run a migration (👍!!!). But the pain level of clearing local state is very high, as my users invest a lot in configuration that is stored locally for a good reason.

@rt2zz thanks so much for the time you put into redux-persist and redux-persist-migrate before v5. 👏👏👏

@levsero
Copy link
Contributor

levsero commented Apr 25, 2018

@aguynamedben yep all worked as expected once the fix was in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants