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:

  1. The deletion event is properly tracked
  2. 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

  1. Foreign key constraints: Add foreign key constraints with ON DELETE CASCADE behavior for vulnerabilities related to security_project_tracked_contexts
  2. Track deletion events: Implement a mechanism to track when a security_project_tracked_context is deleted
  3. 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_contexts with ON 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 VulnerabilityReference records in ElasticSearch
  • Add tests for cascading deletes and ElasticSearch cleanup
  • Document the deletion behavior and its impact on ElasticSearch data

Related Issues

  • #555993 - Update the vulnerability ElasticSearch reference to reflect the ref relationship and details
  • &18520 - CDC implementation (not prioritized)