Track deletion events for security_project_tracked_contexts and cleanup ElasticSearch data
Problem
When a tracked context (security_project_tracked_contexts) is deleted, we need to ensure that:
- The deletion event is properly tracked
- Corresponding data is cleaned up from the ElasticSearch instance
Since the CDC (Change Data Capture) implementation in &18520 is not being prioritized, we need a separate mechanism to handle this cleanup.
Context
This issue was identified in #555993 (comment 2908582690) by @bala.kumar.
Solution Approach
We will introduce a foreign key constraint to ensure data is not retained after the removal of a context. This will automatically cascade the deletion to related vulnerability data.
Requirements
-
Foreign key constraints: Add foreign key constraints with
ON DELETE CASCADEbehavior for vulnerabilities related tosecurity_project_tracked_contexts -
Track deletion events: Implement a mechanism to track when a
security_project_tracked_contextis deleted - ElasticSearch cleanup: Ensure that when a tracked context is deleted, all related vulnerability data is removed from ElasticSearch indices
Implementation Tasks
-
Add foreign key constraint to vulnerability tables referencing security_project_tracked_contextswithON DELETE CASCADE -
Implement deletion event tracking for security_project_tracked_contexts -
Implement ElasticSearch cleanup logic that triggers on tracked context deletion -
Ensure proper cleanup of VulnerabilityReferencerecords in ElasticSearch -
Add tests for cascading deletes and ElasticSearch cleanup -
Document the deletion behavior and its impact on ElasticSearch data