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:
@@ -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,40 +757,44 @@ 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)
|
||||||
console.log(" → Prüfe church_office...");
|
if (await tableExists('falukant_data', 'church_office')) {
|
||||||
const result11 = await queryWithTimeout(`
|
console.log(" → Prüfe church_office...");
|
||||||
DELETE FROM falukant_data.church_office
|
const result11 = await queryWithTimeout(`
|
||||||
WHERE character_id NOT IN (
|
DELETE FROM falukant_data.church_office
|
||||||
SELECT id FROM falukant_data.character
|
WHERE character_id NOT IN (
|
||||||
) OR office_type_id NOT IN (
|
SELECT id FROM falukant_data.character
|
||||||
SELECT id FROM falukant_type.church_office_type
|
) OR office_type_id NOT IN (
|
||||||
) OR region_id NOT IN (
|
SELECT id FROM falukant_type.church_office_type
|
||||||
SELECT id FROM falukant_data.region
|
) OR region_id NOT IN (
|
||||||
);
|
SELECT id FROM falukant_data.region
|
||||||
`, 30000, 'church_office cleanup');
|
);
|
||||||
const deletedCount11 = result11[1] || 0;
|
`, 30000, 'church_office cleanup');
|
||||||
if (deletedCount11 > 0) {
|
const deletedCount11 = result11[1] || 0;
|
||||||
console.log(`✅ ${deletedCount11} verwaiste church_office Einträge entfernt`);
|
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
|
// Cleanup church_application mit ungültigen character_id, office_type_id, region_id oder supervisor_id (nur wenn Tabelle existiert)
|
||||||
console.log(" → Prüfe church_application...");
|
if (await tableExists('falukant_data', 'church_application')) {
|
||||||
const result12 = await queryWithTimeout(`
|
console.log(" → Prüfe church_application...");
|
||||||
DELETE FROM falukant_data.church_application
|
const result12 = await queryWithTimeout(`
|
||||||
WHERE character_id NOT IN (
|
DELETE FROM falukant_data.church_application
|
||||||
SELECT id FROM falukant_data.character
|
WHERE character_id NOT IN (
|
||||||
) OR office_type_id NOT IN (
|
SELECT id FROM falukant_data.character
|
||||||
SELECT id FROM falukant_type.church_office_type
|
) OR office_type_id NOT IN (
|
||||||
) OR region_id NOT IN (
|
SELECT id FROM falukant_type.church_office_type
|
||||||
SELECT id FROM falukant_data.region
|
) OR region_id NOT IN (
|
||||||
) OR supervisor_id NOT IN (
|
SELECT id FROM falukant_data.region
|
||||||
SELECT id FROM falukant_data.character
|
) OR supervisor_id NOT IN (
|
||||||
);
|
SELECT id FROM falukant_data.character
|
||||||
`, 30000, 'church_application cleanup');
|
);
|
||||||
const deletedCount12 = result12[1] || 0;
|
`, 30000, 'church_application cleanup');
|
||||||
if (deletedCount12 > 0) {
|
const deletedCount12 = result12[1] || 0;
|
||||||
console.log(`✅ ${deletedCount12} verwaiste church_application Einträge entfernt`);
|
if (deletedCount12 > 0) {
|
||||||
|
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user