Repository navigation
Conversation
…y task_instance FK When ``airflow db clean`` deletes old dag rows, ``ON DELETE CASCADE`` on ``dag_version.dag_id`` propagates the delete to ``dag_version``. If any ``dag_version`` row is still referenced by a ``task_instance`` via the ``ON DELETE RESTRICT`` foreign key ``task_instance.dag_version_id``, the cascade fails with an ``IntegrityError`` (and, on MySQL, can block the command indefinitely on metadata locks). Add a new ``skip_if_cascade_blocked`` config option to ``_TableConfig`` that generates a correlated ``NOT EXISTS`` subquery on the cascade path. When set on the ``dag`` table config, the cleanup query excludes dags whose ``dag_version`` rows are still referenced by any ``task_instance``. These dags become eligible once their task instances age out and are cleaned by a later run. Related: apache#68339 (which added ``skip_if_referenced`` for the direct ``dag_version`` → ``task_instance`` path, but did not protect the ``dag`` → ``dag_version`` cascade). Closes apache#71864
ColtenOuO
left a comment
There was a problem hiding this comment.
Thanks for PR, but I have some suggestions and my personal opinion
PR title does not follow the naming convention
The PR title should follow the convention.
Suggested title: Fix airflow db clean failing when a dag's cascade delete hits a dag_version still referenced by a task instance
Design concern: skip_if_cascade_blocked
Adding skip_if_cascade_blocked as a separate parameter feels like the wrong direction here — it's structurally the same NOT EXISTS guard as skip_if_referenced, just with one extra JOIN hop, so now we're maintaining two near-identical mechanisms for the same concept. Since dag_version already has skip_if_referenced=[("task_instance", "dag_version_id")] to protect itself, could this instead extend skip_if_referenced to handle one intermediate hop, rather than introducing a whole new field?
ruff / pre-commit
Did you run pre-commit? Some of the formatting here looks like ruff should have caught it.
Gen-AI disclosure
Sorry, this part is purely my personal opinion and unrelated to the project itself (I happened to give feedback on one of your other PRs before, so I have some sense of the situation across a few of your PRs).
I've noticed several of your PRs on Airflow were closed shortly after being opened, and you also appear to have opened a large number of PRs on other projects in a short time with the help of AI. Using AI assistance on Airflow is fine, but it should follow the disclosure requirements in the contributing guide.
AI is a great tool that can help us diagnose problems quickly, but if it's only used to churn out PRs quickly, it just creates trouble and extra work for maintainers, and wastes CI resources.
If you'd like to help Airflow keep getting better, I'd suggest reading through the contributing guide first: https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions
If you choose to use AI, please make sure you thoroughly analyze the problem yourself, understand and verify the changes, and properly run the relevant tests and checks locally using breeze and pre-commit.
Problem
airflow db cleanfails with anIntegrityErrorwhen deleting olddagrows. Thedag→dag_versionON DELETE CASCADEpropagates todag_version, buttask_instance.dag_version_idisON DELETE RESTRICT, so the cascade fails the foreign key constraint (and, on MySQL, can leave the cleanup command blocked on metadata locks).PR #68339 added
skip_if_referencedto protect thedag_versiontable's own cleanup query, but did not protect thedag→dag_versioncascade path.Fix
Add a new
skip_if_cascade_blockedconfig option to_TableConfigthat generates a correlatedNOT EXISTSsubquery across the cascade path. Apply it to thedagtable config: exclude dags whosedag_versionrows are still referenced by anytask_instance.Closes #71864