Raise exception if history can’t be written

This is fiendishly difficult error to discover on your own.

It’s caused when, during the creation of a row in the database, you run
a query on the same table, or a table that joins to the table you’re
inserting into. What I think is happening is that the database is forced
to flush the session before running the query in order to maintain
consistency.

This means that the session is clean by the time the history stuff comes
to do its work, so there’s nothing for it to copy into the history
table, and it silently fails to record history.

Hopefully raising an exception will:
- prevent this from failing silently
- save whoever comes across this issue in the future a whole load of
  time
This commit is contained in:
Chris Hill-Scott
2019-03-06 08:37:55 +00:00
parent ab22a3d88c
commit c257ec105c
3 changed files with 26 additions and 4 deletions

View File

@@ -118,8 +118,8 @@ def dao_fetch_all_services_by_user(user_id, only_active=False):
@transactional
@version_class(Service)
@version_class(Template, TemplateHistory)
@version_class(ApiKey)
@version_class(Template, TemplateHistory, must_write_history=False)
@version_class(ApiKey, must_write_history=False)
def dao_archive_service(service_id):
# have to eager load templates and api keys so that we don't flush when we loop through them
# to ensure that db.session still contains the models when it comes to creating history objects
@@ -373,7 +373,7 @@ def dao_fetch_todays_stats_for_all_services(include_from_test_key=True, only_act
@transactional
@version_class(Service)
@version_class(ApiKey)
@version_class(ApiKey, must_write_history=False)
def dao_suspend_service(service_id):
# have to eager load api keys so that we don't flush when we loop through them
# to ensure that db.session still contains the models when it comes to creating history objects