-
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 person 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.
- Loading branch information
1 parent
5f1e098
commit ffd2fb8
Showing
2 changed files
with
82 additions
and
2 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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
import datetime | ||
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): | ||
article_author_table = sa.Table( | ||
"article_author", | ||
self.Model.metadata, | ||
sa.Column( | ||
"article_id", sa.Integer, sa.ForeignKey("article.id"), primary_key=True, nullable=False | ||
), | ||
sa.Column("author_id", sa.Integer, sa.ForeignKey("author.id"), primary_key=True, nullable=False), | ||
sa.Column( | ||
"created_date", | ||
sa.DateTime, | ||
nullable=False, | ||
server_default=sa.func.current_timestamp(), | ||
default=lambda: datetime.datetime.now(datetime.timezone.utc) | ||
), | ||
) | ||
|
||
class Article(self.Model): | ||
__tablename__ = "article" | ||
__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), nullable=False) | ||
content = sa.Column(sa.UnicodeText) | ||
|
||
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)) | ||
article_id = sa.Column(sa.Integer, sa.ForeignKey(Article.id)) | ||
articles = sa.orm.relationship(Article, secondary=article_author_table, backref="authors") | ||
|
||
self.Article = Article | ||
self.Author = Author | ||
self.article_author_table = article_author_table | ||
|
||
def test_add_record(self): | ||
article = self.Article(name="Article 1") | ||
author = self.Author(name="Author 1", articles=[article]) | ||
@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(article) | ||
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) |