mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-08 21:23:13 -04:00
Try using nested transactions to rollback
This commit is contained in:
@@ -83,6 +83,8 @@ def get_free_sms_fragment_limit(service_id):
|
|||||||
annual_billing = dao_create_or_update_annual_billing_for_year(service_id,
|
annual_billing = dao_create_or_update_annual_billing_for_year(service_id,
|
||||||
annual_billing.free_sms_fragment_limit,
|
annual_billing.free_sms_fragment_limit,
|
||||||
financial_year_start)
|
financial_year_start)
|
||||||
|
from app import db
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
return jsonify(annual_billing.serialize_free_sms_items()), 200
|
return jsonify(annual_billing.serialize_free_sms_items()), 200
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.dao.dao_utils import transactional
|
from app.dao.dao_utils import transactional, nested_transactional
|
||||||
from app.dao.date_util import get_current_financial_year_start_year
|
from app.dao.date_util import get_current_financial_year_start_year
|
||||||
from app.models import AnnualBilling
|
from app.models import AnnualBilling
|
||||||
|
|
||||||
|
|
||||||
@transactional
|
@nested_transactional
|
||||||
def dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start):
|
def dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start):
|
||||||
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,19 @@ from functools import wraps
|
|||||||
from app import db
|
from app import db
|
||||||
from app.history_meta import create_history
|
from app.history_meta import create_history
|
||||||
|
|
||||||
|
def nested_transactional(func):
|
||||||
|
@wraps(func)
|
||||||
|
def commit_or_rollback(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
db.session.begin_nested()
|
||||||
|
res = func(*args, **kwargs)
|
||||||
|
db.session.commit()
|
||||||
|
return res
|
||||||
|
except Exception:
|
||||||
|
db.session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
return commit_or_rollback
|
||||||
|
|
||||||
def transactional(func):
|
def transactional(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from sqlalchemy.sql.expression import func
|
from sqlalchemy.sql.expression import func
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.dao.dao_utils import VersionOptions, transactional, version_class
|
from app.dao.dao_utils import VersionOptions, transactional, version_class, nested_transactional
|
||||||
from app.models import Domain, Organisation, Service, User
|
from app.models import Domain, Organisation, Service, User
|
||||||
|
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ def _update_organisation_services(organisation, attribute, only_where_none=True)
|
|||||||
db.session.add(service)
|
db.session.add(service)
|
||||||
|
|
||||||
|
|
||||||
@transactional
|
@nested_transactional
|
||||||
@version_class(Service)
|
@version_class(Service)
|
||||||
def dao_add_service_to_organisation(service, organisation_id):
|
def dao_add_service_to_organisation(service, organisation_id):
|
||||||
organisation = Organisation.query.filter_by(
|
organisation = Organisation.query.filter_by(
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from sqlalchemy.orm import joinedload
|
|||||||
from sqlalchemy.sql.expression import and_, asc, case, func
|
from sqlalchemy.sql.expression import and_, asc, case, func
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.dao.dao_utils import VersionOptions, transactional, version_class
|
from app.dao.dao_utils import VersionOptions, transactional, version_class, nested_transactional
|
||||||
from app.dao.date_util import get_current_financial_year
|
from app.dao.date_util import get_current_financial_year
|
||||||
from app.dao.email_branding_dao import dao_get_email_branding_by_name
|
from app.dao.email_branding_dao import dao_get_email_branding_by_name
|
||||||
from app.dao.letter_branding_dao import dao_get_letter_branding_by_name
|
from app.dao.letter_branding_dao import dao_get_letter_branding_by_name
|
||||||
@@ -284,7 +284,7 @@ def dao_fetch_service_by_id_and_user(service_id, user_id):
|
|||||||
).one()
|
).one()
|
||||||
|
|
||||||
|
|
||||||
@transactional
|
@nested_transactional
|
||||||
@version_class(Service)
|
@version_class(Service)
|
||||||
def dao_create_service(
|
def dao_create_service(
|
||||||
service,
|
service,
|
||||||
|
|||||||
@@ -119,17 +119,15 @@ def link_service_to_organisation(organisation_id):
|
|||||||
service = dao_fetch_service_by_id(data['service_id'])
|
service = dao_fetch_service_by_id(data['service_id'])
|
||||||
service.organisation = None
|
service.organisation = None
|
||||||
|
|
||||||
dao_add_service_to_organisation(service, organisation_id)
|
from app import db
|
||||||
# Need to do the annual billing update in a separate transaction because the both the
|
|
||||||
# dao_add_service_to_organisation and set_default_free_allowance_for_service are wrapped in a transaction.
|
|
||||||
# Catch and report an error if the annual billing doesn't happen - but don't rollback the service update.
|
|
||||||
try:
|
try:
|
||||||
|
dao_add_service_to_organisation(service, organisation_id)
|
||||||
set_default_free_allowance_for_service(service, year_start=None)
|
set_default_free_allowance_for_service(service, year_start=None)
|
||||||
except SQLAlchemyError:
|
db.session.commit()
|
||||||
# No need to worry about key errors because service.organisation_type has a foreign key to organisation_types
|
except Exception:
|
||||||
current_app.logger.exception(
|
db.session.rollback()
|
||||||
f"Exception caught when trying to update annual billing when the organisation "
|
raise
|
||||||
f"changed for service: {service.id} to organisation: {organisation_id}")
|
|
||||||
|
|
||||||
return '', 204
|
return '', 204
|
||||||
|
|
||||||
|
|||||||
@@ -254,18 +254,16 @@ def create_service():
|
|||||||
# unpack valid json into service object
|
# unpack valid json into service object
|
||||||
valid_service = Service.from_json(data)
|
valid_service = Service.from_json(data)
|
||||||
|
|
||||||
dao_create_service(valid_service, user)
|
from app import db
|
||||||
|
|
||||||
# Need to do the annual billing update in a separate transaction because the both the
|
|
||||||
# dao_add_service_to_organisation and set_default_free_allowance_for_service are wrapped in a transaction.
|
|
||||||
# Catch and report an error if the annual billing doesn't happen - but don't rollback the service update.
|
|
||||||
try:
|
try:
|
||||||
|
db.session.begin_nested()
|
||||||
|
dao_create_service(valid_service, user)
|
||||||
set_default_free_allowance_for_service(valid_service, year_start=None)
|
set_default_free_allowance_for_service(valid_service, year_start=None)
|
||||||
except SQLAlchemyError:
|
db.session.commit()
|
||||||
# No need to worry about key errors because service.organisation_type has a foreign key to organisation_types
|
except Exception:
|
||||||
current_app.logger.exception(
|
db.session.rollback()
|
||||||
f"Exception caught when trying to insert annual billing creating a service {valid_service.id} "
|
raise
|
||||||
f"for organisation_type {valid_service.organisation_type}")
|
|
||||||
|
|
||||||
return jsonify(data=service_schema.dump(valid_service).data), 201
|
return jsonify(data=service_schema.dump(valid_service).data), 201
|
||||||
|
|
||||||
|
|||||||
@@ -233,6 +233,7 @@ def test_add_service_to_organisation(sample_service, sample_organisation):
|
|||||||
sample_organisation.crown = False
|
sample_organisation.crown = False
|
||||||
|
|
||||||
dao_add_service_to_organisation(sample_service, sample_organisation.id)
|
dao_add_service_to_organisation(sample_service, sample_organisation.id)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
assert len(sample_organisation.services) == 1
|
assert len(sample_organisation.services) == 1
|
||||||
assert sample_organisation.services[0].id == sample_service.id
|
assert sample_organisation.services[0].id == sample_service.id
|
||||||
|
|||||||
Reference in New Issue
Block a user