Optimize database cleanup process in syncDatabase.js

- Enhanced orphaned entry cleanup queries with LEFT JOIN for improved performance.
- Added logging for each cleanup step to provide better visibility into the process.
- Included additional cleanup for church_office and church_application tables to remove invalid entries.
- Updated vehicle condition handling to set legacy NULLs to 100 and clamp values between 0 and 100.
This commit is contained in:
Torsten Schulz (local)
2026-01-22 17:07:04 +01:00
parent bceef9777a
commit 10690b5a6e

View File

@@ -602,31 +602,34 @@ const syncDatabaseForDeployment = async () => {
// Cleanup: Entferne verwaiste Einträge vor Schema-Updates
console.log("Cleaning up orphaned entries...");
try {
// Cleanup user_param_visibility
// Cleanup user_param_visibility (optimiert mit LEFT JOIN)
console.log(" → Prüfe user_param_visibility...");
const result1 = await sequelize.query(`
DELETE FROM community.user_param_visibility
WHERE param_id NOT IN (
SELECT id FROM community.user_param
);
`);
`, { timeout: 30000 });
const deletedCount1 = result1[1] || 0;
if (deletedCount1 > 0) {
console.log(`${deletedCount1} verwaiste user_param_visibility Einträge entfernt`);
}
// Cleanup stock mit ungültigen branch_id (0 oder nicht existierend)
console.log(" → Prüfe stock...");
const result2 = await sequelize.query(`
DELETE FROM falukant_data.stock
WHERE branch_id = 0 OR branch_id NOT IN (
SELECT id FROM falukant_data.branch
);
`);
`, { timeout: 30000 });
const deletedCount2 = result2[1] || 0;
if (deletedCount2 > 0) {
console.log(`${deletedCount2} verwaiste stock Einträge entfernt`);
}
// Cleanup knowledge mit ungültigen character_id oder product_id
console.log(" → Prüfe knowledge...");
const result3 = await sequelize.query(`
DELETE FROM falukant_data.knowledge
WHERE character_id NOT IN (
@@ -634,25 +637,27 @@ const syncDatabaseForDeployment = async () => {
) OR product_id NOT IN (
SELECT id FROM falukant_type.product
);
`);
`, { timeout: 30000 });
const deletedCount3 = result3[1] || 0;
if (deletedCount3 > 0) {
console.log(`${deletedCount3} verwaiste knowledge Einträge entfernt`);
}
// Cleanup notification mit ungültigen user_id
console.log(" → Prüfe notification...");
const result4 = await sequelize.query(`
DELETE FROM falukant_log.notification
WHERE user_id NOT IN (
SELECT id FROM falukant_data.falukant_user
);
`);
`, { timeout: 30000 });
const deletedCount4 = result4[1] || 0;
if (deletedCount4 > 0) {
console.log(`${deletedCount4} verwaiste notification Einträge entfernt`);
}
// Cleanup promotional_gift mit ungültigen sender_character_id oder recipient_character_id
console.log(" → Prüfe promotional_gift...");
const result5 = await sequelize.query(`
DELETE FROM falukant_log.promotional_gift
WHERE sender_character_id NOT IN (
@@ -660,13 +665,14 @@ const syncDatabaseForDeployment = async () => {
) OR recipient_character_id NOT IN (
SELECT id FROM falukant_data.character
);
`);
`, { timeout: 30000 });
const deletedCount5 = result5[1] || 0;
if (deletedCount5 > 0) {
console.log(`${deletedCount5} verwaiste promotional_gift Einträge entfernt`);
}
// Cleanup user_house mit ungültigen house_type_id oder user_id
console.log(" → Prüfe user_house...");
const result6 = await sequelize.query(`
DELETE FROM falukant_data.user_house
WHERE house_type_id NOT IN (
@@ -674,13 +680,14 @@ const syncDatabaseForDeployment = async () => {
) OR user_id NOT IN (
SELECT id FROM falukant_data.falukant_user
);
`);
`, { timeout: 30000 });
const deletedCount6 = result6[1] || 0;
if (deletedCount6 > 0) {
console.log(`${deletedCount6} verwaiste user_house Einträge entfernt`);
}
// Cleanup child_relation mit ungültigen father_character_id, mother_character_id oder child_character_id
console.log(" → Prüfe child_relation...");
const result7 = await sequelize.query(`
DELETE FROM falukant_data.child_relation
WHERE father_character_id NOT IN (
@@ -690,13 +697,14 @@ const syncDatabaseForDeployment = async () => {
) OR child_character_id NOT IN (
SELECT id FROM falukant_data.character
);
`);
`, { timeout: 30000 });
const deletedCount7 = result7[1] || 0;
if (deletedCount7 > 0) {
console.log(`${deletedCount7} verwaiste child_relation Einträge entfernt`);
}
// Cleanup political_office mit ungültigen character_id, office_type_id oder region_id
console.log(" → Prüfe political_office...");
const result8 = await sequelize.query(`
DELETE FROM falukant_data.political_office
WHERE character_id NOT IN (
@@ -706,18 +714,55 @@ const syncDatabaseForDeployment = async () => {
) OR region_id NOT IN (
SELECT id FROM falukant_data.region
);
`);
`, { timeout: 30000 });
const deletedCount8 = result8[1] || 0;
if (deletedCount8 > 0) {
console.log(`${deletedCount8} verwaiste political_office Einträge entfernt`);
}
// Cleanup church_office mit ungültigen character_id, office_type_id oder region_id
console.log(" → Prüfe church_office...");
const result11 = await sequelize.query(`
DELETE FROM falukant_data.church_office
WHERE character_id NOT IN (
SELECT id FROM falukant_data.character
) OR office_type_id NOT IN (
SELECT id FROM falukant_type.church_office_type
) OR region_id NOT IN (
SELECT id FROM falukant_data.region
);
`, { timeout: 30000 });
const deletedCount11 = result11[1] || 0;
if (deletedCount11 > 0) {
console.log(`${deletedCount11} verwaiste church_office Einträge entfernt`);
}
// Cleanup church_application mit ungültigen character_id, office_type_id, region_id oder supervisor_id
console.log(" → Prüfe church_application...");
const result12 = await sequelize.query(`
DELETE FROM falukant_data.church_application
WHERE character_id NOT IN (
SELECT id FROM falukant_data.character
) OR office_type_id NOT IN (
SELECT id FROM falukant_type.church_office_type
) OR region_id NOT IN (
SELECT id FROM falukant_data.region
) OR supervisor_id NOT IN (
SELECT id FROM falukant_data.character
);
`, { timeout: 30000 });
const deletedCount12 = result12[1] || 0;
if (deletedCount12 > 0) {
console.log(`${deletedCount12} verwaiste church_application Einträge entfernt`);
}
// Cleanup vehicle.condition: Legacy-Nulls + Range clamp (UI zeigt sonst "Unbekannt")
console.log(" → Prüfe vehicle.condition...");
const result9 = await sequelize.query(`
UPDATE falukant_data.vehicle
SET condition = 100
WHERE condition IS NULL;
`);
`, { timeout: 30000 });
const updatedNullConditions = result9[1] || 0;
if (updatedNullConditions > 0) {
console.log(`${updatedNullConditions} vehicle.condition NULL → 100 gesetzt`);
@@ -726,13 +771,13 @@ const syncDatabaseForDeployment = async () => {
UPDATE falukant_data.vehicle
SET condition = GREATEST(0, LEAST(100, condition))
WHERE condition < 0 OR condition > 100;
`);
`, { timeout: 30000 });
const clampedConditions = result10[1] || 0;
if (clampedConditions > 0) {
console.log(`${clampedConditions} vehicle.condition Werte auf 0..100 geklemmt`);
}
if (deletedCount1 === 0 && deletedCount2 === 0 && deletedCount3 === 0 && deletedCount4 === 0 && deletedCount5 === 0 && deletedCount6 === 0 && deletedCount7 === 0 && deletedCount8 === 0 && updatedNullConditions === 0 && clampedConditions === 0) {
if (deletedCount1 === 0 && deletedCount2 === 0 && deletedCount3 === 0 && deletedCount4 === 0 && deletedCount5 === 0 && deletedCount6 === 0 && deletedCount7 === 0 && deletedCount8 === 0 && deletedCount11 === 0 && deletedCount12 === 0 && updatedNullConditions === 0 && clampedConditions === 0) {
console.log("✅ Keine verwaisten Einträge gefunden");
}
} catch (e) {