86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
import { Pool } from 'pg';
|
||
|
|
|
||
|
|
const pool = new Pool({
|
||
|
|
connectionString: process.env.DATABASE_URL,
|
||
|
|
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
|
||
|
|
});
|
||
|
|
|
||
|
|
export default async function handler(req: NextRequest) {
|
||
|
|
if (req.method !== 'GET') {
|
||
|
|
return new NextResponse(JSON.stringify({ error: 'Method not allowed' }), {
|
||
|
|
status: 405,
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const client = await pool.connect();
|
||
|
|
|
||
|
|
try {
|
||
|
|
console.log('Testing database connection...');
|
||
|
|
|
||
|
|
// Test basic connection
|
||
|
|
const result = await client.query('SELECT NOW() as current_time, version() as postgres_version');
|
||
|
|
|
||
|
|
// Check if users table exists
|
||
|
|
const tableCheck = await client.query(`
|
||
|
|
SELECT EXISTS (
|
||
|
|
SELECT FROM information_schema.tables
|
||
|
|
WHERE table_schema = 'public'
|
||
|
|
AND table_name = 'users'
|
||
|
|
) as users_table_exists;
|
||
|
|
`);
|
||
|
|
|
||
|
|
// Check if cards table exists
|
||
|
|
const cardsCheck = await client.query(`
|
||
|
|
SELECT EXISTS (
|
||
|
|
SELECT FROM information_schema.tables
|
||
|
|
WHERE table_schema = 'public'
|
||
|
|
AND table_name = 'cards'
|
||
|
|
) as cards_table_exists;
|
||
|
|
`);
|
||
|
|
|
||
|
|
// Get table counts
|
||
|
|
let userCount = 0;
|
||
|
|
let cardCount = 0;
|
||
|
|
|
||
|
|
if (tableCheck.rows[0].users_table_exists) {
|
||
|
|
const userCountResult = await client.query('SELECT COUNT(*) as count FROM users');
|
||
|
|
userCount = parseInt(userCountResult.rows[0].count);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (cardsCheck.rows[0].cards_table_exists) {
|
||
|
|
const cardCountResult = await client.query('SELECT COUNT(*) as count FROM cards');
|
||
|
|
cardCount = parseInt(cardCountResult.rows[0].count);
|
||
|
|
}
|
||
|
|
|
||
|
|
return new NextResponse(JSON.stringify({
|
||
|
|
success: true,
|
||
|
|
message: 'Database connection successful',
|
||
|
|
database_info: {
|
||
|
|
current_time: result.rows[0].current_time,
|
||
|
|
postgres_version: result.rows[0].postgres_version,
|
||
|
|
users_table_exists: tableCheck.rows[0].users_table_exists,
|
||
|
|
cards_table_exists: cardsCheck.rows[0].cards_table_exists,
|
||
|
|
user_count: userCount,
|
||
|
|
card_count: cardCount
|
||
|
|
}
|
||
|
|
}), {
|
||
|
|
status: 200,
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Database connection failed:', error);
|
||
|
|
return new NextResponse(JSON.stringify({
|
||
|
|
success: false,
|
||
|
|
error: 'Database connection failed',
|
||
|
|
details: (error as Error).message
|
||
|
|
}), {
|
||
|
|
status: 500,
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
});
|
||
|
|
} finally {
|
||
|
|
client.release();
|
||
|
|
}
|
||
|
|
}
|