Do all version table writes in one commit

The behaviour of stacking the version decorators does not work as
expected.

What you would expect to happen is that each decorator causes a history
row to be written for its respective model object.

What actually happens is that the first decorator adds history records
to the database session, but then causes the database session to commit.
This means that subsequent uses of this decorator find a clean session,
and therefore no changes to copy to their respective history tables.

This commit changes the intended use of the decorator so that it is only
used once per function, and accepts multiple definitions of what to
record history for. This way it can record everything that needs to go
into the history before doing anything that would risk flushing the
session.
This commit is contained in:
Chris Hill-Scott
2019-03-07 17:39:38 +00:00
parent c257ec105c
commit eeb90bed57
3 changed files with 61 additions and 26 deletions

View File

@@ -11,12 +11,15 @@ from app.models import (
)
from app.dao.dao_utils import (
transactional,
version_class
version_class,
VersionOptions,
)
@transactional
@version_class(Template, TemplateHistory)
@version_class(
VersionOptions(Template, history_class=TemplateHistory)
)
def dao_create_template(template):
template.id = uuid.uuid4() # must be set now so version history model can use same id
template.archived = False
@@ -36,7 +39,9 @@ def dao_create_template(template):
@transactional
@version_class(Template, TemplateHistory)
@version_class(
VersionOptions(Template, history_class=TemplateHistory)
)
def dao_update_template(template):
if template.archived:
template.folder = None