fix(scripts): cascade entity_status/signals/metrics in orphan cleanup
This commit is contained in:
@@ -49,10 +49,17 @@ fi
|
||||
|
||||
echo
|
||||
echo "== applying (config_mutation) =="
|
||||
# Delete check_defs first (FK), then the orphan check entities.
|
||||
# check_defs first, then the dependent rows an entity owns (status, signals,
|
||||
# metrics), then the orphan check entities. FKs prevent a plain entity delete.
|
||||
$PSQL_CMD -v ON_ERROR_STOP=1 <<SQL
|
||||
BEGIN;
|
||||
DELETE FROM check_defs WHERE entity_id IN (SELECT id FROM entities e WHERE $ORPHAN_PRED);
|
||||
WITH ids AS (SELECT id FROM entities e WHERE $ORPHAN_PRED)
|
||||
DELETE FROM metric_samples WHERE entity_id IN (SELECT id FROM ids);
|
||||
WITH ids AS (SELECT id FROM entities e WHERE $ORPHAN_PRED)
|
||||
DELETE FROM signals WHERE entity_id IN (SELECT id FROM ids) OR target_entity_id IN (SELECT id FROM ids);
|
||||
WITH ids AS (SELECT id FROM entities e WHERE $ORPHAN_PRED)
|
||||
DELETE FROM entity_status WHERE entity_id IN (SELECT id FROM ids);
|
||||
DELETE FROM entities e WHERE $ORPHAN_PRED;
|
||||
COMMIT;
|
||||
SQL
|
||||
|
||||
Reference in New Issue
Block a user