Skip to content

Commit 578abf8

Browse files
committed
sqldb: add UpsertSelfNode query
This query is less strict in terms of the latest update timestamp field. We want to be less strict with our own node data since we always want our own updates recorded.
1 parent e2475a8 commit 578abf8

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

sqldb/sqlc/graph.sql.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/querier.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/graph.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ WHERE graph_nodes.last_update IS NULL
2121
OR EXCLUDED.last_update > graph_nodes.last_update
2222
RETURNING id;
2323

24+
-- We use a separate upsert for our own node since we want to be less strict
25+
-- about the last_update field. For our own node, we always want to
26+
-- update the record even if the last_update is the same as what we have.
27+
-- name: UpsertSourceNode :one
28+
INSERT INTO graph_nodes (
29+
version, pub_key, alias, last_update, color, signature
30+
) VALUES (
31+
$1, $2, $3, $4, $5, $6
32+
)
33+
ON CONFLICT (pub_key, version)
34+
-- Update the following fields if a conflict occurs on pub_key
35+
-- and version.
36+
DO UPDATE SET
37+
alias = EXCLUDED.alias,
38+
last_update = EXCLUDED.last_update,
39+
color = EXCLUDED.color,
40+
signature = EXCLUDED.signature
41+
WHERE graph_nodes.last_update IS NULL
42+
OR EXCLUDED.last_update >= graph_nodes.last_update
43+
RETURNING id;
44+
2445
-- name: GetNodesByIDs :many
2546
SELECT *
2647
FROM graph_nodes

0 commit comments

Comments
 (0)