Skip to content

Commit

Permalink
Migrate from AsyncStorage to FileStorage (#2084)
Browse files Browse the repository at this point in the history
* Filesystem storage migration working

* Logging

* Add mock for rn-fetch

* Update package.json

* added throw to failed AsyncStorage so data will not be overwritten

* added try catch to filesystem

* async to set and remove methods in  migration

* updated key checking logic

* removed key check

Co-authored-by: andrepimenta <andrepimenta7@gmail.com>
Co-authored-by: sethkfman <seth.kaufman@consensys.net>
Co-authored-by: sethkfman <10342624+sethkfman@users.noreply.github.com>
  • Loading branch information
4 people authored Apr 1, 2021
1 parent c477fde commit d3174cd
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
19 changes: 19 additions & 0 deletions app/__mocks__/rn-fetch-blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const noop = () => ({});

export default {
DocumentDir: noop,
fetch: noop,
base64: noop,
android: noop,
ios: noop,
config: noop,
session: noop,
fs: {
exists: () => Promise.resolve(),
dirs: {
CacheDir: noop,
DocumentDir: noop
}
},
wrap: noop
};
43 changes: 41 additions & 2 deletions app/store/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
import { createStore } from 'redux';
import { persistStore, persistReducer, createMigrate } from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';
import FilesystemStorage from 'redux-persist-filesystem-storage';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import rootReducer from '../reducers';
import { migrations, version } from './migrations';
import Logger from '../util/Logger';

const MigratedStorage = {
async getItem(key) {
try {
const res = await FilesystemStorage.getItem(key);
if (res) {
// Using new storage system
return res;
}
} catch {
//Fail silently
}

// Using old storage system, should only happen once
try {
const res = await AsyncStorage.getItem(key);
if (res) {
// Using old storage system
return res;
}
} catch (error) {
Logger.error(error, { message: 'Failed to run migration' });
throw new Error('Failed async storage storage fetch.');
}
},
async setItem(key, value) {
return await FilesystemStorage.setItem(key, value);
},
async removeItem(key) {
try {
return await FilesystemStorage.removeItem(key);
} catch (error) {
Logger.error(error, { message: 'Failed to remove item' });
}
}
};

const persistConfig = {
key: 'root',
version,
storage: AsyncStorage,
storage: MigratedStorage,
stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
migrate: createMigrate(migrations, { debug: false })
migrate: createMigrate(migrations, { debug: false }),
writeFailHandler: error => Logger.error(error, { message: 'Error persisting data' }) // Log error if saving state fails
};

const pReducer = persistReducer(persistConfig, rootReducer);
Expand Down
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ PODS:
- React-jsi (= 0.63.4)
- ReactNativePayments (1.5.0):
- React
- rn-fetch-blob (0.12.0):
- React-Core
- RNCAsyncStorage (1.12.1):
- React-Core
- RNCCheckbox (0.4.2):
Expand Down Expand Up @@ -460,6 +462,7 @@ DEPENDENCIES:
- React-RCTVibration (from `../node_modules/react-native/Libraries/Vibration`)
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
- "ReactNativePayments (from `../node_modules/@exodus/react-native-payments/lib/ios`)"
- rn-fetch-blob (from `../node_modules/rn-fetch-blob`)
- "RNCAsyncStorage (from `../node_modules/@react-native-community/async-storage`)"
- "RNCCheckbox (from `../node_modules/@react-native-community/checkbox`)"
- "RNCClipboard (from `../node_modules/@react-native-community/clipboard`)"
Expand Down Expand Up @@ -583,6 +586,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon"
ReactNativePayments:
:path: "../node_modules/@exodus/react-native-payments/lib/ios"
rn-fetch-blob:
:path: "../node_modules/rn-fetch-blob"
RNCAsyncStorage:
:path: "../node_modules/@react-native-community/async-storage"
RNCCheckbox:
Expand Down Expand Up @@ -679,6 +684,7 @@ SPEC CHECKSUMS:
React-RCTVibration: ae4f914cfe8de7d4de95ae1ea6cc8f6315d73d9d
ReactCommon: 73d79c7039f473b76db6ff7c6b159c478acbbb3b
ReactNativePayments: a4e3ac915256a4e759c8a04338b558494a63a0f5
rn-fetch-blob: f065bb7ab7fb48dd002629f8bdcb0336602d3cba
RNCAsyncStorage: b03032fdbdb725bea0bd9e5ec5a7272865ae7398
RNCCheckbox: 357578d3b42652c78ee9a1bb9bcfc3195af6e161
RNCClipboard: 8148e21ac347c51fd6cd4b683389094c216bb543
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@
"redux": "4.0.1",
"redux-mock-store": "1.5.3",
"redux-persist": "5.10.0",
"redux-persist-filesystem-storage": "^3.0.0",
"reselect": "^4.0.0",
"rn-fetch-blob": "^0.12.0",
"stream-browserify": "1.0.0",
"through2": "3.0.1",
"url": "0.11.0",
Expand Down Expand Up @@ -285,7 +287,7 @@
"^.+\\.js$": "<rootDir>jest.preprocessor.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(react-native|@react-navigation|@react-native-community|react-navigation|react-navigation-redux-helpers|@sentry))"
"node_modules/(?!(react-native|rn-fetch|redux-persist-filesystem|@react-navigation|@react-native-community|react-navigation|react-navigation-redux-helpers|@sentry))"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
Expand Down
39 changes: 33 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3028,16 +3028,16 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=

base-64@0.1.0, base-64@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
integrity sha1-eAqZyE59YAJgNhURxId2E78k9rs=

base-64@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/base-64/-/base-64-1.0.0.tgz#09d0f2084e32a3fd08c2475b973788eee6ae8f4a"
integrity sha512-kwDPIFCGx0NZHog36dj+tHiwP4QMzsZ3AgMViUBKI0+V5n4U0ufTCUMhnQ04diaRI8EX/QcPfql7zlhZ7j4zgg==

base-64@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
integrity sha1-eAqZyE59YAJgNhURxId2E78k9rs=

base-x@^3.0.2, base-x@^3.0.8:
version "3.0.8"
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.8.tgz#1e1106c2537f0162e8b52474a557ebb09000018d"
Expand Down Expand Up @@ -6619,6 +6619,18 @@ glob-parent@^5.0.0:
dependencies:
is-glob "^4.0.1"

glob@7.0.6:
version "7.0.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.6.tgz#211bafaf49e525b8cd93260d14ab136152b3f57a"
integrity sha1-IRuvr0nlJbjNkyYNFKsTYVKz9Xo=
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.2"
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^6.0.1:
version "6.0.4"
resolved "https://registry.yarnpkg.com/glob/-/glob-6.0.4.tgz#0f08860f6a155127b2fadd4f9ce24b1aab6e4d22"
Expand Down Expand Up @@ -9417,7 +9429,7 @@ minimalistic-crypto-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=

"minimatch@2 || 3", minimatch@^3.0.4:
"minimatch@2 || 3", minimatch@^3.0.2, minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
Expand Down Expand Up @@ -11473,6 +11485,13 @@ redux-mock-store@1.5.3:
dependencies:
lodash.isplainobject "^4.0.6"

redux-persist-filesystem-storage@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/redux-persist-filesystem-storage/-/redux-persist-filesystem-storage-3.0.0.tgz#4848825d6157f694af5218d8ebb3ca6db09d3cb9"
integrity sha512-rJe8Hb5ULC/rUyqt5E1G62r1cUQA7hmdrVd38q32bWRnv+KiIAORuzpcOW5eDkzaS0w3v1juxUcKciNyZmeCsg==
dependencies:
rn-fetch-blob "^0.12.0"

redux-persist@5.10.0:
version "5.10.0"
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-5.10.0.tgz#5d8d802c5571e55924efc1c3a9b23575283be62b"
Expand Down Expand Up @@ -11808,6 +11827,14 @@ rlp@^2.2.4:
dependencies:
bn.js "^4.11.1"

rn-fetch-blob@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/rn-fetch-blob/-/rn-fetch-blob-0.12.0.tgz#ec610d2f9b3f1065556b58ab9c106eeb256f3cba"
integrity sha512-+QnR7AsJ14zqpVVUbzbtAjq0iI8c9tCg49tIoKO2ezjzRunN7YL6zFSFSWZm6d+mE/l9r+OeDM3jmb2tBb2WbA==
dependencies:
base-64 "0.1.0"
glob "7.0.6"

rn-nodeify@10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/rn-nodeify/-/rn-nodeify-10.0.1.tgz#b54a3f2a61eda88b40639ee9262f51f34039e353"
Expand Down

0 comments on commit d3174cd

Please sign in to comment.