update migration to get onlinefix repacks from hydra.db

This commit is contained in:
Zamitto 2024-05-16 23:54:27 -03:00
parent d93e580d0f
commit a8332c858e
2 changed files with 70 additions and 6 deletions

View File

@ -17,9 +17,10 @@ export const createDataSource = (options: Partial<SqliteConnectionOptions>) =>
database: databasePath,
entities: [Game, Repack, UserPreferences, GameShopCache, SteamGame],
synchronize: true,
migrations: migrations,
migrationsRun: true,
...options,
});
export const dataSource = createDataSource({});
export const dataSource = createDataSource({
migrations: migrations,
migrationsRun: true,
});

View File

@ -1,10 +1,73 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { createDataSource } from "@main/data-source";
import { Repack } from "@main/entity";
import { app } from "electron";
import { chunk } from "lodash-es";
import path from "path";
import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class FixRepackUploadDate1715900413313 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`UPDATE repack SET uploadDate = createdAt WHERE uploadDate LIKE '%NaN%';`
await queryRunner.createTable(
new Table({
name: "repack_temp",
columns: [
{ name: "title", type: "varchar" },
{ name: "old_id", type: "int" },
],
}),
true
);
await queryRunner.query(
`INSERT INTO repack_temp (title, old_id) SELECT title, id FROM repack WHERE repacker = 'onlinefix';`
);
await queryRunner.query(`DELETE FROM repack WHERE repacker = 'onlinefix';`);
const updateDataSource = createDataSource({
database: app.isPackaged
? path.join(process.resourcesPath, "hydra.db")
: path.join(__dirname, "..", "..", "hydra.db"),
});
await updateDataSource.initialize();
const updateRepackRepository = updateDataSource.getRepository(Repack);
const updatedOnlineFix = await updateRepackRepository.find({
where: {
repacker: "onlinefix",
},
});
const chunks = chunk(
updatedOnlineFix.map((repack) => {
const { id: _, ...rest } = repack;
return rest;
}),
500
);
for (const chunk of chunks) {
await queryRunner.manager
.createQueryBuilder(Repack, "repack")
.insert()
.values(chunk)
.orIgnore()
.execute();
}
await queryRunner.query(
`UPDATE game
set repackId = (
SELECT id
from repack LEFT JOIN repack_temp ON repack_temp.title = repack.title
)
WHERE EXISTS (select old_id from repack_temp WHERE old_id = game.repackId)`
);
// uncomment this line after debug and test
//queryRunner.dropTable("repack_temp");
}
public async down(_: QueryRunner): Promise<void> {