This commit is contained in:
108
migration_scripts/sync-images-to-all.js
Normal file
108
migration_scripts/sync-images-to-all.js
Normal file
@@ -0,0 +1,108 @@
|
||||
const { Context, Vocab, Sentences } = require('../models');
|
||||
const { sequelize } = require('../config/database');
|
||||
|
||||
// --- helper: xác định slot image từ tên file/URL ---
|
||||
function resolveImageSlot(imageUrl) {
|
||||
if (!imageUrl) return null;
|
||||
const filename = imageUrl.split('/').pop().split('?')[0];
|
||||
const parts = filename.split('_');
|
||||
for (const part of parts) {
|
||||
const key = part.toLowerCase();
|
||||
if (key === 'square') return 'image_square';
|
||||
if (key === 'small') return 'image_small';
|
||||
if (key === 'normal') return 'image_normal';
|
||||
}
|
||||
// Nếu không tìm thấy trong underscore, thử tìm trong toàn bộ URL
|
||||
if (imageUrl.toLowerCase().includes('square')) return 'image_square';
|
||||
if (imageUrl.toLowerCase().includes('small')) return 'image_small';
|
||||
if (imageUrl.toLowerCase().includes('normal')) return 'image_normal';
|
||||
return null;
|
||||
}
|
||||
|
||||
async function syncImages() {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('✅ Kết nối database thành công.');
|
||||
|
||||
const contexts = await Context.findAll({
|
||||
where: { status: 5 }
|
||||
});
|
||||
|
||||
console.log(`📦 Tìm thấy ${contexts.length} bản ghi Context ở status 5.`);
|
||||
|
||||
let vocabUpdated = 0;
|
||||
let sentencesUpdated = 0;
|
||||
let skipped = 0;
|
||||
|
||||
for (const ctx of contexts) {
|
||||
const imageUrl = ctx.image;
|
||||
const slot = resolveImageSlot(imageUrl);
|
||||
|
||||
if (!imageUrl || !slot) {
|
||||
console.warn(`⚠️ ID ${ctx.uuid}: Không xác định được ảnh hoặc slot (URL: ${imageUrl}). Bỏ qua.`);
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
let updated = false;
|
||||
|
||||
// Ưu tiên 1: Dùng reference_id
|
||||
if (ctx.reference_id) {
|
||||
// Thử tìm trong Vocab
|
||||
const vocab = await Vocab.findByPk(ctx.reference_id);
|
||||
if (vocab) {
|
||||
await vocab.update({ [slot]: [imageUrl] });
|
||||
vocabUpdated++;
|
||||
updated = true;
|
||||
} else {
|
||||
// Thử tìm trong Sentences
|
||||
const sentence = await Sentences.findByPk(ctx.reference_id);
|
||||
if (sentence) {
|
||||
await sentence.update({ [slot]: [imageUrl] });
|
||||
sentencesUpdated++;
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ưu tiên 2: Nếu chưa update được (hoặc ko có ref_id), thử match theo text
|
||||
if (!updated) {
|
||||
// Thử khớp Vocab.text = Context.title
|
||||
const vocabByText = await Vocab.findOne({ where: { text: ctx.title } });
|
||||
if (vocabByText) {
|
||||
await vocabByText.update({ [slot]: [imageUrl] });
|
||||
vocabUpdated++;
|
||||
updated = true;
|
||||
} else if (ctx.context) {
|
||||
// Thử khớp Sentences.text = Context.context
|
||||
const sentenceByText = await Sentences.findOne({ where: { text: ctx.context } });
|
||||
if (sentenceByText) {
|
||||
await sentenceByText.update({ [slot]: [imageUrl] });
|
||||
sentencesUpdated++;
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
await ctx.update({ status: 6 });
|
||||
} else {
|
||||
console.warn(`❌ ID ${ctx.uuid}: Không tìm thấy entity đích để cập nhật ảnh (Title: ${ctx.title}).`);
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n--- KẾT QUẢ ĐỒNG BỘ ẢNH ---');
|
||||
console.log(`✅ Cập nhật Vocab: ${vocabUpdated} bản ghi.`);
|
||||
console.log(`✅ Cập nhật Sentences: ${sentencesUpdated} bản ghi.`);
|
||||
console.log(`⚠️ Bỏ qua hoặc lỗi: ${skipped} bản ghi.`);
|
||||
console.log(`📊 Tổng xử lý: ${contexts.length}`);
|
||||
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ Lỗi khi đồng bộ:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
syncImages();
|
||||
Reference in New Issue
Block a user