uranusjr commented on code in PR #42245:
URL: https://github.com/apache/airflow/pull/42245#discussion_r1766115512


##########
airflow/dag_processing/collection.py:
##########
@@ -0,0 +1,408 @@
+#
+# 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.
+
+"""
+Utility code that write DAGs in bulk into the database.
+
+This should generally only be called by internal methods such as
+``DagBag._sync_to_db``, ``DAG.bulk_write_to_db``.
+
+:meta private:
+"""
+
+from __future__ import annotations
+
+import itertools
+import logging
+from typing import TYPE_CHECKING, NamedTuple
+
+from sqlalchemy import func, select
+from sqlalchemy.orm import joinedload, load_only
+from sqlalchemy.sql import expression
+
+from airflow.datasets import Dataset, DatasetAlias
+from airflow.datasets.manager import dataset_manager
+from airflow.models.dag import DAG, DagModel, DagOwnerAttributes, DagTag
+from airflow.models.dagrun import DagRun
+from airflow.models.dataset import (
+    DagScheduleDatasetAliasReference,
+    DagScheduleDatasetReference,
+    DatasetAliasModel,
+    DatasetModel,
+    TaskOutletDatasetReference,
+)
+from airflow.utils.sqlalchemy import with_row_locks
+from airflow.utils.timezone import utcnow
+from airflow.utils.types import DagRunType
+
+if TYPE_CHECKING:
+    from collections.abc import Collection, Iterable, Iterator
+
+    from sqlalchemy.orm import Session
+    from sqlalchemy.sql import Select
+
+    from airflow.typing_compat import Self
+
+log = logging.getLogger(__name__)
+
+
+def collect_orm_dags(dags: dict[str, DAG], *, session: Session) -> dict[str, 
DagModel]:
+    """
+    Collect DagModel objects from DAG objects.
+
+    An existing DagModel is fetched if there's a matching ID in the database.
+    Otherwise, a new DagModel is created and added to the session.
+    """
+    stmt = (
+        select(DagModel)
+        .options(joinedload(DagModel.tags, innerjoin=False))
+        .where(DagModel.dag_id.in_(dags))
+        .options(joinedload(DagModel.schedule_dataset_references))
+        .options(joinedload(DagModel.schedule_dataset_alias_references))
+        .options(joinedload(DagModel.task_outlet_dataset_references))
+    )
+    stmt = with_row_locks(stmt, of=DagModel, session=session)
+    existing_orm_dags = {dm.dag_id: dm for dm in 
session.scalars(stmt).unique()}
+
+    for dag_id, dag in dags.items():
+        if dag_id in existing_orm_dags:
+            continue
+        orm_dag = DagModel(dag_id=dag_id)
+        if dag.is_paused_upon_creation is not None:
+            orm_dag.is_paused = dag.is_paused_upon_creation
+        orm_dag.tags = []
+        log.info("Creating ORM DAG for %s", dag_id)
+        session.add(orm_dag)
+        existing_orm_dags[dag_id] = orm_dag

Review Comment:
   Likely, but I’d prefer doing this in a later PR… this is basically just 
copy-pasted and split into parts.



-- 
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-unsubscr...@airflow.apache.org

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

Reply via email to