Commit graph

51 commits

Author SHA1 Message Date
Andras Banki-Horvath
97c025f289
invoices: raise the number of allowed clients for the Postgres fixture 2025-01-23 09:11:02 +01:00
Andras Banki-Horvath
84598b6dc1
sqldb: ensure schema definitions are fully SQLite compatible
Previously, we applied replacements to our schema definitions
to make them compatible with both SQLite and Postgres backends,
as the files were not fully compatible with either.

With this change, the only replacement required for SQLite has
been moved to the generator script. This adjustment ensures
compatibility by enabling auto-incrementing primary keys that
are treated as 64-bit integers by sqlc.
2025-01-23 09:11:02 +01:00
Andras Banki-Horvath
8d20e2a23b
lnd: run invoice migration on startup
This commit runs the invoice migration if the user has a KV SQL backend
configured.
2025-01-23 09:11:01 +01:00
Andras Banki-Horvath
94e2724a34
sqldb+invoices: Optimize invoice fetching when the reference is only a hash
The current sqlc GetInvoice query experiences incremental slowdowns during
the migration of large invoice databases, primarily due to its complex
predicate set. For this specific use case, a streamlined GetInvoiceByHash
function provides a more efficient solution, maintaining near-constant
lookup times even with extensive table sizes.
2025-01-23 09:11:01 +01:00
Andras Banki-Horvath
d65b630568
sqldb: remove unused preimage query parameter 2025-01-23 09:11:00 +01:00
Andras Banki-Horvath
b7d743929d
sqldb: add a temporary index to store KV invoice hash to ID mapping 2025-01-23 09:11:00 +01:00
Andras Banki-Horvath
3820497d7f
sqldb: set settled_at and settle_index on invocie insertion is set
Previously we intentially did not set settled_at and settle_index when
inserting a new invoice as those fields are set when we settle an
invoice through the usual invoice update. As migration requires that we
set these nullable fields, we can safely add them.
2025-01-23 09:11:00 +01:00
Andras Banki-Horvath
115f96c29a
multi: add call to directly insert an AMP sub-invoice 2025-01-23 09:10:59 +01:00
Andras Banki-Horvath
91c3e1496f
sqldb: separate migration execution from construction
This commit separates the execution of SQL and in-code migrations
from their construction. This change is necessary because,
currently, the SQL schema is migrated during the construction
phase in the lncfg package. However, migrations are typically
executed when individual stores are constructed within the
configuration builder.
2025-01-23 09:10:59 +01:00
Andras Banki-Horvath
b789fb2db3
sqldb: add support for custom in-code migrations
This commit introduces support for custom, in-code migrations, allowing
a specific Go function to be executed at a designated database version
during sqlc migrations. If the current database version surpasses the
specified version, the migration will be skipped.
2025-01-23 09:10:59 +01:00
Andras Banki-Horvath
9acd06d296
sqldb: add table to track custom SQL migrations
This commit adds the migration_tracker table which we'll use to track if
a custom migration has already been done.
2025-01-23 09:10:59 +01:00
Alex Akselrod
780c271b80
sqldb: improve serialization error handling 2024-12-12 10:40:37 -08:00
Elle Mouton
ab7aae0708
multi: rename nolint:lll to nolint:ll
Find and replace all nolint instances refering to the `lll` linter and
replace with `ll` which is the name of our custom version of the `lll`
linter which can be used to ignore log lines during linting.

The next commit will do the configuration of the custom linter and
disable the default one.
2024-12-02 09:14:21 +02:00
Elle Mouton
83751ee208
sqldb: update btclog dep 2024-10-26 14:18:00 +02:00
Andras Banki-Horvath
06d4267a76
sqldb: fix end date filter when querying invoices
Previously, the SQL implementation of the invoice query simply
converted the start and end timestamps to time and used them
in SQL queries to check for inclusivity. However, this logic
failed when the start and end timestamps were equal.

