This is an automated email from the ASF dual-hosted git repository. jedcunningham pushed a commit to branch v2-3-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 7f4a833798f11a6cbfc1c08791ee43d850562bdc Author: Dmytro Kazanzhy <[email protected]> AuthorDate: Sun May 22 15:26:57 2022 +0300 Fix UnboundLocalError when sql is empty list in DbApiHook (#23816) (cherry picked from commit 4b5a101d29d8b0188d41abc5d079b0d363902de1) --- airflow/hooks/dbapi.py | 5 +++++ tests/hooks/test_dbapi.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/airflow/hooks/dbapi.py b/airflow/hooks/dbapi.py index 1f898706a5..da33bacca8 100644 --- a/airflow/hooks/dbapi.py +++ b/airflow/hooks/dbapi.py @@ -178,6 +178,11 @@ class DbApiHook(BaseHook): if scalar: sql = [sql] + if sql: + self.log.debug("Executing %d statements", len(sql)) + else: + raise ValueError("List of SQL statements is empty") + with closing(self.get_conn()) as conn: if self.supports_autocommit: self.set_autocommit(conn, autocommit) diff --git a/tests/hooks/test_dbapi.py b/tests/hooks/test_dbapi.py index 81a63de441..fd2bbd9132 100644 --- a/tests/hooks/test_dbapi.py +++ b/tests/hooks/test_dbapi.py @@ -273,3 +273,8 @@ class TestDbApiHook(unittest.TestCase): assert called == 2 assert self.conn.commit.called assert result == [obj, obj] + + def test_run_no_queries(self): + with pytest.raises(ValueError) as err: + self.db_hook.run(sql=[]) + assert err.value.args[0] == "List of SQL statements is empty"
