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

 ##########
 File path: airflow/models/__init__.py
 ##########
 @@ -4012,17 +4088,55 @@ def create_dagrun(self,
             conf=conf,
             state=state
         )
+
+        dag_model = DagModel.get_dagmodel(self.dag_id)
+
+        tis = self.create_tis(execution_date)
+        edges = self.create_edges(-1)
+
+        last_run = dag_model.get_last_dagrun(include_externally_triggered=True)
+        if last_run is None:
+            same = False
+        else:
+            last_edges = DagEdge.fetch_edges_db(self.dag_id, last_run.graph_id)
+
+            # Compare edges from last run
+            same = True
+            if len(last_edges) != len(edges):
+                same = False
+            else:
+                e1 = set([(edge.from_task, edge.to_task) for edge in 
last_edges])
+                e2 = set([(edge.from_task, edge.to_task) for edge in edges])
+                if e1 != e2:
+                    same = False
+
+        if same:
+            # graph is not changed, keep last graph_id
+            graph_id = last_run.graph_id
+        elif last_run is None or last_run.graph_id is None:
+            # no graph known yet, beginning at 1
+            graph_id = 1
+        else:
+            # graph is changed
+            graph_id = last_run.graph_id + 1
+
+        for edge in edges:
+            edge.graph_id = graph_id
+
+        run.graph_id = graph_id
         session.add(run)
 
+        for ti in tis:
+            session.merge(ti)
+        if not same:
+            for edge in edges:
+                session.merge(edge)
 
 Review comment:
   the `sync_to_db()` calls where not in there yet when I did test this first. 
I will try switching it to `.add` and see how much tests will fail. Maybe it's 
less as before because of all the changes already. It does help to make the 
test more stateless.

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