Skip to content

Commit dcc8353

Browse files
authored
[Chore] Remove SQLA 2.0 deprecations (#76)
1 parent 9171631 commit dcc8353

File tree

9 files changed

+66
-38
lines changed

9 files changed

+66
-38
lines changed

conftest.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
import pytest
55
import sqlalchemy as sa
6-
from sqlalchemy import create_engine
7-
from sqlalchemy.ext.declarative import declarative_base
8-
from sqlalchemy.orm import sessionmaker
6+
from sqlalchemy import create_engine, text
7+
from sqlalchemy.orm import close_all_sessions, declarative_base, sessionmaker
98

109
from postgresql_audit import VersioningManager
1110

@@ -45,10 +44,12 @@ def engine(dns):
4544

4645
@pytest.fixture
4746
def connection(engine):
48-
conn = engine.connect()
49-
conn.execute('CREATE EXTENSION IF NOT EXISTS btree_gist')
50-
yield conn
51-
conn.close()
47+
with engine.connect() as conn:
48+
with conn.begin():
49+
conn.execute(text('CREATE EXTENSION IF NOT EXISTS btree_gist'))
50+
51+
yield conn
52+
conn.close()
5253

5354

5455
@pytest.fixture
@@ -57,7 +58,7 @@ def session(connection):
5758
session = Session()
5859
yield session
5960
session.expunge_all()
60-
session.close_all()
61+
close_all_sessions()
6162

6263

6364
@pytest.fixture

postgresql_audit/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from weakref import WeakSet
66

77
import sqlalchemy as sa
8-
from sqlalchemy import orm
8+
from sqlalchemy import orm, text
99
from sqlalchemy.dialects.postgresql import (
1010
array,
1111
ExcludeConstraint,
@@ -205,13 +205,17 @@ def get_transaction_values(self):
205205
@contextmanager
206206
def disable(self, session):
207207
session.execute(
208-
"SET LOCAL postgresql_audit.enable_versioning = 'false'"
208+
text(
209+
"SET LOCAL postgresql_audit.enable_versioning = 'false'"
210+
)
209211
)
210212
try:
211213
yield
212214
finally:
213215
session.execute(
214-
"SET LOCAL postgresql_audit.enable_versioning = 'true'"
216+
text(
217+
"SET LOCAL postgresql_audit.enable_versioning = 'true'"
218+
)
215219
)
216220

217221
def render_tmpl(self, tmpl_name):
@@ -299,7 +303,7 @@ def audit_table(self, table, exclude_columns=None):
299303
func = sa.func.audit_table
300304
else:
301305
func = getattr(getattr(sa.func, self.schema_name), 'audit_table')
302-
query = sa.select([func(*args)])
306+
query = sa.select(func(*args))
303307
if query not in cached_statements:
304308
cached_statements[query] = StatementExecutor(query)
305309
listener = (table, 'after_create', cached_statements[query])

postgresql_audit/expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class jsonb_change_key_name(expression.FunctionElement):
1515
1616
1717
data = {'key1': 1, 'key3': 4}
18-
query = sa.select([jsonb_merge(data, 'key1', 'key2')])
18+
query = sa.select(jsonb_merge(data, 'key1', 'key2'))
1919
session.execute(query).scalar() # {'key2': 1, 'key3': 4}
2020
"""
2121
type = JSONB()

tests/test_custom_schema.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ def table_creator(
3737
schema_name
3838
):
3939
sa.orm.configure_mappers()
40-
connection.execute('DROP SCHEMA IF EXISTS {} CASCADE'.format(schema_name))
41-
tx = connection.begin()
42-
versioning_manager.transaction_cls.__table__.create(connection)
43-
versioning_manager.activity_cls.__table__.create(connection)
44-
base.metadata.create_all(connection)
45-
tx.commit()
40+
with connection.begin():
41+
connection.execute(
42+
sa.text(
43+
'DROP SCHEMA IF EXISTS {} CASCADE'.format(schema_name)
44+
)
45+
)
46+
versioning_manager.transaction_cls.__table__.create(connection)
47+
versioning_manager.activity_cls.__table__.create(connection)
48+
base.metadata.create_all(connection)
49+
4650
yield
4751
session.expunge_all()
4852
base.metadata.drop_all(connection)

tests/test_json_functions.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ class TestJSONBChangeKeyName(object):
3131
)
3232
def test_raw_sql(self, session, data, old_key, new_key, expected):
3333
result = session.execute(
34-
'''SELECT jsonb_change_key_name(
35-
'{data}'::jsonb,
36-
'{old_key}',
37-
'{new_key}'
38-
)'''.format(
39-
data=data,
40-
old_key=old_key,
41-
new_key=new_key
34+
sa.text(
35+
'''SELECT jsonb_change_key_name(
36+
'{data}'::jsonb,
37+
'{old_key}',
38+
'{new_key}'
39+
)'''.format(
40+
data=data,
41+
old_key=old_key,
42+
new_key=new_key
43+
)
4244
)
4345
).scalar()
4446
assert result == expected
@@ -75,7 +77,7 @@ def test_sqlalchemy_function_expr(
7577
expected
7678
):
7779
result = session.execute(
78-
sa.select([jsonb_change_key_name(data, old_key, new_key)])
80+
sa.select(jsonb_change_key_name(data, old_key, new_key))
7981
).scalar()
8082
assert result == expected
8183

@@ -104,9 +106,11 @@ class TestJSONBConcatOperator(object):
104106
)
105107
def test_raw_sql(self, session, data, merge_data, expected):
106108
result = session.execute(
107-
'''SELECT '{data}'::jsonb || '{merge_data}'::jsonb'''.format(
108-
data=data,
109-
merge_data=merge_data
109+
sa.text(
110+
'''SELECT '{data}'::jsonb || '{merge_data}'::jsonb'''.format(
111+
data=data,
112+
merge_data=merge_data
113+
)
110114
)
111115
).scalar()
112116
assert result == expected

tests/test_operators.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import pytest
33

4+
from sqlalchemy import text
5+
46

57
@pytest.mark.parametrize(
68
('old', 'new', 'result'),
@@ -23,7 +25,9 @@
2325
)
2426
def test_jsonb_subtract(table_creator, session, old, new, result):
2527
assert session.execute(
26-
'SELECT (:new)::jsonb - (:old)::jsonb',
28+
text(
29+
'SELECT (:new)::jsonb - (:old)::jsonb'
30+
),
2731
dict(old=json.dumps(old), new=json.dumps(new))
2832
).scalar() == result
2933

@@ -47,6 +51,8 @@ def test_jsonb_subtract(table_creator, session, old, new, result):
4751
)
4852
def test_jsonb_subtract_text_array(table_creator, session, old, new, result):
4953
assert session.execute(
50-
'SELECT (:new)::jsonb - (:old)::text[]',
54+
text(
55+
'SELECT (:new)::jsonb - (:old)::text[]'
56+
),
5157
dict(old=old, new=json.dumps(new))
5258
).scalar() == result

tests/test_sql_files.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import pytest
33

4+
from sqlalchemy import text
5+
46
from .utils import last_activity
57

68

@@ -9,8 +11,10 @@ class TestActivityCreationWithColumnExclusion(object):
911
@pytest.fixture
1012
def audit_trigger_creator(self, session, user_class):
1113
session.execute(
12-
'''SELECT audit_table('{0}', '{{"age"}}')'''.format(
13-
user_class.__tablename__
14+
text(
15+
'''SELECT audit_table('{0}', '{{"age"}}')'''.format(
16+
user_class.__tablename__
17+
)
1418
)
1519
)
1620

tests/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
from sqlalchemy import text
2+
3+
14
def last_activity(connection, schema=None):
25
if schema is not None:
36
schema_prefix = '{}.'.format(schema)
47
else:
58
schema_prefix = ''
69
return dict(
710
connection.execute(
8-
'SELECT * FROM {}activity ORDER BY issued_at '
9-
'DESC LIMIT 1'.format(schema_prefix)
11+
text(
12+
'SELECT * FROM {}activity ORDER BY issued_at '
13+
'DESC LIMIT 1'.format(schema_prefix)
14+
)
1015
).fetchone()
1116
)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tox (http://tox.testrun.org/) is a tool for running tests
1+
# Tox (https://tox.wiki/) is a tool for running tests
22
# in multiple virtualenvs. This configuration file will run the
33
# test suite on all supported python versions. To use it, "pip install tox"
44
# and then run "tox" from this directory.

0 commit comments

Comments
 (0)