Skip to content

Commit

Permalink
57539: Fixed creation unnamed table when using public schema
Browse files Browse the repository at this point in the history
Problem:
- Table on public schema being lost when tried to be created

Solution:
- Used db_schema_name argument to specify schema name of adbc_ingest
  • Loading branch information
shabab477 committed Mar 24, 2024
1 parent e51039a commit 1e206b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,9 @@ def to_sql(
raise ValueError("datatypes not supported") from exc

with self.con.cursor() as cur:
total_inserted = cur.adbc_ingest(table_name, tbl, mode=mode)
total_inserted = cur.adbc_ingest(
table_name=name, data=tbl, mode=mode, db_schema_name=schema
)

self.con.commit()
return total_inserted
Expand Down
42 changes: 41 additions & 1 deletion pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,6 @@ def test_read_procedure(conn, request):
res2 = sql.read_sql("CALL get_testdb();", conn)
tm.assert_frame_equal(df, res2)


@pytest.mark.parametrize("conn", postgresql_connectable)
@pytest.mark.parametrize("expected_count", [2, "Success!"])
def test_copy_from_callable_insertion_method(conn, expected_count, request):
Expand Down Expand Up @@ -1373,6 +1372,47 @@ def insert_on_conflict(table, conn, keys, data_iter):
pandasSQL.drop_table("test_insert_conflict")


@pytest.mark.parametrize("conn", postgresql_connectable)
def test_to_sql_on_public_schema_postgres(conn, request):
# GH 15988: Example in to_sql docstring
conn = request.getfixturevalue(conn)

from sqlalchemy.engine import Engine
from sqlalchemy.sql import text

create_sql = text(
"""
CREATE TABLE test_public_schema (
a integer PRIMARY KEY,
b numeric,
c text
);
"""
)
if isinstance(conn, Engine):
with conn.connect() as con:
with con.begin():
con.execute(create_sql)
else:
with conn.begin():
conn.execute(create_sql)

test_data = DataFrame([[1, 2.1, "a"], [2, 3.1, "b"]], columns=list("abc"))
test_data.to_sql(
name="test_public_schema",
con=conn,
if_exists="append",
index=False,
schema="public"
)

df_out = sql.read_sql_table("test_public_schema", conn, schema="public")
assert test_data.equals(df_out)

# Cleanup
with sql.SQLDatabase(conn, need_transaction=True) as pandasSQL:
pandasSQL.drop_table("test_public_schema")

@pytest.mark.parametrize("conn", mysql_connectable)
def test_insertion_method_on_conflict_update(conn, request):
# GH 14553: Example in to_sql docstring
Expand Down

0 comments on commit 1e206b0

Please sign in to comment.