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


##########
tests/dag_processing/test_processor.py:
##########
@@ -635,6 +636,30 @@ def test_import_error_tracebacks(self, tmpdir):
             )
             session.rollback()
 
+    def test_process_file_should_remove_unknown_serialized_dags(self):
+        dag_file = os.path.join(
+            os.path.dirname(os.path.realpath(__file__)), 
'../dags/test_only_dummy_tasks.py'
+        )

Review Comment:
   ```suggestion
           dag_file = os.path.join(TEST_DAGS_FOLDER, 'test_only_dummy_tasks.py')
   ```
   Then import `TEST_DAGS_FOLDER` from `tests.models`



##########
airflow/models/serialized_dag.py:
##########
@@ -220,6 +220,35 @@ 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, cls.fileloc == 
dag_file_path)
+            .all()
+        )
+        dag_ids_from_db = [i[0] for i in rows]

Review Comment:
   ```suggestion
           dag_ids_from_db = [i.dag_id for i in rows]
   ```



##########
airflow/models/serialized_dag.py:
##########
@@ -220,6 +220,35 @@ 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, cls.fileloc == 
dag_file_path)
+            .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]
+        if dag_ids_to_remove:
+            # lower the number of rows deleted at once
+            for dag_id in dag_ids_to_remove:
+                session.execute(cls.__table__.delete().where(cls.dag_id == 
dag_id))

Review Comment:
   ```suggestion
               
session.query(cls.dag_id).filter(cls.dag_id.in_(dag_ids_to_remove)).delete(synchronize_session='fetch')
   ```



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