Skip to content

Commit 8e583a6

Browse files
committed
chore: legacy state file migration in browser to phstore
1 parent 8428033 commit 8e583a6

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/preferences/PreferencesImpl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ define(function (require, exports, module) {
3030

3131
const PreferencesBase = require("./PreferencesBase"),
3232
Async = require("utils/Async"),
33-
FileSystem = require("filesystem/FileSystem"),
33+
StateManager = require("preferences/StateManager"),
3434

3535
// The SETTINGS_FILENAME is used with a preceding "." within user projects
3636
SETTINGS_FILENAME = "phcode.json",
@@ -85,6 +85,7 @@ define(function (require, exports, module) {
8585
var userScopeLoading = manager.addScope("user", userScope);
8686

8787
_addScopePromises.push(userScopeLoading);
88+
_addScopePromises.push(StateManager._migrateLegacyStateFile());
8889

8990
// Set up the .phcode.json file handling
9091
userScopeLoading

src/preferences/StateManager.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
/*unittests: Preferences Manager */
23+
/*global fs*/
2324

2425
/**
2526
* StateManager
@@ -28,8 +29,12 @@
2829
define(function (require, exports, module) {
2930
const _ = require("thirdparty/lodash"),
3031
EventDispatcher = require("utils/EventDispatcher"),
32+
Metrics = require("utils/Metrics"),
3133
ProjectManager = require("project/ProjectManager");
3234

35+
const LEGACY_STATE_MANAGER_MIGRATED = "ph_state_manager_migrated";
36+
const LEGACY_STATE_FILE_PATH = "/fs/app/state.json";
37+
3338
const PROJECT_CONTEXT = "project";
3439
const GLOBAL_CONTEXT = "global";
3540
const PROJECT_THEN_GLOBAL_CONTEXT = "any";
@@ -235,6 +240,68 @@ define(function (require, exports, module) {
235240
return createExtensionStateManager(prefix);
236241
}
237242

243+
/**
244+
* We used file based state.json in the earlier state manager impl. no we moved to phstore. So we have to move
245+
* earlier users to phstore to prevent losing their files. This code can be deleted anytime after 4 months
246+
* from this commit.
247+
* @private
248+
*/
249+
function _migrateLegacyStateFile() {
250+
if(Phoenix.firstBoot){
251+
// nothing to migrate, fresh install
252+
setVal(LEGACY_STATE_MANAGER_MIGRATED, true);
253+
return new $.Deferred().resolve().promise();
254+
}
255+
if(getVal(LEGACY_STATE_MANAGER_MIGRATED)){
256+
return new $.Deferred().resolve().promise();
257+
}
258+
const _migrated = new $.Deferred();
259+
console.log("Migrating legacy state file", LEGACY_STATE_FILE_PATH);
260+
fs.readFile(LEGACY_STATE_FILE_PATH, "utf8", function (err, data) {
261+
setVal(LEGACY_STATE_MANAGER_MIGRATED, true);
262+
if (err) {
263+
// if error, ignore and continue. state file not found(unlikely to be here)
264+
_migrated.resolve();
265+
return;
266+
}
267+
try{
268+
const keysToMigrate = [
269+
"afterFirstLaunch",
270+
"sidebar",
271+
"workingSetSortMethod",
272+
"healthDataUsage",
273+
"main-toolbar",
274+
"recentProjects",
275+
"healthDataNotificationShown",
276+
"searchHistory",
277+
"problems-panel"
278+
];
279+
const oldState = JSON.parse(data);
280+
for(let key of keysToMigrate) {
281+
if(oldState[key]){
282+
console.log("Migrated Legacy state: ", key, oldState[key]);
283+
setVal(key, oldState[key]);
284+
}
285+
}
286+
fs.unlink(LEGACY_STATE_FILE_PATH, (unlinkErr)=>{
287+
if(unlinkErr){
288+
console.error(`Error deleting legacy state file ${LEGACY_STATE_FILE_PATH}`, unlinkErr);
289+
}
290+
});
291+
} catch (e) {
292+
console.error("Error migrating legacy state file", LEGACY_STATE_FILE_PATH);
293+
}
294+
Metrics.countEvent(Metrics.EVENT_TYPE.PLATFORM, "legacyState", "migrated");
295+
_migrated.resolve();
296+
console.log("Legacy state migration completed");
297+
});
298+
return _migrated.promise();
299+
}
300+
301+
// private API
302+
exports._migrateLegacyStateFile = _migrateLegacyStateFile;
303+
304+
// public api
238305
exports.get = getVal;
239306
exports.set = setVal;
240307
exports.definePreference = definePreferenceInternal;

0 commit comments

Comments
 (0)