QR-master/scripts/test-db-lead.ts

65 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();