Skip to content

Commit

Permalink
Set type of update_condition to Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJBoyer committed May 8, 2024
1 parent 272da42 commit 3ddb27d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/src/db/bulk_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Provides a bulk_upsert function
"""
from typing import Any, Sequence
from typing import Any, Optional, Sequence

import psycopg
from psycopg import rows, sql
Expand All @@ -11,16 +11,14 @@
Cursor = psycopg.Cursor
kwargs_row = rows.kwargs_row

EMPTY_SQL = sql.SQL("")


def bulk_upsert(
cur: psycopg.Cursor,
table: str,
attributes: Sequence[str],
objects: Sequence[Any],
constraint: str,
update_condition: sql.SQL = EMPTY_SQL,
update_condition: Optional[sql.SQL] = None,
) -> None:
"""Bulk insert or update a sequence of objects.
Expand All @@ -37,6 +35,9 @@ def bulk_upsert(
update_condition: optional WHERE clause to limit updates for a
conflicting row
"""
if not update_condition:
update_condition = sql.SQL("")

temp_table = f"temp_{table}"
create_temp_table(cur, temp_table=temp_table, src_table=table)
bulk_insert(cur, table=temp_table, columns=attributes, objects=objects)
Expand Down Expand Up @@ -103,7 +104,7 @@ def write_from_table_to_table(
dest_table: str,
columns: Sequence[str],
constraint: str,
update_condition: sql.SQL = EMPTY_SQL,
update_condition: Optional[sql.SQL] = None,
) -> None:
"""
Write data from one table to another.
Expand All @@ -117,6 +118,9 @@ def write_from_table_to_table(
update_condition: optional WHERE clause to limit updates for a
conflicting row
"""
if not update_condition:
update_condition = sql.SQL("")

columns_sql = sql.SQL(",").join(map(sql.Identifier, columns))
update_sql = sql.SQL(",").join(
[
Expand Down

0 comments on commit 3ddb27d

Please sign in to comment.