Skip to content

Commit

Permalink
Fix UnboundLocalError when sql is empty list in DbApiHook (#23816)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazanzhy authored May 22, 2022
1 parent fe91db7 commit 4b5a101
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions airflow/hooks/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/hooks/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 4b5a101

Please sign in to comment.