@@ -233,6 +233,10 @@ module U.Codebase.Sqlite.Queries
233233 expectCurrentProjectPath ,
234234 setCurrentProjectPath ,
235235
236+ -- * History Comments
237+ commentOnCausal ,
238+ getLatestCausalComment ,
239+
236240 -- * migrations
237241 runCreateSql ,
238242 addTempEntityTables ,
@@ -254,6 +258,7 @@ module U.Codebase.Sqlite.Queries
254258 addUpdateBranchTable ,
255259 addDerivedDependentsByDependencyIndex ,
256260 addUpgradeBranchTable ,
261+ addHistoryComments ,
257262
258263 -- ** schema version
259264 currentSchemaVersion ,
@@ -284,6 +289,12 @@ module U.Codebase.Sqlite.Queries
284289 x2cDecl ,
285290 checkBranchExistsForCausalHash ,
286291
292+ -- * Config
293+ getAuthorName ,
294+ setAuthorName ,
295+ getConfigValue ,
296+ setConfigValue ,
297+
287298 -- * Types
288299 TextPathSegments ,
289300 JsonParseFailure (.. ),
@@ -319,6 +330,8 @@ import Data.Time qualified as Time
319330import Data.Vector qualified as Vector
320331import Network.URI (URI )
321332import U.Codebase.Branch.Type (NamespaceStats (.. ))
333+ import U.Codebase.Config (AuthorName , ConfigKey )
334+ import U.Codebase.Config qualified as Config
322335import U.Codebase.Decl qualified as C
323336import U.Codebase.Decl qualified as C.Decl
324337import U.Codebase.HashTags (BranchHash (.. ), CausalHash (.. ), PatchHash (.. ))
@@ -336,6 +349,7 @@ import U.Codebase.Sqlite.DbId
336349 CausalHashId (.. ),
337350 HashId (.. ),
338351 HashVersion ,
352+ HistoryCommentId ,
339353 ObjectId (.. ),
340354 PatchObjectId (.. ),
341355 ProjectBranchId (.. ),
@@ -351,6 +365,7 @@ import U.Codebase.Sqlite.Decode
351365import U.Codebase.Sqlite.Entity (SyncEntity )
352366import U.Codebase.Sqlite.Entity qualified as Entity
353367import U.Codebase.Sqlite.HashHandle (HashHandle (.. ))
368+ import U.Codebase.Sqlite.HistoryComment (HistoryComment (.. ))
354369import U.Codebase.Sqlite.LocalIds
355370 ( LocalDefnId (.. ),
356371 LocalIds ,
@@ -413,7 +428,7 @@ type TextPathSegments = [Text]
413428-- * main squeeze
414429
415430currentSchemaVersion :: SchemaVersion
416- currentSchemaVersion = 22
431+ currentSchemaVersion = 23
417432
418433runCreateSql :: Transaction ()
419434runCreateSql =
@@ -499,6 +514,10 @@ addUpgradeBranchTable :: Transaction ()
499514addUpgradeBranchTable =
500515 executeStatements $ (embedProjectStringFile " sql/019-add-upgrade-branch-table.sql" )
501516
517+ addHistoryComments :: Transaction ()
518+ addHistoryComments =
519+ executeStatements $ (embedProjectStringFile " sql/020-add-history-comments.sql" )
520+
502521schemaVersion :: Transaction SchemaVersion
503522schemaVersion =
504523 queryOneCol
@@ -4022,3 +4041,73 @@ saveSquashResult bhId chId =
40224041 )
40234042 ON CONFLICT DO NOTHING
40244043 |]
4044+
4045+ getLatestCausalComment ::
4046+ CausalHashId ->
4047+ Transaction (Maybe (HistoryComment CausalHashId HistoryCommentId ))
4048+ getLatestCausalComment causalHashId =
4049+ queryMaybeRow @ (HistoryCommentId , CausalHashId , Text , Text , Text )
4050+ [sql |
4051+ SELECT cc.id, cc.causal_hash_id, cc.author, ccr.subject, ccr.contents
4052+ FROM history_comments AS cc
4053+ JOIN history_comment_revisions AS ccr ON cc.id = ccr.comment_id
4054+ WHERE cc.causal_hash_id = :causalHashId
4055+ ORDER BY ccr.created_at DESC
4056+ LIMIT 1
4057+ |]
4058+ <&> fmap \ (commentId, causal, author, subject, content) ->
4059+ HistoryComment {author, subject, content, commentId, causal}
4060+
4061+ commentOnCausal :: HistoryComment CausalHashId () -> Transaction ()
4062+ commentOnCausal HistoryComment {author, content, subject, causal = causalHashId} = do
4063+ mayExistingCommentId <-
4064+ queryMaybeCol @ HistoryCommentId
4065+ [sql |
4066+ SELECT id
4067+ FROM history_comments
4068+ WHERE causal_hash_id = :causalHashId
4069+ |]
4070+ commentId <- case mayExistingCommentId of
4071+ Nothing ->
4072+ queryOneCol @ HistoryCommentId
4073+ [sql |
4074+ INSERT INTO history_comments (author, causal_hash_id, created_at)
4075+ VALUES (:author, :causalHashId, strftime('%s', 'now', 'subsec'))
4076+ RETURNING id
4077+ |]
4078+ Just cid -> pure cid
4079+ execute
4080+ [sql |
4081+ INSERT INTO history_comment_revisions (comment_id, subject, contents, created_at)
4082+ VALUES (:commentId, :subject, :content, strftime('%s', 'now', 'subsec'))
4083+ |]
4084+
4085+ getAuthorName :: Transaction (Maybe AuthorName )
4086+ getAuthorName = do
4087+ r <- getConfigValue Config. AuthorNameKey <&> fmap Config. mkAuthorName
4088+ case r of
4089+ Just (Left err) -> error $ " getAuthorName: " <> Text. unpack err
4090+ Just (Right authorName) -> pure (Just authorName)
4091+ Nothing -> pure Nothing
4092+
4093+ setAuthorName :: AuthorName -> Transaction ()
4094+ setAuthorName authorName =
4095+ setConfigValue Config. AuthorNameKey (Config. unAuthorName authorName)
4096+
4097+ setConfigValue :: ConfigKey -> Text -> Transaction ()
4098+ setConfigValue key value =
4099+ execute
4100+ [sql |
4101+ INSERT INTO config (key, value)
4102+ VALUES (:key, :value)
4103+ ON CONFLICT (key) DO UPDATE SET value = excluded.value
4104+ |]
4105+
4106+ getConfigValue :: ConfigKey -> Transaction (Maybe Text )
4107+ getConfigValue key =
4108+ queryMaybeCol
4109+ [sql |
4110+ SELECT value
4111+ FROM config
4112+ WHERE key = :key
4113+ |]
0 commit comments