mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
New strategy for transaction management.
Introduce a contextmanger function to handle exceptions and nested transactions. Using the nested_transaction will start a nested transaction with `db.session.begin_nested`, once the nested transaction is complete the commit will happen. `@transactional` has been updated to commit unless in a nested transaction.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import nested_transactional, transactional
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.dao.date_util import get_current_financial_year_start_year
|
||||
from app.models import AnnualBilling
|
||||
|
||||
|
||||
@nested_transactional
|
||||
@transactional
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import itertools
|
||||
from contextlib import contextmanager
|
||||
from functools import wraps
|
||||
|
||||
from app import db
|
||||
@@ -10,7 +11,10 @@ def transactional(func):
|
||||
def commit_or_rollback(*args, **kwargs):
|
||||
try:
|
||||
res = func(*args, **kwargs)
|
||||
db.session.commit()
|
||||
|
||||
if not db.session.registry().transaction.nested:
|
||||
db.session.commit()
|
||||
|
||||
return res
|
||||
except Exception:
|
||||
db.session.rollback()
|
||||
@@ -18,21 +22,18 @@ def transactional(func):
|
||||
return commit_or_rollback
|
||||
|
||||
|
||||
def nested_transactional(func):
|
||||
# This creates a save point for the nested transaction.
|
||||
# You must manage the commit or rollback from outer most call of the nested of the transactions.
|
||||
@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
|
||||
@contextmanager
|
||||
def nested_transaction():
|
||||
try:
|
||||
db.session.begin_nested()
|
||||
yield
|
||||
db.session.commit()
|
||||
|
||||
return commit_or_rollback
|
||||
if not db.session.registry().transaction.nested:
|
||||
db.session.commit()
|
||||
except Exception:
|
||||
db.session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
class VersionOptions():
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
from sqlalchemy.sql.expression import func
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import (
|
||||
VersionOptions,
|
||||
nested_transactional,
|
||||
transactional,
|
||||
version_class,
|
||||
)
|
||||
from app.dao.dao_utils import VersionOptions, transactional, version_class
|
||||
from app.models import Domain, Organisation, Service, User
|
||||
|
||||
|
||||
@@ -110,7 +105,7 @@ def _update_organisation_services(organisation, attribute, only_where_none=True)
|
||||
db.session.add(service)
|
||||
|
||||
|
||||
@nested_transactional
|
||||
@transactional
|
||||
@version_class(Service)
|
||||
def dao_add_service_to_organisation(service, organisation_id):
|
||||
organisation = Organisation.query.filter_by(
|
||||
|
||||
@@ -7,12 +7,7 @@ from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.sql.expression import and_, asc, case, func
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import (
|
||||
VersionOptions,
|
||||
nested_transactional,
|
||||
transactional,
|
||||
version_class,
|
||||
)
|
||||
from app.dao.dao_utils import VersionOptions, transactional, version_class
|
||||
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.letter_branding_dao import dao_get_letter_branding_by_name
|
||||
@@ -289,7 +284,7 @@ def dao_fetch_service_by_id_and_user(service_id, user_id):
|
||||
).one()
|
||||
|
||||
|
||||
@nested_transactional
|
||||
@transactional
|
||||
@version_class(Service)
|
||||
def dao_create_service(
|
||||
service,
|
||||
|
||||
Reference in New Issue
Block a user