Skip to content

Commit 19db3cc

Browse files
Merge branch 'main' into docs/fix-readthedocs
2 parents 1ba8376 + b3f215e commit 19db3cc

File tree

2 files changed

+4
-42
lines changed

2 files changed

+4
-42
lines changed

awswrangler/postgresql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def _create_table(
7979
index: bool,
8080
dtype: dict[str, str] | None,
8181
varchar_lengths: dict[str, int] | None,
82+
unique_keys: list[str] | None = None,
8283
) -> None:
8384
if mode == "overwrite":
8485
if overwrite_method in ["drop", "cascade"]:
@@ -101,6 +102,8 @@ def _create_table(
101102
converter_func=_data_types.pyarrow2postgresql,
102103
)
103104
cols_str: str = "".join([f"{_identifier(k)} {v},\n" for k, v in postgresql_types.items()])[:-2]
105+
if unique_keys:
106+
cols_str += f",\nUNIQUE ({', '.join([_identifier(k) for k in unique_keys])})"
104107
sql = f"CREATE TABLE IF NOT EXISTS {_identifier(schema)}.{_identifier(table)} (\n{cols_str})"
105108
_logger.debug("Create table query:\n%s", sql)
106109
cursor.execute(sql)
@@ -619,6 +622,7 @@ def to_sql(
619622
index=index,
620623
dtype=dtype,
621624
varchar_lengths=varchar_lengths,
625+
unique_keys=upsert_conflict_columns or insert_conflict_columns,
622626
)
623627
if index:
624628
df.reset_index(level=df.index.names, inplace=True)

tests/unit/test_postgresql.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,6 @@ def test_dfs_are_equal_for_different_chunksizes(postgresql_table, postgresql_con
285285

286286

287287
def test_upsert(postgresql_table, postgresql_con):
288-
create_table_sql = (
289-
f"CREATE TABLE public.{postgresql_table} "
290-
"(c0 varchar NULL PRIMARY KEY,"
291-
"c1 int NULL DEFAULT 42,"
292-
"c2 int NOT NULL);"
293-
)
294-
with postgresql_con.cursor() as cursor:
295-
cursor.execute(create_table_sql)
296-
postgresql_con.commit()
297-
298288
df = pd.DataFrame({"c0": ["foo", "bar"], "c2": [1, 2]})
299289

300290
with pytest.raises(wr.exceptions.InvalidArgumentValue):
@@ -369,17 +359,6 @@ def test_upsert(postgresql_table, postgresql_con):
369359

370360

371361
def test_upsert_multiple_conflict_columns(postgresql_table, postgresql_con):
372-
create_table_sql = (
373-
f"CREATE TABLE public.{postgresql_table} "
374-
"(c0 varchar NULL PRIMARY KEY,"
375-
"c1 int NOT NULL,"
376-
"c2 int NOT NULL,"
377-
"UNIQUE (c1, c2));"
378-
)
379-
with postgresql_con.cursor() as cursor:
380-
cursor.execute(create_table_sql)
381-
postgresql_con.commit()
382-
383362
df = pd.DataFrame({"c0": ["foo", "bar"], "c1": [1, 2], "c2": [3, 4]})
384363
upsert_conflict_columns = ["c1", "c2"]
385364

@@ -437,16 +416,6 @@ def test_upsert_multiple_conflict_columns(postgresql_table, postgresql_con):
437416

438417

439418
def test_insert_ignore_duplicate_columns(postgresql_table, postgresql_con):
440-
create_table_sql = (
441-
f"CREATE TABLE public.{postgresql_table} "
442-
"(c0 varchar NULL PRIMARY KEY,"
443-
"c1 int NULL DEFAULT 42,"
444-
"c2 int NOT NULL);"
445-
)
446-
with postgresql_con.cursor() as cursor:
447-
cursor.execute(create_table_sql)
448-
postgresql_con.commit()
449-
450419
df = pd.DataFrame({"c0": ["foo", "bar"], "c2": [1, 2]})
451420

452421
wr.postgresql.to_sql(
@@ -501,17 +470,6 @@ def test_insert_ignore_duplicate_columns(postgresql_table, postgresql_con):
501470

502471

503472
def test_insert_ignore_duplicate_multiple_columns(postgresql_table, postgresql_con):
504-
create_table_sql = (
505-
f"CREATE TABLE public.{postgresql_table} "
506-
"(c0 varchar NULL PRIMARY KEY,"
507-
"c1 int NOT NULL,"
508-
"c2 int NOT NULL,"
509-
"UNIQUE (c1, c2));"
510-
)
511-
with postgresql_con.cursor() as cursor:
512-
cursor.execute(create_table_sql)
513-
postgresql_con.commit()
514-
515473
df = pd.DataFrame({"c0": ["foo", "bar"], "c1": [1, 2], "c2": [3, 4]})
516474
insert_conflict_columns = ["c1", "c2"]
517475

0 commit comments

Comments
 (0)