Skip to content

Commit

Permalink
feat: delete hydra.db if migrations are corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
zamitto committed Jan 5, 2025
1 parent 92e641e commit 097aff1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
43 changes: 25 additions & 18 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand Down
15 changes: 8 additions & 7 deletions src/main/knex-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ class MigrationSource implements Knex.MigrationSource<HydraMigration> {
}
}

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(),
Expand Down
1 change: 0 additions & 1 deletion src/main/services/user/get-user-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 097aff1

Please sign in to comment.