Skip to content

Commit

Permalink
Save backup in a file while migrating from 2.3.3 to 2.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinkyaraj-23 committed Dec 11, 2024
1 parent f7b36ac commit 5e7b427
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 19 deletions.
11 changes: 7 additions & 4 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
"@umami/state": "workspace:^",
"@umami/test-utils": "workspace:^",
"@umami/tezos": "workspace:^",
"@umami/utils": "workspace:^",
"@umami/typescript-config": "workspace:^",
"@umami/tzkt": "workspace:^",
"@umami/utils": "workspace:^",
"@vitejs/plugin-react": "^4.3.4",
"babel-jest": "^29.7.0",
"bignumber.js": "^9.1.2",
Expand Down Expand Up @@ -151,11 +151,14 @@
"vite-plugin-checker": "^0.8.0",
"vite-plugin-node-polyfills": "^0.17.0",
"vite-plugin-sri": "^0.0.2",
"zod": "^3.23.8",
"@hookform/resolvers": "^3.9.1"
"zod": "^3.23.8"
},
"packageManager": "[email protected]",
"dependencies": {
"electron-updater": "6.3.9"
"electron-log": "^5.2.4",
"electron-updater": "6.3.9",
"level": "^9.0.0",
"level-supports": "^6.0.0",
"level-transcoder": "^1.0.1"
}
}
67 changes: 64 additions & 3 deletions apps/desktop/public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ const path = require("path");
const url = require("url");
const process = require("process");
const { autoUpdater } = require("electron-updater");
const { Level } = require("level");
const log = require('electron-log');
const fs = require('fs');

const APP_PROTOCOL = "app";
const APP_HOST = "assets";
// create in memory store of the leveldb database of previous version which had file:// protocol

const appURL = app.isPackaged
? url.format({
Expand All @@ -26,6 +31,57 @@ protocol.registerSchemesAsPrivileged([
},
]);

// Configure electron-log
log.transports.file.file = path.join(app.getPath('userData'), 'Local Storage', 'umami-desktop.log');

async function readAndCopyValues() {
// Path to the LevelDB database
const dbPath = path.join(app.getPath("userData"), "Local Storage", "leveldb");

// Check if the LevelDB database exists
if (!fs.existsSync(dbPath)) {
log.info("LevelDB database not found at path. Code:EM01", dbPath);
return;
}
// check if backup file exists
if (fs.existsSync(path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json"))) {
log.info("Backup file already exists. Code:EM02");
return;
}

// Open the LevelDB database
const db = new Level(dbPath, { valueEncoding: "utf-8" });
await db.open();
try {
const accountsValue = await db.get("_file://\x00\x01persist:accounts");
let rootValue = await db.get("_file://\x00\x01persist:root");
if ( !accountsValue || !rootValue) {
log.info("No data found in the database. Code:EM03");
return;
}
console.log(accountsValue);
console.log(rootValue.length);
const storage = {
"persist:accounts": {accountsValue},
"persist:root": {rootValue},
};
const rawBackup = JSON.stringify(storage);
try{
fs.appendFileSync(path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json"), rawBackup);
}
catch(err){
console.log("Error during leveldb backup creation Code:EM2.", err);
}
console.log("Migration done successfully");
}
catch (err) {
log.error("Error during key migration Code:EM4.", err);
} finally {
db.close().catch((err) => {
log.error("Error closing the database. Code: EM5", err);
});
}
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
Expand Down Expand Up @@ -169,12 +225,13 @@ function start() {
app.quit();
return;
}
let waitForMigration = true;

// Check for app updates, download and notify UI if update is available to be installed.
try {
autoUpdater.checkForUpdatesAndNotify();
} catch (e) {
console.log(e);
log.error(e);
}

if (!app.isDefaultProtocolClient("umami")) {
Expand Down Expand Up @@ -223,7 +280,11 @@ function start() {
// This method will be called when Electron has finished its initialization and
// is ready to create the browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow);
app.whenReady().then(async () => {
// Execute readAndCopyValues at the beginning
await readAndCopyValues();
createWindow();
});

app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
Expand All @@ -236,7 +297,7 @@ function start() {
// Send event to UI when app update is ready to be installed.
// If the update installation won't be triggered by the user, it will be applied the next time the app starts.
autoUpdater.on("update-downloaded", event => {
console.log(`Umami update ${event.version} downloaded and ready to be installed`, url);
log.info(`Umami update ${event.version} downloaded and ready to be installed`, url);
return mainWindow.webContents.send("app-update-downloaded");
});

Expand Down
Loading

0 comments on commit 5e7b427

Please sign in to comment.