sqldb: simplify and fixup the existing invoice store schema and queries

This commit attempts to fix some issues with the invoice store's schema that we
couldn't foresee before the implementation was finished. This is safe as the
schema has not been instantiated yet outside of unit tests. Furthermore the
commit updates invoice store SQL queries according to fixes in the schema as
well as to prepare the higher level implementation in the upcoming commits.
This commit is contained in:
Andras Banki-Horvath
2023-11-22 17:23:23 +01:00
parent 3e0f98a75a
commit 6a360fb2e2
12 changed files with 781 additions and 937 deletions

View File

@@ -7,122 +7,116 @@ package sqlc
import (
"context"
"database/sql"
"time"
)
const deleteInvoiceEvents = `-- name: DeleteInvoiceEvents :exec
DELETE
FROM invoice_events
WHERE invoice_id = $1
`
func (q *Queries) DeleteInvoiceEvents(ctx context.Context, invoiceID int32) error {
_, err := q.db.ExecContext(ctx, deleteInvoiceEvents, invoiceID)
return err
}
const insertInvoiceEvent = `-- name: InsertInvoiceEvent :exec
const onAMPSubInvoiceCanceled = `-- name: OnAMPSubInvoiceCanceled :exec
INSERT INTO invoice_events (
created_at, invoice_id, htlc_id, set_id, event_type, event_metadata
added_at, event_type, invoice_id, set_id
) VALUES (
$1, $2, $3, $4, $5, $6
$1, 4, $2, $3
)
`
type InsertInvoiceEventParams struct {
CreatedAt time.Time
InvoiceID int32
HtlcID sql.NullInt64
SetID []byte
EventType int32
EventMetadata []byte
type OnAMPSubInvoiceCanceledParams struct {
AddedAt time.Time
InvoiceID int64
SetID []byte
}
func (q *Queries) InsertInvoiceEvent(ctx context.Context, arg InsertInvoiceEventParams) error {
_, err := q.db.ExecContext(ctx, insertInvoiceEvent,
arg.CreatedAt,
arg.InvoiceID,
arg.HtlcID,
arg.SetID,
arg.EventType,
arg.EventMetadata,
)
func (q *Queries) OnAMPSubInvoiceCanceled(ctx context.Context, arg OnAMPSubInvoiceCanceledParams) error {
_, err := q.db.ExecContext(ctx, onAMPSubInvoiceCanceled, arg.AddedAt, arg.InvoiceID, arg.SetID)
return err
}
const selectInvoiceEvents = `-- name: SelectInvoiceEvents :many
SELECT id, created_at, invoice_id, htlc_id, set_id, event_type, event_metadata
FROM invoice_events
WHERE (
invoice_id = $1 OR
$1 IS NULL
) AND (
htlc_id = $2 OR
$2 IS NULL
) AND (
set_id = $3 OR
$3 IS NULL
) AND (
event_type = $4 OR
$4 IS NULL
) AND (
created_at >= $5 OR
$5 IS NULL
) AND (
created_at <= $6 OR
$6 IS NULL
)
LIMIT $8 OFFSET $7
const onAMPSubInvoiceCreated = `-- name: OnAMPSubInvoiceCreated :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id, set_id
) VALUES (
$1, 3, $2, $3
)
`
type SelectInvoiceEventsParams struct {
InvoiceID sql.NullInt32
HtlcID sql.NullInt64
SetID []byte
EventType sql.NullInt32
CreatedAfter sql.NullTime
CreatedBefore sql.NullTime
NumOffset int32
NumLimit int32
type OnAMPSubInvoiceCreatedParams struct {
AddedAt time.Time
InvoiceID int64
SetID []byte
}
func (q *Queries) SelectInvoiceEvents(ctx context.Context, arg SelectInvoiceEventsParams) ([]InvoiceEvent, error) {
rows, err := q.db.QueryContext(ctx, selectInvoiceEvents,
arg.InvoiceID,
arg.HtlcID,
arg.SetID,
arg.EventType,
arg.CreatedAfter,
arg.CreatedBefore,
arg.NumOffset,
arg.NumLimit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []InvoiceEvent
for rows.Next() {
var i InvoiceEvent
if err := rows.Scan(
&i.ID,
&i.CreatedAt,
&i.InvoiceID,
&i.HtlcID,
&i.SetID,
&i.EventType,
&i.EventMetadata,
); 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
func (q *Queries) OnAMPSubInvoiceCreated(ctx context.Context, arg OnAMPSubInvoiceCreatedParams) error {
_, err := q.db.ExecContext(ctx, onAMPSubInvoiceCreated, arg.AddedAt, arg.InvoiceID, arg.SetID)
return err
}
const onAMPSubInvoiceSettled = `-- name: OnAMPSubInvoiceSettled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id, set_id
) VALUES (
$1, 5, $2, $3
)
`
type OnAMPSubInvoiceSettledParams struct {
AddedAt time.Time
InvoiceID int64
SetID []byte
}
func (q *Queries) OnAMPSubInvoiceSettled(ctx context.Context, arg OnAMPSubInvoiceSettledParams) error {
_, err := q.db.ExecContext(ctx, onAMPSubInvoiceSettled, arg.AddedAt, arg.InvoiceID, arg.SetID)
return err
}
const onInvoiceCanceled = `-- name: OnInvoiceCanceled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id
) VALUES (
$1, 1, $2
)
`
type OnInvoiceCanceledParams struct {
AddedAt time.Time
InvoiceID int64
}
func (q *Queries) OnInvoiceCanceled(ctx context.Context, arg OnInvoiceCanceledParams) error {
_, err := q.db.ExecContext(ctx, onInvoiceCanceled, arg.AddedAt, arg.InvoiceID)
return err
}
const onInvoiceCreated = `-- name: OnInvoiceCreated :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id
) VALUES (
$1, 0, $2
)
`
type OnInvoiceCreatedParams struct {
AddedAt time.Time
InvoiceID int64
}
func (q *Queries) OnInvoiceCreated(ctx context.Context, arg OnInvoiceCreatedParams) error {
_, err := q.db.ExecContext(ctx, onInvoiceCreated, arg.AddedAt, arg.InvoiceID)
return err
}
const onInvoiceSettled = `-- name: OnInvoiceSettled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id
) VALUES (
$1, 2, $2
)
`
type OnInvoiceSettledParams struct {
AddedAt time.Time
InvoiceID int64
}
func (q *Queries) OnInvoiceSettled(ctx context.Context, arg OnInvoiceSettledParams) error {
_, err := q.db.ExecContext(ctx, onInvoiceSettled, arg.AddedAt, arg.InvoiceID)
return err
}