kaxil commented on code in PR #73173:
URL: https://github.com/apache/airflow/pull/73173#discussion_r4025381256
##########
airflow-core/src/airflow/utils/db_cleanup.py:
##########
@@ -517,7 +583,31 @@ def _build_query(
if dag_ids:
conditions.append(base_table_dag_id_col.in_(dag_ids))
if exclude_dag_ids:
- conditions.append(base_table_dag_id_col.not_in(exclude_dag_ids))
+ # A NULL dag id belongs to no Dag, so it is not one of the
excluded Dags' rows and stays
+ # eligible. NOT IN alone would yield NULL for it and silently
retain it forever -- which
+ # is every `job` row, since core never sets Job.dag_id.
+ conditions.append(
+ or_(base_table_dag_id_col.is_(None),
base_table_dag_id_col.not_in(exclude_dag_ids))
+ )
+ elif (dag_ids or exclude_dag_ids) and dag_id_scope is not None:
+ fk_col = base_table.c[dag_id_scope.fk_column]
+ referenced = table(
+ dag_id_scope.referenced_table,
+ column(dag_id_scope.referenced_pk_column),
+ column(dag_id_scope.referenced_dag_id_column),
+ )
+
+ def _rows_for(target_dag_ids: list[str]):
+ return
select(referenced.c[dag_id_scope.referenced_pk_column]).where(
+
referenced.c[dag_id_scope.referenced_dag_id_column].in_(target_dag_ids)
+ )
+
+ if dag_ids:
+ conditions.append(fk_col.in_(_rows_for(dag_ids)))
+ if exclude_dag_ids:
+ # A NULL foreign key belongs to no Dag, so it is not one of the
excluded Dags' rows and
+ # stays eligible. Testing it with NOT IN alone would yield NULL
and silently retain it.
Review Comment:
Trimmed, the second one now just points at the first, which keeps the `job`
detail.
--
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]