From 2baa787b7234dee166921b9cba77a216bc47e521 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 7 Nov 2023 22:50:31 +0100 Subject: [PATCH] [backend] Dynamically import redisClient in postgre.ts and dependencies This fixes unnecessary redisClient instances being created during migration runs, which previously prevented the revertmigration command from exiting by itself due to a TypeORM inconsistency between migration:run and migration:revert. --- packages/backend/src/db/postgre.ts | 2 +- .../src/migration/1695749948350-move-antenna-to-cache.ts | 4 +++- packages/backend/src/services/chart/core.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/db/postgre.ts b/packages/backend/src/db/postgre.ts index 6dd8fa30f..0a1d24843 100644 --- a/packages/backend/src/db/postgre.ts +++ b/packages/backend/src/db/postgre.ts @@ -75,7 +75,6 @@ import { NoteEdit } from "@/models/entities/note-edit.js"; import { entities as charts } from "@/services/chart/entities.js"; import { envOption } from "../env.js"; import { dbLogger } from "./logger.js"; -import { redisClient } from "./redis.js"; import { OAuthApp } from "@/models/entities/oauth-app.js"; import { OAuthToken } from "@/models/entities/oauth-token.js"; @@ -238,6 +237,7 @@ export async function initDb(force = false) { export async function resetDb() { const reset = async () => { + const { redisClient } = await import("./redis.js"); await redisClient.flushdb(); const tables = await db.query(`SELECT relname AS "table" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) diff --git a/packages/backend/src/migration/1695749948350-move-antenna-to-cache.ts b/packages/backend/src/migration/1695749948350-move-antenna-to-cache.ts index 68d4bc566..a6ea0eee4 100644 --- a/packages/backend/src/migration/1695749948350-move-antenna-to-cache.ts +++ b/packages/backend/src/migration/1695749948350-move-antenna-to-cache.ts @@ -1,5 +1,4 @@ import { MigrationInterface, QueryRunner } from "typeorm"; -import { redisClient } from "../db/redis.js"; export class MoveAntennaToCache1695749948350 implements MigrationInterface { name = "MoveAntennaToCache1695749948350"; @@ -19,6 +18,7 @@ export class MoveAntennaToCache1695749948350 implements MigrationInterface { console.log('ANTENNA_MIGRATION_SKIP = true, skipping antenna note migration'); } else { + const { redisClient } = await import("../db/redis.js"); const total = await queryRunner.query(`SELECT COUNT(1) FROM "antenna_note"`) .then(p => p[0]['count']); @@ -41,6 +41,8 @@ export class MoveAntennaToCache1695749948350 implements MigrationInterface { query = `SELECT "id", "noteId", "antennaId" FROM "antenna_note" WHERE "id" > '${res.at(-1).id}' ORDER BY "id" ASC LIMIT ${Math.min(readLimit, remaining)}`; } + + redisClient.quit(); } await queryRunner.query(`DROP TABLE IF EXISTS "antenna_note"`); diff --git a/packages/backend/src/services/chart/core.ts b/packages/backend/src/services/chart/core.ts index 36fe37326..812421271 100644 --- a/packages/backend/src/services/chart/core.ts +++ b/packages/backend/src/services/chart/core.ts @@ -15,7 +15,6 @@ import { subtractTime, addTime, } from "@/prelude/time.js"; -import { getChartInsertLock } from "@/misc/app-lock.js"; import { db } from "@/db/postgre.js"; import promiseLimit from "promise-limit"; @@ -430,6 +429,7 @@ export default abstract class Chart { ? `${this.name}:${date}:${span}:${group}` : `${this.name}:${date}:${span}`; + const { getChartInsertLock } = await import("@/misc/app-lock.js"); const unlock = await getChartInsertLock(lockKey); try { // ロック内でもう1回チェックする