From cafc88c37cd8072aef2defb043b39a6519a7417e Mon Sep 17 00:00:00 2001 From: pandoli365 Date: Wed, 8 Apr 2026 12:24:12 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=82=A4=EB=A7=88=20=EA=B0=95?= =?UTF-8?q?=EC=A0=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/index.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/database/index.ts b/src/database/index.ts index 474e501..437f5a4 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -4,7 +4,25 @@ import { PrismaClient } from '@prisma/client'; import { logger } from '../utils/logger'; // Prisma 7 requires a driver adapter for direct database connections. -const pool = new Pool({ connectionString: process.env.DATABASE_URL }); +const resolveDbSchema = (): string => { + const raw = process.env.DATABASE_URL || ''; + try { + const u = new URL(raw); + // Prisma-style: ?schema=kord_live + return u.searchParams.get('schema') || 'public'; + } catch { + return 'public'; + } +}; + +const dbSchema = resolveDbSchema(); +const pool = new Pool({ + connectionString: process.env.DATABASE_URL, + // pg(Pool) doesn't understand Prisma's `?schema=...` param. + // Force PostgreSQL search_path so both Prisma adapter and raw queries + // operate on the intended schema (e.g. kord_live). + options: `-c search_path=${dbSchema}`, +}); const adapter = new PrismaPg(pool); export const prisma = new PrismaClient({ @@ -17,8 +35,9 @@ export const connectDB = async () => { // Adapter-based client connects when first used, // but we can test the pool connection here. const client = await pool.connect(); + const { rows } = await client.query('SHOW search_path;'); client.release(); - logger.info('Connected to PostgreSQL successfully via Driver Adapter.'); + logger.info(`Connected to PostgreSQL successfully via Driver Adapter. (search_path=${rows?.[0]?.search_path ?? 'unknown'})`); } catch (error) { logger.error('Failed to connect to PostgreSQL:', error); process.exit(1);