mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
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:
@@ -9,7 +9,8 @@ from flask import current_app
|
||||
from app import db
|
||||
from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
version_class,
|
||||
VersionOptions,
|
||||
)
|
||||
from app.dao.organisation_dao import dao_get_organisation_by_email_address
|
||||
from app.dao.service_sms_sender_dao import insert_service_sms_sender
|
||||
@@ -117,9 +118,11 @@ def dao_fetch_all_services_by_user(user_id, only_active=False):
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Service)
|
||||
@version_class(Template, TemplateHistory, must_write_history=False)
|
||||
@version_class(ApiKey, must_write_history=False)
|
||||
@version_class(
|
||||
VersionOptions(ApiKey, must_write_history=False),
|
||||
VersionOptions(Service),
|
||||
VersionOptions(Template, history_class=TemplateHistory, 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
|
||||
@@ -372,8 +375,10 @@ def dao_fetch_todays_stats_for_all_services(include_from_test_key=True, only_act
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Service)
|
||||
@version_class(ApiKey, must_write_history=False)
|
||||
@version_class(
|
||||
VersionOptions(ApiKey, must_write_history=False),
|
||||
VersionOptions(Service),
|
||||
)
|
||||
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
|
||||
@@ -381,12 +386,12 @@ def dao_suspend_service(service_id):
|
||||
joinedload('api_keys'),
|
||||
).filter(Service.id == service_id).one()
|
||||
|
||||
service.active = False
|
||||
|
||||
for api_key in service.api_keys:
|
||||
if not api_key.expiry_date:
|
||||
api_key.expiry_date = datetime.utcnow()
|
||||
|
||||
service.active = False
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Service)
|
||||
|
||||
Reference in New Issue
Block a user