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

58 lines
2.1 KiB
JavaScript

const { Context, Vocab, Sentences } = require('../models');
const { sequelize } = require('../config/database');
async function fixReferenceIds() {
try {
await sequelize.authenticate();
console.log('✅ Kết nối database thành công.');
// Chỉ kiểm tra các bản ghi status 5 (hoặc bạn có thể bỏ where để check tất cả)
const contexts = await Context.findAll({
where: { status: 5 }
});
console.log(`🔍 Đang kiểm tra ${contexts.length} bản ghi Context để sửa Reference ID...`);
let fixedCount = 0;
let verifiedCount = 0;
for (const ctx of contexts) {
// 1. Thử tìm trong Vocab trước
const vocab = await Vocab.findOne({ where: { text: ctx.title } });
if (vocab) {
if (ctx.reference_id !== vocab.vocab_id) {
await ctx.update({ reference_id: vocab.vocab_id });
fixedCount++;
console.log(`✅ Fixed ID: '${ctx.title}' -> ${vocab.vocab_id} (Vocab)`);
} else {
verifiedCount++;
}
continue;
}
// 2. Nếu không thấy trong Vocab, thử tìm trong Sentences bằng trường context
const sentence = await Sentences.findOne({ where: { text: ctx.context } });
if (sentence) {
if (ctx.reference_id !== sentence.id) {
await ctx.update({ reference_id: sentence.id });
fixedCount++;
console.log(`✅ Fixed ID: '${ctx.title}' -> ${sentence.id} (Sentence)`);
} else {
verifiedCount++;
}
}
}
console.log('\n--- KẾT QUẢ SỬA LỖI ---');
console.log(`✅ Đã sửa: ${fixedCount} bản ghi.`);
console.log(`✔️ Đã chuẩn sẵn: ${verifiedCount} bản ghi.`);
process.exit(0);
} catch (error) {
console.error('❌ Lỗi:', error);
process.exit(1);
}
}
fixReferenceIds();