Enhance database cleanup operations in syncDatabase.js

- Added a helper function to check for table existence before performing cleanup operations, ensuring that invalid queries are avoided.
- Updated cleanup logic for church_office and church_application tables to only execute if the respective tables exist, improving robustness and preventing errors.
- Maintained existing logging for cleanup operations to provide visibility into the process.
This commit is contained in:
Torsten Schulz (local)
2026-01-23 09:42:49 +01:00
parent 586aaec506
commit 92d792246c

View File

@@ -22,6 +22,22 @@ const queryWithTimeout = async (query, timeoutMs = 30000, description = 'Query')
throw error;
}
};
// Helper: Prüft ob Tabelle existiert
const tableExists = async (schema, tableName) => {
try {
const result = await sequelize.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = '${schema}'
AND table_name = '${tableName}'
);
`, { type: sequelize.QueryTypes.SELECT });
return result[0]?.exists || false;
} catch (error) {
return false;
}
};
import initializeTypes from './initializeTypes.js';
import initializeSettings from './initializeSettings.js';
import initializeUserRights from './initializeUserRights.js';
@@ -741,40 +757,44 @@ const syncDatabaseForDeployment = async () => {
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 queryWithTimeout(`
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
);
`, 30000, 'church_office cleanup');
const deletedCount11 = result11[1] || 0;
if (deletedCount11 > 0) {
console.log(`${deletedCount11} verwaiste church_office Einträge entfernt`);
// Cleanup church_office mit ungültigen character_id, office_type_id oder region_id (nur wenn Tabelle existiert)
if (await tableExists('falukant_data', 'church_office')) {
console.log(" → Prüfe church_office...");
const result11 = await queryWithTimeout(`
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
);
`, 30000, 'church_office cleanup');
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 queryWithTimeout(`
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
);
`, 30000, 'church_application cleanup');
const deletedCount12 = result12[1] || 0;
if (deletedCount12 > 0) {
console.log(`${deletedCount12} verwaiste church_application Einträge entfernt`);
// Cleanup church_application mit ungültigen character_id, office_type_id, region_id oder supervisor_id (nur wenn Tabelle existiert)
if (await tableExists('falukant_data', 'church_application')) {
console.log(" → Prüfe church_application...");
const result12 = await queryWithTimeout(`
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
);
`, 30000, 'church_application cleanup');
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")