graph/db: add a framework for testing against SQL backends incrementally

In this commit, we implement the postgres and sqlite versions of the
NewTestDB function. We add the various build flags so that only one of
the three versions of this function can be active at a time.

We also introduce the SQLStore struct which is the SQL implementation of
the V1Store interface.

NOTE: it currently temporarily embeds the KVStore struct so that we can
implement the V1Store interface incrementally. For any method not
implemented, things will fall back to the KVStore. This is ONLY the
case for the time being while this struct is purely used in unit tests
only. Once all the methods have been implemented, the KVStore field will
be removed from the SQLStore struct.
This commit is contained in:
Elle Mouton
2025-04-05 19:24:48 +02:00
parent 383453635b
commit b4121acb1f
5 changed files with 126 additions and 0 deletions

42
graph/db/test_postgres.go Normal file
View File

@@ -0,0 +1,42 @@
//go:build test_db_postgres && !test_db_sqlite
package graphdb
import (
"database/sql"
"testing"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/stretchr/testify/require"
)
// NewTestDB is a helper function that creates a SQLStore backed by a postgres
// database for testing. At the moment, it embeds a KVStore but once the
// SQLStore fully implements the V1Store interface, the KVStore will be removed.
func NewTestDB(t testing.TB) *SQLStore {
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
require.NoError(t, err)
t.Cleanup(backendCleanup)
graphStore, err := NewKVStore(backend)
require.NoError(t, err)
pgFixture := sqldb.NewTestPgFixture(
t, sqldb.DefaultPostgresFixtureLifetime,
)
t.Cleanup(func() {
pgFixture.TearDown(t)
})
db := sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
executor := sqldb.NewTransactionExecutor(
db, func(tx *sql.Tx) SQLQueries {
return db.WithTx(tx)
},
)
return NewSQLStore(executor, graphStore)
}