gopidesupavan commented on code in PR #43826:
URL: https://github.com/apache/airflow/pull/43826#discussion_r1854088949


##########
airflow/dag_processing/collection.py:
##########
@@ -425,3 +427,97 @@ def add_task_asset_references(
                 for task_id, asset_id in referenced_outlets
                 if (task_id, asset_id) not in orm_refs
             )
+
+    def add_asset_trigger_references(
+        self, assets: dict[tuple[str, str], AssetModel], *, session: Session
+    ) -> None:
+        # Update references from assets being used
+        refs_to_add: dict[tuple[str, str], set[str]] = {}
+        refs_to_remove: dict[tuple[str, str], set[str]] = {}
+        triggers: dict[str, BaseTrigger] = {}
+        for name_uri, asset in self.assets.items():
+            asset_model = assets[name_uri]
+            trigger_repr_to_trigger_dict: dict[str, BaseTrigger] = {
+                repr(trigger): trigger for trigger in asset.watchers
+            }
+            triggers.update(trigger_repr_to_trigger_dict)
+            trigger_repr_from_asset: set[str] = 
set(trigger_repr_to_trigger_dict.keys())
+
+            trigger_repr_from_asset_model: set[str] = {
+                BaseTrigger.repr(trigger.classpath, trigger.kwargs) for 
trigger in asset_model.triggers
+            }
+
+            # Optimization: no diff between the DB and DAG definitions, no 
update needed
+            if trigger_repr_from_asset == trigger_repr_from_asset_model:
+                continue
+
+            diff_to_add = trigger_repr_from_asset - 
trigger_repr_from_asset_model
+            diff_to_remove = trigger_repr_from_asset_model - 
trigger_repr_from_asset
+            if diff_to_add:
+                refs_to_add[name_uri] = diff_to_add
+            if diff_to_remove:
+                refs_to_remove[name_uri] = diff_to_remove
+
+        if refs_to_add:
+            all_trigger_reprs: set[str] = {
+                trigger_repr for trigger_reprs in refs_to_add.values() for 
trigger_repr in trigger_reprs
+            }
+
+            all_trigger_keys: list[tuple[str, str]] = [

Review Comment:
   Is `all_trigger_keys` contain duplicates? If so, are the duplicates 
necessary, or is there something I might be missing?  :)
   
   I noticed that `all_trigger_reprs` contains only unique elements, so I’m 
wondering if the same should apply to all_trigger_keys, especially since both 
are derived from the values in `refs_to_add`.



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