99 lines
3.2 KiB
TypeScript
99 lines
3.2 KiB
TypeScript
// Script to verify database connection and show current database URL
|
|
import * as dotenv from 'dotenv';
|
|
import * as path from 'path';
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
// Load environment variables
|
|
dotenv.config({ path: path.resolve(process.cwd(), '.env.local') });
|
|
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
|
|
console.log('🔍 Database Connection Verification\n');
|
|
console.log('DATABASE_URL:', databaseUrl ? databaseUrl.replace(/:[^:@]+@/, ':****@') : 'NOT SET');
|
|
console.log('');
|
|
|
|
if (!databaseUrl) {
|
|
console.error('❌ DATABASE_URL is not set!');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Check if it's local Supabase
|
|
const isLocalSupabase = databaseUrl.includes('127.0.0.1:54322') || databaseUrl.includes('localhost:54322');
|
|
console.log('📍 Database Type:', isLocalSupabase ? '✅ Local Supabase' : '⚠️ Remote Database');
|
|
console.log('');
|
|
|
|
// Test connection - use adapter pattern like db.ts
|
|
let prismaAdapter: any = undefined;
|
|
try {
|
|
const { PrismaPg } = require('@prisma/adapter-pg');
|
|
const { Pool } = require('pg');
|
|
const pool = new Pool({ connectionString: databaseUrl });
|
|
prismaAdapter = new PrismaPg(pool);
|
|
console.log('✅ Using PrismaPg adapter\n');
|
|
} catch (error) {
|
|
console.log('⚠️ PrismaPg adapter not available, using default client\n');
|
|
}
|
|
|
|
// Prisma 7.x: adapter is passed via connection string, not constructor
|
|
const prisma = new PrismaClient({
|
|
log: ['error'],
|
|
});
|
|
|
|
async function verifyConnection() {
|
|
try {
|
|
console.log('🔌 Testing database connection...');
|
|
|
|
// Simple query to test connection
|
|
const result = await prisma.$queryRaw`SELECT 1 as test`;
|
|
console.log('✅ Database connection successful!\n');
|
|
|
|
// Check if Song table exists
|
|
console.log('📊 Checking database schema...');
|
|
const tables = await prisma.$queryRaw<Array<{ tablename: string }>>`
|
|
SELECT tablename
|
|
FROM pg_tables
|
|
WHERE schemaname = 'public'
|
|
ORDER BY tablename
|
|
`;
|
|
|
|
console.log(`✅ Found ${tables.length} tables in public schema:`);
|
|
tables.forEach(t => console.log(` - ${t.tablename}`));
|
|
|
|
// Check Song table specifically
|
|
const songTableExists = tables.some(t => t.tablename === 'Song');
|
|
console.log('');
|
|
if (songTableExists) {
|
|
console.log('✅ Song table exists');
|
|
|
|
// Check Song table structure
|
|
const songColumns = await prisma.$queryRaw<Array<{ column_name: string; data_type: string }>>`
|
|
SELECT column_name, data_type
|
|
FROM information_schema.columns
|
|
WHERE table_schema = 'public' AND table_name = 'Song'
|
|
ORDER BY ordinal_position
|
|
`;
|
|
|
|
console.log(` Columns (${songColumns.length}):`);
|
|
songColumns.forEach(col => {
|
|
console.log(` - ${col.column_name} (${col.data_type})`);
|
|
});
|
|
|
|
// Count songs
|
|
const songCount = await prisma.song.count();
|
|
console.log(`\n📈 Current songs in database: ${songCount}`);
|
|
} else {
|
|
console.log('❌ Song table does NOT exist!');
|
|
console.log(' Run: npx prisma db push');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ Database connection failed!');
|
|
console.error('Error:', error instanceof Error ? error.message : String(error));
|
|
process.exit(1);
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
verifyConnection();
|