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 2021/12/07 06:38:29 UTC

[GitHub] [airflow] uranusjr commented on a change in pull request #19931: Deprecate some functions in the experimental API

uranusjr commented on a change in pull request #19931:
URL: https://github.com/apache/airflow/pull/19931#discussion_r763678681



##########
File path: airflow/api/common/delete_dag.py
##########
@@ -0,0 +1,82 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Delete DAGs APIs."""
+import logging
+
+from sqlalchemy import or_
+
+from airflow import models
+from airflow.exceptions import AirflowException, DagNotFound
+from airflow.models import DagModel, TaskFail
+from airflow.models.serialized_dag import SerializedDagModel
+from airflow.utils.session import provide_session
+from airflow.utils.state import State
+
+log = logging.getLogger(__name__)
+
+
+@provide_session
+def delete_dag(dag_id: str, keep_records_in_log: bool = True, session=None) -> int:
+    """
+    :param dag_id: the dag_id of the DAG to delete
+    :param keep_records_in_log: whether keep records of the given dag_id
+        in the Log table in the backend database (for reasons like auditing).
+        The default value is True.
+    :param session: session used
+    :return count of deleted dags
+    """
+    log.info("Deleting DAG: %s", dag_id)
+    running_tis = (
+        session.query(models.TaskInstance.state)
+        .filter(models.TaskInstance.dag_id == dag_id)
+        .filter(models.TaskInstance.state == State.RUNNING)
+        .first()
+    )
+    if running_tis:
+        raise AirflowException("TaskInstances still running")
+    dag = session.query(DagModel).filter(DagModel.dag_id == dag_id).first()
+    if dag is None:
+        raise DagNotFound(f"Dag id {dag_id} not found")
+
+    # Scheduler removes DAGs without files from serialized_dag table every dag_dir_list_interval.
+    # There may be a lag, so explicitly removes serialized DAG here.
+    if SerializedDagModel.has_dag(dag_id=dag_id, session=session):
+        SerializedDagModel.remove_dag(dag_id=dag_id, session=session)
+
+    count = 0
+
+    for model in models.base.Base._decl_class_registry.values():

Review comment:
       May be a good chance to fix this as well
   
   https://github.com/apache/airflow/issues/15354#issuecomment-987388275




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