import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { console.log('šŸ”„ Starting Database Diagnostics...'); try { // 1. Test Connection console.log('1ļøāƒ£ Testing basic connection...'); await prisma.$connect(); console.log('āœ… Connected to database successfully.'); // 2. Test Lead Table Existence console.log('2ļøāƒ£ Testing Lead table access...'); try { const count = await prisma.lead.count(); console.log(`āœ… Lead table found. Current count: ${count}`); } catch (e: any) { console.error('āŒ FAILED to access Lead table.'); if (e.code === 'P2021') { console.error(' šŸ‘‰ Error P2021: The table "Lead" does not exist in the current database.'); console.error(' šŸ‘‰ SOLUTION: Run "npx prisma migrate deploy"'); } else { console.error(' šŸ‘‰ Error:', e.message); } throw e; // rethrow to stop } // 3. Test Writing a dummy lead (optional, rolling back transaction) console.log('3ļøāƒ£ Testing write permission...'); await prisma.$transaction(async (tx) => { const lead = await tx.lead.create({ data: { email: 'test_diagnostic_script@example.com', source: 'diagnostic-script', reprintCost: 0, updatesPerYear: 0, annualSavings: 0 } }); console.log('āœ… Successfully created test lead with ID:', lead.id); // We purposefully throw an error to rollback this transaction so we don't dirty the DB throw new Error('ROLLBACK_TEST'); }).catch((e) => { if (e.message === 'ROLLBACK_TEST') { console.log('āœ… Transaction rollback successful (cleaning up test data).'); } else { throw e; } }); console.log('\nšŸŽ‰ ALL CHECKS PASSED! The database is effectively readable and writable.'); } catch (error) { console.error('\nšŸ’„ DIAGNOSTICS FAILED'); console.error(error); process.exit(1); } finally { await prisma.$disconnect(); } } main();