Enhance syncDatabase function to include cleanup for orphaned promotional_gift entries, removing invalid sender and recipient character references, and improve logging for orphaned entry detection.
This commit is contained in:
@@ -96,21 +96,35 @@ const syncDatabase = async () => {
|
||||
console.log(`✅ ${deletedCount3} verwaiste knowledge Einträge entfernt`);
|
||||
}
|
||||
|
||||
// Cleanup notification mit ungültigen user_id
|
||||
const result4 = await sequelize.query(`
|
||||
DELETE FROM falukant_log.notification
|
||||
WHERE user_id NOT IN (
|
||||
SELECT id FROM falukant_data.falukant_user
|
||||
);
|
||||
`);
|
||||
const deletedCount4 = result4[1] || 0;
|
||||
if (deletedCount4 > 0) {
|
||||
console.log(`✅ ${deletedCount4} verwaiste notification Einträge entfernt`);
|
||||
}
|
||||
// Cleanup notification mit ungültigen user_id
|
||||
const result4 = await sequelize.query(`
|
||||
DELETE FROM falukant_log.notification
|
||||
WHERE user_id NOT IN (
|
||||
SELECT id FROM falukant_data.falukant_user
|
||||
);
|
||||
`);
|
||||
const deletedCount4 = result4[1] || 0;
|
||||
if (deletedCount4 > 0) {
|
||||
console.log(`✅ ${deletedCount4} verwaiste notification Einträge entfernt`);
|
||||
}
|
||||
|
||||
if (deletedCount1 === 0 && deletedCount2 === 0 && deletedCount3 === 0 && deletedCount4 === 0) {
|
||||
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||
}
|
||||
// Cleanup promotional_gift mit ungültigen sender_character_id oder recipient_character_id
|
||||
const result5 = await sequelize.query(`
|
||||
DELETE FROM falukant_log.promotional_gift
|
||||
WHERE sender_character_id NOT IN (
|
||||
SELECT id FROM falukant_data.character
|
||||
) OR recipient_character_id NOT IN (
|
||||
SELECT id FROM falukant_data.character
|
||||
);
|
||||
`);
|
||||
const deletedCount5 = result5[1] || 0;
|
||||
if (deletedCount5 > 0) {
|
||||
console.log(`✅ ${deletedCount5} verwaiste promotional_gift Einträge entfernt`);
|
||||
}
|
||||
|
||||
if (deletedCount1 === 0 && deletedCount2 === 0 && deletedCount3 === 0 && deletedCount4 === 0 && deletedCount5 === 0) {
|
||||
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('⚠️ Konnte verwaiste Einträge nicht bereinigen:', e?.message || e);
|
||||
}
|
||||
@@ -287,7 +301,21 @@ const syncDatabaseForDeployment = async () => {
|
||||
console.log(`✅ ${deletedCount4} verwaiste notification Einträge entfernt`);
|
||||
}
|
||||
|
||||
if (deletedCount1 === 0 && deletedCount2 === 0 && deletedCount3 === 0 && deletedCount4 === 0) {
|
||||
// Cleanup promotional_gift mit ungültigen sender_character_id oder recipient_character_id
|
||||
const result5 = await sequelize.query(`
|
||||
DELETE FROM falukant_log.promotional_gift
|
||||
WHERE sender_character_id NOT IN (
|
||||
SELECT id FROM falukant_data.character
|
||||
) OR recipient_character_id NOT IN (
|
||||
SELECT id FROM falukant_data.character
|
||||
);
|
||||
`);
|
||||
const deletedCount5 = result5[1] || 0;
|
||||
if (deletedCount5 > 0) {
|
||||
console.log(`✅ ${deletedCount5} verwaiste promotional_gift Einträge entfernt`);
|
||||
}
|
||||
|
||||
if (deletedCount1 === 0 && deletedCount2 === 0 && deletedCount3 === 0 && deletedCount4 === 0 && deletedCount5 === 0) {
|
||||
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user