sqldb: add a temporary index to store KV invoice hash to ID mapping

This commit is contained in:
Andras Banki-Horvath 2024-09-11 14:14:25 +02:00
parent 3820497d7f
commit b7d743929d
No known key found for this signature in database
GPG key ID: 80E5375C094198D8
7 changed files with 109 additions and 0 deletions

View file

@ -56,6 +56,11 @@ var (
Version: 5,
SchemaVersion: 5,
},
{
Name: "000006_invoice_migration",
Version: 6,
SchemaVersion: 6,
},
}
)

View file

@ -11,6 +11,15 @@ import (
"time"
)
const clearKVInvoiceHashIndex = `-- name: ClearKVInvoiceHashIndex :exec
DELETE FROM invoice_payment_hashes
`
func (q *Queries) ClearKVInvoiceHashIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, clearKVInvoiceHashIndex)
return err
}
const deleteCanceledInvoices = `-- name: DeleteCanceledInvoices :execresult
DELETE
FROM invoices
@ -405,6 +414,19 @@ func (q *Queries) GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]Invoi
return items, nil
}
const getKVInvoicePaymentHashByAddIndex = `-- name: GetKVInvoicePaymentHashByAddIndex :one
SELECT hash
FROM invoice_payment_hashes
WHERE add_index = $1
`
func (q *Queries) GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error) {
row := q.db.QueryRowContext(ctx, getKVInvoicePaymentHashByAddIndex, addIndex)
var hash []byte
err := row.Scan(&hash)
return hash, err
}
const insertInvoice = `-- name: InsertInvoice :one
INSERT INTO invoices (
hash, preimage, memo, amount_msat, cltv_delta, expiry, payment_addr,
@ -533,6 +555,24 @@ func (q *Queries) InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertI
return err
}
const insertKVInvoiceKeyAndAddIndex = `-- name: InsertKVInvoiceKeyAndAddIndex :exec
INSERT INTO invoice_payment_hashes (
id, add_index
) VALUES (
$1, $2
)
`
type InsertKVInvoiceKeyAndAddIndexParams struct {
ID int32
AddIndex int64
}
func (q *Queries) InsertKVInvoiceKeyAndAddIndex(ctx context.Context, arg InsertKVInvoiceKeyAndAddIndexParams) error {
_, err := q.db.ExecContext(ctx, insertKVInvoiceKeyAndAddIndex, arg.ID, arg.AddIndex)
return err
}
const insertMigratedInvoice = `-- name: InsertMigratedInvoice :one
INSERT INTO invoices (
hash, preimage, settle_index, settled_at, memo, amount_msat, cltv_delta,
@ -601,6 +641,22 @@ func (q *Queries) NextInvoiceSettleIndex(ctx context.Context) (int64, error) {
return current_value, err
}
const setKVInvoicePaymentHash = `-- name: SetKVInvoicePaymentHash :exec
UPDATE invoice_payment_hashes
SET hash = $2
WHERE id = $1
`
type SetKVInvoicePaymentHashParams struct {
ID int32
Hash []byte
}
func (q *Queries) SetKVInvoicePaymentHash(ctx context.Context, arg SetKVInvoicePaymentHashParams) error {
_, err := q.db.ExecContext(ctx, setKVInvoicePaymentHash, arg.ID, arg.Hash)
return err
}
const updateInvoiceAmountPaid = `-- name: UpdateInvoiceAmountPaid :execresult
UPDATE invoices
SET amount_paid_msat = $2

View file

@ -0,0 +1 @@
DROP TABLE IF EXISTS invoice_payment_hashes;

View file

