Skip to content

Commit 7b36b6c

Browse files
committed
Fix up query syntax
1 parent efce396 commit 7b36b6c

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

share-api/src/Share/Web/UCM/HistoryComments/Queries.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Share.Web.UCM.HistoryComments.Queries
88
where
99

1010
import Control.Lens
11+
import Data.Foldable qualified as Foldable
1112
import Data.Set qualified as Set
1213
import Data.Set.NonEmpty (NESet)
1314
import Data.Set.NonEmpty qualified as NESet
@@ -71,9 +72,12 @@ insertThumbprints :: (PG.QueryA m) => NESet Text -> m ()
7172
insertThumbprints thumbprints = do
7273
PG.execute_
7374
[PG.sql|
75+
WITH thumbprints(thumbprint) AS (
76+
SELECT * FROM ^{PG.singleColumnTable $ Foldable.toList thumbprints}
77+
)
7478
INSERT INTO personal_keys (thumbprint)
75-
SELECT * FROM ^{PG.singleColumnTable $ toList thumbprints}
76-
ON CONFLICT (thumbprint) DO NOTHING
79+
SELECT thumbprint FROM thumbprints
80+
ON CONFLICT DO NOTHING
7781
|]
7882

7983
-- Convert milliseconds since epoch to UTCTime _exactly_.
@@ -111,18 +115,19 @@ insertHistoryComments !_authZ projectId chunks = PG.pipelined $ do
111115
PG.execute_
112116
[PG.sql|
113117
WITH new_comments(author, created_at_ms, author_thumbprint, causal_hash, comment_hash) AS (
114-
VALUES ^{PG.toTable commentsTable}
118+
SELECT * FROM ^{PG.toTable commentsTable}
115119
)
116120
INSERT INTO history_comments(causal_id, author, created_at_ms, comment_hash, author_key_id)
117121
SELECT causal.id, nc.author, nc.created_at_ms, nc.comment_hash, pk.id
118122
FROM new_comments nc
119-
JOIN causal causal
123+
JOIN causals causal
120124
ON causal.hash = nc.causal_hash
121125
JOIN personal_keys pk
122126
ON pk.thumbprint = nc.author_thumbprint
123-
ON CONFLICT DO NOTHING
127+
ON CONFLICT (comment_hash) DO NOTHING
124128
|]
125129
where
130+
commentsTable :: [(Text, Int64, Text, Hash32, Hash32)]
126131
commentsTable =
127132
comments <&> \HistoryComment {..} ->
128133
( author,

sql/2025-11-20_history-comments.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ CREATE TABLE personal_keys (
77
-- The user registered to this key, which is proven by providing a signed
88
-- assertion using the key.
99
-- It may be null if the key is not yet registered to a user.
10-
user_id INTEGER NULL REFERENCES users(id) ON DELETE SET NULL,
11-
created_at TIMESTAMPTZ NOT NULL,
10+
user_id UUID NULL REFERENCES users(id) ON DELETE SET NULL,
11+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
1212

1313
-- Ensure that public_jwk and user_id are either both null or both non-null.
14-
CHECK (public_jwk IS NULL = user_id IS NULL)
14+
CHECK ((public_jwk IS NULL) = (user_id IS NULL))
1515
);
1616

1717
CREATE INDEX idx_personal_keys_user_id ON personal_keys(user_id, created_at)
@@ -59,7 +59,7 @@ CREATE TABLE history_comment_revisions (
5959
revision_hash TEXT UNIQUE NOT NULL,
6060

6161
-- The signature of the comment's author on the revision hash.
62-
author_signature BLOB NOT NULL
62+
author_signature BYTEA NOT NULL
6363
);
6464

6565
CREATE INDEX idx_history_comment_revisions_comment_id ON history_comment_revisions(comment_id, created_at_ms DESC);
@@ -69,7 +69,7 @@ CREATE INDEX idx_history_comment_revisions_comment_id ON history_comment_revisio
6969
-- on a given project.
7070
CREATE TABLE history_comment_revisions_project_discovery (
7171
comment_revision_id INTEGER NOT NULL REFERENCES history_comment_revisions(id),
72-
project_id INTEGER NOT NULL REFERENCES projects(id),
72+
project_id UUID NOT NULL REFERENCES projects(id),
7373
discovered_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
7474
PRIMARY KEY (project_id, comment_revision_id)
7575
);

0 commit comments

Comments
 (0)