Skip to content
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

Migrates tree_id from an Integer to Unicode(32) #53

Open
wants to merge 7 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
19 changes: 4 additions & 15 deletions sqlalchemy_mptt/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sqlalchemy.orm import object_session
from sqlalchemy.orm.base import NO_VALUE
from sqlalchemy.sql import func
import uuid


def _insert_subtree(table, connection, node_size,
Expand Down Expand Up @@ -73,8 +74,7 @@ def mptt_before_insert(mapper, connection, instance):
instance.left = 1
instance.right = 2
instance.level = 1
tree_id = connection.scalar(
select([func.max(table.c.tree_id) + 1])) or 1
tree_id = str(uuid.uuid4())
instance.tree_id = tree_id
else:
(parent_pos_left,
Expand Down Expand Up @@ -197,7 +197,7 @@ def mptt_before_update(mapper, connection, instance):
'is_parent': False}
# if move_before to top level
elif not right_sibling_parent:
left_sibling_tree_id = right_sibling_tree_id - 1
left_sibling_tree_id = str(uuid.uuid4())

# if placed after a particular node
if hasattr(instance, 'mptt_move_after'):
Expand Down Expand Up @@ -296,18 +296,7 @@ def mptt_before_update(mapper, connection, instance):
parent_tree_id, parent_level, node_level, left_sibling,
table_pk)
else:
# if insert after
if left_sibling_tree_id or left_sibling_tree_id == 0:
tree_id = left_sibling_tree_id + 1
connection.execute(
table.update(table.c.tree_id > left_sibling_tree_id)
.values(tree_id=table.c.tree_id + 1)
)
# if just insert
else:
tree_id = connection.scalar(
select([func.max(table.c.tree_id) + 1]))

tree_id = str(uuid.uuid4())
connection.execute(
table.update(table_pk.in_(subtree))
.values(
Expand Down
4 changes: 2 additions & 2 deletions sqlalchemy_mptt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""
SQLAlchemy nested sets mixin
"""
from sqlalchemy import Index, Column, Integer, ForeignKey, desc, asc
from sqlalchemy import Index, Column, Integer, ForeignKey, desc, asc, Unicode
from sqlalchemy.orm import backref, relationship, object_session
from sqlalchemy.orm.session import Session
from sqlalchemy.ext.declarative import declared_attr
Expand Down Expand Up @@ -67,7 +67,7 @@ def get_pk_column(cls):

@declared_attr
def tree_id(cls):
return Column("tree_id", Integer)
return Column("tree_id", Unicode(32))

@declared_attr
def parent_id(cls):
Expand Down
8 changes: 5 additions & 3 deletions sqlalchemy_mptt/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ def test_session_expire_for_move_after_to_new_tree(self):
node.move_after('1')
self.session.flush()

self.assertEqual(node.tree_id, 2)
node = self.session.query(self.model). \
filter(self.model.get_pk_column() == 4).one()

self.assertEqual(node.level, 1)
self.assertEqual(node.parent_id, None)

self.assertEqual(children[0].tree_id, 2)
self.assertEqual(children[0].tree_id, node.tree_id)
self.assertEqual(children[0].parent_id, 4)
self.assertEqual(children[0].level, 2)

self.assertEqual(children[1].tree_id, 2)
self.assertEqual(children[1].tree_id, node.tree_id)
self.assertEqual(children[1].parent_id, 4)
self.assertEqual(children[1].level, 2)
Loading