From 85cb2a4cc8dcc3b0350ed46a2768fd9007ce4549 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Mon, 27 May 2024 00:01:26 -0300 Subject: [PATCH] feat: add migration --- ...76027208-alter_lastTimePlayed_to_datime.ts | 48 +++++++++++++++++++ src/main/migrations/index.ts | 6 ++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/main/migrations/1716776027208-alter_lastTimePlayed_to_datime.ts 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..403de539 --- /dev/null +++ b/src/main/migrations/1716776027208-alter_lastTimePlayed_to_datime.ts @@ -0,0 +1,48 @@ +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) || '-' || -- Ano + 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 || '-' || -- Mês + SUBSTR(lastTimePlayed, 6, 2) || ' ' || -- Dia + SUBSTR(lastTimePlayed, 18, 8) -- Hora; + 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 = '${game.lastTimePlayed.toUTCString()}' WHERE id = ${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, +];