diff --git a/api/test-db-local.js b/api/test-db-local.js new file mode 100644 index 0000000..a1c724d --- /dev/null +++ b/api/test-db-local.js @@ -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 + }); + } +} \ No newline at end of file