Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use transactions for endpoints that have multiple database operations that may fail #25

Open
rjwebb opened this issue Dec 8, 2023 · 0 comments

Comments

@rjwebb
Copy link
Contributor

rjwebb commented Dec 8, 2023

The existing codebase doesn't use database transactions for anything, so there are certain operations (such as creating a new user) that may fail, leaving the database in an invalid state.

For example, the "create user" function in https://github.com/canvasxyz/metropolis/blob/main/server/src/auth/create-user.ts#L13 consists of a number of steps that all need to take place successfully:

  • check if a user exists with the same email - this is just a select query
  • insert the new user entry - this may fail if there has been a race condition and a new user has been created since the previous step took place
  • insert a hashed password into the database - if this step fails, we end up with a user entry that has no password, so it will be impossible to create a new user with the same email address
  • create a session cookie
  • add the session cookie to the response

We should just wrap the entire sequence of steps in a transaction, so that if something fails the pending changes to the database can be discarded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants