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

 ##########
 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
+
+        for edge in edges:
+            edge.graph_id = graph_id
+
+        run.graph_id = graph_id
         session.add(run)
 
+        session.add_all(tis)
+        if not is_dag_unchanged:
+            session.add_all(edges)
+
         session.commit()
 
         run.dag = self
 
-        # create the associated task instances
-        # state is None at the moment of creation
-        run.verify_integrity(session=session)
 
 Review comment:
   I did remove this because this is not required in the current setup. This 
method will verify if the tasks are still the same as the dag that is given. 
Because I switch from session.merge to session.add the run/tasks should never 
exist already in the database.
   This is also the method where before the tasks are created while this now 
happens in create_dagrun itself.

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