kaxil commented on a change in pull request #9851:
URL: https://github.com/apache/airflow/pull/9851#discussion_r455934821



##########
File path: airflow/models/serialized_dag.py
##########
@@ -233,3 +233,18 @@ def bulk_sync_to_db(dags: List[DAG], session: Session = 
None):
                     min_update_interval=MIN_SERIALIZED_DAG_UPDATE_INTERVAL,
                     session=session
                 )
+
+    @classmethod
+    @provide_session
+    def get_last_updated_date(cls, dag_id: str, session: Session = None) -> 
datetime:
+        """
+        Get the date when the Serialized DAG associated to DAG was last updated
+        in serialized_dag table
+
+        :param dag_id: DAG ID
+        :type dag_id: str
+        :param session: ORM Session
+        :type session: Session
+        """
+        result = session.query(cls.last_updated).filter(cls.dag_id == 
dag_id).one()
+        return result.last_updated

Review comment:
       
https://docs.sqlalchemy.org/en/13/orm/query.html?highlight=query%20get#sqlalchemy.orm.query.Query.get
   
   >Query.get() is only used to return a single mapped instance, not multiple 
instances or individual column constructs, and strictly on a single primary key 
value. The originating Query must be constructed in this way, i.e. against a 
single mapped entity, with no additional filtering criterion.
   
   ```
   In [8]: with create_session() as session3:
      ...:     
print(session3.query(SerializedDagModel.last_updated).get("example_xcom_args").scalar())
      ...:
   ---------------------------------------------------------------------------
   InvalidRequestError                       Traceback (most recent call last)
   <ipython-input-8-9a4a54c2d854> in <module>
         1 with create_session() as session3:
   ----> 2     
print(session3.query(SerializedDagModel.last_updated).get("example_xcom_args").scalar())
   
   
~/.virtualenvs/airflow_master/lib/python3.7/site-packages/sqlalchemy/orm/query.py
 in get(self, ident)
      1002
      1003         """
   -> 1004         return self._get_impl(ident, loading.load_on_pk_identity)
      1005
      1006     def _identity_lookup(
   
   
~/.virtualenvs/airflow_master/lib/python3.7/site-packages/sqlalchemy/orm/query.py
 in _get_impl(self, primary_key_identity, db_load_fn, identity_token)
      1063             primary_key_identity = 
primary_key_identity.__composite_values__()
      1064
   -> 1065         mapper = self._only_full_mapper_zero("get")
      1066
      1067         is_dict = isinstance(primary_key_identity, dict)
   
   
~/.virtualenvs/airflow_master/lib/python3.7/site-packages/sqlalchemy/orm/query.py
 in _only_full_mapper_zero(self, methname)
       429             raise sa_exc.InvalidRequestError(
       430                 "%s() can only be used against "
   --> 431                 "a single mapped class." % methname
       432             )
       433         return self._primary_entity.entity_zero
   
   InvalidRequestError: get() can only be used against a single mapped class.
   ```
   
   Since it is just getting `last_updated` column it will be a relatively quick 
operation 
   




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


Reply via email to