Skip to content

Prepare support for SQLAlchemy 2.0 #81

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Add mixin to model
.. code-block:: python

from sqlalchemy import Column, Integer, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from sqlalchemy_mptt.mixins import BaseNestedSets

Expand Down
2 changes: 1 addition & 1 deletion docs/initialize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Create model with MPTT mixin:
:linenos:

from sqlalchemy import Column, Integer, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from sqlalchemy_mptt.mixins import BaseNestedSets

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SQLAlchemy>=1.0.0
SQLAlchemy>=1.4
158 changes: 64 additions & 94 deletions sqlalchemy_mptt/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _insert_subtree(
delta_rgt = delta_lft + node_size - 1

connection.execute(
table.update(
table.update().where(
table_pk.in_(subtree)
).values(
lft=table.c.lft - node_pos_left + delta_lft,
Expand All @@ -53,7 +53,7 @@ def _insert_subtree(

# step 2: update key of right side
connection.execute(
table.update(
table.update().where(
and_(
table.c.rgt > delta_lft - 1,
table_pk.notin_(subtree),
Expand All @@ -62,12 +62,10 @@ def _insert_subtree(
).values(
rgt=table.c.rgt + node_size,
lft=case(
[
(
table.c.lft > left_sibling['lft'],
table.c.lft + node_size
)
],
(
table.c.lft > left_sibling['lft'],
table.c.lft + node_size
),
else_=table.c.lft
)
)
Expand All @@ -94,9 +92,7 @@ def mptt_before_insert(mapper, connection, instance):
instance.level = instance.get_default_level()
tree_id = connection.scalar(
select(
[
func.max(table.c.tree_id) + 1
]
func.max(table.c.tree_id) + 1
)
) or 1
instance.tree_id = tree_id
Expand All @@ -106,39 +102,33 @@ def mptt_before_insert(mapper, connection, instance):
parent_tree_id,
parent_level) = connection.execute(
select(
[
table.c.lft,
table.c.rgt,
table.c.tree_id,
table.c.level
]
table.c.lft,
table.c.rgt,
table.c.tree_id,
table.c.level
).where(
table_pk == instance.parent_id
)
).fetchone()

# Update key of right side
connection.execute(
table.update(
table.update().where(
and_(table.c.rgt >= parent_pos_right,
table.c.tree_id == parent_tree_id)
).values(
lft=case(
[
(
table.c.lft > parent_pos_right,
table.c.lft + 2
)
],
(
table.c.lft > parent_pos_right,
table.c.lft + 2
),
else_=table.c.lft
),
rgt=case(
[
(
table.c.rgt >= parent_pos_right,
table.c.rgt + 2
)
],
(
table.c.rgt >= parent_pos_right,
table.c.rgt + 2
),
else_=table.c.rgt
)
)
Expand All @@ -158,10 +148,8 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
table_pk = getattr(table.c, db_pk.name)
lft, rgt = connection.execute(
select(
[
table.c.lft,
table.c.rgt
]
table.c.lft,
table.c.rgt
).where(
table_pk == pk
)
Expand All @@ -171,7 +159,7 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
if delete:
mapper.base_mapper.confirm_deleted_rows = False
connection.execute(
table.delete(
table.delete().where(
table_pk == pk
)
)
Expand All @@ -190,28 +178,24 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
END
"""
connection.execute(
table.update(
table.update().where(
and_(
table.c.rgt > rgt,
table.c.tree_id == tree_id
)
).values(
lft=case(
[
(
table.c.lft > lft,
table.c.lft - delta
)
],
(
table.c.lft > lft,
table.c.lft - delta
),
else_=table.c.lft
),
rgt=case(
[
(
table.c.rgt >= rgt,
table.c.rgt - delta
)
],
(
table.c.rgt >= rgt,
table.c.rgt - delta
),
else_=table.c.rgt
)
)
Expand Down Expand Up @@ -243,25 +227,21 @@ def mptt_before_update(mapper, connection, instance):
right_sibling_tree_id
) = connection.execute(
select(
[
table.c.lft,
table.c.rgt,
table.c.parent_id,
table.c.level,
table.c.tree_id
]
table.c.lft,
table.c.rgt,
table.c.parent_id,
table.c.level,
table.c.tree_id
).where(
table_pk == instance.mptt_move_before
)
).fetchone()
current_lvl_nodes = connection.execute(
select(
[
table.c.lft,
table.c.rgt,
table.c.parent_id,
table.c.tree_id
]
table.c.lft,
table.c.rgt,
table.c.parent_id,
table.c.tree_id
).where(
and_(
table.c.level == right_sibling_level,
Expand Down Expand Up @@ -296,12 +276,10 @@ def mptt_before_update(mapper, connection, instance):
left_sibling_tree_id
) = connection.execute(
select(
[
table.c.lft,
table.c.rgt,
table.c.parent_id,
table.c.tree_id
]
table.c.lft,
table.c.rgt,
table.c.parent_id,
table.c.tree_id
).where(
table_pk == instance.mptt_move_after
)
Expand All @@ -320,7 +298,7 @@ def mptt_before_update(mapper, connection, instance):
ORDER BY left_key
"""
subtree = connection.execute(
select([table_pk])
select(table_pk)
.where(
and_(
table.c.lft >= instance.left,
Expand All @@ -345,13 +323,11 @@ def mptt_before_update(mapper, connection, instance):
node_level
) = connection.execute(
select(
[
table.c.lft,
table.c.rgt,
table.c.tree_id,
table.c.parent_id,
table.c.level
]
table.c.lft,
table.c.rgt,
table.c.tree_id,
table.c.parent_id,
table.c.level
).where(
table_pk == node_id
)
Expand All @@ -375,13 +351,11 @@ def mptt_before_update(mapper, connection, instance):
parent_level
) = connection.execute(
select(
[
table_pk,
table.c.rgt,
table.c.lft,
table.c.tree_id,
table.c.level
]
table_pk,
table.c.rgt,
table.c.lft,
table.c.tree_id,
table.c.level
).where(
table_pk == instance.parent_id
)
Expand All @@ -405,13 +379,11 @@ def mptt_before_update(mapper, connection, instance):
parent_level
) = connection.execute(
select(
[
table_pk,
table.c.rgt,
table.c.lft,
table.c.tree_id,
table.c.level
]
table_pk,
table.c.rgt,
table.c.lft,
table.c.tree_id,
table.c.level
).where(
table_pk == instance.parent_id
)
Expand Down Expand Up @@ -449,7 +421,7 @@ def mptt_before_update(mapper, connection, instance):
if left_sibling_tree_id or left_sibling_tree_id == 0:
tree_id = left_sibling_tree_id + 1
connection.execute(
table.update(
table.update().where(
table.c.tree_id > left_sibling_tree_id
).values(
tree_id=table.c.tree_id + 1
Expand All @@ -459,14 +431,12 @@ def mptt_before_update(mapper, connection, instance):
else:
tree_id = connection.scalar(
select(
[
func.max(table.c.tree_id) + 1
]
func.max(table.c.tree_id) + 1
)
)

connection.execute(
table.update(
table.update().where(
table_pk.in_(
subtree
)
Expand Down
15 changes: 9 additions & 6 deletions sqlalchemy_mptt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class BaseNestedSets(object):
.. code::

from sqlalchemy import Boolean, Column, create_engine, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker, declarative_base

from sqlalchemy_mptt.mixins import BaseNestedSets

Expand Down Expand Up @@ -65,7 +64,11 @@ def get_pk_name(cls):

@classmethod
def get_pk_column(cls):
return getattr(cls, cls.get_pk_name())
col = getattr(cls, cls.get_pk_name())
# might be a Mapped column
if hasattr(col, "column") and hasattr(col.column, "name"):
return col.column
return col

def get_pk_value(self):
return getattr(self, self.get_pk_name())
Expand Down Expand Up @@ -342,7 +345,7 @@ def drilldown_tree(self, session=None, json=False, json_fields=None):
)

def path_to_root(self, session=None, order=desc):
"""Generate path from a leaf or intermediate node to the root.
r"""Generate path from a leaf or intermediate node to the root.

For example:

Expand Down Expand Up @@ -372,7 +375,7 @@ def path_to_root(self, session=None, order=desc):
return self._base_order(query, order=order)

def get_siblings(self, include_self=False, session=None):
"""
r"""
* https://github.com/uralbash/sqlalchemy_mptt/issues/64
* https://django-mptt.readthedocs.io/en/latest/models.html#get-siblings-include-self-false

Expand Down Expand Up @@ -414,7 +417,7 @@ def get_siblings(self, include_self=False, session=None):
return query

def get_children(self, session=None):
"""
r"""
* https://github.com/uralbash/sqlalchemy_mptt/issues/64
* https://github.com/django-mptt/django-mptt/blob/fd76a816e05feb5fb0fc23126d33e514460a0ead/mptt/models.py#L563

Expand Down
3 changes: 1 addition & 2 deletions sqlalchemy_mptt/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

from sqlalchemy import Column, Boolean, Integer, create_engine
from sqlalchemy.event import contains
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker, declarative_base

from sqlalchemy_mptt import mptt_sessionmaker

Expand Down
Loading