스키마 강제 변경
This commit is contained in:
parent
415c5f44ee
commit
cafc88c37c
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue