Skip to content

Commit

Permalink
[Wallet] Add redux migration v0 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Jul 24, 2019
1 parent 0b95817 commit 87763cd
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 98 deletions.
21 changes: 21 additions & 0 deletions packages/mobile/src/redux/migrations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { migrations } from 'src/redux/migrations'
import { getLatestSchema } from 'test/schemas'

describe('Redux persist migrations', () => {
it('work for v-1 to v0', () => {
const defaultSchema = getLatestSchema()
const vNeg1Stub = {
...defaultSchema,
app: {
...defaultSchema.app,
numberVerified: true,
inviteCodeEntered: true,
},
}
const migratedSchema = migrations[0](vNeg1Stub)
expect(migratedSchema.invite.redeemComplete).toEqual(true)
expect(migratedSchema.identity.startedVerification).toEqual(true)
expect(migratedSchema.identity.askedContactsPermission).toEqual(true)
expect(migratedSchema.identity.isLoadingImportContacts).toEqual(false)
})
})
16 changes: 16 additions & 0 deletions packages/mobile/src/redux/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const migrations = {
0: (state: any) => {
return {
...state,
invite: {
...state.invite,
redeemComplete: state.app.inviteCodeEntered,
},
identity: {
startedVerification: state.app.numberVerified,
askedContactsPermission: state.app.numberVerified,
isLoadingImportContacts: false,
},
}
},
}
6 changes: 4 additions & 2 deletions packages/mobile/src/redux/store.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { applyMiddleware, compose, createStore } from 'redux'
import { persistReducer, persistStore } from 'redux-persist'
import { createMigrate, persistReducer, persistStore } from 'redux-persist'
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'
import storage from 'redux-persist/lib/storage'
import createSagaMiddleware from 'redux-saga'
import thunk from 'redux-thunk'
import { migrations } from 'src/redux/migrations'
import rootReducer from 'src/redux/reducers'
import { rootSaga } from 'src/redux/sagas'

const persistConfig: any = {
key: 'root',
version: -1, // default is -1, increment as we make migrations
version: 0, // default is -1, increment as we make migrations
storage,
blacklist: ['home', 'geth', 'exchange', 'networkInfo', 'alert'],
stateReconciler: autoMergeLevel2,
migrate: createMigrate(migrations, { debug: true }),
}

if (__DEV__) {
Expand Down
200 changes: 104 additions & 96 deletions packages/mobile/test/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,112 @@
import { RootState } from 'src/redux/reducers'

const schemaVersions = [
// Default (version -1 schema)
{
app: {
inviteCodeEntered: false,
loggedIn: false,
numberVerified: false,
error: null,
dismissErrorAfter: null,
language: null,
doingBackupFlow: false,
message: null,
dismissMessageAfter: null,
analyticsEnabled: true,
},
networkInfo: {
connected: true,
},
send: {
isSending: false,
suggestedFee: '',
recentPhoneNumbers: [],
recipientCache: {},
},
goldToken: {
balance: null,
educationCompleted: false,
lastFetch: null,
},
stableToken: {
balance: null,
educationCompleted: false,
lastFetch: null,
},
home: {
loading: false,
notifications: [],
},
medianator: {
exchangeRate: '1',
},
transactions: {
standbyTransactions: [],
},
web3: {
isReady: false,
syncProgress: 0,
syncProgressData: {
currentBlock: 0,
highestBlock: 0,
startBlock: 0,
},
latestBlockNumber: 0,
account: '0x0000000000000000000000000000000000007E57',
commentKey: '0x0000000000000000000000000000000000008F68',
gasPriceLastUpdated: 0,
},
identity: {
attestationCodes: [],
numCompleteAttestations: 0,
verificationFailed: false,
addressToE164Number: {},
e164NumberToAddress: {},
startedVerification: false,
askedContactsPermission: false,
isLoadingImportContacts: false,
},
account: {
name: 'John Doe',
e164PhoneNumber: '+14155556666',
defaultCountryCode: '+1',
contactDetails: {
contactId: 'contactId',
thumbnailPath: null,
},
devModeActive: false,
devModeClickCount: 0,
photosNUXClicked: false,
pincodeSet: false,
accountCreationTime: 99999999999999,
paymentRequests: [],
showFakeData: false,
backupCompleted: false,
dismissedEarnRewards: false,
dismissedInviteFriends: false,
},
invite: {
isSendingInvite: false,
invitees: {},
redeemedInviteCode: '',
redeemComplete: false,
// Default (version -1 schema)
const vNeg1Schema = {
app: {
inviteCodeEntered: false,
loggedIn: false,
numberVerified: false,
error: null,
dismissErrorAfter: null,
language: null,
doingBackupFlow: false,
message: null,
dismissMessageAfter: null,
analyticsEnabled: true,
},
networkInfo: {
connected: true,
},
send: {
isSending: false,
suggestedFee: '',
recentPhoneNumbers: [],
recipientCache: {},
},
goldToken: {
balance: null,
educationCompleted: false,
lastFetch: null,
},
stableToken: {
balance: null,
educationCompleted: false,
lastFetch: null,
},
home: {
loading: false,
notifications: [],
},
medianator: {
exchangeRate: '1',
},
transactions: {
standbyTransactions: [],
},
web3: {
isReady: false,
syncProgress: 0,
syncProgressData: {
currentBlock: 0,
highestBlock: 0,
startBlock: 0,
},
escrow: {
sentEscrowedPayments: [],
currentPaymentId: '',
latestBlockNumber: 0,
account: '0x0000000000000000000000000000000000007E57',
commentKey: '0x0000000000000000000000000000000000008F68',
gasPriceLastUpdated: 0,
},
identity: {
attestationCodes: [],
numCompleteAttestations: 0,
verificationFailed: false,
addressToE164Number: {},
e164NumberToAddress: {},
},
account: {
name: 'John Doe',
e164PhoneNumber: '+14155556666',
defaultCountryCode: '+1',
contactDetails: {
contactId: 'contactId',
thumbnailPath: null,
},
devModeActive: false,
devModeClickCount: 0,
photosNUXClicked: false,
pincodeSet: false,
accountCreationTime: 99999999999999,
paymentRequests: [],
showFakeData: false,
backupCompleted: false,
dismissedEarnRewards: false,
dismissedInviteFriends: false,
},
]
invite: {
isSendingInvite: false,
invitees: {},
redeemedInviteCode: '',
},
escrow: {
sentEscrowedPayments: [],
currentPaymentId: '',
},
}

const v0Schema = {
...vNeg1Schema,
identity: {
...vNeg1Schema.identity,
startedVerification: false,
askedContactsPermission: false,
isLoadingImportContacts: false,
},
invite: {
...vNeg1Schema.invite,
redeemComplete: false,
},
}

export function getLatestSchema(): Partial<RootState> {
return schemaVersions[schemaVersions.length - 1] as Partial<RootState>
return v0Schema as Partial<RootState>
}

0 comments on commit 87763cd

Please sign in to comment.