mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-11 12:12:57 +02:00
sqldb+graphdb: SQL methods for Node CRUD
In this commit, we add the various sqlc queries that we need in order to implement the following V1Store methods: - AddLightningNode - FetchLightningNode - HasLightningNode - AddrsForNode - DeleteLightningNode - FetchNodeFeatures These are implemented by SQLStore which then lets us use the SQLStore backend for the following unit tests: - TestNodeInsertionAndDeletion - TestLightningNodePersistence
This commit is contained in:
359
sqldb/sqlc/graph.sql.go
Normal file
359
sqldb/sqlc/graph.sql.go
Normal file
@@ -0,0 +1,359 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
// source: graph.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
|
||||
DELETE FROM node_extra_types
|
||||
WHERE node_id = $1
|
||||
AND type = $2
|
||||
`
|
||||
|
||||
type DeleteExtraNodeTypeParams struct {
|
||||
NodeID int64
|
||||
Type int64
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
|
||||
DELETE FROM node_addresses
|
||||
WHERE node_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
|
||||
DELETE FROM nodes
|
||||
WHERE pub_key = $1
|
||||
AND version = $2
|
||||
`
|
||||
|
||||
type DeleteNodeByPubKeyParams struct {
|
||||
PubKey []byte
|
||||
Version int16
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
|
||||
return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
|
||||
}
|
||||
|
||||
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
|
||||
DELETE FROM node_features
|
||||
WHERE node_id = $1
|
||||
AND feature_bit = $2
|
||||
`
|
||||
|
||||
type DeleteNodeFeatureParams struct {
|
||||
NodeID int64
|
||||
FeatureBit int32
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
|
||||
return err
|
||||
}
|
||||
|
||||
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
|
||||
SELECT node_id, type, value
|
||||
FROM node_extra_types
|
||||
WHERE node_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]NodeExtraType, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []NodeExtraType
|
||||
for rows.Next() {
|
||||
var i NodeExtraType
|
||||
if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getNodeAddressesByPubKey = `-- name: GetNodeAddressesByPubKey :many
|
||||
SELECT a.type, a.address
|
||||
FROM nodes n
|
||||
LEFT JOIN node_addresses a ON a.node_id = n.id
|
||||
WHERE n.pub_key = $1 AND n.version = $2
|
||||
ORDER BY a.type ASC, a.position ASC
|
||||
`
|
||||
|
||||
type GetNodeAddressesByPubKeyParams struct {
|
||||
PubKey []byte
|
||||
Version int16
|
||||
}
|
||||
|
||||
type GetNodeAddressesByPubKeyRow struct {
|
||||
Type sql.NullInt16
|
||||
Address sql.NullString
|
||||
}
|
||||
|
||||
func (q *Queries) GetNodeAddressesByPubKey(ctx context.Context, arg GetNodeAddressesByPubKeyParams) ([]GetNodeAddressesByPubKeyRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getNodeAddressesByPubKey, arg.PubKey, arg.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetNodeAddressesByPubKeyRow
|
||||
for rows.Next() {
|
||||
var i GetNodeAddressesByPubKeyRow
|
||||
if err := rows.Scan(&i.Type, &i.Address); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
|
||||
SELECT id, version, pub_key, alias, last_update, color, signature
|
||||
FROM nodes
|
||||
WHERE pub_key = $1
|
||||
AND version = $2
|
||||
`
|
||||
|
||||
type GetNodeByPubKeyParams struct {
|
||||
PubKey []byte
|
||||
Version int16
|
||||
}
|
||||
|
||||
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (Node, error) {
|
||||
row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
|
||||
var i Node
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Version,
|
||||
&i.PubKey,
|
||||
&i.Alias,
|
||||
&i.LastUpdate,
|
||||
&i.Color,
|
||||
&i.Signature,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getNodeFeatures = `-- name: GetNodeFeatures :many
|
||||
SELECT node_id, feature_bit
|
||||
FROM node_features
|
||||
WHERE node_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]NodeFeature, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []NodeFeature
|
||||
for rows.Next() {
|
||||
var i NodeFeature
|
||||
if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
|
||||
SELECT f.feature_bit
|
||||
FROM nodes n
|
||||
JOIN node_features f ON f.node_id = n.id
|
||||
WHERE n.pub_key = $1
|
||||
AND n.version = $2
|
||||
`
|
||||
|
||||
type GetNodeFeaturesByPubKeyParams struct {
|
||||
PubKey []byte
|
||||
Version int16
|
||||
}
|
||||
|
||||
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []int32
|
||||
for rows.Next() {
|
||||
var feature_bit int32
|
||||
if err := rows.Scan(&feature_bit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, feature_bit)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertNodeAddress = `-- name: InsertNodeAddress :exec
|
||||
/* ─────────────────────────────────────────────
|
||||
node_addresses table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
INSERT INTO node_addresses (
|
||||
node_id,
|
||||
type,
|
||||
address,
|
||||
position
|
||||
) VALUES (
|
||||
$1, $2, $3, $4
|
||||
)
|
||||
`
|
||||
|
||||
type InsertNodeAddressParams struct {
|
||||
NodeID int64
|
||||
Type int16
|
||||
Address string
|
||||
Position int32
|
||||
}
|
||||
|
||||
func (q *Queries) InsertNodeAddress(ctx context.Context, arg InsertNodeAddressParams) error {
|
||||
_, err := q.db.ExecContext(ctx, insertNodeAddress,
|
||||
arg.NodeID,
|
||||
arg.Type,
|
||||
arg.Address,
|
||||
arg.Position,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertNodeFeature = `-- name: InsertNodeFeature :exec
|
||||
/* ─────────────────────────────────────────────
|
||||
node_features table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
INSERT INTO node_features (
|
||||
node_id, feature_bit
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
`
|
||||
|
||||
type InsertNodeFeatureParams struct {
|
||||
NodeID int64
|
||||
FeatureBit int32
|
||||
}
|
||||
|
||||
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
|
||||
_, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertNode = `-- name: UpsertNode :one
|
||||
/* ─────────────────────────────────────────────
|
||||
nodes table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
INSERT INTO nodes (
|
||||
version, pub_key, alias, last_update, color, signature
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6
|
||||
)
|
||||
ON CONFLICT (pub_key, version)
|
||||
-- Update the following fields if a conflict occurs on pub_key
|
||||
-- and version.
|
||||
DO UPDATE SET
|
||||
alias = EXCLUDED.alias,
|
||||
last_update = EXCLUDED.last_update,
|
||||
color = EXCLUDED.color,
|
||||
signature = EXCLUDED.signature
|
||||
WHERE EXCLUDED.last_update > nodes.last_update
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type UpsertNodeParams struct {
|
||||
Version int16
|
||||
PubKey []byte
|
||||
Alias sql.NullString
|
||||
LastUpdate sql.NullInt64
|
||||
Color sql.NullString
|
||||
Signature []byte
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, upsertNode,
|
||||
arg.Version,
|
||||
arg.PubKey,
|
||||
arg.Alias,
|
||||
arg.LastUpdate,
|
||||
arg.Color,
|
||||
arg.Signature,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
|
||||
/* ─────────────────────────────────────────────
|
||||
node_extra_types table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
INSERT INTO node_extra_types (
|
||||
node_id, type, value
|
||||
)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (type, node_id)
|
||||
-- Update the value if a conflict occurs on type
|
||||
-- and node_id.
|
||||
DO UPDATE SET value = EXCLUDED.value
|
||||
`
|
||||
|
||||
type UpsertNodeExtraTypeParams struct {
|
||||
NodeID int64
|
||||
Type int64
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
|
||||
_, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
|
||||
return err
|
||||
}
|
@@ -13,13 +13,18 @@ import (
|
||||
type Querier interface {
|
||||
ClearKVInvoiceHashIndex(ctx context.Context) error
|
||||
DeleteCanceledInvoices(ctx context.Context) (sql.Result, error)
|
||||
DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error
|
||||
DeleteInvoice(ctx context.Context, arg DeleteInvoiceParams) (sql.Result, error)
|
||||
DeleteNodeAddresses(ctx context.Context, nodeID int64) error
|
||||
DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error)
|
||||
DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error
|
||||
FetchAMPSubInvoiceHTLCs(ctx context.Context, arg FetchAMPSubInvoiceHTLCsParams) ([]FetchAMPSubInvoiceHTLCsRow, error)
|
||||
FetchAMPSubInvoices(ctx context.Context, arg FetchAMPSubInvoicesParams) ([]AmpSubInvoice, error)
|
||||
FetchSettledAMPSubInvoices(ctx context.Context, arg FetchSettledAMPSubInvoicesParams) ([]FetchSettledAMPSubInvoicesRow, error)
|
||||
FilterInvoices(ctx context.Context, arg FilterInvoicesParams) ([]Invoice, error)
|
||||
GetAMPInvoiceID(ctx context.Context, setID []byte) (int64, error)
|
||||
GetDatabaseVersion(ctx context.Context) (int32, error)
|
||||
GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]NodeExtraType, error)
|
||||
// This method may return more than one invoice if filter using multiple fields
|
||||
// from different invoices. It is the caller's responsibility to ensure that
|
||||
// we bubble up an error in those cases.
|
||||
@@ -31,6 +36,10 @@ type Querier interface {
|
||||
GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error)
|
||||
GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error)
|
||||
GetMigration(ctx context.Context, version int32) (time.Time, error)
|
||||
GetNodeAddressesByPubKey(ctx context.Context, arg GetNodeAddressesByPubKeyParams) ([]GetNodeAddressesByPubKeyRow, error)
|
||||
GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (Node, error)
|
||||
GetNodeFeatures(ctx context.Context, nodeID int64) ([]NodeFeature, error)
|
||||
GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error)
|
||||
InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error
|
||||
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
|
||||
InsertInvoice(ctx context.Context, arg InsertInvoiceParams) (int64, error)
|
||||
@@ -39,6 +48,8 @@ type Querier interface {
|
||||
InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertInvoiceHTLCCustomRecordParams) error
|
||||
InsertKVInvoiceKeyAndAddIndex(ctx context.Context, arg InsertKVInvoiceKeyAndAddIndexParams) error
|
||||
InsertMigratedInvoice(ctx context.Context, arg InsertMigratedInvoiceParams) (int64, error)
|
||||
InsertNodeAddress(ctx context.Context, arg InsertNodeAddressParams) error
|
||||
InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error
|
||||
NextInvoiceSettleIndex(ctx context.Context) (int64, error)
|
||||
OnAMPSubInvoiceCanceled(ctx context.Context, arg OnAMPSubInvoiceCanceledParams) error
|
||||
OnAMPSubInvoiceCreated(ctx context.Context, arg OnAMPSubInvoiceCreatedParams) error
|
||||
@@ -55,6 +66,8 @@ type Querier interface {
|
||||
UpdateInvoiceHTLCs(ctx context.Context, arg UpdateInvoiceHTLCsParams) error
|
||||
UpdateInvoiceState(ctx context.Context, arg UpdateInvoiceStateParams) (sql.Result, error)
|
||||
UpsertAMPSubInvoice(ctx context.Context, arg UpsertAMPSubInvoiceParams) (sql.Result, error)
|
||||
UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error)
|
||||
UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
|
@@ -0,0 +1,112 @@
|
||||
/* ─────────────────────────────────────────────
|
||||
nodes table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
-- name: UpsertNode :one
|
||||
INSERT INTO nodes (
|
||||
version, pub_key, alias, last_update, color, signature
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6
|
||||
)
|
||||
ON CONFLICT (pub_key, version)
|
||||
-- Update the following fields if a conflict occurs on pub_key
|
||||
-- and version.
|
||||
DO UPDATE SET
|
||||
alias = EXCLUDED.alias,
|
||||
last_update = EXCLUDED.last_update,
|
||||
color = EXCLUDED.color,
|
||||
signature = EXCLUDED.signature
|
||||
WHERE EXCLUDED.last_update > nodes.last_update
|
||||
RETURNING id;
|
||||
|
||||
-- name: GetNodeByPubKey :one
|
||||
SELECT *
|
||||
FROM nodes
|
||||
WHERE pub_key = $1
|
||||
AND version = $2;
|
||||
|
||||
-- name: DeleteNodeByPubKey :execresult
|
||||
DELETE FROM nodes
|
||||
WHERE pub_key = $1
|
||||
AND version = $2;
|
||||
|
||||
/* ─────────────────────────────────────────────
|
||||
node_features table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
-- name: InsertNodeFeature :exec
|
||||
INSERT INTO node_features (
|
||||
node_id, feature_bit
|
||||
) VALUES (
|
||||
$1, $2
|
||||
);
|
||||
|
||||
-- name: GetNodeFeatures :many
|
||||
SELECT *
|
||||
FROM node_features
|
||||
WHERE node_id = $1;
|
||||
|
||||
-- name: GetNodeFeaturesByPubKey :many
|
||||
SELECT f.feature_bit
|
||||
FROM nodes n
|
||||
JOIN node_features f ON f.node_id = n.id
|
||||
WHERE n.pub_key = $1
|
||||
AND n.version = $2;
|
||||
|
||||
-- name: DeleteNodeFeature :exec
|
||||
DELETE FROM node_features
|
||||
WHERE node_id = $1
|
||||
AND feature_bit = $2;
|
||||
|
||||
/* ─────────────────────────────────────────────
|
||||
node_addresses table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
-- name: InsertNodeAddress :exec
|
||||
INSERT INTO node_addresses (
|
||||
node_id,
|
||||
type,
|
||||
address,
|
||||
position
|
||||
) VALUES (
|
||||
$1, $2, $3, $4
|
||||
);
|
||||
|
||||
-- name: GetNodeAddressesByPubKey :many
|
||||
SELECT a.type, a.address
|
||||
FROM nodes n
|
||||
LEFT JOIN node_addresses a ON a.node_id = n.id
|
||||
WHERE n.pub_key = $1 AND n.version = $2
|
||||
ORDER BY a.type ASC, a.position ASC;
|
||||
|
||||
-- name: DeleteNodeAddresses :exec
|
||||
DELETE FROM node_addresses
|
||||
WHERE node_id = $1;
|
||||
|
||||
/* ─────────────────────────────────────────────
|
||||
node_extra_types table queries
|
||||
─────────────────────────────────────────────
|
||||
*/
|
||||
|
||||
-- name: UpsertNodeExtraType :exec
|
||||
INSERT INTO node_extra_types (
|
||||
node_id, type, value
|
||||
)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (type, node_id)
|
||||
-- Update the value if a conflict occurs on type
|
||||
-- and node_id.
|
||||
DO UPDATE SET value = EXCLUDED.value;
|
||||
|
||||
-- name: GetExtraNodeTypes :many
|
||||
SELECT *
|
||||
FROM node_extra_types
|
||||
WHERE node_id = $1;
|
||||
|
||||
-- name: DeleteExtraNodeType :exec
|
||||
DELETE FROM node_extra_types
|
||||
WHERE node_id = $1
|
||||
AND type = $2;
|
||||
|
Reference in New Issue
Block a user