-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(141): handle after_flush_postexec when creating version objects
when creating version objects if a user has created after_flush_postexec hook, which keeps calling `after_flush` untill it exhausts 100 attempts or session is no longer dirty, this is picked up by mapper as after_update which within same transaction adds a update operation type, so we have a check if target is already in UoW we continue with operation type that it is in untill transaction is completed. We also updated a existing testcase named `test_replace_deleted_object_with_update` as it was updating the pk of article object, but the pk being identity of object is used by operations to track target, so changing a non identity column to validate partial flush does not impact other objects
- Loading branch information
1 parent
5f1e098
commit 596301c
Showing
3 changed files
with
50 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/reported_bugs/test_bug_141_after_flush_postexec_op_type_issue.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import sqlalchemy as sa | ||
from copy import copy | ||
|
||
from tests import TestCase | ||
from sqlalchemy_history import version_class | ||
|
||
|
||
class TestBug141(TestCase): | ||
# ref: https://github.com/corridor/sqlalchemy-history/issues/141 | ||
def create_models(self): | ||
class Author(self.Model): | ||
__tablename__ = "author" | ||
__versioned__ = copy(self.options) | ||
|
||
id = sa.Column( | ||
sa.Integer, sa.Sequence(f"{__tablename__}_seq", start=1), autoincrement=True, primary_key=True | ||
) | ||
name = sa.Column(sa.Unicode(255)) | ||
|
||
self.Author = Author | ||
|
||
def test_add_record(self): | ||
author = self.Author(name="Author 1") | ||
@sa.event.listens_for(self.session, 'after_flush_postexec') | ||
def after_flush_postexec(session, flush_context): | ||
if author.name != "yoyoyoyoyo": | ||
author.name = "yoyoyoyoyo" | ||
self.session.add(author) | ||
self.session.commit() | ||
|
||
versioned_objs = self.session.query(version_class(self.Author)).all() | ||
assert len(versioned_objs) == 1 | ||
assert versioned_objs[0].operation_type == 0 | ||
assert versioned_objs[0].name == "yoyoyoyoyo" | ||
author.name = "sdfeoinfe" | ||
self.session.add(author) | ||
self.session.commit() | ||
versioned_objs = self.session.query(version_class(self.Author)).all() | ||
assert len(versioned_objs) == 2 | ||
assert versioned_objs[0].operation_type == 0 | ||
assert versioned_objs[1].operation_type == 1 | ||
assert versioned_objs[0].name == versioned_objs[1].name == "yoyoyoyoyo" | ||
sa.event.remove(self.session, "after_flush_postexec", after_flush_postexec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters