This is an automated email from the ASF dual-hosted git repository.
jedcunningham pushed a commit to branch v2-10-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/v2-10-test by this push:
new e96929970d5 Ensure check_query_exists returns a bool (#43978) (#46707)
e96929970d5 is described below
commit e96929970d5a6664762e34f6b359d724527f655f
Author: Wei Lee <[email protected]>
AuthorDate: Thu Feb 13 15:28:26 2025 +0800
Ensure check_query_exists returns a bool (#43978) (#46707)
Co-authored-by: John C. Merfeld <[email protected]>
---
airflow/utils/db.py | 3 ++-
tests/utils/test_db.py | 16 +++++++++++++++-
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/airflow/utils/db.py b/airflow/utils/db.py
index c92ab223b43..63a9b06ef84 100644
--- a/airflow/utils/db.py
+++ b/airflow/utils/db.py
@@ -1951,7 +1951,8 @@ def check_query_exists(query_stmt: Select, *, session:
Session) -> bool:
:meta private:
"""
count_stmt =
select(literal(True)).select_from(query_stmt.order_by(None).subquery())
- return session.scalar(count_stmt)
+ # we must cast to bool because scalar() can return None
+ return bool(session.scalar(count_stmt))
def exists_query(*where: ClauseElement, session: Session) -> bool:
diff --git a/tests/utils/test_db.py b/tests/utils/test_db.py
index 7468f84a83c..97f148dd83f 100644
--- a/tests/utils/test_db.py
+++ b/tests/utils/test_db.py
@@ -31,12 +31,13 @@ from alembic.config import Config
from alembic.migration import MigrationContext
from alembic.runtime.environment import EnvironmentContext
from alembic.script import ScriptDirectory
-from sqlalchemy import MetaData, Table
+from sqlalchemy import Column, Integer, MetaData, Table, select
from sqlalchemy.sql import Select
from airflow.models import Base as airflow_base
from airflow.settings import engine
from airflow.utils.db import (
+ LazySelectSequence,
_get_alembic_config,
check_bad_references,
check_migrations,
@@ -326,3 +327,16 @@ class TestDb:
mock_session, task_fail_table, mock_select,
dangling_task_fail_table_name
)
mock_session.rollback.assert_called_once()
+
+ def test_bool_lazy_select_sequence(self):
+ class MockSession:
+ def __init__(self):
+ pass
+
+ def scalar(self, stmt):
+ return None
+
+ t = Table("t", MetaData(), Column("id", Integer, primary_key=True))
+ lss = LazySelectSequence.from_select(select(t.c.id), order_by=[],
session=MockSession())
+
+ assert bool(lss) is False