Skip to content

Commit a01325a

Browse files
authored
Refactoring/618 resolve remaining deprecation warnings (#619)
* Activate all warnings again (except for ignores) * Resolve failing tests where DML used and needs engine.begin() * Modify documentation for related changes
1 parent 2e513c8 commit a01325a

File tree

6 files changed

+68
-58
lines changed

6 files changed

+68
-58
lines changed

README.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ Using SQLAlchemy with EXASOL DB
9595

9696
.. code-block:: python
9797
98-
from sqlalchemy import create_engine
99-
url = "exa+websocket://A_USER:[email protected]:1234/my_schema?CONNECTIONLCALL=en_US.UTF-8"
98+
from sqlalchemy import create_engine
99+
url = "exa+websocket://A_USER:[email protected]:1234/my_schema?CONNECTIONLCALL=en_US.UTF-8"
100100
e = create_engine(url)
101101
query = "select 42 from dual"
102102
with engine.connect() as con:
@@ -109,6 +109,12 @@ Examples:
109109
from sqlalchemy import create_engine
110110
111111
engine = create_engine("exa+websocket://sys:[email protected]:8888")
112+
113+
# don't rely on autocommit for DML and DDL
114+
with engine.begin() as con:
115+
...
116+
117+
# for non-DML or non-DDL queries
112118
with engine.connect() as con:
113119
...
114120
@@ -132,8 +138,8 @@ Examples:
132138

133139
.. code-block:: python
134140
135-
from sqlalchemy import create_engine
136-
url = "exa+pyodbc://A_USER:[email protected]:1234/my_schema?CONNECTIONLCALL=en_US.UTF-8&driver=EXAODBC"
141+
from sqlalchemy import create_engine
142+
url = "exa+pyodbc://A_USER:[email protected]:1234/my_schema?CONNECTIONLCALL=en_US.UTF-8&driver=EXAODBC"
137143
e = create_engine(url)
138144
query = "select 42 from dual"
139145
with engine.connect() as con:
@@ -143,8 +149,8 @@ Examples:
143149

144150
.. code-block:: python
145151
146-
from sqlalchemy import create_engine
147-
url = "exa+turbodbc://A_USER:[email protected]:1234/my_schema?CONNECTIONLCALL=en_US.UTF-8&driver=EXAODBC"
152+
from sqlalchemy import create_engine
153+
url = "exa+turbodbc://A_USER:[email protected]:1234/my_schema?CONNECTIONLCALL=en_US.UTF-8&driver=EXAODBC"
148154
e = create_engine(url)
149155
query = "select 42 from dual"
150156
with engine.connect() as con:
@@ -166,7 +172,7 @@ General Notes
166172

167173
.. _developer guide: https://github.com/exasol/sqlalchemy-exasol/blob/master/doc/developer_guide/developer_guide.rst
168174
.. _odbc_driver: https://docs.exasol.com/db/latest/connect_exasol/drivers/odbc/odbc_linux.htm
169-
.. _test_drive: https://cloud.exasol.com/signup
175+
.. _test_drive: https://cloud.exasol.com/signup
170176
.. _test_docker_image: https://github.com/exasol/docker-db
171177

172178
Known Issues
@@ -177,5 +183,3 @@ Known Issues
177183
Development & Testing
178184
---------------------
179185
See `developer guide`_
180-
181-

doc/changes/unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This allows us to use the latest dependencies, which do not have open vulnerabil
1313
- #614: Altered params input into `Connection.execute()` to be handled properly with `dict`
1414
- #616: Altered usage of MetaData which was binding to a connection to instead bind in the needed object or function
1515
- #617: Enacted warning for the deprecation of the `autoload` parameter and requirement of `bind`
16+
- #618: Switched DML & DDL executions from `engine.connect()` to `engine.begin()` usage
1617

1718
## Internal
1819

pyproject.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,8 @@ exasol-toolbox = "^1.9.0"
9292
[tool.pytest.ini_options]
9393
addopts = "--tb native -v -r fxX"
9494
filterwarnings = [
95-
# before end of #413, switch this from a text specific issue to a general error on a warning to ensure known deprecation issues have been resolved
96-
"error:.*Passing a string.*",
97-
"error:.* a single dictionary.*",
98-
"error:.*The MetaData.bind argument.*",
99-
"error:.*The autoload parameter.*",
100-
"error:.*schema methods that invoke SQL against.*",
95+
"error",
96+
"ignore::pytest.PytestUnraisableExceptionWarning",
10197
# this is used for turbodbc and pyodbc as historically we'd like to remove them.
10298
# thus far, it seems like this is not a strict requirement for the migration,
10399
# so we will ignore them.

test/integration/exasol/test_exasol.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,27 @@ def define_tables(cls, metadata):
4747
def test_insert_with_default_value(self):
4848
t = self.tables.t
4949

50-
config.db.execute(t.insert(), [{"name": "Henrik"}])
51-
(_, _, active_from) = config.db.execute(t.select()).fetchone()
50+
with config.db.begin() as conn:
51+
conn.execute(t.insert(), [{"name": "Henrik"}])
52+
53+
with config.db.connect() as conn:
54+
(_, _, active_from) = conn.execute(t.select()).fetchone()
55+
5256
assert active_from == datetime.date(1900, 1, 1)
5357

5458

5559
class KeywordTest(fixtures.TablesTest):
5660
__backend__ = True
5761

5862
def test_keywords(self):
59-
keywords = config.db.execute(
60-
sql.text(
61-
"select distinct(lower(keyword)) as keyword "
62-
+ "from SYS.EXA_SQL_KEYWORDS where reserved = True order by keyword"
63-
)
64-
).fetchall()
63+
with config.db.connect() as conn:
64+
keywords = conn.execute(
65+
sql.text(
66+
"select distinct(lower(keyword)) as keyword "
67+
+ "from SYS.EXA_SQL_KEYWORDS where reserved = True order by keyword"
68+
)
69+
).fetchall()
70+
6571
db_keywords = {k[0] for k in keywords}
6672

6773
assert db_keywords >= RESERVED_WORDS
@@ -108,13 +114,15 @@ def test_alter_table_distribute_by(self):
108114
dbc = DistributeByConstraint("a", "b")
109115
self.tables.t.append_constraint(dbc)
110116

111-
config.db.execute(DropConstraint(dbc))
117+
with config.db.begin() as conn:
118+
conn.execute(DropConstraint(dbc))
112119

113120
insp = inspect(testing.db)
114121
for c in insp.get_columns("t"):
115122
assert c["is_distribution_key"] == False
116123

117-
config.db.execute(AddConstraint(dbc))
124+
with config.db.begin() as conn:
125+
conn.execute(AddConstraint(dbc))
118126

119127
insp = inspect(testing.db)
120128
for c in insp.get_columns("t"):

test/integration/exasol/test_regression.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class TranslateMap(fixtures.TestBase):
3131
def setup_class(cls):
3232
cls.table_name = "my_table"
3333
cls.tenant_schema_name = "tenant_schema"
34-
engine = config.db
35-
with config.db.connect() as conn:
34+
with config.db.begin() as conn:
3635
conn.execute(CreateSchema(cls.tenant_schema_name))
3736
metadata = MetaData()
3837
Table(
@@ -42,11 +41,11 @@ def setup_class(cls):
4241
Column("name", String(1000), nullable=False),
4342
schema=cls.tenant_schema_name,
4443
)
45-
metadata.create_all(engine)
44+
metadata.create_all(conn)
4645

4746
@classmethod
4847
def teardown_class(cls):
49-
with config.db.connect() as conn:
48+
with config.db.begin() as conn:
5049
conn.execute(DropSchema(cls.tenant_schema_name, cascade=True))
5150

5251
def test_use_schema_translate_map_in_get_last_row_id(self):
@@ -62,7 +61,7 @@ def test_use_schema_translate_map_in_get_last_row_id(self):
6261
schema=schema_name,
6362
)
6463
engine = create_engine(config.db.url, execution_options=options)
65-
with engine.connect() as conn:
64+
with engine.begin() as conn:
6665
conn.execute(my_table.insert().values(name="John Doe"))
6766

6867

@@ -80,8 +79,7 @@ class Introspection(fixtures.TestBase):
8079
@classmethod
8180
def setup_class(cls):
8281
def _create_tables(schema, tables):
83-
engine = config.db
84-
with engine.connect():
82+
with config.db.begin() as conn:
8583
metadata = MetaData()
8684
for name in tables:
8785
Table(
@@ -91,11 +89,10 @@ def _create_tables(schema, tables):
9189
Column("random_field", String(1000)),
9290
schema=schema,
9391
)
94-
metadata.create_all(engine)
92+
metadata.create_all(conn)
9593

9694
def _create_views(schema, views):
97-
engine = config.db
98-
with engine.connect() as conn:
95+
with config.db.begin() as conn:
9996
for name in views:
10097
conn.execute(
10198
sql.text(
@@ -112,17 +109,15 @@ def _create_views(schema, views):
112109

113110
@classmethod
114111
def teardown_class(cls):
115-
engine = config.db
116-
117112
def _drop_tables(schema):
118113
metadata = MetaData(schema=schema)
119-
with engine.connect() as conn:
114+
with config.db.begin() as conn:
120115
metadata.reflect(bind=conn)
121116
to_be_deleted = [metadata.tables[name] for name in metadata.tables]
122-
metadata.drop_all(engine, to_be_deleted)
117+
metadata.drop_all(conn, to_be_deleted)
123118

124119
def _drop_views(schema, views):
125-
with engine.connect() as conn:
120+
with config.db.begin() as conn:
126121
for name in views:
127122
conn.execute(sql.text(f"DROP VIEW {schema}.{name};"))
128123

@@ -143,4 +138,4 @@ def test_introspection_of_views_works_with(self, pool_type):
143138
engine = create_engine(config.db.url, poolclass=pool_type)
144139
inspector = inspect(engine)
145140
tables = inspector.get_view_names(schema=self.schema)
146-
assert expected == tables
141+
assert tables == expected

test/integration/exasol/test_update.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
22
from sqlalchemy import *
3-
from sqlalchemy import testing
43
from sqlalchemy.testing import (
54
eq_,
65
fixtures,
76
)
7+
from sqlalchemy.testing.fixtures import config
88
from sqlalchemy.testing.schema import (
99
Column,
1010
Table,
@@ -82,30 +82,33 @@ def fixtures(cls):
8282

8383

8484
@pytest.mark.skipif(
85-
testing.db.dialect.driver == "turbodbc", reason="not supported by turbodbc"
85+
config.db.dialect.driver == "turbodbc", reason="not supported by turbodbc"
8686
)
8787
class UpdateTest(_UpdateTestBase, fixtures.TablesTest):
8888
__backend__ = True
8989

9090
def test_update_simple(self):
9191
"""test simple update and assert that exasol returns the right rowcount"""
9292
users = self.tables[f"{self.schema}.users"]
93-
result = testing.db.execute(
94-
users.update().values(name="peter").where(users.c.id == 10)
95-
)
96-
expected = [(7, "jack"), (8, "ed"), (9, "fred"), (10, "peter")]
93+
94+
with config.db.begin() as conn:
95+
result = conn.execute(
96+
users.update().values(name="peter").where(users.c.id == 10)
97+
)
98+
9799
assert result.rowcount == 1
98-
self._assert_users(users, expected)
100+
self._assert_users(users, [(7, "jack"), (8, "ed"), (9, "fred"), (10, "peter")])
99101

100102
def test_update_simple_multiple_rows_rowcount(self):
101103
"""test simple update and assert that exasol returns the right rowcount"""
102104
users = self.tables[f"{self.schema}.users"]
103-
result = testing.db.execute(
104-
users.update().values(name="peter").where(users.c.id >= 9)
105-
)
106-
expected = [(7, "jack"), (8, "ed"), (9, "peter"), (10, "peter")]
105+
106+
with config.db.begin() as conn:
107+
result = conn.execute(
108+
users.update().values(name="peter").where(users.c.id >= 9)
109+
)
107110
assert result.rowcount == 2
108-
self._assert_users(users, expected)
111+
self._assert_users(users, [(7, "jack"), (8, "ed"), (9, "peter"), (10, "peter")])
109112

110113
def test_update_executemany(self):
111114
"""test that update with executemany work as well, but rowcount
@@ -123,8 +126,8 @@ def test_update_executemany(self):
123126
{"oldname": "fred", "newname": "fred2"},
124127
]
125128

126-
result = testing.db.execute(stmt, values)
127-
expected = [(7, "jack2"), (8, "ed"), (9, "fred2"), (10, "chuck")]
129+
with config.db.begin() as conn:
130+
result = conn.execute(stmt, values)
128131

129132
# Depending on the dialect it either reports that the affected rows information
130133
# is not available (-1) or it reports the actual number of updated/affected rows(2)
@@ -133,12 +136,15 @@ def test_update_executemany(self):
133136
expected_rowcount = [expected_rowcount_odbc, expected_rowcount_wss]
134137
assert result.rowcount in expected_rowcount
135138

136-
self._assert_users(users, expected)
139+
self._assert_users(
140+
users, [(7, "jack2"), (8, "ed"), (9, "fred2"), (10, "chuck")]
141+
)
137142

138143
def _assert_addresses(self, addresses, expected):
139144
stmt = addresses.select().order_by(addresses.c.id)
140-
eq_(testing.db.execute(stmt).fetchall(), expected)
145+
eq_(config.db.execute(stmt).fetchall(), expected)
141146

142147
def _assert_users(self, users, expected):
143148
stmt = users.select().order_by(users.c.id)
144-
eq_(testing.db.execute(stmt).fetchall(), expected)
149+
with config.db.connect() as conn:
150+
eq_(conn.execute(stmt).fetchall(), expected)

0 commit comments

Comments
 (0)