From c61617200b7830994bf85fc38485241e513391a6 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Sun, 26 Oct 2025 12:16:13 +0200 Subject: [PATCH] sqldb: update node query for v2 Here we update the UpdateNode query so that it can be used to insert the new blockheight field for a v2 node. --- sqldb/sqlc/graph.sql.go | 46 ++++++++++++++++++++++-------------- sqldb/sqlc/queries/graph.sql | 18 +++++++++----- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/sqldb/sqlc/graph.sql.go b/sqldb/sqlc/graph.sql.go index 8b10e4967..cdf15a90b 100644 --- a/sqldb/sqlc/graph.sql.go +++ b/sqldb/sqlc/graph.sql.go @@ -3673,9 +3673,9 @@ const upsertNode = `-- name: UpsertNode :one */ INSERT INTO graph_nodes ( - version, pub_key, alias, last_update, color, signature + version, pub_key, alias, last_update, block_height, color, signature ) VALUES ( - $1, $2, $3, $4, $5, $6 + $1, $2, $3, $4, $5, $6, $7 ) ON CONFLICT (pub_key, version) -- Update the following fields if a conflict occurs on pub_key @@ -3683,20 +3683,24 @@ ON CONFLICT (pub_key, version) DO UPDATE SET alias = EXCLUDED.alias, last_update = EXCLUDED.last_update, + block_height = EXCLUDED.block_height, color = EXCLUDED.color, signature = EXCLUDED.signature -WHERE graph_nodes.last_update IS NULL - OR EXCLUDED.last_update > graph_nodes.last_update +WHERE (graph_nodes.last_update IS NULL + OR EXCLUDED.last_update > graph_nodes.last_update) +AND (graph_nodes.block_height IS NULL + OR EXCLUDED.block_height >= graph_nodes.block_height) RETURNING id ` type UpsertNodeParams struct { - Version int16 - PubKey []byte - Alias sql.NullString - LastUpdate sql.NullInt64 - Color sql.NullString - Signature []byte + Version int16 + PubKey []byte + Alias sql.NullString + LastUpdate sql.NullInt64 + BlockHeight sql.NullInt64 + Color sql.NullString + Signature []byte } func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) { @@ -3705,6 +3709,7 @@ func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, arg.PubKey, arg.Alias, arg.LastUpdate, + arg.BlockHeight, arg.Color, arg.Signature, ) @@ -3801,9 +3806,9 @@ func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEnt const upsertSourceNode = `-- name: UpsertSourceNode :one INSERT INTO graph_nodes ( - version, pub_key, alias, last_update, color, signature + version, pub_key, alias, last_update, block_height, color, signature ) VALUES ( - $1, $2, $3, $4, $5, $6 + $1, $2, $3, $4, $5, $6, $7 ) ON CONFLICT (pub_key, version) -- Update the following fields if a conflict occurs on pub_key @@ -3811,20 +3816,24 @@ ON CONFLICT (pub_key, version) DO UPDATE SET alias = EXCLUDED.alias, last_update = EXCLUDED.last_update, + block_height = EXCLUDED.block_height, color = EXCLUDED.color, signature = EXCLUDED.signature WHERE graph_nodes.last_update IS NULL OR EXCLUDED.last_update >= graph_nodes.last_update +AND (graph_nodes.block_height IS NULL + OR EXCLUDED.block_height >= graph_nodes.block_height) RETURNING id ` type UpsertSourceNodeParams struct { - Version int16 - PubKey []byte - Alias sql.NullString - LastUpdate sql.NullInt64 - Color sql.NullString - Signature []byte + Version int16 + PubKey []byte + Alias sql.NullString + LastUpdate sql.NullInt64 + BlockHeight sql.NullInt64 + Color sql.NullString + Signature []byte } // We use a separate upsert for our own node since we want to be less strict @@ -3836,6 +3845,7 @@ func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodePara arg.PubKey, arg.Alias, arg.LastUpdate, + arg.BlockHeight, arg.Color, arg.Signature, ) diff --git a/sqldb/sqlc/queries/graph.sql b/sqldb/sqlc/queries/graph.sql index a8ff040d9..480437655 100644 --- a/sqldb/sqlc/queries/graph.sql +++ b/sqldb/sqlc/queries/graph.sql @@ -5,9 +5,9 @@ -- name: UpsertNode :one INSERT INTO graph_nodes ( - version, pub_key, alias, last_update, color, signature + version, pub_key, alias, last_update, block_height, color, signature ) VALUES ( - $1, $2, $3, $4, $5, $6 + $1, $2, $3, $4, $5, $6, $7 ) ON CONFLICT (pub_key, version) -- Update the following fields if a conflict occurs on pub_key @@ -15,10 +15,13 @@ ON CONFLICT (pub_key, version) DO UPDATE SET alias = EXCLUDED.alias, last_update = EXCLUDED.last_update, + block_height = EXCLUDED.block_height, color = EXCLUDED.color, signature = EXCLUDED.signature -WHERE graph_nodes.last_update IS NULL - OR EXCLUDED.last_update > graph_nodes.last_update +WHERE (graph_nodes.last_update IS NULL + OR EXCLUDED.last_update > graph_nodes.last_update) +AND (graph_nodes.block_height IS NULL + OR EXCLUDED.block_height >= graph_nodes.block_height) RETURNING id; -- We use a separate upsert for our own node since we want to be less strict @@ -26,9 +29,9 @@ RETURNING id; -- update the record even if the last_update is the same as what we have. -- name: UpsertSourceNode :one INSERT INTO graph_nodes ( - version, pub_key, alias, last_update, color, signature + version, pub_key, alias, last_update, block_height, color, signature ) VALUES ( - $1, $2, $3, $4, $5, $6 + $1, $2, $3, $4, $5, $6, $7 ) ON CONFLICT (pub_key, version) -- Update the following fields if a conflict occurs on pub_key @@ -36,10 +39,13 @@ ON CONFLICT (pub_key, version) DO UPDATE SET alias = EXCLUDED.alias, last_update = EXCLUDED.last_update, + block_height = EXCLUDED.block_height, color = EXCLUDED.color, signature = EXCLUDED.signature WHERE graph_nodes.last_update IS NULL OR EXCLUDED.last_update >= graph_nodes.last_update +AND (graph_nodes.block_height IS NULL + OR EXCLUDED.block_height >= graph_nodes.block_height) RETURNING id; -- name: GetNodesByIDs :many