From aad0e20bf9e51f89e83e30680fcd840e7d4d68c4 Mon Sep 17 00:00:00 2001 From: Dmytro Kazanzhy Date: Thu, 19 May 2022 22:58:54 +0300 Subject: [PATCH] Fix UnboundLocalError when sql is empty list in DbApiHook --- 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 1f898706a5e91..da33bacca8447 100644 --- a/airflow/hooks/dbapi.py +++ b/airflow/hooks/dbapi.py @@ -178,6 +178,11 @@ def run(self, sql, autocommit=False, parameters=None, handler=None): 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 81a63de441d69..fd2bbd913247a 100644 --- a/tests/hooks/test_dbapi.py +++ b/tests/hooks/test_dbapi.py @@ -273,3 +273,8 @@ def handler(cur): 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"