From 87763cdf2c3e4927e4543bd5a228fc6d188d4bde Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Wed, 24 Jul 2019 13:55:02 +0200 Subject: [PATCH] [Wallet] Add redux migration v0 (#143) --- packages/mobile/src/redux/migrations.test.ts | 21 ++ packages/mobile/src/redux/migrations.ts | 16 ++ packages/mobile/src/redux/store.ts | 6 +- packages/mobile/test/schemas.ts | 200 ++++++++++--------- 4 files changed, 145 insertions(+), 98 deletions(-) create mode 100644 packages/mobile/src/redux/migrations.test.ts create mode 100644 packages/mobile/src/redux/migrations.ts diff --git a/packages/mobile/src/redux/migrations.test.ts b/packages/mobile/src/redux/migrations.test.ts new file mode 100644 index 00000000000..d0236049da9 --- /dev/null +++ b/packages/mobile/src/redux/migrations.test.ts @@ -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) + }) +}) diff --git a/packages/mobile/src/redux/migrations.ts b/packages/mobile/src/redux/migrations.ts new file mode 100644 index 00000000000..a2c4dd3a4d0 --- /dev/null +++ b/packages/mobile/src/redux/migrations.ts @@ -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, + }, + } + }, +} diff --git a/packages/mobile/src/redux/store.ts b/packages/mobile/src/redux/store.ts index ac88c9d927f..8141bff7e99 100644 --- a/packages/mobile/src/redux/store.ts +++ b/packages/mobile/src/redux/store.ts @@ -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__) { diff --git a/packages/mobile/test/schemas.ts b/packages/mobile/test/schemas.ts index 2469ca4614f..cf40d411f37 100644 --- a/packages/mobile/test/schemas.ts +++ b/packages/mobile/test/schemas.ts @@ -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 { - return schemaVersions[schemaVersions.length - 1] as Partial + return v0Schema as Partial }