This commit addresses and corrects this issue.
2024-09-03 19:40:47 +02:00
Andras Banki-Horvath
b57910ee3a
sqldb+invoices: synchronize SQL invoice updater behavior with KV version
Previously SQL invoice updater ignored the set ID hint when updating an
AMP invoice resulting in update subscriptions returning all of the AMP
state as well as all AMP HTLCs. This commit synchornizes behavior with
the KV implementation such that we now only return relevant AMP state
and HTLCs when updating an AMP invoice.
2024-09-03 19:40:46 +02:00
Alex Akselrod
f1b7953465
invoices/sqldb: query by ChanID when updating AMP invoice preimage 2024-09-03 19:40:45 +02:00
Andras Banki-Horvath
d0c1cec8c1
sqldb: switch away from pq to pgx for Postgres
Completely switch to the better maintained pgx driver.
2024-07-09 08:39:58 +02:00
Andras Banki-Horvath
95b99420fa
sqldb: add unit test for the invoice expiry migration 2024-07-09 08:39:57 +02:00
Andras Banki-Horvath
ed36598504
sqldb: add helpers to create test DBs migrated up to a select version 2024-07-09 08:39:57 +02:00
Andras Banki-Horvath
5292c76e10
sqldb: extract migration into method
Based on: https://github.com/lightninglabs/taproot-assets/pull/707
2024-07-09 08:39:57 +02:00
Andras Banki-Horvath
323af946e0
sqldb+invoices: add migration to fix incorrectly stored invoice expiries
Previously, when using the native schema, invoice expiries were incorrectly
stored as 64-bit values (expiry in nanoseconds instead of seconds), causing
overflow issues. Since we cannot determine the original values, we will set
the expiries for existing invoices to 1 hour with this migration.
2024-07-09 08:39:57 +02:00
Andras Banki-Horvath
892561f8f0
sqldb: bump modernc.org/sqlite to 1.29.10 which fixes init data race
Tracking issue: https://gitlab.com/cznic/sqlite/-/issues/180
2024-07-04 17:32:12 +02:00
Olaoluwa Osuntokun
eb605a02fd
sqldb+kvdb: update to sqlite v1.29.8
Updates to SQLite 3.45.3: https://www.sqlite.org/releaselog/3_45_3.html.

May also address https://github.com/lightningnetwork/lnd/issues/8666.
2024-04-26 12:26:59 -07:00
Andras Banki-Horvath
043e4aff01
sqldb+invoices: fix ordering bug in FilterInvoices
Previously if the `reverse` named arg was unset (value of NULL), then
SQL would order by NULL instead of ID causing undifined ordering of the
returned rows. To fix that we check for NULL and also make sure to set
the `reverse` arg in the code explicitly as it in the generated code it
is an `interface{}` instead of `bool`.
2024-04-11 15:04:04 +02:00
Andras Banki-Horvath
478ae1e9b0
sqldb: cleanup scope state reset by adding reset closure to ExecTx
For SQL transactions, we often accumulate results in variables declared
outside the closure's scope. To eliminate the need for manually clearing
these containers, we introduce a reset function to ExecTx, mirroring the
approach already adopted in kvdb.
2024-04-11 15:04:04 +02:00
Andras Banki-Horvath
c6073a14ca
sqldb: ensure that we're using serializable isolation 2024-04-11 15:04:04 +02:00
Olaoluwa Osuntokun
329fcc6498
kvdb+sqldb: update SQL error parsing to account for non wrapped errs
Some sub-systems like btcwallet will return an error from the database,
but they won't properly wrap it. As a result, we were unable to actually
catch the serialization errors in the first place. To work around this,
we'll now attempt to parse the error string directly.
2024-04-11 15:04:03 +02:00
Andras Banki-Horvath
43f4b14c28
kvdb+sqldb: use the same tx retry helper 2024-04-11 15:04:02 +02:00
Andras Banki-Horvath
8f729241d9
sqldb: turn sqldb into a go package 2024-04-09 20:46:11 +02:00
Andras Banki-Horvath
7f5c8219ef
sqldb+invoices: move SQL invoice store impl to invoices package 2024-04-09 20:46:11 +02:00
Andras Banki-Horvath
6d316ef56f
sqldb: export sql null type helpers 2024-04-09 20:46:11 +02:00
Andras Banki-Horvath
a1a7982646
sqldb: reset out of scope containers on potential ExecTx retry
As SQL serializaton errors can lead to transaction retries we need to
ensure that we reset any out of scope containers where we accumulate
results. Otherwise partial data from a previously executed transacton
retry may be returned along with the latest result.
2024-03-27 17:59:54 +01:00
Andras Banki-Horvath
ce1b57da2d
sqldb: exclude sqlite from the JS and unsupported platform builds 2024-03-01 10:08:09 +01:00
Andras Banki-Horvath
aba45018a8
sqldb: merge SQLite and Postgres configs with the kvdb counterparts
This commit is part of a refactor that unifies configuration of the
sqldb and kvdb packages for SQL backends.
In order to unify the SQLite and Postgres configuration under sqldb we
first need to ensure that the final config types are compatible with
the alreay deployed versions.
2024-03-01 10:08:09 +01:00
Andras Banki-Horvath
06730824a2
sqldb: fixup PostgreSQL fixture to allow creating separate DBs per test
This change will enable us to use a single PostgreSQL container instead of
spawning new a one for each (parallel) unit test reducing overall test
runtime.
2024-03-01 10:08:08 +01:00
Andras Banki-Horvath
3d56d2f1ee
sqldb: add full SQL InvoiceStore implementation 2024-03-01 10:08:08 +01:00
Andras Banki-Horvath
5cf67c4440
sqldb: add utility functions for SQL NULL types 2024-03-01 10:08:07 +01:00
Andras Banki-Horvath
74e7a50f4a
sqldb: convert primary key violation to ErrSQLUniqueConstraintViolation
Besides the usual unique constraint violation that we already support we
also want to return the same error if the primary key constraint is
violated.
2024-03-01 10:08:07 +01:00
Andras Banki-Horvath
6a360fb2e2
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.
2024-03-01 10:08:07 +01:00
Andras Banki-Horvath
3e0f98a75a
sqldb: introduce replace for the BIGINT type alias 2024-03-01 10:08:07 +01:00
positiveblue
e7d82fa530
tools: update sqlc version to v1.25.0 2024-03-01 10:08:07 +01:00
Afanti
fff785c909
fix: correct the typo 2023-12-29 21:27:13 +08:00
positiveblue
5a7d7c2e4f
sqldb: add the sqldb package
This commit provides the scaffolding for using the new sql stores.
The new interfaces, structs and methods are in sync with other projects
like Taproot Assets.

