ferruzzi commented on code in PR #50677:
URL: https://github.com/apache/airflow/pull/50677#discussion_r2110590988
##########
airflow-core/src/airflow/models/deadline.py:
##########
@@ -186,3 +159,52 @@ def serialize_deadline_alert(self):
"callback_kwargs": self.callback_kwargs,
}
)
+
+
+@provide_session
+def _fetch_from_db(model_reference: Column, session=None, **conditions) ->
datetime:
+ """
+ Fetch a datetime value from the database using the provided model
reference and filtering conditions.
+
+ For example, to fetch a TaskInstance's start_date:
+ _fetch_from_db(
+ TaskInstance.start_date, dag_id='example_dag',
task_id='example_task', run_id='example_run'
+ )
+
+ This generates SQL equivalent to:
+ SELECT start_date
+ FROM task_instance
+ WHERE dag_id = 'example_dag'
+ AND task_id = 'example_task'
+ AND run_id = 'example_run'
+
+ :param model_reference: SQLAlchemy Column to select (e.g.,
DagRun.logical_date, TaskInstance.start_date)
+ :param conditions: Filtering conditions applied as equality comparisons in
the WHERE clause.
+ Multiple conditions are combined with AND.
+ :param session: SQLAlchemy session (auto-provided by decorator)
+ """
+ query = select(model_reference)
+
+ for key, value in conditions.items():
+ query = query.where(getattr(model_reference.class_, key) == value)
+
+ compiled_query = query.compile(compile_kwargs={"literal_binds": True})
+ pretty_query = "\n ".join(str(compiled_query).splitlines())
+ logger.debug(
+ "Executing query:\n %r\nAs SQL:\n %s",
+ query,
+ pretty_query,
+ )
+
+ try:
+ result = session.scalar(query)
+ except SQLAlchemyError as e:
+ logger.error("Database query failed: (%s)", str(e))
Review Comment:
Is there a similar way to simplify the block below that which is currently:
```
if result is None:
message = f"No matching record found in the database for query:\n
{pretty_query}"
logger.error(message)
raise ValueError(message)
```
I always felt python needed a "log then raise" method, but maybe there has
been one all along?
--
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]