rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.97k stars 867 forks source link

When should I add new migration to the state? #1476

Closed alamenai closed 2 months ago

alamenai commented 3 months ago

My current state looks like :


/* eslint-disable @typescript-eslint/no-explicit-any */
export const migrations = {
  0: (state: any) => {
    return {
      ...state,
    }
  },
  1: (state: any) => {
    const measures = state.customInstallation.panel.measures || {}
    return {
      ...state,
      customInstallation: {
        ...state.customInstallation,
        panel: {
          ...state.customInstallation.panel,
          measures: {
            ...measures,
            neighborSpace: measures.neighborSpace || 0.5,
            obstructionSpace: measures.obstructionSpace || measures.roofSuperStructure || 0.3,
          },
        },
      },
    }
  },
  2: (state: any) => {
    return {
      ...state,
      organization: {
        adminDeleteEnabled: false,
        createdAt: undefined,
        createdBy: undefined,
        hasImage: false,
        id: undefined,
        imageUrl: undefined,
        maxAllowedMemberships: 0,
        name: undefined,
        privateMetadata: {},
        publicMetadata: {},
        slug: undefined,
        updatedAt: undefined,
      },
    }
  },
}

I have created new slice with its reducers and added it to the store :

import { createSlice } from "@reduxjs/toolkit"

import { BuildingAddress } from "../types"
import { reducers } from "./reducers"

export const initialState: BuildingAddress = {
  id: undefined,
  city: undefined,
  houseNumber: undefined,
  street: undefined,
  zipCode: undefined,
}

export const buildingAddressSlice = createSlice({
  name: "buildingAddress",
  initialState,
  reducers,
})

export const { initialAddress, addressChanged } = buildingAddressSlice.actions

export default buildingAddressSlice.reducer

I just confused if should I add next migration or it's made when my current state change it's structure ( not the new added ones ).

I mean like this :


/* eslint-disable @typescript-eslint/no-explicit-any */
export const migrations = {
  0: (state: any) => {
    return {
      ...state,
    }
  },
  1: (state: any) => {
    const measures = state.customInstallation.panel.measures || {}
    return {
      ...state,
      customInstallation: {
        ...state.customInstallation,
        panel: {
          ...state.customInstallation.panel,
          measures: {
            ...measures,
            neighborSpace: measures.neighborSpace || 0.5,
            obstructionSpace: measures.obstructionSpace || measures.roofSuperStructure || 0.3,
          },
        },
      },
    }
  },
  2: (state: any) => {
    return {
      ...state,
      organization: {
        adminDeleteEnabled: false,
        createdAt: undefined,
        createdBy: undefined,
        hasImage: false,
        id: undefined,
        imageUrl: undefined,
        maxAllowedMemberships: 0,
        name: undefined,
        privateMetadata: {},
        publicMetadata: {},
        slug: undefined,
        updatedAt: undefined,
      },
    }
  },
3:(state:any)=>{
  return { 
    ...state,
   buildingAddress:initialBuildingAddress
}
}
varpstar commented 2 months ago

Imho you should only use migration if you are not sure whether the selected state reconciler can process the merge correctly. So if you add new slices, it is not necessary to create a migration as it will be merged automatically