@ -0,0 +1,17 @@
-- invoice_payment_hashes table contains the hash of the invoices. This table
-- is used during KV to SQL invoice migration as in our KV representation we
-- don't have a mapping from hash to add index.
CREATE TABLE IF NOT EXISTS invoice_payment_hashes (
-- id represents is the key of the invoice in the KV store.
id INTEGER PRIMARY KEY,
-- add_index is the KV add index of the invoice.
add_index BIGINT NOT NULL,
-- hash is the payment hash for this invoice.
hash BLOB
);
-- Create an indexes on the add_index and hash columns to speed up lookups.
CREATE INDEX IF NOT EXISTS invoice_payment_hashes_add_index_idx ON invoice_payment_hashes(add_index);
CREATE INDEX IF NOT EXISTS invoice_payment_hashes_hash_idx ON invoice_payment_hashes(hash);

View file

@ -87,6 +87,12 @@ type InvoiceHtlcCustomRecord struct {
HtlcID int64
}
type InvoicePaymentHash struct {
ID int32
AddIndex int64
Hash []byte
}
type InvoiceSequence struct {
Name string
CurrentValue int64

View file

@ -11,6 +11,7 @@ import (
)
type Querier interface {
ClearKVInvoiceHashIndex(ctx context.Context) error
DeleteCanceledInvoices(ctx context.Context) (sql.Result, error)
DeleteInvoice(ctx context.Context, arg DeleteInvoiceParams) (sql.Result, error)
FetchAMPSubInvoiceHTLCs(ctx context.Context, arg FetchAMPSubInvoiceHTLCsParams) ([]FetchAMPSubInvoiceHTLCsRow, error)
@ -27,6 +28,7 @@ type Querier interface {
GetInvoiceFeatures(ctx context.Context, invoiceID int64) ([]InvoiceFeature, error)
GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error)
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)
InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
@ -34,6 +36,7 @@ type Querier interface {
InsertInvoiceFeature(ctx context.Context, arg InsertInvoiceFeatureParams) error
InsertInvoiceHTLC(ctx context.Context, arg InsertInvoiceHTLCParams) (int64, error)
InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertInvoiceHTLCCustomRecordParams) error
InsertKVInvoiceKeyAndAddIndex(ctx context.Context, arg InsertKVInvoiceKeyAndAddIndexParams) error
InsertMigratedInvoice(ctx context.Context, arg InsertMigratedInvoiceParams) (int64, error)
NextInvoiceSettleIndex(ctx context.Context) (int64, error)
OnAMPSubInvoiceCanceled(ctx context.Context, arg OnAMPSubInvoiceCanceledParams) error
@ -42,6 +45,7 @@ type Querier interface {
OnInvoiceCanceled(ctx context.Context, arg OnInvoiceCanceledParams) error
OnInvoiceCreated(ctx context.Context, arg OnInvoiceCreatedParams) error
OnInvoiceSettled(ctx context.Context, arg OnInvoiceSettledParams) error
SetKVInvoicePaymentHash(ctx context.Context, arg SetKVInvoicePaymentHashParams) error
SetMigration(ctx context.Context, arg SetMigrationParams) error
UpdateAMPSubInvoiceHTLCPreimage(ctx context.Context, arg UpdateAMPSubInvoiceHTLCPreimageParams) (sql.Result, error)
UpdateAMPSubInvoiceState(ctx context.Context, arg UpdateAMPSubInvoiceStateParams) error

View file

@ -179,3 +179,23 @@ INSERT INTO invoice_htlc_custom_records (
SELECT ihcr.htlc_id, key, value
FROM invoice_htlcs ih JOIN invoice_htlc_custom_records ihcr ON ih.id=ihcr.htlc_id
WHERE ih.invoice_id = $1;
-- name: InsertKVInvoiceKeyAndAddIndex :exec
INSERT INTO invoice_payment_hashes (
id, add_index
) VALUES (
$1, $2
);
-- name: SetKVInvoicePaymentHash :exec
UPDATE invoice_payment_hashes
SET hash = $2
WHERE id = $1;
-- name: GetKVInvoicePaymentHashByAddIndex :one
SELECT hash
FROM invoice_payment_hashes
WHERE add_index = $1;
-- name: ClearKVInvoiceHashIndex :exec
DELETE FROM invoice_payment_hashes;