Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DbApiHook.insert_rows when rows is a generator #38972

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion airflow/providers/common/sql/hooks/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ def insert_rows(
stacklevel=2,
)

nb_rows = 0
with self._create_autocommit_connection() as conn:
conn.commit()
with closing(conn.cursor()) as cur:
Expand All @@ -584,6 +585,7 @@ def insert_rows(
cur.executemany(sql, values)
conn.commit()
self.log.info("Loaded %s rows into %s so far", len(chunked_rows), table)
nb_rows += len(chunked_rows)
else:
for i, row in enumerate(rows, 1):
values = self._serialize_cells(row, conn)
Expand All @@ -593,8 +595,9 @@ def insert_rows(
if commit_every and i % commit_every == 0:
conn.commit()
self.log.info("Loaded %s rows into %s so far", i, table)
nb_rows += 1
conn.commit()
self.log.info("Done loading. Loaded a total of %s rows into %s", len(rows), table)
self.log.info("Done loading. Loaded a total of %s rows into %s", nb_rows, table)

@classmethod
def _serialize_cells(cls, row, conn=None):
Expand Down