Skip to content

Commit

Permalink
properly rollback failed db commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Short authored and jezdez committed Aug 16, 2018
1 parent 16846d7 commit 6f3d513
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion redash/handlers/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from redash.permissions import (can_modify, require_admin_or_owner,
require_object_modify_permission,
require_permission)
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import StaleDataError


Expand Down Expand Up @@ -127,7 +128,7 @@ def post(self, dashboard_slug):

require_object_modify_permission(dashboard, self.current_user)

updates = project(dashboard_properties, ('name', 'layout', 'version', 'tags',
updates = project(dashboard_properties, ('name', 'layout', 'version', 'tags',
'is_draft', 'dashboard_filters_enabled'))

# SQLAlchemy handles the case where a concurrent transaction beats us
Expand All @@ -143,7 +144,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 = serialize_dashboard(dashboard, with_widgets=True, user=self.current_user)
self.record_event({
Expand Down
2 changes: 2 additions & 0 deletions redash/handlers/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def post(self, data_source_id):
try:
models.db.session.commit()
except IntegrityError as e:
models.db.session.rollback()
if req['name'] in e.message:
abort(400, message="Data source with the name {} already exists.".format(req['name']))

Expand Down Expand Up @@ -127,6 +128,7 @@ def post(self):

models.db.session.commit()
except IntegrityError as e:
models.db.session.rollback()
if req['name'] in e.message:
abort(400, message="Data source with the name {} already exists.".format(req['name']))

Expand Down
3 changes: 2 additions & 1 deletion redash/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def post(self):
models.db.session.add(user)
models.db.session.commit()
except IntegrityError as e:
models.db.session.rollback()
if "email" in e.message:
abort(400, message='Email already taken.')
abort(500)
Expand Down Expand Up @@ -179,7 +180,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({
Expand Down

0 comments on commit 6f3d513

Please sign in to comment.