-
Notifications
You must be signed in to change notification settings - Fork 866
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
Redux-Persist v5 migrate from one storage system to another #806
Comments
Hi, I've managed to get this to work using v5's migrate feature.
You then just include this function as the |
@hutchy2570 where I put this code? |
This is my take on migrating from custom (legacy) storage // configureStore.js
import migrations, { createMigrate } from './migrations'
export default function () {
const persistConfig = {
migrate: createMigrate(migrations),
// rest of config
}
// ...
} // migrations/index.js
import { createMigrate as createReduxMigrate } from 'redux-persist'
import migrateLegacyState from './migrateLegacyState'
/**
* Legacy migration wrapper
*/
export function createMigrate(migrations, config) {
return (state, currentVersion) => {
const reduxMigrate = createReduxMigrate(migrations, config)
// If state from current storage is empty try migrate state from legacy storage
// This also triggers versioned migrations
if (!state) {
try {
console.log('redux-persist/legacy: no inbound state, running legacy state migration')
state = migrateLegacyState(state)
} catch (migrateLegacyStateError) {
console.error('redux-persist/legacy: migration error', migrateLegacyStateError)
}
}
return reduxMigrate(state, currentVersion)
}
}
/**
* Versioned migrations
*/
export default {
} |
its also possible to do this with a custom getStoredState implementation. Thats what we did for v4 to v5 migration: https://github.com/rt2zz/redux-persist/blob/master/docs/MigrationGuide-v5.md#experimental-v4-to-v5-state-migration |
Good point, @rt2zz Looking at the |
My solution for migrating from one storage system to another -- an intermediate storage system: (Edit: this works but still didn't solve the problem I was really having, which is that my app is crashing with out of memory issues. Logging "Out of memory" from the line import { createStore, applyMiddleware } from 'redux';
import { createMigrate, persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import ExpoFileSystemStorage from "redux-persist-expo-filesystem"
import { PersistGate } from 'redux-persist/lib/integration/react';
// migrating from AsyncStorage to Expo FileSystem storage
// put in on May 15 2019, can probably remove in a couple months but don't have to
const MigratedStorage = {
async getItem(key) {
try {
const res = await ExpoFileSystemStorage.getItem(key);
if (res) {
// Using new storage system
return res;
}
} catch (e) {}
// Using old storage system, should only happen once
const res = await storage.getItem(key);
storage.setItem(key, ''); // clear old storage
return res;
},
setItem(key, value) {
return ExpoFileSystemStorage.setItem(key, value);
},
removeItem(key) {
return ExpoFileSystemStorage.removeItem(key);
}
};
const migrations = {
...
};
const persistConfig = {
key: 'root',
storage: MigratedStorage,
migrate: createMigrate(migrations, { debug: false }),
};
const reducer = persistReducer(persistConfig, rootReducer);
const store = createStore(reducer, applyMiddleware(thunkMiddleware));
const persistor = persistStore(store); |
I have followed this tutorial and it works fine only after I close the app and reopen it. if you don't close the app or reload it, it still uses the old data. can you guide me how to solve it? |
This code do not work, because when you migrate to new storage, this storage is empty and "migrate" is not called for that storage on first launch.
Then in config use
This method also let you migrate any nested persisted reducer separately |
We were having similar problem. We didn't change the storage location, but it stopped picking the data on launch once we upgraded our Async storage package. Following code is working for now(I feel it's still hacky so need good amount of testing).
|
@st1ng your solution is the simplest one ! Yet the BEST ! Thanks !! |
Successfully migrated from localstorage to indexedDB!
|
I'm working on a project where I need to migrate my data initially stored using AsyncStorage to another one: redux-persist-fs-storage
Based on this issue: #679 I've manage to get the data stored in AsyncStorage this way:
I'm looking a way to inject these data (asyncState) into the new fileSystem store.
In v4 we could achieve that using a rehydrate method on the persistor.
fsPersistor.rehydrate(asyncState, { serial: false })
It's not a migration from v4 to v5, just from a store to another using v5.
Can't find a way to do it in v5. An input on how to do it would be great :)
Cheers
The text was updated successfully, but these errors were encountered: