Skip to content

Commit

Permalink
Track latest known remote for cloned/pushed projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Jan 17, 2025
1 parent 4324c53 commit 6491d5f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
39 changes: 32 additions & 7 deletions codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ module U.Codebase.Sqlite.Queries
-- ** remote project branches
loadRemoteBranch,
ensureRemoteProjectBranch,
setRemoteProjectBranchLastKnownCausalHash,
expectRemoteProjectBranchName,
setRemoteProjectBranchName,
insertBranchRemoteMapping,
Expand Down Expand Up @@ -256,6 +257,7 @@ module U.Codebase.Sqlite.Queries
addCurrentProjectPathTable,
addProjectBranchReflogTable,
addProjectBranchCausalHashIdColumn,
trackLatestRemoteHead,

-- ** schema version
currentSchemaVersion,
Expand Down Expand Up @@ -486,6 +488,10 @@ addProjectBranchCausalHashIdColumn :: Transaction ()
addProjectBranchCausalHashIdColumn =
executeStatements $(embedProjectStringFile "sql/014-add-project-branch-causal-hash-id.sql")

trackLatestRemoteHead :: Transaction ()
trackLatestRemoteHead =
executeStatements $(embedProjectStringFile "sql/018-track-latest-remote-head.sql")

schemaVersion :: Transaction SchemaVersion
schemaVersion =
queryOneCol
Expand Down Expand Up @@ -4149,7 +4155,8 @@ loadRemoteBranch rpid host rbid =
project_id,
branch_id,
host,
name
name,
last_known_causal_hash
FROM
remote_project_branch
WHERE
Expand All @@ -4158,28 +4165,46 @@ loadRemoteBranch rpid host rbid =
AND host = :host
|]

ensureRemoteProjectBranch :: RemoteProjectId -> URI -> RemoteProjectBranchId -> ProjectBranchName -> Transaction ()
ensureRemoteProjectBranch rpid host rbid name =
ensureRemoteProjectBranch :: RemoteProjectId -> URI -> RemoteProjectBranchId -> ProjectBranchName -> Maybe CausalHashId -> Transaction ()
ensureRemoteProjectBranch rpid host rbid name lastKnownCausalHash =
execute
[sql|
INSERT INTO remote_project_branch (
project_id,
host,
branch_id,
name)
name,
last_known_head)
VALUES (
:rpid,
:host,
:rbid,
:name)
:name,
:lastKnownCausalHash
)
ON CONFLICT (
project_id,
branch_id,
host)
-- should this update the name instead?
DO NOTHING
DO UPDATE
SET name = :name,
last_known_causal_hash = :lastKnownCausalHash
|]

setRemoteProjectBranchLastKnownCausalHash :: URI -> RemoteProjectId -> RemoteProjectBranchId -> CausalHashId -> Transaction ()
setRemoteProjectBranchLastKnownCausalHash host rpid rbid causalHashId =
execute
[sql|
UPDATE
remote_project_branch
SET
last_known_causal_hash = :causalHashId
WHERE
project_id = :rpid
AND branch_id = :rbid
AND host = :host
|]

expectRemoteProjectBranchName :: URI -> RemoteProjectId -> RemoteProjectBranchId -> Transaction ProjectBranchName
expectRemoteProjectBranchName host projectId branchId =
queryOneCol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ where

import Network.URI (URI)
import Network.URI.Orphans.Sqlite ()
import U.Codebase.Sqlite.DbId (RemoteProjectBranchId, RemoteProjectId)
import U.Codebase.Sqlite.DbId (CausalHashId, RemoteProjectBranchId, RemoteProjectId)
import Unison.Core.Orphans.Sqlite ()
import Unison.Core.Project (ProjectBranchName)
import Unison.Prelude
Expand All @@ -15,7 +15,9 @@ data RemoteProjectBranch = RemoteProjectBranch
{ projectId :: RemoteProjectId,
branchId :: RemoteProjectBranchId,
host :: URI,
name :: ProjectBranchName
name :: ProjectBranchName,
-- Note that there's no guarantee that the causals for this hash have been downloaded/synced into the codebase.
lastKnownCausalHash :: CausalHashId
}
deriving stock (Generic, Show)
deriving anyclass (ToRow, FromRow)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Add a field for tracking the latest known causal hash for each remote project branch.
-- It's helpful for when we need to tell Share how much we know about a branch.

ALTER TABLE remote_project
-- Note that there isn't a guarantee this hash has actually been synced into the codebase.
ADD COLUMN last_known_causal_hash INTEGER NULL REFERENCES hash(id)
ON DELETE SET NULL;
3 changes: 2 additions & 1 deletion codebase2/codebase-sqlite/unison-codebase-sqlite.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.36.0.
-- This file has been generated from package.yaml by hpack version 0.37.0.
--
-- see: https://github.com/sol/hpack

Expand All @@ -24,6 +24,7 @@ extra-source-files:
sql/012-add-current-project-path-table.sql
sql/013-add-project-branch-reflog-table.sql
sql/014-add-project-branch-causal-hash-id.sql
sql/018-track-latest-remote-head.sql
sql/create.sql

source-repository head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ migrations regionVar getDeclType termBuffer declBuffer rootCodebasePath =
sqlMigration 14 Q.addSquashResultTable,
sqlMigration 15 Q.addSquashResultTableIfNotExists,
sqlMigration 16 Q.cdToProjectRoot,
(17 {- This migration takes a raw sqlite connection -}, \conn -> migrateSchema16To17 conn)
(17 {- This migration takes a raw sqlite connection -}, \conn -> migrateSchema16To17 conn),
sqlMigration 18 Q.trackLatestRemoteHead
]
where
runT :: Sqlite.Transaction () -> Sqlite.Connection -> IO ()
Expand Down
5 changes: 5 additions & 0 deletions unison-cli/src/Unison/Cli/Share/Projects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ import Unison.Codebase.Editor.Output qualified as Output
import Unison.Hash32 (Hash32)
import Unison.Prelude
import Unison.Project (ProjectAndBranch (..), ProjectBranchName, ProjectName)
import Unison.Share.API.Hash qualified as HashJWT
import Unison.Share.API.Projects qualified as Share.API
import Unison.Share.Codeserver (defaultCodeserver)
import Unison.Share.Types (codeserverBaseURL)
import Unison.Sync.Common qualified as Sync

-- | Get a project by id.
--
Expand Down Expand Up @@ -193,14 +195,17 @@ onGotProjectBranch :: Share.API.ProjectBranch -> Cli RemoteProjectBranch
onGotProjectBranch branch = do
let projectId = RemoteProjectId (branch ^. #projectId)
let branchId = RemoteProjectBranchId (branch ^. #branchId)
let causalHash = Sync.hash32ToCausalHash $ HashJWT.hashJWTHash (branch ^. #branchHead)
projectName <- validateProjectName (branch ^. #projectName)
branchName <- validateBranchName (branch ^. #branchName)
Cli.runTransaction do
causalHashId <- Queries.saveCausalHash causalHash
Queries.ensureRemoteProjectBranch
projectId
hardCodedUri
branchId
branchName
(Just causalHashId)
pure
RemoteProjectBranch
{ projectId,
Expand Down

0 comments on commit 6491d5f

Please sign in to comment.