move deactivate functionality into one database transactions

this means that any errors will cause the entire thing to roll back

unfortunately, to do this we have to circumvent our regular code, which calls commit a lot, and lazily loads a lot of things, which will flush, and cause the version decorators to fail. so we have to write a lot of stuff by hand and re-select the service (even though it's already been queried) just to populate the api_keys and templates relationship on it
This commit is contained in:
Leo Hemsted
2016-11-10 17:07:02 +00:00
parent 17cf582502
commit 9ae6e14140
5 changed files with 33 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
import uuid
from datetime import date
from datetime import date, datetime
from sqlalchemy import asc, func
from sqlalchemy.orm import joinedload
@@ -69,6 +69,33 @@ def dao_fetch_all_services_by_user(user_id, only_active=False):
return query.all()
@transactional
@version_class(Service)
@version_class(Template, TemplateHistory)
@version_class(ApiKey)
def dao_deactive_service(service_id):
# have to eager load templates and api keys so that we don't flush when we loop through them
service = Service.query.options(
joinedload('templates'),
joinedload('api_keys'),
).filter(Service.id == service_id).one()
service.active = False
service.name = '_archived_' + service.name
service.email_from = '_archived_' + service.email_from
for template in service.templates:
template.archived = True
for api_key in service.api_keys:
api_key.expiry_date = datetime.utcnow()
db.session.add(service)
db.session.add_all(service.templates)
db.session.add_all(service.api_keys)
def dao_fetch_service_by_id_and_user(service_id, user_id):
return Service.query.filter(
Service.users.any(id=user_id),