3
votes

I want to write a migration for redux-persist, but somehow migration is not being applied. I've followed the official documentation about migrations: https://github.com/rt2zz/redux-persist/blob/master/docs/migrations.md

But something in the code below is not working since my redux-persist is not applying the migration.

When I try to debug this migration it says that version number is the same.

Redux-persist version match noop migration

Not sure if I'm missing something in the config or migration writing.

In my store I have something like this for persist configuration

const persistConfig: PersistConfig = {
    key: 'primary',
    storage,
    blacklist: [

    ],
    stateReconciler: autoMergeLevel2,
    version: 1,
    migrate: createMigrate(migrations, { debug: true })
} as PersistConfig;

and my migration looks like:

const migrations = {
    0: (state: StoreState) => {
        return {
            ...state,
            application: {
                ...state.application
            }
        }
    },
    1: (state: StoreState) => {
        return {
            ...state,
            application: {
                ...state.application,
                items:[{id: 1, isDone: false}, {id: 2, isDone: false}].map((item, index)=>{
                          if(item.id == 1){
                               item.isDone = true
                            }
                        }),
            },
        }
    }
}

In my first version of the state and application reducer I had object items which looked like this:

items:[
   {id: 1},
   {id: 2}
]

and now it needs to look like:

items:[
   {id: 1, isDone: false/true},
   {id: 2, isDone: false/true}
]
1

1 Answers

2
votes

You need to upgrade the version in persistConfig object for redux-persist. If a migration has run for a version once it will not run again for the same version.

Before

const persistConfig = {
  key: "root",
  storage,
  version: 0,
  migrate: createMigrate(migrations, { debug: true }),
  stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
  whitelist: ["auth", "user", "app", "card"]
};

After

const persistConfig = {
  key: "root",
  storage,
  version: 1,
  migrate: createMigrate(migrations, { debug: true }),
  stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
  whitelist: ["auth", "user", "app", "card"]
};

enter image description here