anitakar commented on a change in pull request #7217: [AIRFLOW-5946] Store source code in db URL: https://github.com/apache/airflow/pull/7217#discussion_r383356331
########## File path: airflow/models/dagcode.py ########## @@ -0,0 +1,108 @@ +# 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. +import logging +from typing import List + +from sqlalchemy import Column, Index, Integer, String, Text, and_ + +from airflow.models import Base +from airflow.utils import timezone +from airflow.utils.session import provide_session +from airflow.utils.sqlalchemy import UtcDateTime + +log = logging.getLogger(__name__) + + +class DagCodeModel(Base): + """A table for DAGs code. + + dag_code table contains code of DAG files synchronized by scheduler. + This feature is controlled by: + + * ``[core] store_serialized_dags = True``: enable this feature + * ``[core] store_code = True``: enable this feature + + For details on dag serialization see SerializedDagModel + """ + __tablename__ = 'dag_code' + + fileloc = Column(String(2000), primary_key=True) + # The max length of fileloc exceeds the limit of indexing. + fileloc_hash = Column(Integer, primary_key=True) + last_updated = Column(UtcDateTime, nullable=False) + source_code = Column(Text()) + + __table_args__ = ( + Index('idx_fileloc_code_hash', fileloc_hash, unique=False), + ) + + def __init__(self, full_filepath: str): + self.fileloc = full_filepath + self.fileloc_hash = DagCodeModel.dag_fileloc_hash(self.fileloc) + self.last_updated = timezone.utcnow() + self.source_code = DagCodeModel._read_code(self.fileloc) + + @classmethod + def _read_code(cls, fileloc: str): + try: + with open(fileloc, 'r') as source: + source_code = source.read() + except IOError: + source_code = "Couldn't read source file {}.".format(fileloc) + return source_code + + @provide_session + def write_code(self, session=None): + """Writes code into database. + + :param session: ORM Session + """ + session.merge(self) Review comment: I have changed it to sync_to_db. I have applied your suggestion in production code. Unfortunately, it was trickier in test code. That is where I have noticed that we have sync_to_db for Dag. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
