Arunodoy18 opened a new pull request, #61435:
URL: https://github.com/apache/airflow/pull/61435

   ### SUMMARY
   
   Fix foreign key constraint failures when running `airflow db clean` on the 
`dag_version` table.
   
   Previously, cleanup logic filtered `dag_version` rows only based on 
timestamp conditions.
   This allowed deletion attempts for rows still referenced by 
`task_instance.dag_version_id`,
   causing foreign key constraint errors during database cleanup.
   
   This change excludes `dag_version` rows that are still referenced by:
   - task_instance.dag_version_id
   - dag_run.created_dag_version_id
   
   This ensures cleanup respects referential integrity and prevents runtime 
failures.
   
   ---
   
   ### ROOT CAUSE
   
   The cleanup query did not account for existing foreign key relationships:
   - task_instance.dag_version_id → dag_version.id (RESTRICT)
   - dag_run.created_dag_version_id → dag_version.id (SET NULL)
   
   ---
   
   ### SOLUTION
   
   Add filtering logic to exclude referenced dag_version rows using a subquery 
union of:
   - task_instance.dag_version_id
   - dag_run.created_dag_version_id
   
   ---
   
   ### TESTING
   
   - Added unit tests verifying referenced dag_version rows are preserved
   - Verified unreferenced rows are cleaned correctly
   - Ran existing DB cleanup test suite
   - Verified no regressions
   
   ---
   
   closes: #61390  
   related: #52147  
   related: #59474
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to