Fix SQLite deadlock when creating user

This avoids reading from the database after a transaction has been
started in CreateUser(), fixing the deadlock that occurred before.

Closes #53
This commit is contained in:
Matt Baer 2019-01-06 21:30:34 -05:00
parent f53ced382f
commit 1c58c64c7c
1 changed files with 4 additions and 4 deletions

View File

@ -155,16 +155,16 @@ func (db *datastore) dateSub(l int, unit string) string {
}
func (db *datastore) CreateUser(u *User, collectionTitle string) error {
if db.PostIDExists(u.Username) {
return impart.HTTPError{http.StatusConflict, "Invalid collection name."}
}
// New users get a `users` and `collections` row.
t, err := db.Begin()
if err != nil {
return err
}
if db.PostIDExists(u.Username) {
return impart.HTTPError{http.StatusConflict, "Invalid collection name."}
}
// 1. Add to `users` table
// NOTE: Assumes User's Password is already hashed!
res, err := t.Exec("INSERT INTO users (username, password, email) VALUES (?, ?, ?)", u.Username, u.HashedPass, u.Email)