You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/04/11 21:11:21 UTC

[GitHub] [airflow] ashb commented on a diff in pull request #22917: remove stale serialized dags

ashb commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r847739271


##########
airflow/models/serialized_dag.py:
##########
@@ -220,6 +220,36 @@ def remove_dag(cls, dag_id: str, session: Session = None):
         """
         session.execute(cls.__table__.delete().where(cls.dag_id == dag_id))
 
+    @classmethod
+    @provide_session
+    def remove_unknown_dags_in_dag_file(
+        cls, active_dag_ids: List[str], dag_file_path: str, session=None
+    ) -> List[str]:
+        """
+        Deletes serialized_dags whose dag_ids are not in the active_dag_ids for a given dag_file_path
+
+        :return: a list of inactive dag ids
+        :rtype: list[str]
+        """
+        file_path_hash = DagCode.dag_fileloc_hash(dag_file_path)
+        # there is a db index on fileloc_hash
+        rows = session.query(cls.dag_id).filter(cls.fileloc_hash == file_path_hash).all()
+        dag_ids_from_db = [i[0] for i in rows]
+
+        dag_ids_to_remove = [dag_id for dag_id in dag_ids_from_db if dag_id not in active_dag_ids]

Review Comment:
   It's unlikely, but theoretically possible that we could have a hash collision, so we should also check the full path matches.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org