Skip to content

compat: sqlalchemy deprecations #40471

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

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
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
42 changes: 35 additions & 7 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
datetime,
time,
)
from distutils.version import LooseVersion
from functools import partial
import re
from typing import (
Expand Down Expand Up @@ -77,6 +78,16 @@ def _is_sqlalchemy_connectable(con):
return False


def _gt14() -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we have a test decorator for this type of thing right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a decorator to skip a test depending on the version, not to re-write the test

"""
Check if sqlalchemy.__version__ is at least 1.4.0, when several
deprecations were made.
"""
import sqlalchemy

return LooseVersion(sqlalchemy.__version__) >= LooseVersion("1.4.0")


def _convert_params(sql, params):
"""Convert SQL and params args to DBAPI2.0 compliant format."""
args = [sql]
Expand Down Expand Up @@ -823,7 +834,10 @@ def sql_schema(self):

def _execute_create(self):
# Inserting table into database, add to MetaData object
self.table = self.table.tometadata(self.pd_sql.meta)
if _gt14():
self.table = self.table.to_metadata(self.pd_sql.meta)
else:
self.table = self.table.tometadata(self.pd_sql.meta)
self.table.create()

def create(self):
Expand Down Expand Up @@ -1596,9 +1610,17 @@ def to_sql(
# Only check when name is not a number and name is not lower case
engine = self.connectable.engine
with self.connectable.connect() as conn:
table_names = engine.table_names(
schema=schema or self.meta.schema, connection=conn
)
if _gt14():
from sqlalchemy import inspect

insp = inspect(conn)
table_names = insp.get_table_names(
schema=schema or self.meta.schema
)
else:
table_names = engine.table_names(
schema=schema or self.meta.schema, connection=conn
)
if name not in table_names:
msg = (
f"The provided table name '{name}' is not found exactly as "
Expand All @@ -1613,9 +1635,15 @@ def tables(self):
return self.meta.tables

def has_table(self, name: str, schema: Optional[str] = None):
return self.connectable.run_callable(
self.connectable.dialect.has_table, name, schema or self.meta.schema
)
if _gt14():
import sqlalchemy as sa

insp = sa.inspect(self.connectable)
return insp.has_table(name, schema or self.meta.schema)
else:
return self.connectable.run_callable(
self.connectable.dialect.has_table, name, schema or self.meta.schema
)

def get_table(self, table_name: str, schema: Optional[str] = None):
schema = schema or self.meta.schema
Expand Down
24 changes: 19 additions & 5 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@

import pandas.io.sql as sql
from pandas.io.sql import (
_gt14,
read_sql_query,
read_sql_table,
)

try:
import sqlalchemy
from sqlalchemy import inspect
from sqlalchemy.ext import declarative
from sqlalchemy.orm import session as sa_session
import sqlalchemy.schema
Expand Down Expand Up @@ -1487,7 +1489,11 @@ def test_create_table(self):
pandasSQL = sql.SQLDatabase(temp_conn)
pandasSQL.to_sql(temp_frame, "temp_frame")

assert temp_conn.has_table("temp_frame")
if _gt14():
insp = inspect(temp_conn)
assert insp.has_table("temp_frame")
else:
assert temp_conn.has_table("temp_frame")

def test_drop_table(self):
temp_conn = self.connect()
Expand All @@ -1499,11 +1505,18 @@ def test_drop_table(self):
pandasSQL = sql.SQLDatabase(temp_conn)
pandasSQL.to_sql(temp_frame, "temp_frame")

assert temp_conn.has_table("temp_frame")
if _gt14():
insp = inspect(temp_conn)
assert insp.has_table("temp_frame")
else:
assert temp_conn.has_table("temp_frame")

pandasSQL.drop_table("temp_frame")

assert not temp_conn.has_table("temp_frame")
if _gt14():
assert not insp.has_table("temp_frame")
else:
assert not temp_conn.has_table("temp_frame")

def test_roundtrip(self):
self._roundtrip()
Expand Down Expand Up @@ -1843,9 +1856,10 @@ def test_nan_string(self):
tm.assert_frame_equal(result, df)

def _get_index_columns(self, tbl_name):
from sqlalchemy.engine import reflection
from sqlalchemy import inspect

insp = inspect(self.conn)

insp = reflection.Inspector.from_engine(self.conn)
ixs = insp.get_indexes(tbl_name)
ixs = [i["column_names"] for i in ixs]
return ixs
Expand Down