Add database test endpoint for local debugging
🔧 Local Development Debugging:
- Add api/test-db-local.js for database connection testing
- Helps diagnose local environment issues
- Tests table existence and user data
Note: Local Vercel dev having database connection issues
Consider testing on production environment instead
This commit is contained in:
parent
e9b2da09a5
commit
d71d85ee43
1 changed files with 63 additions and 0 deletions
63
api/test-db-local.js
Normal file
63
api/test-db-local.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
const { Pool } = require('pg');
|
||||
|
||||
export default async function handler(req, res) {
|
||||
try {
|
||||
const pool = new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
|
||||
});
|
||||
|
||||
const client = await pool.connect();
|
||||
|
||||
// Test basic connection
|
||||
const result = await client.query('SELECT NOW() as current_time');
|
||||
|
||||
// Test if users table exists
|
||||
const tableCheck = await client.query(`
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name IN ('users', 'roles', 'user_roles')
|
||||
`);
|
||||
|
||||
// If users table exists, check for test user
|
||||
let userCheck = null;
|
||||
if (tableCheck.rows.some(row => row.table_name === 'users')) {
|
||||
try {
|
||||
userCheck = await client.query(
|
||||
'SELECT id, username, email FROM users WHERE username = $1',
|
||||
['testuser']
|
||||
);
|
||||
} catch (err) {
|
||||
userCheck = { error: err.message };
|
||||
}
|
||||
}
|
||||
|
||||
client.release();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Database connection test',
|
||||
timestamp: result.rows[0].current_time,
|
||||
environment: {
|
||||
NODE_ENV: process.env.NODE_ENV,
|
||||
has_database_url: !!process.env.DATABASE_URL,
|
||||
has_jwt_secret: !!process.env.JWT_SECRET
|
||||
},
|
||||
tables: tableCheck.rows.map(row => row.table_name),
|
||||
testUser: userCheck ? {
|
||||
exists: userCheck.rows ? userCheck.rows.length > 0 : false,
|
||||
data: userCheck.rows ? userCheck.rows[0] : null,
|
||||
error: userCheck.error || null
|
||||
} : null
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Database test error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue