const { Context } = require('../models'); const { Op } = require('sequelize'); const { sequelize } = require('../config/database'); async function checkNullRefId() { try { await sequelize.authenticate(); console.log('✅ Kết nối database thành công.'); // Kiểm tra tất cả các bản ghi có reference_id là null hoặc chuỗi rỗng const nullRefCount = await Context.count({ where: { [Op.or]: [ { reference_id: null }, { reference_id: '' } ] } }); const totalCount = await Context.count(); // Thêm thống kê theo status để dễ hình dung const statsByStatus = await Context.findAll({ attributes: ['status', [sequelize.fn('COUNT', sequelize.col('uuid')), 'count']], where: { [Op.or]: [ { reference_id: null }, { reference_id: '' } ] }, group: ['status'] }); console.log('\n--- KẾT QUẢ KIỂM TRA REFERENCE_ID ---'); console.log(`📊 Tổng số bản ghi trong bảng: ${totalCount}`); console.log(`❌ Số bản ghi có reference_id bị NULL/Trống: ${nullRefCount}`); console.log(`📈 Tỷ lệ lỗi: ${((nullRefCount / totalCount) * 100).toFixed(2)}%`); if (statsByStatus.length > 0) { console.log('\nPhân loại theo Status:'); statsByStatus.forEach(stat => { console.log(` - Status ${stat.get('status')}: ${stat.get('count')} bản ghi`); }); } process.exit(0); } catch (error) { console.error('❌ Lỗi khi kiểm tra:', error); process.exit(1); } } checkNullRefId();