FrankYang0529 commented on code in PR #75673:
URL: https://github.com/apache/airflow/pull/75673#discussion_r4386156178


##########
airflow-core/src/airflow/migrations/versions/0150_3_5_0_add_dag_parse_warning.py:
##########
@@ -0,0 +1,52 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Extend dag_warning for parse-time failures (file scope, no dag FK).
+
+Revision ID: b8f2c1d04e91
+Revises: c71065a1b14f
+Create Date: 2026-10-26 00:00:00.000000
+
+"""
+
+from __future__ import annotations
+
+import sqlalchemy as sa
+from alembic import op
+
+from airflow.models.base import StringID
+
+revision = "b8f2c1d04e91"
+down_revision = "c71065a1b14f"
+branch_labels = None
+depends_on = None
+airflow_version = "3.5.0"
+
+
+def upgrade():
+    with op.batch_alter_table("dag_warning") as batch_op:
+        batch_op.drop_constraint("dcw_dag_id_fkey", type_="foreignkey")
+        batch_op.add_column(sa.Column("bundle_name", StringID(), 
nullable=True))
+        batch_op.add_column(sa.Column("filename", sa.String(length=1024), 
nullable=True))
+
+
+def downgrade():
+    with op.batch_alter_table("dag_warning") as batch_op:
+        batch_op.drop_column("filename")
+        batch_op.drop_column("bundle_name")
+        batch_op.create_foreign_key("dcw_dag_id_fkey", "dag", ["dag_id"], 
["dag_id"], ondelete="CASCADE")

Review Comment:
   `downgrade()` re-creates `dcw_dag_id_fkey`. If `dag_warning` holds 
parse-failure rows whose `dag_id` has no row in `dag`, the downgrade stops with 
`sqlite3.IntegrityError: FOREIGN KEY constraint failed`. Deleting the rows that 
the old schema can't hold before re-creating the key.



##########
task-sdk/src/airflow/sdk/importers/python_importer.py:
##########
@@ -250,9 +254,10 @@ def _handle_sigsegv(signum, frame):
         if mod_name in sys.modules:
             del sys.modules[mod_name]
 
+        DagContext.current_autoregister_module_name = mod_name
+
         def parse() -> list[types.ModuleType]:
             try:
-                DagContext.current_autoregister_module_name = mod_name

Review Comment:
   These change revert #74911. The related test 
`test_failed_load_does_not_break_later_dags` will fail.



##########
airflow-core/src/airflow/models/dagwarning.py:
##########
@@ -51,25 +52,35 @@ class DagWarning(Base):
     warning_type: Mapped[str] = mapped_column(String(WARNING_TYPE_MAX_LENGTH), 
primary_key=True)
     message: Mapped[str] = mapped_column(Text, nullable=False)
     timestamp: Mapped[datetime] = mapped_column(UtcDateTime, nullable=False, 
default=timezone.utcnow)
-
-    dag_model = relationship("DagModel", viewonly=True, lazy="selectin")
-
-    __tablename__ = "dag_warning"
-    __table_args__ = (
-        ForeignKeyConstraint(
-            ("dag_id",),
-            ["dag.dag_id"],
-            name="dcw_dag_id_fkey",
-            ondelete="CASCADE",
-        ),

Review Comment:
   Dropping `dcw_dag_id_fkey` also drops its `ON DELETE CASCADE`. Several tests 
clean up with `clear_db_dags()`. That helper deletes only `DagModel` rows and 
has relied on the cascade to remove warnings. Adding `delete(DagWarning)` to 
`clear_db_dags()` would restore the old cleanup for every caller.



##########
airflow-core/src/airflow/models/dagwarning.py:
##########


Review Comment:
   This method also deletes the new parse-failure rows, so the warning 
disappears. In that case, a Dag that used to load starts failing, and the Dag 
author passes the Dag's real `dag_id` to `emit_dag_warning()`. The Dag 
processor marks the Dag stale once the Dag has been missing from the parse 
results for longer than `stale_dag_threshold`. This method then deletes every 
`dag_warning` row whose Dag is stale, no matter which warning type the row has.
   
   If this method skips file-scoped rows, the warning stays. Here is the change 
for the SQLite branch. The other branch needs the same 
`cls.bundle_name.is_(None)` condition.
   
   ```python
   query = delete(cls).where(
       cls.dag_id.in_(dag_ids_stmt.scalar_subquery()),
       cls.bundle_name.is_(None),
   )
   ```
   
   The change has one side effect. When a Dag file is deleted, the warnings of 
the file should go away, but with the change they stay. So the change also 
needs the cleanup for removed files that I suggested in my comment on 
`collection.py`.



##########
airflow-core/src/airflow/dag_processing/collection.py:
##########
@@ -383,6 +387,36 @@ def _update_dag_warnings(
         session.merge(warning_to_add)
 
 
+def dag_parse_failure_warnings(bundle_name: str, warnings_by_rel_path: 
dict[str, list]) -> list[DagWarning]:
+    return [
+        DagWarning(
+            dag_id=record.dag_id,
+            warning_type=record.warning_type,
+            message=record.message,
+            bundle_name=bundle_name,
+            filename=rel_path,
+        )
+        for rel_path, records in warnings_by_rel_path.items()
+        for record in records
+    ]
+
+
+def _update_file_scoped_parse_failure_warnings(
+    files_parsed: set[tuple[str, str]],
+    parse_failure_warnings: Iterable[DagWarning],
+    session: Session,
+) -> None:
+    if files_parsed:

Review Comment:
   The cleanup here only covers files in `files_parsed`. When a file is removed 
from the bundle, its warning rows are never deleted. 
`clear_orphaned_import_errors` handles this case for import errors, but nothing 
handles it for `dag_warning`.



-- 
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