faraday/db/sqlc/queries/chanevents.sql
bitromortac 2363173325 chanevents+db: add scid-to-peer index
Add ScidToPeerMap, which materialises a snapshot of every short channel
id paired with the pubkey of the channel's remote peer. Forwarding-data
sources index events by short channel id, but downstream analyses need
to attribute behaviour to the peer, not the channel. The map skips
channels whose short channel id is still zero (unconfirmed), so callers
see only fully advertised channels. Coverage extends TestStore with a
two-channel fixture pinning the join.
2026-05-13 14:32:12 +02:00

40 lines
1.1 KiB
SQL

-- name: InsertPeer :one
INSERT INTO peers (pubkey) VALUES ($1) RETURNING id;
-- name: GetPeerByPubKey :one
SELECT * FROM peers WHERE pubkey = $1;
-- name: InsertChannel :one
INSERT INTO channels (channel_point, short_channel_id, peer_id) VALUES ($1, $2, $3) RETURNING id;
-- name: GetChannelByChanPoint :one
SELECT * FROM channels WHERE channel_point = $1;
-- name: GetChannelByShortChanID :one
SELECT * FROM channels WHERE short_channel_id = $1;
-- name: InsertChannelEvent :exec
INSERT INTO channel_events (
channel_id, event_type, timestamp, local_balance_sat, remote_balance_sat,
is_sync
) VALUES ($1, $2, $3, $4, $5, $6);
-- name: GetChannelEvents :many
SELECT * FROM channel_events
WHERE channel_id = $1
AND id > $2
AND timestamp >= $3
AND timestamp < $4
ORDER BY id ASC
LIMIT $5;
-- name: GetLatestChannelEventBefore :one
SELECT * FROM channel_events
WHERE channel_id = $1 AND event_type = $2 AND timestamp < $3
ORDER BY timestamp DESC, id DESC
LIMIT 1;
-- name: GetChannels :many
SELECT c.id, c.short_channel_id, p.pubkey
FROM channels c
JOIN peers p ON c.peer_id = p.id;