Skip to content

Commit

Permalink
Merge pull request #903 from hydralauncher/feat/remove-synchronize-ty…
Browse files Browse the repository at this point in the history
…peorm

feat: use knex migrations
  • Loading branch information
zamitto authored Sep 2, 2024
2 parents 2dcfcce + 6b3d3c8 commit ff43f82
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 107 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
out
.gitignore
migration.stub
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ pnpm-lock.yaml
LICENSE.md
tsconfig.json
tsconfig.*.json
src/main/migrations
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"build:mac": "electron-vite build && electron-builder --mac",
"build:linux": "electron-vite build && electron-builder --linux",
"prepare": "husky",
"typeorm:migration-create": "yarn typeorm migration:create"
"knex:migrate:make": "knex --knexfile src/main/knexfile.ts migrate:make --esm"
},
"dependencies": {
"@electron-toolkit/preload": "^3.0.0",
Expand All @@ -43,7 +43,7 @@
"@vanilla-extract/recipes": "^0.5.2",
"auto-launch": "^5.0.6",
"axios": "^1.6.8",
"better-sqlite3": "^9.5.0",
"better-sqlite3": "^11.2.1",
"check-disk-space": "^3.4.0",
"classnames": "^2.5.1",
"color": "^4.2.3",
Expand All @@ -61,6 +61,7 @@
"iso-639-1": "3.1.2",
"jsdom": "^24.0.0",
"jsonwebtoken": "^9.0.2",
"knex": "^3.1.0",
"lodash-es": "^4.17.21",
"lottie-react": "^2.4.0",
"parse-torrent": "^11.0.16",
Expand Down Expand Up @@ -106,6 +107,7 @@
"prettier": "^3.2.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-svgr": "^4.2.0"
Expand Down
4 changes: 1 addition & 3 deletions src/main/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "@main/entity";

import { databasePath } from "./constants";
import * as migrations from "./migrations";

export const dataSource = new DataSource({
type: "better-sqlite3",
Expand All @@ -23,7 +22,6 @@ export const dataSource = new DataSource({
DownloadQueue,
UserAuth,
],
synchronize: true,
synchronize: false,
database: databasePath,
migrations,
});
6 changes: 0 additions & 6 deletions src/main/entity/repack.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ export class Repack {
@Column("text", { unique: true })
magnet: string;

/**
* @deprecated Direct scraping capability has been removed
*/
@Column("int", { nullable: true })
page: number;

@Column("text")
repacker: string;

Expand Down
Binary file added src/main/hydra.dev.db
Binary file not shown.
22 changes: 21 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { logger, PythonInstance, WindowManager } from "@main/services";
import { dataSource } from "@main/data-source";
import * as resources from "@locales";
import { userPreferencesRepository } from "@main/repository";
import { knexClient, migrationConfig } from "./knex-client";

const { autoUpdater } = updater;

Expand Down Expand Up @@ -52,6 +53,18 @@ if (process.defaultApp) {
app.setAsDefaultProtocolClient(PROTOCOL);
}

const runMigrations = async () => {
await knexClient.migrate.list(migrationConfig).then((result) => {
logger.log(
"Migrations to run:",
result[1].map((migration) => migration.name)
);
});

await knexClient.migrate.latest(migrationConfig);
await knexClient.destroy();
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
Expand All @@ -63,8 +76,15 @@ 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 dataSource.initialize();
await dataSource.runMigrations();

await import("./main");

Expand Down
29 changes: 29 additions & 0 deletions src/main/knex-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import knex, { Knex } from "knex";
import { databasePath } from "./constants";
import { Hydra2_0_3 } from "./migrations/20240830143811_Hydra_2_0_3";
import { RepackUris } from "./migrations/20240830143906_RepackUris";

export type HydraMigration = Knex.Migration & { name: string };

class MigrationSource implements Knex.MigrationSource<HydraMigration> {
getMigrations(): Promise<HydraMigration[]> {
return Promise.resolve([Hydra2_0_3, RepackUris]);
}
getMigrationName(migration: HydraMigration): string {
return migration.name;
}
getMigration(migration: HydraMigration): Promise<Knex.Migration> {
return Promise.resolve(migration);
}
}

export const knexClient = knex({
client: "better-sqlite3",
connection: {
filename: databasePath,
},
});

export const migrationConfig: Knex.MigratorConfig = {
migrationSource: new MigrationSource(),
};
10 changes: 10 additions & 0 deletions src/main/knexfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const config = {
development: {
migrations: {
extension: "ts",
stub: "migrations/migration.stub",
},
},
};

export default config;
50 changes: 0 additions & 50 deletions src/main/migrations/1724081695967-Hydra_2_0_3.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/migrations/1724081984535-DowloadsRefactor.ts

This file was deleted.

Loading

0 comments on commit ff43f82

Please sign in to comment.