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; 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 initializeTypes from './initializeTypes.js';
import initializeSettings from './initializeSettings.js'; import initializeSettings from './initializeSettings.js';
import initializeUserRights from './initializeUserRights.js'; import initializeUserRights from './initializeUserRights.js';
@@ -741,7 +757,8 @@ const syncDatabaseForDeployment = async () => {
console.log(`${deletedCount8} verwaiste political_office Einträge entfernt`); console.log(`${deletedCount8} verwaiste political_office Einträge entfernt`);
} }
// Cleanup church_office mit ungültigen character_id, office_type_id oder region_id // 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..."); console.log(" → Prüfe church_office...");
const result11 = await queryWithTimeout(` const result11 = await queryWithTimeout(`
DELETE FROM falukant_data.church_office DELETE FROM falukant_data.church_office
@@ -757,8 +774,10 @@ const syncDatabaseForDeployment = async () => {
if (deletedCount11 > 0) { if (deletedCount11 > 0) {
console.log(`${deletedCount11} verwaiste church_office Einträge entfernt`); 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 // 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..."); console.log(" → Prüfe church_application...");
const result12 = await queryWithTimeout(` const result12 = await queryWithTimeout(`
DELETE FROM falukant_data.church_application DELETE FROM falukant_data.church_application
@@ -776,6 +795,7 @@ const syncDatabaseForDeployment = async () => {
if (deletedCount12 > 0) { if (deletedCount12 > 0) {
console.log(`${deletedCount12} verwaiste church_application Einträge entfernt`); console.log(`${deletedCount12} verwaiste church_application Einträge entfernt`);
} }
}
// Cleanup vehicle.condition: Legacy-Nulls + Range clamp (UI zeigt sonst "Unbekannt") // Cleanup vehicle.condition: Legacy-Nulls + Range clamp (UI zeigt sonst "Unbekannt")
console.log(" → Prüfe vehicle.condition..."); console.log(" → Prüfe vehicle.condition...");