diff --git a/src/main/index.ts b/src/main/index.ts index a1c45402a..a2c1c087b 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,14 +3,14 @@ import updater from "electron-updater"; import i18n from "i18next"; import path from "node:path"; import url from "node:url"; -import fs from "node:fs"; +import fs, { rmSync } from "node:fs"; import { electronApp, optimizer } from "@electron-toolkit/utils"; import { logger, WindowManager } from "@main/services"; import { dataSource } from "@main/data-source"; import resources from "@locales"; import { userPreferencesRepository } from "@main/repository"; -import { knexClient, migrationConfig } from "./knex-client"; -import { databaseDirectory } from "./constants"; +import { getKnexClient, migrationConfig } from "./knex-client"; +import { databaseDirectory, databasePath } from "./constants"; import { PythonRPC } from "./services/python-rpc"; import { Aria2 } from "./services/aria2"; import { loadState } from "./main"; @@ -56,14 +56,25 @@ const runMigrations = async () => { fs.mkdirSync(databaseDirectory, { recursive: true }); } - await knexClient.migrate.list(migrationConfig).then((result) => { - logger.log( - "Migrations to run:", - result[1].map((migration) => migration.name) - ); - }); - - await knexClient.migrate.latest(migrationConfig); + for (let trial = 0; trial < 2; trial++) { + const knexClient = getKnexClient(); + + try { + await knexClient.migrate.list(migrationConfig).then((result) => { + logger.log( + "Migrations to run:", + result[1].map((migration) => migration.name) + ); + }); + + await knexClient.migrate.latest(migrationConfig); + return; + } catch (err) { + logger.log("Migrations failed to run, deleting db and trying again", err); + await knexClient.destroy(); + rmSync(databasePath); + } + } }; // This method will be called when Electron has finished @@ -77,13 +88,9 @@ app.whenReady().then(async () => { return net.fetch(url.pathToFileURL(decodeURI(filePath)).toString()); }); - await runMigrations() - .then(() => { - logger.log("Migrations executed successfully"); - }) - .catch((err) => { - logger.log("Migrations failed to run:", err); - }); + await runMigrations().then(() => { + logger.log("Migrations executed successfully"); + }); await dataSource.initialize(); diff --git a/src/main/knex-client.ts b/src/main/knex-client.ts index 821efc808..355666ada 100644 --- a/src/main/knex-client.ts +++ b/src/main/knex-client.ts @@ -49,13 +49,14 @@ class MigrationSource implements Knex.MigrationSource { } } -export const knexClient = knex({ - debug: !app.isPackaged, - client: "better-sqlite3", - connection: { - filename: databasePath, - }, -}); +export const getKnexClient = () => + knex({ + debug: !app.isPackaged, + client: "better-sqlite3", + connection: { + filename: databasePath, + }, + }); export const migrationConfig: Knex.MigratorConfig = { migrationSource: new MigrationSource(), diff --git a/src/main/services/user/get-user-data.ts b/src/main/services/user/get-user-data.ts index 7e9244547..1b8cb1e03 100644 --- a/src/main/services/user/get-user-data.ts +++ b/src/main/services/user/get-user-data.ts @@ -42,7 +42,6 @@ export const getUserData = () => { }) .catch(async (err) => { if (err instanceof UserNotLoggedInError) { - logger.info("User is not logged in", err); return null; } logger.error("Failed to get logged user"); diff --git a/src/preload/index.ts b/src/preload/index.ts index 2a512af2d..05388a8df 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -151,7 +151,8 @@ contextBridge.exposeInMainWorld("electron", { onAchievementUnlocked: (cb: () => void) => { const listener = (_event: Electron.IpcRendererEvent) => cb(); ipcRenderer.on("on-achievement-unlocked", listener); - return () => ipcRenderer.removeListener("on-achievement-unlocked", listener); + return () => + ipcRenderer.removeListener("on-achievement-unlocked", listener); }, /* Hardware */