Implement cleanup of orphaned user_param_visibility entries before schema updates in syncDatabase functions
This commit is contained in:
23
backend/sql/cleanup_orphaned_user_param_visibility.sql
Normal file
23
backend/sql/cleanup_orphaned_user_param_visibility.sql
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
-- Cleanup script: Entfernt verwaiste Einträge aus user_param_visibility
|
||||||
|
-- Diese Einträge verweisen auf nicht existierende user_param Einträge
|
||||||
|
-- und verhindern das Hinzufügen des Foreign Key Constraints
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Lösche alle user_param_visibility Einträge, deren param_id nicht mehr in user_param existiert
|
||||||
|
DELETE FROM community.user_param_visibility
|
||||||
|
WHERE param_id NOT IN (
|
||||||
|
SELECT id FROM community.user_param
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Zeige an, wie viele Einträge gelöscht wurden
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
deleted_count INTEGER;
|
||||||
|
BEGIN
|
||||||
|
GET DIAGNOSTICS deleted_count = ROW_COUNT;
|
||||||
|
RAISE NOTICE 'Gelöschte verwaiste Einträge: %', deleted_count;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
@@ -54,6 +54,27 @@ const syncDatabase = async () => {
|
|||||||
console.warn('⚠️ Konnte traffic_light-Spalte nicht vorab sicherstellen:', e?.message || e);
|
console.warn('⚠️ Konnte traffic_light-Spalte nicht vorab sicherstellen:', e?.message || e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleanup: Entferne verwaiste user_param_visibility Einträge vor Schema-Updates (nur wenn Schema-Updates aktiviert)
|
||||||
|
if (currentStage === 'dev') {
|
||||||
|
console.log("Cleaning up orphaned user_param_visibility entries...");
|
||||||
|
try {
|
||||||
|
const result = await sequelize.query(`
|
||||||
|
DELETE FROM community.user_param_visibility
|
||||||
|
WHERE param_id NOT IN (
|
||||||
|
SELECT id FROM community.user_param
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
const deletedCount = result[1] || 0;
|
||||||
|
if (deletedCount > 0) {
|
||||||
|
console.log(`✅ ${deletedCount} verwaiste user_param_visibility Einträge entfernt`);
|
||||||
|
} else {
|
||||||
|
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('⚠️ Konnte verwaiste user_param_visibility Einträge nicht bereinigen:', e?.message || e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Setting up associations...");
|
console.log("Setting up associations...");
|
||||||
setupAssociations();
|
setupAssociations();
|
||||||
|
|
||||||
@@ -172,6 +193,25 @@ const syncDatabaseForDeployment = async () => {
|
|||||||
console.warn('⚠️ Konnte Transport-Spalten nicht nullable machen:', e?.message || e);
|
console.warn('⚠️ Konnte Transport-Spalten nicht nullable machen:', e?.message || e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleanup: Entferne verwaiste user_param_visibility Einträge vor Schema-Updates
|
||||||
|
console.log("Cleaning up orphaned user_param_visibility entries...");
|
||||||
|
try {
|
||||||
|
const result = await sequelize.query(`
|
||||||
|
DELETE FROM community.user_param_visibility
|
||||||
|
WHERE param_id NOT IN (
|
||||||
|
SELECT id FROM community.user_param
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
const deletedCount = result[1] || 0;
|
||||||
|
if (deletedCount > 0) {
|
||||||
|
console.log(`✅ ${deletedCount} verwaiste user_param_visibility Einträge entfernt`);
|
||||||
|
} else {
|
||||||
|
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('⚠️ Konnte verwaiste user_param_visibility Einträge nicht bereinigen:', e?.message || e);
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Setting up associations...");
|
console.log("Setting up associations...");
|
||||||
setupAssociations();
|
setupAssociations();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user