스키마 강제 변경

This commit is contained in:
김판돌 2026-04-08 12:24:12 +09:00
parent 415c5f44ee
commit cafc88c37c
1 changed files with 21 additions and 2 deletions

View File

@ -4,7 +4,25 @@ import { PrismaClient } from '@prisma/client';
import { logger } from '../utils/logger'; import { logger } from '../utils/logger';
// Prisma 7 requires a driver adapter for direct database connections. // 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); const adapter = new PrismaPg(pool);
export const prisma = new PrismaClient({ export const prisma = new PrismaClient({
@ -17,8 +35,9 @@ export const connectDB = async () => {
// Adapter-based client connects when first used, // Adapter-based client connects when first used,
// but we can test the pool connection here. // but we can test the pool connection here.
const client = await pool.connect(); const client = await pool.connect();
const { rows } = await client.query('SHOW search_path;');
client.release(); 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) { } catch (error) {
logger.error('Failed to connect to PostgreSQL:', error); logger.error('Failed to connect to PostgreSQL:', error);
process.exit(1); process.exit(1);