mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-11 14:48:14 +01:00
graph/db: refactor SQL DB creation files
Factor out the transaction executor construction so that we can have access to the raw BatchedSQLQueries type from within tests.
This commit is contained in:
@@ -6,14 +6,12 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
"github.com/lightningnetwork/lnd/sqldb"
|
"github.com/lightningnetwork/lnd/sqldb"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTestDB is a helper function that creates a SQLStore backed by a postgres
|
// newBatchQuerier creates a new BatchedSQLQueries instance for testing
|
||||||
// database for testing.
|
// using a PostgreSQL database fixture.
|
||||||
func NewTestDB(t testing.TB) V1Store {
|
func newBatchQuerier(t testing.TB) BatchedSQLQueries {
|
||||||
pgFixture := sqldb.NewTestPgFixture(
|
pgFixture := sqldb.NewTestPgFixture(
|
||||||
t, sqldb.DefaultPostgresFixtureLifetime,
|
t, sqldb.DefaultPostgresFixtureLifetime,
|
||||||
)
|
)
|
||||||
@@ -23,18 +21,9 @@ func NewTestDB(t testing.TB) V1Store {
|
|||||||
|
|
||||||
db := sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
|
db := sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
|
||||||
|
|
||||||
executor := sqldb.NewTransactionExecutor(
|
return sqldb.NewTransactionExecutor(
|
||||||
db, func(tx *sql.Tx) SQLQueries {
|
db, func(tx *sql.Tx) SQLQueries {
|
||||||
return db.WithTx(tx)
|
return db.WithTx(tx)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
store, err := NewSQLStore(
|
|
||||||
&SQLStoreConfig{
|
|
||||||
ChainHash: *chaincfg.MainNetParams.GenesisHash,
|
|
||||||
}, executor,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
return store
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
graph/db/test_sql.go
Normal file
23
graph/db/test_sql.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
//go:build test_db_postgres || test_db_sqlite
|
||||||
|
|
||||||
|
package graphdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewTestDB is a helper function that creates a SQLStore backed by a SQL
|
||||||
|
// database for testing.
|
||||||
|
func NewTestDB(t testing.TB) V1Store {
|
||||||
|
store, err := NewSQLStore(
|
||||||
|
&SQLStoreConfig{
|
||||||
|
ChainHash: *chaincfg.MainNetParams.GenesisHash,
|
||||||
|
}, newBatchQuerier(t),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
@@ -6,28 +6,17 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
"github.com/lightningnetwork/lnd/sqldb"
|
"github.com/lightningnetwork/lnd/sqldb"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTestDB is a helper function that creates a SQLStore backed by a sqlite
|
// newBatchQuerier creates a new BatchedSQLQueries instance for testing
|
||||||
// database for testing.
|
// using a SQLite database.
|
||||||
func NewTestDB(t testing.TB) V1Store {
|
func newBatchQuerier(t testing.TB) BatchedSQLQueries {
|
||||||
db := sqldb.NewTestSqliteDB(t).BaseDB
|
db := sqldb.NewTestSqliteDB(t).BaseDB
|
||||||
|
|
||||||
executor := sqldb.NewTransactionExecutor(
|
return sqldb.NewTransactionExecutor(
|
||||||
db, func(tx *sql.Tx) SQLQueries {
|
db, func(tx *sql.Tx) SQLQueries {
|
||||||
return db.WithTx(tx)
|
return db.WithTx(tx)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
store, err := NewSQLStore(
|
|
||||||
&SQLStoreConfig{
|
|
||||||
ChainHash: *chaincfg.MainNetParams.GenesisHash,
|
|
||||||
}, executor,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
return store
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user