Clear temp entity tables on login

This commit is contained in:
Chris Penner 2023-08-25 13:35:08 -07:00
parent 9882d5445c
commit 7839269619
2 changed files with 20 additions and 0 deletions

View File

@ -220,6 +220,7 @@ module U.Codebase.Sqlite.Queries
saveTempEntityInMain,
expectTempEntity,
deleteTempEntity,
clearTempEntityTables,
-- * elaborate hashes
elaborateHashes,
@ -2712,6 +2713,15 @@ deleteTempEntity hash =
WHERE hash = :hash
|]
-- | Clears the `temp_entity` and `temp_entity_missing_dependency` tables.
-- The hashjwts stored in temp entity tables can sometimes go stale, so we clear them out.
-- This is safe because temp entities are generally considered ephemeral
-- except during an active pull.
clearTempEntityTables :: Transaction ()
clearTempEntityTables = do
execute [sql| DELETE FROM temp_entity_missing_dependency |]
execute [sql| DELETE FROM temp_entity |]
-- | "Elaborate" a set of `temp_entity` hashes.
--
-- Given a set of `temp_entity` hashes, returns the (known) set of transitive dependencies that haven't already been

View File

@ -22,6 +22,7 @@ import Network.URI (URI (..), parseURI)
import Network.Wai
import Network.Wai qualified as Wai
import Network.Wai.Handler.Warp qualified as Warp
import U.Codebase.Sqlite.Queries qualified as Q
import Unison.Auth.CredentialManager (getCredentials, saveCredentials)
import Unison.Auth.Discovery (discoveryURIForCodeserver, fetchDiscoveryDoc)
import Unison.Auth.Types
@ -106,6 +107,15 @@ authLogin host = do
userInfo <- bailOnFailure (getUserInfo doc accessToken)
let codeserverId = codeserverIdFromCodeserverURI host
let creds = codeserverCredentials discoveryURI tokens fetchTime userInfo
-- Before saving new credentials we clear the temp entity caches,
-- this is to handle the case that the user logged into a new user and that they have
-- some hashJWTs for a different user around which won't work against the new user
-- credentials.
--
-- It also means that if the server changes signing-keys the user will simply get
-- "unauthenticated", call `auth.login`, and that will clear out any hashjwts signed with
-- the old key.
Cli.runTransaction Q.clearTempEntityTables
liftIO (saveCredentials credentialManager codeserverId creds)
Cli.respond Output.Success
pure userInfo