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 Jan 16, 2020
1 parent 3bdf9d8 commit d2943b9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions redash/handlers/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)
from redash.security import csp_allows_embeding
from redash.serializers import serialize_dashboard
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import StaleDataError


Expand Down Expand Up @@ -215,7 +216,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
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 @@ -69,6 +69,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,
Expand Down Expand Up @@ -159,6 +160,7 @@ def post(self):

models.db.session.commit()
except IntegrityError as e:
models.db.session.rollback()
if req["name"] in str(e):
abort(
400,
Expand Down
3 changes: 2 additions & 1 deletion redash/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d2943b9

Please sign in to comment.