From 2388d4b97429784ee5a5f5f69c606e71719ef520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20=E2=80=9CStorm1er=E2=80=9D=20Decaunes?= Date: Wed, 18 Mar 2020 10:18:22 +0100 Subject: [PATCH] fix: Ensure storage upgrade path from any version to above. --- public/upgrader.js | 55 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/public/upgrader.js b/public/upgrader.js index 89f0923..f816700 100644 --- a/public/upgrader.js +++ b/public/upgrader.js @@ -11,9 +11,7 @@ function storageHaveCurrentVersion() { function getAllAvailableVersion() { var keys = Object.keys(electronSettings.getAll()); - var versionAsRegex = isDev - ? /^\d*_\d*_\d*(-dev)$/ - : /^\d*_\d*_\d*$/; + var versionAsRegex = isDev ? /^\d*_\d*_\d*(-dev)$/ : /^\d*_\d*_\d*$/; var validKeys = keys.filter((val, index) => { return versionAsRegex.test(val); }); @@ -32,42 +30,43 @@ function storageHasPreviousVersion() { function getPreviousVersion() { var validKeys = getAllAvailableVersion(); const index = validKeys.indexOf(app_version_as_string); - return validKeys[index-1]; + return validKeys[index - 1]; } +/** + * Upgrade from any version to above. + * + * Just a copy from the previous version to the new one. + * Not executed if the previous version is a special case, see upgradeFromPreviousVersion() below. + */ +function from_any_copyStorage(previousVersion) { + electronSettings.set(`${app_version_as_string}`, electronSettings.get(previousVersion)); +} + +/** + * Upgrade from 2.0.0 to above. + * + * Some of the storage was not under the version scope, + * We move those so the storage is more secure and consistent from version to version. + */ function from_2_0_0_MoveAppContext(previousVersion) { - electronSettings.set( - `${app_version_as_string}.appContext`, - electronSettings.get(previousVersion) - ); + electronSettings.set(`${app_version_as_string}.appContext`, electronSettings.get(previousVersion)); } function from_2_0_0_MoveLightMode() { - electronSettings.set( - `${app_version_as_string}.lightMode`, - electronSettings.get("lightMode") - ); + electronSettings.set(`${app_version_as_string}.lightMode`, electronSettings.get("lightMode")); } function from_2_0_0_MoveLocale() { - electronSettings.set( - `${app_version_as_string}.locale`, - electronSettings.get("locale") - ); + electronSettings.set(`${app_version_as_string}.locale`, electronSettings.get("locale")); } function from_2_0_0_MoveReApplyPeriodically() { - electronSettings.set( - `${app_version_as_string}.reApplyPeriodically`, - electronSettings.get("reApplyPeriodically") - ); + electronSettings.set(`${app_version_as_string}.reApplyPeriodically`, electronSettings.get("reApplyPeriodically")); } function from_2_0_0_MoveVotedPresets() { - electronSettings.set( - `${app_version_as_string}.votedPresets`, - electronSettings.get("votedPresets") - ); + electronSettings.set(`${app_version_as_string}.votedPresets`, electronSettings.get("votedPresets")); } function upgradeFromPreviousVersion() { - isDev && console.log('Upgrading storage...'); + if (isDev) console.log("Upgrading storage..."); const previousVersion = getPreviousVersion(); switch (previousVersion) { case "2_0_0": @@ -77,13 +76,13 @@ function upgradeFromPreviousVersion() { from_2_0_0_MoveLocale(previousVersion); from_2_0_0_MoveReApplyPeriodically(previousVersion); from_2_0_0_MoveVotedPresets(previousVersion); - isDev && console.log('... from 2.0.0'); + if (isDev) console.log("... from 2.0.0"); break; - + default: + from_any_copyStorage(previousVersion); break; } - } const upgrader = () => {