Skip to content

Commit da5b694

Browse files
authored
Address deprecation warnings from SQLAlchemy v1.4 (#107)
* Remove warning suppression * Show all SQLA 2.x warnings * Add compatibility layer for legacy SQLA versions * Address warnings * Add copyright notice * Refactor version detection logic
1 parent f442683 commit da5b694

File tree

7 files changed

+166
-156
lines changed

7 files changed

+166
-156
lines changed

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test(session, sqlalchemy):
110110
session.install(f"sqlalchemy~={sqlalchemy}.0")
111111
session.install("-e", ".")
112112
pytest_args = session.posargs or ["--pyargs", "sqlalchemy_mptt"]
113-
session.run("pytest", *pytest_args, env={"SQLALCHEMY_SILENCE_UBER_WARNING": "1"})
113+
session.run("pytest", *pytest_args, env={"SQLALCHEMY_WARN_20": "1"})
114114

115115

116116
@nox.session(default=False)

sqlalchemy_mptt/events.py

Lines changed: 84 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# vim:fenc=utf-8
44
#
55
# Copyright © 2014 uralbash <root@uralbash.ru>
6+
# Copyright (c) 2025 Fayaz Yusuf Khan <fayaz.yusuf.khan@gmail.com>
67
#
78
# Distributed under terms of the MIT license.
89

@@ -13,11 +14,13 @@
1314
import weakref
1415

1516
# SQLAlchemy
16-
from sqlalchemy import and_, case, event, select, inspection
17+
from sqlalchemy import and_, event, inspection
1718
from sqlalchemy.orm import object_session
1819
from sqlalchemy.sql import func
1920
from sqlalchemy.orm.base import NO_VALUE
2021

22+
from sqlalchemy_mptt.sqlalchemy_compat import compat_layer
23+
2124

2225
def _insert_subtree(
2326
table,
@@ -41,9 +44,9 @@ def _insert_subtree(
4144
delta_rgt = delta_lft + node_size - 1
4245

4346
connection.execute(
44-
table.update(
45-
table_pk.in_(subtree)
46-
).values(
47+
table.update()
48+
.where(table_pk.in_(subtree))
49+
.values(
4750
lft=table.c.lft - node_pos_left + delta_lft,
4851
rgt=table.c.rgt - node_pos_right + delta_rgt,
4952
level=table.c.level - node_level + parent_level + 1,
@@ -53,21 +56,14 @@ def _insert_subtree(
5356

5457
# step 2: update key of right side
5558
connection.execute(
56-
table.update(
57-
and_(
58-
table.c.rgt > delta_lft - 1,
59-
table_pk.notin_(subtree),
60-
table.c.tree_id == parent_tree_id
61-
)
62-
).values(
59+
table.update()
60+
.where(table.c.rgt > delta_lft - 1)
61+
.where(table_pk.notin_(subtree))
62+
.where(table.c.tree_id == parent_tree_id)
63+
.values(
6364
rgt=table.c.rgt + node_size,
64-
lft=case(
65-
[
66-
(
67-
table.c.lft > left_sibling['lft'],
68-
table.c.lft + node_size
69-
)
70-
],
65+
lft=compat_layer.case(
66+
(table.c.lft > left_sibling['lft'], table.c.lft + node_size),
7167
else_=table.c.lft
7268
)
7369
)
@@ -93,10 +89,8 @@ def mptt_before_insert(mapper, connection, instance):
9389
instance.right = 2
9490
instance.level = instance.get_default_level()
9591
tree_id = connection.scalar(
96-
select(
97-
[
98-
func.max(table.c.tree_id) + 1
99-
]
92+
compat_layer.select(
93+
func.max(table.c.tree_id) + 1
10094
)
10195
) or 1
10296
instance.tree_id = tree_id
@@ -105,40 +99,28 @@ def mptt_before_insert(mapper, connection, instance):
10599
parent_pos_right,
106100
parent_tree_id,
107101
parent_level) = connection.execute(
108-
select(
109-
[
110-
table.c.lft,
111-
table.c.rgt,
112-
table.c.tree_id,
113-
table.c.level
114-
]
102+
compat_layer.select(
103+
table.c.lft,
104+
table.c.rgt,
105+
table.c.tree_id,
106+
table.c.level
115107
).where(
116108
table_pk == instance.parent_id
117109
)
118110
).fetchone()
119111

120112
# Update key of right side
121113
connection.execute(
122-
table.update(
123-
and_(table.c.rgt >= parent_pos_right,
124-
table.c.tree_id == parent_tree_id)
125-
).values(
126-
lft=case(
127-
[
128-
(
129-
table.c.lft > parent_pos_right,
130-
table.c.lft + 2
131-
)
132-
],
114+
table.update()
115+
.where(table.c.rgt >= parent_pos_right)
116+
.where(table.c.tree_id == parent_tree_id)
117+
.values(
118+
lft=compat_layer.case(
119+
(table.c.lft > parent_pos_right, table.c.lft + 2),
133120
else_=table.c.lft
134121
),
135-
rgt=case(
136-
[
137-
(
138-
table.c.rgt >= parent_pos_right,
139-
table.c.rgt + 2
140-
)
141-
],
122+
rgt=compat_layer.case(
123+
(table.c.rgt >= parent_pos_right, table.c.rgt + 2),
142124
else_=table.c.rgt
143125
)
144126
)
@@ -157,11 +139,9 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
157139
db_pk = instance.get_pk_column()
158140
table_pk = getattr(table.c, db_pk.name)
159141
lft, rgt = connection.execute(
160-
select(
161-
[
162-
table.c.lft,
163-
table.c.rgt
164-
]
142+
compat_layer.select(
143+
table.c.lft,
144+
table.c.rgt
165145
).where(
166146
table_pk == pk
167147
)
@@ -171,7 +151,7 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
171151
if delete:
172152
mapper.base_mapper.confirm_deleted_rows = False
173153
connection.execute(
174-
table.delete(
154+
table.delete().where(
175155
table_pk == pk
176156
)
177157
)
@@ -190,28 +170,16 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
190170
END
191171
"""
192172
connection.execute(
193-
table.update(
194-
and_(
195-
table.c.rgt > rgt,
196-
table.c.tree_id == tree_id
197-
)
198-
).values(
199-
lft=case(
200-
[
201-
(
202-
table.c.lft > lft,
203-
table.c.lft - delta
204-
)
205-
],
173+
table.update()
174+
.where(table.c.rgt > rgt)
175+
.where(table.c.tree_id == tree_id)
176+
.values(
177+
lft=compat_layer.case(
178+
(table.c.lft > lft, table.c.lft - delta),
206179
else_=table.c.lft
207180
),
208-
rgt=case(
209-
[
210-
(
211-
table.c.rgt >= rgt,
212-
table.c.rgt - delta
213-
)
214-
],
181+
rgt=compat_layer.case(
182+
(table.c.rgt >= rgt, table.c.rgt - delta),
215183
else_=table.c.rgt
216184
)
217185
)
@@ -242,26 +210,22 @@ def mptt_before_update(mapper, connection, instance):
242210
right_sibling_level,
243211
right_sibling_tree_id
244212
) = connection.execute(
245-
select(
246-
[
247-
table.c.lft,
248-
table.c.rgt,
249-
table.c.parent_id,
250-
table.c.level,
251-
table.c.tree_id
252-
]
213+
compat_layer.select(
214+
table.c.lft,
215+
table.c.rgt,
216+
table.c.parent_id,
217+
table.c.level,
218+
table.c.tree_id
253219
).where(
254220
table_pk == instance.mptt_move_before
255221
)
256222
).fetchone()
257223
current_lvl_nodes = connection.execute(
258-
select(
259-
[
260-
table.c.lft,
261-
table.c.rgt,
262-
table.c.parent_id,
263-
table.c.tree_id
264-
]
224+
compat_layer.select(
225+
table.c.lft,
226+
table.c.rgt,
227+
table.c.parent_id,
228+
table.c.tree_id
265229
).where(
266230
and_(
267231
table.c.level == right_sibling_level,
@@ -295,13 +259,11 @@ def mptt_before_update(mapper, connection, instance):
295259
left_sibling_parent,
296260
left_sibling_tree_id
297261
) = connection.execute(
298-
select(
299-
[
300-
table.c.lft,
301-
table.c.rgt,
302-
table.c.parent_id,
303-
table.c.tree_id
304-
]
262+
compat_layer.select(
263+
table.c.lft,
264+
table.c.rgt,
265+
table.c.parent_id,
266+
table.c.tree_id
305267
).where(
306268
table_pk == instance.mptt_move_after
307269
)
@@ -320,7 +282,7 @@ def mptt_before_update(mapper, connection, instance):
320282
ORDER BY left_key
321283
"""
322284
subtree = connection.execute(
323-
select([table_pk])
285+
compat_layer.select(table_pk)
324286
.where(
325287
and_(
326288
table.c.lft >= instance.left,
@@ -344,14 +306,12 @@ def mptt_before_update(mapper, connection, instance):
344306
node_parent_id,
345307
node_level
346308
) = connection.execute(
347-
select(
348-
[
349-
table.c.lft,
350-
table.c.rgt,
351-
table.c.tree_id,
352-
table.c.parent_id,
353-
table.c.level
354-
]
309+
compat_layer.select(
310+
table.c.lft,
311+
table.c.rgt,
312+
table.c.tree_id,
313+
table.c.parent_id,
314+
table.c.level
355315
).where(
356316
table_pk == node_id
357317
)
@@ -374,14 +334,12 @@ def mptt_before_update(mapper, connection, instance):
374334
parent_tree_id,
375335
parent_level
376336
) = connection.execute(
377-
select(
378-
[
379-
table_pk,
380-
table.c.rgt,
381-
table.c.lft,
382-
table.c.tree_id,
383-
table.c.level
384-
]
337+
compat_layer.select(
338+
table_pk,
339+
table.c.rgt,
340+
table.c.lft,
341+
table.c.tree_id,
342+
table.c.level
385343
).where(
386344
table_pk == instance.parent_id
387345
)
@@ -404,14 +362,12 @@ def mptt_before_update(mapper, connection, instance):
404362
parent_tree_id,
405363
parent_level
406364
) = connection.execute(
407-
select(
408-
[
409-
table_pk,
410-
table.c.rgt,
411-
table.c.lft,
412-
table.c.tree_id,
413-
table.c.level
414-
]
365+
compat_layer.select(
366+
table_pk,
367+
table.c.rgt,
368+
table.c.lft,
369+
table.c.tree_id,
370+
table.c.level
415371
).where(
416372
table_pk == instance.parent_id
417373
)
@@ -449,28 +405,24 @@ def mptt_before_update(mapper, connection, instance):
449405
if left_sibling_tree_id or left_sibling_tree_id == 0:
450406
tree_id = left_sibling_tree_id + 1
451407
connection.execute(
452-
table.update(
453-
table.c.tree_id > left_sibling_tree_id
454-
).values(
408+
table.update()
409+
.where(table.c.tree_id > left_sibling_tree_id)
410+
.values(
455411
tree_id=table.c.tree_id + 1
456412
)
457413
)
458414
# if just insert
459415
else:
460416
tree_id = connection.scalar(
461-
select(
462-
[
463-
func.max(table.c.tree_id) + 1
464-
]
417+
compat_layer.select(
418+
func.max(table.c.tree_id) + 1
465419
)
466420
)
467421

468422
connection.execute(
469-
table.update(
470-
table_pk.in_(
471-
subtree
472-
)
473-
).values(
423+
table.update()
424+
.where(table_pk.in_(subtree))
425+
.values(
474426
lft=table.c.lft - node_pos_left + 1,
475427
rgt=table.c.rgt - node_pos_left + 1,
476428
level=table.c.level - node_level + default_level,

0 commit comments

Comments
 (0)