diff --git a/src/main/entity/game.entity.ts b/src/main/entity/game.entity.ts index 91e19ea6..d6362928 100644 --- a/src/main/entity/game.entity.ts +++ b/src/main/entity/game.entity.ts @@ -59,7 +59,7 @@ export class Game { @Column("int", { default: 0 }) bytesDownloaded: number; - @Column("text", { nullable: true }) + @Column("datetime", { nullable: true }) lastTimePlayed: Date | null; @Column("float", { default: 0 }) diff --git a/src/main/migrations/1716776027208-alter_lastTimePlayed_to_datime.ts b/src/main/migrations/1716776027208-alter_lastTimePlayed_to_datime.ts new file mode 100644 index 00000000..6a562915 --- /dev/null +++ b/src/main/migrations/1716776027208-alter_lastTimePlayed_to_datime.ts @@ -0,0 +1,49 @@ +import { Game } from "@main/entity"; +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AlterLastTimePlayedToDatime1716776027208 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + // 2024-05-27 02:08:17 + // Mon, 27 May 2024 02:08:17 GMT + const updateLastTimePlayedValues = ` + UPDATE game SET lastTimePlayed = (SELECT + SUBSTR(lastTimePlayed, 13, 4) || '-' || -- Year + CASE SUBSTR(lastTimePlayed, 9, 3) + WHEN 'Jan' THEN '01' + WHEN 'Feb' THEN '02' + WHEN 'Mar' THEN '03' + WHEN 'Apr' THEN '04' + WHEN 'May' THEN '05' + WHEN 'Jun' THEN '06' + WHEN 'Jul' THEN '07' + WHEN 'Aug' THEN '08' + WHEN 'Sep' THEN '09' + WHEN 'Oct' THEN '10' + WHEN 'Nov' THEN '11' + WHEN 'Dec' THEN '12' + END || '-' || -- Month + SUBSTR(lastTimePlayed, 6, 2) || ' ' || -- Day + SUBSTR(lastTimePlayed, 18, 8) -- hh:mm:ss; + FROM game) + WHERE lastTimePlayed IS NOT NULL; + `; + + await queryRunner.query(updateLastTimePlayedValues); + } + + public async down(queryRunner: QueryRunner): Promise { + const queryBuilder = queryRunner.manager.createQueryBuilder(Game, "game"); + + const result = await queryBuilder.getMany(); + + for (const game of result) { + if (!game.lastTimePlayed) continue; + await queryRunner.query( + `UPDATE game set lastTimePlayed = ? WHERE id = ?;`, + [game.lastTimePlayed.toUTCString(), game.id] + ); + } + } +} diff --git a/src/main/migrations/index.ts b/src/main/migrations/index.ts index 65061fac..c0c96e45 100644 --- a/src/main/migrations/index.ts +++ b/src/main/migrations/index.ts @@ -1,3 +1,7 @@ import { FixRepackUploadDate1715900413313 } from "./1715900413313-fix_repack_uploadDate"; +import { AlterLastTimePlayedToDatime1716776027208 } from "./1716776027208-alter_lastTimePlayed_to_datime"; -export default [FixRepackUploadDate1715900413313]; +export default [ + FixRepackUploadDate1715900413313, + AlterLastTimePlayedToDatime1716776027208, +]; diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 16646934..65819502 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -56,10 +56,7 @@ export const startProcessWatcher = async () => { await gameRepository.update(game.id, { playTimeInMilliseconds: game.playTimeInMilliseconds + delta, - }); - - gameRepository.update(game.id, { - lastTimePlayed: new Date().toUTCString(), + lastTimePlayed: new Date(), }); }