From 1c58c64c7c85f98e71ff4c551f9b2b04d6597e5e Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sun, 6 Jan 2019 21:30:34 -0500 Subject: [PATCH] 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 --- database.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/database.go b/database.go index 382c45f..28a7e3b 100644 --- a/database.go +++ b/database.go @@ -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)