From 7f20e476e7dcdd1d1256d8737cc7b78573b2c5a5 Mon Sep 17 00:00:00 2001 From: Allen Short Date: Wed, 21 Mar 2018 20:38:48 +0000 Subject: [PATCH] Properly rollback failed db commits --- redash/handlers/dashboards.py | 5 +++++ redash/handlers/data_sources.py | 2 ++ redash/handlers/users.py | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/redash/handlers/dashboards.py b/redash/handlers/dashboards.py index 1575fe2e28..8cc9f4a0ab 100644 --- a/redash/handlers/dashboards.py +++ b/redash/handlers/dashboards.py @@ -21,6 +21,7 @@ DashboardSerializer, public_dashboard, ) +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm.exc import StaleDataError @@ -218,7 +219,11 @@ def post(self, dashboard_slug): try: models.db.session.commit() except StaleDataError: + models.db.session.rollback() abort(409) + except IntegrityError: + models.db.session.rollback() + abort(400) result = DashboardSerializer( dashboard, with_widgets=True, user=self.current_user diff --git a/redash/handlers/data_sources.py b/redash/handlers/data_sources.py index 5746dab0ee..e3d372e221 100644 --- a/redash/handlers/data_sources.py +++ b/redash/handlers/data_sources.py @@ -68,6 +68,7 @@ def post(self, data_source_id): try: models.db.session.commit() except IntegrityError as e: + models.db.session.rollback() if req["name"] in str(e): abort( 400, @@ -158,6 +159,7 @@ def post(self): models.db.session.commit() except IntegrityError as e: + models.db.session.rollback() if req["name"] in str(e): abort( 400, diff --git a/redash/handlers/users.py b/redash/handlers/users.py index bc2e98c534..446a8c5f66 100644 --- a/redash/handlers/users.py +++ b/redash/handlers/users.py @@ -157,6 +157,7 @@ def post(self): models.db.session.add(user) models.db.session.commit() except IntegrityError as e: + models.db.session.rollback() if "email" in str(e): abort(400, message="Email already taken.") abort(500) @@ -287,7 +288,7 @@ def post(self, user_id): message = "Email already taken." else: message = "Error updating record" - + models.db.session.rollback() abort(400, message=message) self.record_event(