about summary refs log tree commit diff
path: root/src/LoginAttempts.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-30T18·52+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-30T18·52+0100
commitea31a014977c68c808f914556dfe9a04e96205eb (patch)
tree9f5fba528ed5d9d05284185cb8e67278e0962b8d /src/LoginAttempts.hs
parent8ebc89b44b3fc0e6025b33a3e7ec37e9ebb385cc (diff)
Debug LoginAttempts.increment
When this was an UPDATE statement with a WHERE clause, and the LoginAttempts
table was vacant, nothing would happen. Thankfully, SQLite supports an UPSERT
clause so that I can INSERT a new record or UPDATE conditionally.

And the best part is: it works!
Diffstat (limited to 'src/LoginAttempts.hs')
-rw-r--r--src/LoginAttempts.hs5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/LoginAttempts.hs b/src/LoginAttempts.hs
index f5a5bde6a004..d78e12e3fd8a 100644
--- a/src/LoginAttempts.hs
+++ b/src/LoginAttempts.hs
@@ -23,7 +23,8 @@ forUsername dbFile username = withConnection dbFile $ \conn -> do
     [T.LoginAttempt{..}] -> pure (Just loginAttemptNumAttempts)
     _  -> pure Nothing
 
+-- | INSERT a failed login attempt for `username` or UPDATE an existing entry.
 increment :: FilePath -> T.Username -> IO ()
 increment dbFile username = withConnection dbFile $ \conn ->
-  execute conn "UPDATE LoginAttempts SET numAttempts = numAttempts + 1 WHERE username = ?"
-    (Only username)
+  execute conn "INSERT INTO LoginAttempts (username,numAttempts) VALUES (?,?) ON CONFLICT (username) DO UPDATE SET numAttempts = numAttempts + 1"
+    (username, 1 :: Integer)