fix: revert DB driver to static imports, fix test regressions
- Revert createRequire() for DB drivers — require() can't resolve .ts files in Vitest, breaking 49 test suites - Make cache invalidation fail-safe via try-catch around revalidateTag (throws outside Next.js context: tests, tasks service, websocket) - Fix 6 integration test imports: barrel no longer exports concrete classes, point tests at direct source paths - Fix Redis cache handler lint errors: remove non-null assertions, suppress __NEXT_BUILD_ID underscore warning
This commit is contained in:
11 files changed
+174
-87
No files matched your search
@@ -8,26 +8,37 @@
|
||||
// - `invalidateIntegrationDataCache` (from integration-data-cache.ts): for integration DATA changes
|
||||
// (widget interact mutations). Clears module cache only — pair with requestHandler.invalidateCache().
|
||||
|
||||
import { revalidateTag } from "next/cache";
|
||||
|
||||
import { cacheTags } from "./cache-tags";
|
||||
import { invalidateIntegrationDataCache } from "./integration-data-cache";
|
||||
|
||||
// ponytail: revalidateTag throws outside Next.js request context (tests, tasks, websocket).
|
||||
// Ceiling: if Next.js context detection changes, update the try-catch.
|
||||
const safeRevalidateTag = (tag: string) => {
|
||||
try {
|
||||
// Dynamic import avoidance — revalidateTag is only available within Next.js runtime.
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const { revalidateTag } = require("next/cache") as typeof import("next/cache");
|
||||
revalidateTag(tag, "max");
|
||||
} catch {
|
||||
// Not in a Next.js request context — no-op
|
||||
}
|
||||
};
|
||||
|
||||
export const invalidateBoardCache = (boardId: string, boardName?: string) => {
|
||||
revalidateTag(cacheTags.board(boardId), "max");
|
||||
if (boardName) revalidateTag(cacheTags.boardByName(boardName), "max");
|
||||
revalidateTag(cacheTags.boardList(), "max");
|
||||
safeRevalidateTag(cacheTags.board(boardId));
|
||||
if (boardName) safeRevalidateTag(cacheTags.boardByName(boardName));
|
||||
safeRevalidateTag(cacheTags.boardList());
|
||||
};
|
||||
|
||||
export const invalidateIntegrationCache = (integrationId: string) => {
|
||||
invalidateIntegrationDataCache(integrationId);
|
||||
revalidateTag(cacheTags.integration(integrationId), "max");
|
||||
safeRevalidateTag(cacheTags.integration(integrationId));
|
||||
};
|
||||
|
||||
export const invalidateUserCache = (userId: string) => {
|
||||
revalidateTag(cacheTags.user(userId), "max");
|
||||
safeRevalidateTag(cacheTags.user(userId));
|
||||
};
|
||||
|
||||
export const invalidateServerSettingsCache = () => {
|
||||
revalidateTag(cacheTags.serverSettings(), "max");
|
||||
safeRevalidateTag(cacheTags.serverSettings());
|
||||
};
|
||||
@@ -1,15 +1,12 @@
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
import { DB_CASING } from "../constants";
|
||||
import { dbEnv } from "../env";
|
||||
import { createDbMapping } from "../mapping";
|
||||
import { createMysqlDb } from "./mysql";
|
||||
import { createPostgresDb } from "./postgresql";
|
||||
import type { SharedDrizzleConfig } from "./shared";
|
||||
import { WinstonDrizzleLogger } from "./shared";
|
||||
import { createSqliteDb } from "./sqlite";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
export type Database<TSchema extends Record<string, unknown>> = ReturnType<
|
||||
typeof import("./sqlite").createSqliteDb<TSchema>
|
||||
>;
|
||||
export type Database<TSchema extends Record<string, unknown>> = ReturnType<typeof createSqliteDb<TSchema>>;
|
||||
|
||||
export const createSharedConfig = <TSchema extends Record<string, unknown>>(
|
||||
schema: TSchema,
|
||||
@@ -21,15 +18,10 @@ export const createSharedConfig = <TSchema extends Record<string, unknown>>(
|
||||
|
||||
export const createDb = <TSchema extends Record<string, unknown>>(schema: TSchema) => {
|
||||
const config = createSharedConfig(schema);
|
||||
// ponytail: conditional require — only the configured driver is loaded.
|
||||
// Ceiling: all 3 stay on disk for runtime driver switching; only loaded graph shrinks.
|
||||
const driver = dbEnv.DRIVER ?? "better-sqlite3";
|
||||
switch (driver) {
|
||||
case "mysql2":
|
||||
return (require("./mysql") as typeof import("./mysql")).createMysqlDb(config);
|
||||
case "node-postgres":
|
||||
return (require("./postgresql") as typeof import("./postgresql")).createPostgresDb(config);
|
||||
default:
|
||||
return (require("./sqlite") as typeof import("./sqlite")).createSqliteDb(config);
|
||||
}
|
||||
|
||||
return createDbMapping({
|
||||
mysql2: () => createMysqlDb(config),
|
||||
"node-postgres": () => createPostgresDb(config),
|
||||
"better-sqlite3": () => createSqliteDb(config),
|
||||
});
|
||||
};
|
||||
@@ -4,7 +4,7 @@ import { beforeAll, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { Aria2Integration } from "../src";
|
||||
import { Aria2Integration } from "../src/download-client/aria2/aria2-integration";
|
||||
|
||||
vi.mock("@homarr/db", async (importActual) => {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||
|
||||
@@ -5,7 +5,7 @@ import { beforeAll, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { HomeAssistantIntegration } from "../src";
|
||||
import { HomeAssistantIntegration } from "../src/homeassistant/homeassistant-integration";
|
||||
import { TestConnectionError } from "../src/base/test-connection/test-connection-error";
|
||||
|
||||
vi.mock("@homarr/db", async (importActual) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
|
||||
import { createId } from "@homarr/common";
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { NextcloudIntegration } from "../src";
|
||||
import { NextcloudIntegration } from "../src/nextcloud/nextcloud.integration";
|
||||
|
||||
// Mock the database
|
||||
vi.mock("@homarr/db", async (importActual) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { beforeAll, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { NzbGetIntegration } from "../src";
|
||||
import { NzbGetIntegration } from "../src/download-client/nzbget/nzbget-integration";
|
||||
import { TestConnectionError } from "../src/base/test-connection/test-connection-error";
|
||||
|
||||
vi.mock("@homarr/db", async (importActual) => {
|
||||
|
||||
@@ -4,7 +4,8 @@ import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { PiHoleIntegrationV5, PiHoleIntegrationV6 } from "../src";
|
||||
import { PiHoleIntegrationV5 } from "../src/pi-hole/v5/pi-hole-integration-v5";
|
||||
import { PiHoleIntegrationV6 } from "../src/pi-hole/v6/pi-hole-integration-v6";
|
||||
import type { SessionStore } from "../src/base/session-store";
|
||||
import { TestConnectionError } from "../src/base/test-connection/test-connection-error";
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { beforeAll, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { SabnzbdIntegration } from "../src";
|
||||
import { SabnzbdIntegration } from "../src/download-client/sabnzbd/sabnzbd-integration";
|
||||
import { TestConnectionError } from "../src/base/test-connection/test-connection-error";
|
||||
import type { DownloadClientItem } from "../src/interfaces/downloads/download-client-items";
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
type CacheEntry = Awaited<ReturnType<CacheHandler["get"]>> & {};
|
||||
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
const BUILD_ID = process.env.__NEXT_BUILD_ID ?? "dev";
|
||||
|
||||
export class RedisCacheHandler implements CacheHandler {
|
||||
@@ -138,7 +139,8 @@ export class RedisCacheHandler implements CacheHandler {
|
||||
if (keys.length > 0) {
|
||||
const values = await this.redis.mget(...keys);
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const tag = keys[i]!.replace("nextCache:tag:", "");
|
||||
const tag = keys[i]?.replace("nextCache:tag:", "");
|
||||
if (!tag) continue;
|
||||
const val = values[i];
|
||||
if (val) this.tagCache.set(tag, Number(val));
|
||||
}
|
||||
@@ -173,7 +175,8 @@ export class RedisCacheHandler implements CacheHandler {
|
||||
const val = values[i];
|
||||
if (val) {
|
||||
const ts = Number(val);
|
||||
this.tagCache.set(missingTags[i]!, ts);
|
||||
const missingTag = missingTags[i];
|
||||
if (missingTag) this.tagCache.set(missingTag, ts);
|
||||
if (ts > maxTimestamp) maxTimestamp = ts;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user