Skip to content

Commit 864a484

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 864a484

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
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/migrations/000001_invoices.up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ VALUES ('settle_index', 0)
1313
-- invoices table contains all the information shared by all the invoice types.
1414
CREATE TABLE IF NOT EXISTS invoices (
1515
-- The id of the invoice. Translates to the AddIndex.
16-
id INTEGER PRIMARY KEY,
16+
id BIGINT PRIMARY KEY,
1717

1818
-- The hash for this invoice. The invoice hash will always identify that
1919
-- invoice.
@@ -104,7 +104,7 @@ CREATE INDEX IF NOT EXISTS invoice_feature_invoice_id_idx ON invoice_features(in
104104
CREATE TABLE IF NOT EXISTS invoice_htlcs (
105105
-- The id for this htlc. Used in foreign keys instead of the
106106
-- htlc_id/chan_id combination.
107-
id INTEGER PRIMARY KEY,
107+
id BIGINT PRIMARY KEY,
108108

109109
-- Short chan id indicating the htlc's origin. uint64 stored as text.
110110
chan_id TEXT NOT NULL,

sqldb/sqlc/migrations/000003_invoice_events.up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- invoice_event_types stores the different types of invoice events.
22
CREATE TABLE IF NOT EXISTS invoice_event_types(
3-
id INTEGER PRIMARY KEY,
3+
id BIGINT PRIMARY KEY,
44

55
description TEXT NOT NULL
66
);
@@ -34,7 +34,7 @@ WHERE NOT EXISTS (
3434
-- AMP sub invoices. This table can be used to create a historical view of what
3535
-- happened to the node's invoices.
3636
CREATE TABLE IF NOT EXISTS invoice_events (
37-
id INTEGER PRIMARY KEY,
37+
id BIGINT PRIMARY KEY,
3838

3939
-- added_at is the timestamp when this event was added.
4040
added_at TIMESTAMP NOT NULL,

sqldb/sqlc/migrations/000006_invoice_migration.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- don't have a mapping from hash to add index.
44
CREATE TABLE IF NOT EXISTS invoice_payment_hashes (
55
-- id represents is the key of the invoice in the KV store.
6-
id INTEGER PRIMARY KEY,
6+
id BIGINT PRIMARY KEY,
77

88
-- add_index is the KV add index of the invoice.
99
add_index BIGINT NOT NULL,

sqldb/sqlc/migrations/000008_graph.up.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
CREATE TABLE IF NOT EXISTS graph_nodes (
88
-- The db ID of the node. This will only be used DB level
99
-- relations.
10-
id INTEGER PRIMARY KEY,
10+
id BIGINT PRIMARY KEY,
1111

1212
-- The protocol version that this node was gossiped on.
1313
version SMALLINT NOT NULL,
@@ -105,7 +105,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS graph_source_nodes_unique ON graph_source_node
105105
-- channels stores all teh channels that we are aware of in the graph.
106106
CREATE TABLE IF NOT EXISTS graph_channels (
107107
-- The db ID of the channel.
108-
id INTEGER PRIMARY KEY,
108+
id BIGINT PRIMARY KEY,
109109

110110
-- The protocol version that this node was gossiped on.
111111
version SMALLINT NOT NULL,
@@ -218,7 +218,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS graph_channel_extra_types_unique ON graph_chan
218218

219219
CREATE TABLE IF NOT EXISTS graph_channel_policies (
220220
-- The db ID of the channel policy.
221-
id INTEGER PRIMARY KEY,
221+
id BIGINT PRIMARY KEY,
222222

223223
-- The protocol version that this update was gossiped on.
224224
version SMALLINT NOT NULL,
@@ -343,7 +343,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS graph_zombie_channels_channel_id_version_idx
343343

344344
CREATE TABLE IF NOT EXISTS graph_prune_log (
345345
-- The block height that the prune was performed at.
346-
-- NOTE: we don't use INTEGER PRIMARY KEY here since that would
346+
-- NOTE: we don't use BIGINT PRIMARY KEY here since that would
347347
-- get transformed into an auto-incrementing type by our SQL type
348348
-- replacement logic. We don't want that since this must be the
349349
-- actual block height and not an auto-incrementing value.

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)