Skip to content

Commit f95ffb1

Browse files
authored
support for sqlalchemy 2.0 (catherinedevlin#219)
1 parent c4e978a commit f95ffb1

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

.github/workflows/ci.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ jobs:
4343
run: |
4444
pytest --durations-min=5 --ignore=src/tests/integration
4545
46+
test-sqlalchemy-v1:
47+
strategy:
48+
matrix:
49+
python-version: ['3.11']
50+
os: [ubuntu-latest, macos-latest, windows-latest]
51+
52+
runs-on: ${{ matrix.os }}
53+
54+
steps:
55+
- uses: actions/checkout@v2
56+
57+
- name: Set up Python ${{ matrix.python-version }}
58+
uses: actions/setup-python@v2
59+
with:
60+
python-version: ${{ matrix.python-version }}
61+
62+
- name: Lint with flake8
63+
run: |
64+
python -m pip install --upgrade pip
65+
# run flake8 on .py files
66+
pip install flake8
67+
flake8
68+
# run flake8 on notebooks (.ipynb, .md, etc)
69+
pip install jupytext nbqa
70+
nbqa flake8 .
71+
- name: Install dependencies
72+
run: |
73+
pip install "sqlalchemy<2"
74+
pip install ".[dev]"
75+
- name: Test with pytest
76+
run: |
77+
pytest --durations-min=5 --ignore=src/tests/integration
4678
4779
# run: pkgmt check
4880
check:

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## 0.6.3 (2023-03-06)
88

99
* [Fix] Displaying variable substitution warning only when the variable to expand exists in the user's namespace
10-
10+
* [Fix] Adds support for SQL Alchemy 2.0
1111
## 0.6.2 (2023-03-05)
1212

1313
* [Fix] Deprecation warning incorrectly displayed [#213](https://github.com/ploomber/jupysql/issues/213)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
install_requires = [
1919
"prettytable",
2020
"ipython>=1.0",
21-
"sqlalchemy>=0.6.7,<2.0",
21+
"sqlalchemy",
2222
"sqlparse",
2323
"ipython-genutils>=0.1.0",
2424
"jinja2",

src/sql/connection.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"For technical support: https://ploomber.io/community"
1212
"\nDocumentation: https://jupysql.ploomber.io/en/latest/connecting.html"
1313
)
14+
IS_SQLALCHEMY_ONE = int(sqlalchemy.__version__.split(".")[0]) == 1
1415

1516
# Check Full List: https://docs.sqlalchemy.org/en/20/dialects
1617
MISSING_PACKAGE_LIST_EXCEPT_MATCHERS = {
@@ -193,11 +194,23 @@ def _error_module_not_found(cls, e):
193194
return ModuleNotFoundError("test")
194195

195196
def __init__(self, engine, alias=None):
196-
self.dialect = engine.url.get_dialect()
197-
self.metadata = sqlalchemy.MetaData(bind=engine)
197+
self.url = engine.url
198198
self.name = self.assign_name(engine)
199+
self.dialect = self.url.get_dialect()
199200
self.session = engine.connect()
200-
self.connections[alias or repr(self.metadata.bind.url)] = self
201+
202+
if IS_SQLALCHEMY_ONE:
203+
self.metadata = sqlalchemy.MetaData(bind=engine)
204+
205+
self.connections[
206+
alias
207+
or (
208+
repr(sqlalchemy.MetaData(bind=engine).bind.url)
209+
if IS_SQLALCHEMY_ONE
210+
else repr(engine.url)
211+
)
212+
] = self
213+
201214
self.connect_args = None
202215
self.alias = alias
203216
Connection.current = self
@@ -298,7 +311,7 @@ def connection_list(cls):
298311
result = []
299312
for key in sorted(cls.connections):
300313
conn = cls.connections[key]
301-
engine_url = conn.metadata.bind.url # type: sqlalchemy.engine.url.URL
314+
engine_url = conn.metadata.bind.url if IS_SQLALCHEMY_ONE else conn.url
302315

303316
prefix = "* " if conn == cls.current else " "
304317

@@ -312,7 +325,7 @@ def connection_list(cls):
312325
return "\n".join(result)
313326

314327
@classmethod
315-
def _close(cls, descriptor):
328+
def close(cls, descriptor):
316329
if isinstance(descriptor, Connection):
317330
conn = descriptor
318331
else:
@@ -328,20 +341,18 @@ def _close(cls, descriptor):
328341
if descriptor in cls.connections:
329342
cls.connections.pop(descriptor)
330343
else:
331-
cls.connections.pop(str(conn.metadata.bind.url))
332-
333-
conn.session.close()
334-
335-
def close(self):
336-
self.__class__._close(self)
344+
cls.connections.pop(
345+
str(conn.metadata.bind.url) if IS_SQLALCHEMY_ONE else str(conn.url)
346+
)
347+
conn.session.close()
337348

338349
@classmethod
339350
def _get_curr_connection_info(cls):
340351
"""Returns the dialect, driver, and database server version info"""
341352
if not cls.current:
342353
return None
343354

344-
engine = cls.current.metadata.bind
355+
engine = cls.current.metadata.bind if IS_SQLALCHEMY_ONE else cls.current
345356
return {
346357
"dialect": getattr(engine.dialect, "name", None),
347358
"driver": getattr(engine.dialect, "driver", None),

src/sql/magic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _execute(self, payload, line, cell, local_ns):
266266
if args.connections:
267267
return sql.connection.Connection.connections
268268
elif args.close:
269-
return sql.connection.Connection._close(args.close)
269+
return sql.connection.Connection.close(args.close)
270270

271271
connect_arg = command.connection
272272

tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def setup(c, version=None, doc=False):
3232

3333
@task(aliases=["d"])
3434
def doc(c):
35-
with c.cd('doc'):
35+
with c.cd("doc"):
3636
c.run(
3737
"python3 -m sphinx -T -E -W --keep-going -b html \
3838
-d _build/doctrees -D language=en . _build/html"

0 commit comments

Comments
 (0)