Skip to content

Commit 590505a

Browse files
committed
Track latest known remote for cloned/pushed projects
1 parent fbd7f43 commit 590505a

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ module U.Codebase.Sqlite.Queries
146146
-- ** remote project branches
147147
loadRemoteBranch,
148148
ensureRemoteProjectBranch,
149+
setRemoteProjectBranchLastKnownCausalHash,
149150
expectRemoteProjectBranchName,
150151
setRemoteProjectBranchName,
151152
insertBranchRemoteMapping,
@@ -258,6 +259,7 @@ module U.Codebase.Sqlite.Queries
258259
addProjectBranchReflogTable,
259260
addProjectBranchCausalHashIdColumn,
260261
addProjectBranchLastAccessedColumn,
262+
trackLatestRemoteHead,
261263

262264
-- ** schema version
263265
currentSchemaVersion,
@@ -422,7 +424,7 @@ type TextPathSegments = [Text]
422424
-- * main squeeze
423425

424426
currentSchemaVersion :: SchemaVersion
425-
currentSchemaVersion = 18
427+
currentSchemaVersion = 19
426428

427429
runCreateSql :: Transaction ()
428430
runCreateSql =
@@ -492,6 +494,10 @@ addProjectBranchLastAccessedColumn :: Transaction ()
492494
addProjectBranchLastAccessedColumn =
493495
executeStatements $(embedProjectStringFile "sql/015-add-project-branch-last-accessed.sql")
494496

497+
trackLatestRemoteHead :: Transaction ()
498+
trackLatestRemoteHead =
499+
executeStatements $(embedProjectStringFile "sql/016-track-latest-remote-head.sql")
500+
495501
schemaVersion :: Transaction SchemaVersion
496502
schemaVersion =
497503
queryOneCol
@@ -4140,7 +4146,8 @@ loadRemoteBranch rpid host rbid =
41404146
project_id,
41414147
branch_id,
41424148
host,
4143-
name
4149+
name,
4150+
last_known_causal_hash
41444151
FROM
41454152
remote_project_branch
41464153
WHERE
@@ -4149,28 +4156,46 @@ loadRemoteBranch rpid host rbid =
41494156
AND host = :host
41504157
|]
41514158

4152-
ensureRemoteProjectBranch :: RemoteProjectId -> URI -> RemoteProjectBranchId -> ProjectBranchName -> Transaction ()
4153-
ensureRemoteProjectBranch rpid host rbid name =
4159+
ensureRemoteProjectBranch :: RemoteProjectId -> URI -> RemoteProjectBranchId -> ProjectBranchName -> Maybe CausalHashId -> Transaction ()
4160+
ensureRemoteProjectBranch rpid host rbid name lastKnownCausalHash =
41544161
execute
41554162
[sql|
41564163
INSERT INTO remote_project_branch (
41574164
project_id,
41584165
host,
41594166
branch_id,
4160-
name)
4167+
name,
4168+
last_known_head)
41614169
VALUES (
41624170
:rpid,
41634171
:host,
41644172
:rbid,
4165-
:name)
4173+
:name,
4174+
:lastKnownCausalHash
4175+
)
41664176
ON CONFLICT (
41674177
project_id,
41684178
branch_id,
41694179
host)
4170-
-- should this update the name instead?
4171-
DO NOTHING
4180+
DO UPDATE
4181+
SET name = :name,
4182+
last_known_causal_hash = :lastKnownCausalHash
41724183
|]
41734184

4185+
setRemoteProjectBranchLastKnownCausalHash :: URI -> RemoteProjectId -> RemoteProjectBranchId -> CausalHashId -> Transaction ()
4186+
setRemoteProjectBranchLastKnownCausalHash host rpid rbid causalHashId =
4187+
execute
4188+
[sql|
4189+
UPDATE
4190+
remote_project_branch
4191+
SET
4192+
last_known_causal_hash = :causalHashId
4193+
WHERE
4194+
project_id = :rpid
4195+
AND branch_id = :rbid
4196+
AND host = :host
4197+
|]
4198+
41744199
expectRemoteProjectBranchName :: URI -> RemoteProjectId -> RemoteProjectBranchId -> Transaction ProjectBranchName
41754200
expectRemoteProjectBranchName host projectId branchId =
41764201
queryOneCol

