rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.95k stars 865 forks source link

createMigrate only worked the first time, version change didn't help #957

Open dayhaysoos opened 5 years ago

dayhaysoos commented 5 years ago

I used migration to add an object and it worked just fine. I needed to add another object but it doesn't seem to work this time:

import { createMigrate, persistStore, persistReducer } from 'redux-persist';
import thunk from 'redux-thunk';
// import logger from 'redux-logger';
import rootReducer from './reducers/index';

import storage from 'redux-persist/lib/storage';

const migrations = {
    0: (state) => ({
        ...state,
        claudio: {}
    }),
    1: (state) => ({
        claudio: state.favorites.moves.claudio,
    })
};

const persistConfig = {
    key: 'root',
    version: 3.6,
    storage,
    migrate: createMigrate(migrations, { debug: true }),
    whitelist: [
        'settings',
        'favorites'
    ],
};

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const persistedReducer = persistReducer(persistConfig, rootReducer);

let store = createStore(
    persistedReducer,
    {},
    composeEnhancers(
        applyMiddleware(
            thunk,
            // logger
        ),
    ),
);

let persistor = persistStore(store);

export default { store, persistor };

When it worked the first time, the "claudio" value was "shaheen" and I ticked the version up to 1.

As you can see, I replaced shaheen with claudio and now i"m on version 3.6. With debug on, it shows the migrationKeys array is empty.

Is there anything else I should be changing to get this to work?

dayhaysoos commented 5 years ago

Still having issues with this. I don't think the functions inside the migrations object are being called at all

simondell commented 5 years ago

From my experiments, I find:

So, to run your migrations[1]...

import { createMigrate, persistStore, persistReducer } from 'redux-persist';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index'; // this must export a store with a `claudio` key

import storage from 'redux-persist/lib/storage';

const migrations = {
    0: (state) => ({
        ...state,
        claudio: {}
    }),
    1: (state) => ({
        claudio: state.favorites.moves.claudio,
    })
};

const persistConfig = {
    key: 'root',
    version: 1, // version must match the migrations keys
    storage,
    migrate: createMigrate(migrations, { debug: true }),
    whitelist: [
        'settings',
        'favorites',
        'claudio', // if you whitelist reducers, you must include your new reducer in your list
    ],
};

//... the rest of your code looked fine