potiuk opened a new pull request, #68523: URL: https://github.com/apache/airflow/pull/68523
`airflow db clean` aborted with a `ForeignKeyViolation` when an old `dag_version` row scoped for deletion was still referenced by a newer task instance (or dag run) that was not itself in scope: ``` psycopg2.errors.ForeignKeyViolation: update or delete on table "dag_version" violates foreign key constraint "task_instance_dag_version_id_fkey" on table "task_instance" DETAIL: Key (id)=(0198edf9-f9f1-7d73-92c1-fac5359c10b7) is still referenced from table "task_instance". ``` `db clean` cleans tables in dependency order but filters each table by its own recency column, so an old `dag_version` (old `created_at`, not the latest for its dag) gets scoped for deletion while a newer task instance still referencing it is not. `task_instance.dag_version_id` is `ON DELETE RESTRICT` (so the delete raises) and `dag_run.created_dag_version_id` is `ON DELETE SET NULL` (so the delete would silently null a surviving run's reference). This adds a `skip_if_referenced_by` option to the cleanup table config. `dag_version` rows whose `id` still appears in `task_instance.dag_version_id` or `dag_run.created_dag_version_id` are excluded from deletion; they are removed on a later run once the referencing rows have themselves aged out of scope. Tests cover both the skip path and that an unreferenced old version is still deleted. Thanks to @HsiuChuanHsu for the reproduction and the original approach in #56567. closes: #56192 --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes — Claude Code (Opus 4.8) Generated-by: Claude Code (Opus 4.8) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) -- 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]