codebase2/codebase-sqlite/U/Codebase/Sqlite/RemoteProjectBranch.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ where
55

66
import Network.URI (URI)
77
import Network.URI.Orphans.Sqlite ()
8-
import U.Codebase.Sqlite.DbId (RemoteProjectBranchId, RemoteProjectId)
8+
import U.Codebase.Sqlite.DbId (CausalHashId, RemoteProjectBranchId, RemoteProjectId)
99
import Unison.Core.Orphans.Sqlite ()
1010
import Unison.Core.Project (ProjectBranchName)
1111
import Unison.Prelude
@@ -15,7 +15,9 @@ data RemoteProjectBranch = RemoteProjectBranch
1515
{ projectId :: RemoteProjectId,
1616
branchId :: RemoteProjectBranchId,
1717
host :: URI,
18-
name :: ProjectBranchName
18+
name :: ProjectBranchName,
19+
-- Note that there's no guarantee that the causals for this hash have been downloaded/synced into the codebase.
20+
lastKnownCausalHash :: CausalHashId
1921
}
2022
deriving stock (Generic, Show)
2123
deriving anyclass (ToRow, FromRow)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Add a field for tracking the latest known causal hash for each remote project branch.
2+
-- It's helpful for when we need to tell Share how much we know about a branch.
3+
4+
ALTER TABLE remote_project
5+
-- Note that there isn't a guarantee this hash has actually been synced into the codebase.
6+
ADD COLUMN last_known_causal_hash INTEGER NULL REFERENCES hash(id)
7+
ON DELETE SET NULL;

codebase2/codebase-sqlite/unison-codebase-sqlite.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extra-source-files:
2525
sql/013-add-project-branch-reflog-table.sql
2626
sql/014-add-project-branch-causal-hash-id.sql
2727
sql/015-add-project-branch-last-accessed.sql
28+
sql/016-track-latest-remote-head.sql
2829
sql/create.sql
2930

3031
source-repository head

parser-typechecker/src/Unison/Codebase/SqliteCodebase/Migrations.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ migrations regionVar getDeclType termBuffer declBuffer rootCodebasePath =
8585
sqlMigration 15 Q.addSquashResultTableIfNotExists,
8686
sqlMigration 16 Q.cdToProjectRoot,
8787
(17 {- This migration takes a raw sqlite connection -}, \conn -> migrateSchema16To17 conn),
88-
sqlMigration 18 Q.addProjectBranchLastAccessedColumn
88+
sqlMigration 18 Q.addProjectBranchLastAccessedColumn,
89+
sqlMigration 19 Q.trackLatestRemoteHead
8990
]
9091
where
9192
runT :: Sqlite.Transaction () -> Sqlite.Connection -> IO ()

unison-cli/src/Unison/Cli/Share/Projects.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ import Unison.Codebase.Editor.Output qualified as Output
4949
import Unison.Hash32 (Hash32)
5050
import Unison.Prelude
5151
import Unison.Project (ProjectAndBranch (..), ProjectBranchName, ProjectName)
52+
import Unison.Share.API.Hash qualified as HashJWT
5253
import Unison.Share.API.Projects qualified as Share.API
5354
import Unison.Share.Codeserver (defaultCodeserver)
5455
import Unison.Share.Types (codeserverBaseURL)
56+
import Unison.Sync.Common qualified as Sync
5557

5658
-- | Get a project by id.
5759
--
@@ -193,14 +195,17 @@ onGotProjectBranch :: Share.API.ProjectBranch -> Cli RemoteProjectBranch
193195
onGotProjectBranch branch = do
194196
let projectId = RemoteProjectId (branch ^. #projectId)
195197
let branchId = RemoteProjectBranchId (branch ^. #branchId)
198+
let causalHash = Sync.hash32ToCausalHash $ HashJWT.hashJWTHash (branch ^. #branchHead)
196199
projectName <- validateProjectName (branch ^. #projectName)
197200
branchName <- validateBranchName (branch ^. #branchName)
198201
Cli.runTransaction do
202+
causalHashId <- Queries.saveCausalHash causalHash
199203
Queries.ensureRemoteProjectBranch
200204
projectId
201205
hardCodedUri
202206
branchId
203207
branchName
208+
(Just causalHashId)
204209
pure
205210
RemoteProjectBranch
206211
{ projectId,

0 commit comments

Comments
 (0)