Files
sena_db_api_layer/migration_scripts/check-null-ref-id.js
vuongps38770 72283443ab
All checks were successful
Deploy to Production / deploy (push) Successful in 20s
update
2026-02-28 20:00:38 +07:00

54 lines
1.8 KiB
JavaScript

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