Repository navigation
Fix airflow db clean silently skipping three tables when scoped to Dags - #73173
Conversation
The cleanup config named a dag_id column on three tables that do not have one. task_reschedule.dag_id was dropped in 3.0.0, deadline.dag_id in 3.1.0, and asset_event has only ever had source_dag_id. Any run passing --dag-ids or --exclude-dag-ids built SQL against a column that does not exist, and run_cleanup suppresses a failing table into a warning, so the command reported success and exited 0 while never purging a row from any of them. deadline and task_reschedule now reach their Dag the way the schema does, through dag_run and task_instance, so a table with no dag id of its own can be scoped through a foreign key. Dropping the filter instead would have been worse for deadline: it is cleaned as a dependent of dag_run precisely so its rows are archived before the ON DELETE CASCADE removes them, so an unscoped pass would purge deadlines belonging to the very Dags whose runs --exclude-dag-ids preserves. --exclude-dag-ids now also treats a NULL dag id as eligible. A row attributed to no Dag is not one of the excluded Dags' rows, but NOT IN alone yields NULL for it and retains it forever. That was every job row, since core never sets Job.dag_id, and it becomes reachable for asset_event and log too. The guard test only ever called run_cleanup without Dag filters, which is the branch that dereferences these column names, so all three configs drifted unnoticed across two releases. It is now parametrized over the filter combinations and fails on each of them.
|
Two PRs were already working on this, both are tagged "closes: #69030", and I've been working with those authors. #66350 has been open since 4 May and has been through several rounds of review. #71732 came in August and was cherry-picked into #66350 to avoid the two conflicting. Same three tables, same cause, same fix. Your Please don't close #66350. It also re-applies |
Commented on that PR too, thanks Dennis |
Follow-up to #70923, which restored the coverage check for
db clean. That check compares table names against the config, not the columns the config names, so it cannot catch this.airflow db clean --dag-idsor--exclude-dag-idssilently purges nothing from three tables. Each names adag_idcolumn it does not have:task_reschedule.dag_idwas dropped in 3.0.0,deadline.dag_idin 3.1.0, andasset_eventhas only ever hadsource_dag_id. The query fails on a missing column,run_cleanupsuppresses a failing table into a warning, and the command exits 0. An operator who always scopes by Dag has been growing those three tables forever while cleanup reported success.deadlineandtask_reschedulenow reach their Dag the way the schema does, throughdag_runandtask_instance.Why scope them rather than just drop the filter, which would be the smaller diff:
deadlineis cleaned as a dependent ofdag_runprecisely so its rows are archived before theON DELETE CASCADEremoves them. An unscoped pass would purge deadlines belonging to the very Dags whose runs--exclude-dag-idsis preserving, turning a silent no-op into silent data loss.--exclude-dag-idsnow also treats a NULL dag id as eligible. A row attributed to no Dag is not one of the excluded Dags' rows, butNOT INalone yields NULL for it and retains it forever. That is everyjobrow, since core never setsJob.dag_id, and it becomes reachable forasset_eventandlogtoo. This changes behaviour for those tables, in the direction the flag already documents.The guard test is the part that stops this recurring. It only ever called
run_cleanupwithout Dag filters, and the Dag filter is the only thing that dereferences these column names, so all three configs drifted unnoticed across two releases. It is now parametrized over the filter combinations and fails on each of them.