fix(health): use proper getDatabase to fix 503 error on health endpoint
swadhinbiswas committed 3 months ago
20ab9f4
1 parent 8786651
commit 20ab9f4
2 files changed
+12 -7
Showing 2 changed files+12-7
▼src/pages/api/health.ts
+8-3
| @@ -1,4 +1,5 @@ | ||
1 | -import { getDb } from "@/db/adapter"; | |
1 | +import { getDatabase } from "@/db"; | |
2 | +import { sql } from "drizzle-orm"; | |
2 | 3 | import type { APIRoute } from "astro"; |
3 | 4 | import { withErrorHandler } from "@/lib/errors"; |
4 | 5 | import { isDistributedLocking } from "@/lib/distributed-lock"; |
| @@ -14,8 +15,12 @@ export const GET: APIRoute = withErrorHandler(async () => { | ||
14 | 15 | // Check database |
15 | 16 | const dbStart = Date.now(); |
16 | 17 | try { |
17 | - const db = getDb(); | |
18 | - await db.rawQuery("SELECT 1"); | |
18 | + const db = getDatabase() as any; | |
19 | + if (db.execute) { | |
20 | + await db.execute(sql`SELECT 1`); | |
21 | + } else { | |
22 | + await db.run(sql`SELECT 1`); | |
23 | + } | |
19 | 24 | checks.database = { status: "ok", latency: Date.now() - dbStart }; |
20 | 25 | } catch (error) { |
21 | 26 | checks.database = { |
22 | 27 | |
▼tests/unit/health-route.test.ts
+4-4
| @@ -6,12 +6,12 @@ const state = vi.hoisted(() => ({ | ||
6 | 6 | queueMultiInstanceSafe: true, |
7 | 7 | })); |
8 | 8 | |
9 | -const rawQueryMock = vi.hoisted(() => vi.fn(async () => [{ "?column?": 1 }])); | |
9 | +const executeMock = vi.hoisted(() => vi.fn(async () => [{ "?column?": 1 }])); | |
10 | 10 | const fsAccessMock = vi.hoisted(() => vi.fn(async () => undefined)); |
11 | 11 | |
12 | -vi.mock("@/db/adapter", () => ({ | |
13 | - getDb: () => ({ | |
14 | - rawQuery: rawQueryMock, | |
12 | +vi.mock("@/db", () => ({ | |
13 | + getDatabase: () => ({ | |
14 | + execute: executeMock, | |
15 | 15 | }), |
16 | 16 | })); |
17 | 17 | |
18 | 18 | |