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

Safe migration from AsyncStorage to redux-persist-filesystem-storage #679

Open
maxkomarychev opened this issue Jan 24, 2018 · 6 comments
Open

Comments

@maxkomarychev
Copy link

maxkomarychev commented Jan 24, 2018

Hello,

update I'm on redux-persist v4
update 2 works for redux-persist v4 ONLY

I am writing custom migration routine that will take data that was saved with one storage and save it under another one. More specifically I want to migrate from AsyncStorage to https://github.com/robwalkerco/redux-persist-filesystem-storage to prevent issues related to sqlite size/cursor memory limits on android. Please find snippet below, do you think it is the best way to do such thing? Is there any better way to tell if store was previously saved than checking _.isEmpty(state)?

update added try-catch

const fsPersistor = persistStore(
    this.store,
    { storage: FilesystemStorage },
    (fsError, fsResult) => {
        if (_.isEmpty(fsResult)) {
            try {
                getStoredState(
                    { storage: AsyncStorage },
                    (asyncError, asyncResult) => {
                        if (!_.isEmpty(asyncResult)) {
                            fsPersistor.rehydrate(asyncResult, {
                                serial: false,
                            })
                        }
                    },
                )
            } catch (getStateError) {
                console.warn('getStoredState error', getStateError)
            }
        }
    },
)

update 2 with promises

const fsPersistor = persistStore(
    this.store,
    { storage: FilesystemStorage },
    async (fsError, fsResult) => {
        if (_.isEmpty(fsResult)) {
            try {
                const asyncState = await getStoredState({
                    storage: AsyncStorage,
                })
                if (!_.isEmpty(asyncState)) {
                    fsPersistor.rehydrate(asyncState, {
                        serial: false,
                    })
                }
            } catch (getStateError) {
                console.warn('getStoredState error', getStateError)
            }
        }
    },
)

Thanks.

@maxkomarychev
Copy link
Author

maxkomarychev commented Jan 25, 2018

another version with promises (1st message updated)

const fsPersistor = persistStore(
    this.store,
    { storage: FilesystemStorage },
    async (fsError, fsResult) => {
        if (_.isEmpty(fsResult)) {
            try {
                const asyncState = await getStoredState({
                    storage: AsyncStorage,
                })
                if (!_.isEmpty(asyncState)) {
                    fsPersistor.rehydrate(asyncState, {
                        serial: false,
                    })
                }
            } catch (getStateError) {
                console.warn('getStoredState error', getStateError)
            }
        }
    },
)

maxkomarychev added a commit to maxkomarychev/redux-persist-filesystem-storage that referenced this issue Jan 26, 2018
maxkomarychev added a commit to maxkomarychev/redux-persist-filesystem-storage that referenced this issue Jan 26, 2018
maxkomarychev added a commit to maxkomarychev/redux-persist-filesystem-storage that referenced this issue Jan 26, 2018
Provide sample snippet which lets app migrate data previously stored in
`AsyncStorage` to `redux-persist-filesystem-storage`

Original issues:
rt2zz/redux-persist#199
rt2zz/redux-persist#284

Converastions related to provided snippet:
rt2zz/redux-persist#679
robwalkerco#7
maxkomarychev added a commit to maxkomarychev/redux-persist-filesystem-storage that referenced this issue Jan 26, 2018
Provide sample snippet which lets app migrate data previously stored in
`AsyncStorage` to `redux-persist-filesystem-storage`

Original issues:
rt2zz/redux-persist#199
rt2zz/redux-persist#284

Converastions related to provided snippet:
rt2zz/redux-persist#679
robwalkerco#7
@rt2zz
Copy link
Owner

rt2zz commented Feb 1, 2018

this looks great / correct! Unfortunately this approach only works for v4, but very similar code can be adapted for a v4 -> v5 upgrade.

Basically in v5 the approach is to have a custom config.getStoredState that does a fallback check for v4: https://github.com/rt2zz/redux-persist/blob/master/docs/MigrationGuide-v5.md#experimental-v4-to-v5-state-migration

Open to suggestion on how to make these types of migrations more painless. I am contemplating something like createGetStoredStateChain([getStoredStateA, getStoredStateFallback]) which basically will go down the chain until it finds usable stored state.

@maxkomarychev
Copy link
Author

maxkomarychev commented Feb 2, 2018

this looks great / correct! Unfortunately this approach only works for v4

that is true, I updated 1st post

@alanlanglois
Copy link

alanlanglois commented Apr 4, 2018

I'm a little confuse, any place where I can find an example of AsyncStorage to redux-persist-filesystem-storage migration using V5?

@alanlanglois
Copy link

Working on it
I can get previously stored data using AsyncStorage:

let persistor = persistStore(store, persistConfig, async () => {
      try {
          console.log("Get Async State")
          const asyncState = await getStoredState({
              key: 'root',
              storage: AsyncStorage,
          })

          console.log("Async State: " + asyncState )
          if (asyncState) {
              console.log("Stored Async Data" + JSON.stringify(asyncState));
          }
      } catch (error) {
          console.warn('Get Async State error', error)
      }

but I can't really see how rehydrate the file storage store with these data, as fsPersistor.rehydrate doesn't exists anymore in v5.

Should I use Transform? or is there a method replacing .rehydrate in v5?

@hutchy2570
Copy link

See #806 (comment)

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

4 participants