diff --git a/sqldb/migrations.go b/sqldb/migrations.go index 83634d0e5..7722fda4f 100644 --- a/sqldb/migrations.go +++ b/sqldb/migrations.go @@ -56,6 +56,11 @@ var ( Version: 5, SchemaVersion: 5, }, + { + Name: "000006_invoice_migration", + Version: 6, + SchemaVersion: 6, + }, } ) diff --git a/sqldb/sqlc/invoices.sql.go b/sqldb/sqlc/invoices.sql.go index 4356b74c9..0fea7541e 100644 --- a/sqldb/sqlc/invoices.sql.go +++ b/sqldb/sqlc/invoices.sql.go @@ -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 diff --git a/sqldb/sqlc/migrations/000006_invoice_migration.down.sql b/sqldb/sqlc/migrations/000006_invoice_migration.down.sql new file mode 100644 index 000000000..a95d34f3a --- /dev/null +++ b/sqldb/sqlc/migrations/000006_invoice_migration.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS invoice_payment_hashes; diff --git a/sqldb/sqlc/migrations/000006_invoice_migration.up.sql b/sqldb/sqlc/migrations/000006_invoice_migration.up.sql new file mode 100644 index 000000000..2e78b0372 --- /dev/null +++ b/sqldb/sqlc/migrations/000006_invoice_migration.up.sql @@ -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); diff --git a/sqldb/sqlc/models.go b/sqldb/sqlc/models.go index 1476d0470..f69d0352b 100644 --- a/sqldb/sqlc/models.go +++ b/sqldb/sqlc/models.go @@ -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 diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index 3471dc015..6f05b32c2 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -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 diff --git a/sqldb/sqlc/queries/invoices.sql b/sqldb/sqlc/queries/invoices.sql index 52abd9fc7..025189a65 100644 --- a/sqldb/sqlc/queries/invoices.sql +++ b/sqldb/sqlc/queries/invoices.sql @@ -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;