Skip to content

Commit

Permalink
feat: Migrate settings when upgrading to 1.23.x
Browse files Browse the repository at this point in the history
Part of #1296.
  • Loading branch information
tcrammond committed Mar 13, 2019
1 parent f936868 commit 7b497da
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2136,21 +2136,22 @@ window.onbeforeunload = function () {

if (!FF) {
TogglButton.checkPermissions();
}

// Check whether new version is installed
browser.runtime.onInstalled.addListener(function (details) {
if (details.reason === 'install') {
TogglButton.checkPermissions(0);
} else if (details.reason === 'update') {
if (
details.previousVersion[0] === '0' &&
process.env.VERSION[0] === '1'
) {
TogglButton.checkPermissions(1);
}
// Check whether new version is installed
browser.runtime.onInstalled.addListener(function (details) {
if (details.reason === 'install') {
if (!FF) TogglButton.checkPermissions(0);
} else if (details.reason === 'update') {
console.info(`Updated from ${details.previousVersion} to ${process.env.VERSION}.`);
const [ prevMajor, prevMinor ] = details.previousVersion.split('.').map(Number);
const [ nextMajor, nextMinor ] = process.env.VERSION.split('.').map(Number);
if (prevMajor === 1 && prevMinor <= 22 && nextMajor === 1 && nextMinor >= 23) {
// Attempt to migrate legacy localstorage settings to storage.sync settings
db._migrateToStorageSync();
}
});
}
}
});

if (browser.commands) {
browser.commands.onCommand.addListener(function (command) {
Expand Down
49 changes: 49 additions & 0 deletions src/scripts/lib/db.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bugsnagClient from './bugsnag';
import origins from '../origins';
const browser = require('webextension-polyfill');

Expand Down Expand Up @@ -195,6 +196,19 @@ export default class Db {
});
}

setMultiple (settings) {
return browser.storage.sync
.set(settings)
.catch((e) => {
console.error(`Error attempting to save settings:`, settings, e);
})
.finally(() => {
if (process.env.DEBUG) {
console.info(`Saved multiple settings :`, settings);
}
});
}

getLocalCollection (key) {
let collection = localStorage.getItem(key);
if (!collection) {
Expand Down Expand Up @@ -244,4 +258,39 @@ export default class Db {
callback();
}
}

_migrateToStorageSync () {
console.info('Migrating settings to v2');
bugsnagClient.leaveBreadcrumb('Attempting settings migration to v2');

try {
const allSettings = { ...DEFAULT_SETTINGS, ...CORE_SETTINGS };
const oldSettings = Object.keys(allSettings)
.reduce((accumulator, key) => {
const defaultValue = allSettings[key];
let value = localStorage.getItem(key);
if (value && typeof defaultValue === 'boolean') {
value = JSON.parse(value);
}
accumulator[key] = (typeof value === 'undefined' ? defaultValue : value);
return accumulator;
}, {});

if (process.env.DEBUG) {
console.log('Found old settings: ', oldSettings);
}

this.setMultiple(oldSettings)
.then(() => {
console.info('Succesully migrated old settings to v2');
bugsnagClient.leaveBreadcrumb('Migrated settings to v2');
})
.catch((e) => {
console.error('Failed to migrate settings to v2; ');
bugsnagClient.notify(e);
});
} catch (e) {
bugsnagClient.notify(e);
}
}
}

0 comments on commit 7b497da

Please sign in to comment.