ffinfo commented on a change in pull request #4396: [AIRFLOW-3585] - Add edges 
to database
URL: https://github.com/apache/airflow/pull/4396#discussion_r259610125
 
 

 ##########
 File path: airflow/models/__init__.py
 ##########
 @@ -4019,17 +4073,50 @@ def create_dagrun(self,
             conf=conf,
             state=state
         )
+
+        dag_model = DagModel.get_dagmodel(self.dag_id)
+
+        tis = self.create_tis(execution_date)
+        # Setting graph_id temparary on -1, will set set later.
+        edges = self.create_edges(graph_id=-1)
+
+        last_dagrun = 
dag_model.get_last_dagrun(include_externally_triggered=True)
+        if last_dagrun is None:
+            is_dag_unchanged = False
+        else:
+            last_edges = DagEdge.fetch_edges(self.dag_id, last_dagrun.graph_id)
+
+            # Compare edges from last run
+            prev_edges = [(edge.task_from, edge.task_to) for edge in 
last_edges]
+            current_edges = [(edge.task_from, edge.task_to) for edge in edges]
+            is_dag_unchanged = len(current_edges) == len(prev_edges)
+            is_dag_unchanged &= set(current_edges) == set(prev_edges)
+
+        if is_dag_unchanged:
+            # graph is not changed, keep last graph_id
+            graph_id = last_dagrun.graph_id
+        elif last_dagrun is None or last_dagrun.graph_id is None:
+            # no graph known yet, beginning at 1
+            graph_id = 1
+        else:
+            # graph is changed
+            graph_id = last_dagrun.graph_id + 1
 
 Review comment:
   In case of backfilling this might be not completely correct. I think I 
should switch this to a max query and do a +1 on that. This is almost the same 
but this do ensures that the graph_id it not yet used in the database. Not sure 
if this is required but just for safety I will implement this.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to