fix: Ensure storage upgrade path from any version to above.

This commit is contained in:
Quentin “Storm1er” Decaunes 2020-03-18 10:18:22 +01:00
parent 13bcd78473
commit 2388d4b974

View File

@ -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 = () => {