- Transactional Queries: the sqldb package defines the interfaces required
to execute transactional queries to our storage interface.

- Migration Files Embedded: the migration files are embedded into the binary.

- Database Migrations: I kept the use of 'golang-migrate' to ensure our
codebase remains in sync with the other projects, but can be changed.

- Build Flags for Conditional DB Target: flexibility to specify our database
target at compile-time based on the build flags in the same way we do
with our kv stores.

- Update modules: ran `go mod tidy`.
2023-07-27 03:31:12 -07:00
positiveblue
730db7ee8c
sqlc: generate go code from SQL
run `make sqlc`

All the code in this commit is auto-generated.
2023-07-10 17:57:06 -07:00
positiveblue
877b711360
sqldb: add invoice event queries 2023-07-10 17:56:29 -07:00
positiveblue
fb16287b31
sqldb: add invoice events SQL schema 2023-07-10 17:56:27 -07:00
positiveblue
6deb916216
sqldb: add AMP invoice queries 2023-07-10 17:53:57 -07:00
positiveblue
eda95e6607
sqldb: add AMP invoices SQL schema
Schema for AMP invocies.

AMP invoices can be paid multiple times and each payment to an AMP invoice
is identified by a `set_id`.

The A in AMP stands for `Atomic`. All the htlcs belonging to the same
AMP payment (share the same set_id) will be resolved at the same time
with the same result: settled/canceled.

AMP invoices do not have an "invoice preimage". Instead, each htcl has
its own hash/preimage. When a new htlc is added the hash for that htlc
is attached to it. When all the htlcs of a set_id have been received we
are able to compute the preimage for each one of them.
2023-07-10 17:53:56 -07:00
positiveblue
7aa2f390fe
sqldb: add invoice queries
Set of queries to deal with invoices. A couple of things to take into
account:

    - Because the queries are not rewritten at runtime, we cannot have a
      generic `INSERT` with different tuples.
    - Because the queries are not rewritten at runtime, we cannot build
      one query with only the filters that matter for that queries. The
      two options are a combinatorial approach (a new query for every
      permutation) or a generic query using the pattern

          ```
            SELECT *
            FROM table
            WHERE (
                -- Can be read as:
                -- Match the filter 1 value if filter_1 != nil
                column_1 >= sqlc.narg('filter_1') OR
                sqlc.narg('filter_1') IS NULL
            ) AND (
                column_2 >= sqlc.narg('filter_2') OR
                sqlc.narg('filter_2') IS NULL
            ) ...
          ```
2023-07-10 17:53:29 -07:00