-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_database.js
More file actions
88 lines (71 loc) · 4.24 KB
/
Copy pathcheck_database.js
File metadata and controls
88 lines (71 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { Pool } = require('pg');
// PostgreSQL connection
const pool = new Pool({
user: process.env.DB_USER || 'meshbi',
password: process.env.DB_PASSWORD || 'meshbi_password',
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
database: process.env.DB_NAME || 'meshbi_school'
});
async function checkDatabase() {
try {
console.log('\n╔═══════════════════════════════════════════════════════════╗');
console.log('║ 🔍 DATABASE INTEGRITY CHECK ║');
console.log('╚═══════════════════════════════════════════════════════════╝\n');
// Check connection
console.log('1️⃣ Testing Database Connection...');
const connTest = await pool.query('SELECT NOW()');
console.log(' ✅ Connected Successfully\n');
// Check tables exist
console.log('2️⃣ Checking Tables...');
const tables = await pool.query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name
`);
console.log(` ✅ Found ${tables.rows.length} tables\n`);
tables.rows.forEach(t => console.log(` - ${t.table_name}`));
// Count records in each table
console.log('\n3️⃣ Counting Records in Each Table...\n');
const users = await pool.query('SELECT COUNT(*) as count FROM users');
console.log(` 👥 Users: ${users.rows[0].count} records`);
const students = await pool.query('SELECT COUNT(*) as count FROM students');
console.log(` 🎓 Students: ${students.rows[0].count} records`);
const teachers = await pool.query('SELECT COUNT(*) as count FROM users WHERE role = \'teacher\'');
console.log(` 👨🏫 Teachers: ${teachers.rows[0].count} records`);
const subjects = await pool.query('SELECT COUNT(*) as count FROM subjects');
console.log(` 📚 Subjects: ${subjects.rows[0].count} records`);
const grades = await pool.query('SELECT COUNT(*) as count FROM grades');
console.log(` ⭐ Grades: ${grades.rows[0].count} records`);
const payments = await pool.query('SELECT COUNT(*) as count FROM payments');
console.log(` 💳 Payments: ${payments.rows[0].count} records`);
const auditLogs = await pool.query('SELECT COUNT(*) as count FROM audit_logs');
console.log(` 📋 Audit Logs: ${auditLogs.rows[0].count} records\n`);
// Show sample of students
if (students.rows[0].count > 0) {
console.log('4️⃣ Sample Student Records:\n');
const sampleStudents = await pool.query('SELECT id, name, level FROM students LIMIT 5');
sampleStudents.rows.forEach(s => {
console.log(` ID: ${s.id}, Name: ${s.name}, Level: ${s.level}`);
});
} else {
console.log('4️⃣ ⚠️ No student records found!\n');
}
// Show test users
console.log('\n5️⃣ Test User Accounts:\n');
const testUsers = await pool.query('SELECT id, name, email, role, secondary_role FROM users ORDER BY id');
testUsers.rows.forEach(u => {
console.log(` ${u.name} (${u.email})`);
console.log(` Role: ${u.role} | Secondary: ${u.secondary_role || 'None'}`);
});
console.log('\n╔═══════════════════════════════════════════════════════════╗');
console.log('║ ✅ CHECK COMPLETE ║');
console.log('╚═══════════════════════════════════════════════════════════╝\n');
process.exit(0);
} catch (error) {
console.error('\n❌ DATABASE ERROR:\n', error.message);
process.exit(1);
}
}
checkDatabase();