mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-24 20:53:07 +02:00
Merge pull request #10158 from ellemouton/graphTest
graph/db: expand TestPopulateViaMigration for easy testing
This commit is contained in:
@@ -240,6 +240,11 @@ func connectKVDBSqlite(t testing.TB, dbPath, fileName string) V1Store {
|
|||||||
|
|
||||||
// connectBBoltDB creates a new BBolt database connection for testing.
|
// connectBBoltDB creates a new BBolt database connection for testing.
|
||||||
func connectBBoltDB(t testing.TB, dbPath, fileName string) V1Store {
|
func connectBBoltDB(t testing.TB, dbPath, fileName string) V1Store {
|
||||||
|
return newKVStore(t, kvdbBBolt(t, dbPath, fileName))
|
||||||
|
}
|
||||||
|
|
||||||
|
// kvdbBBolt creates a new bbolt backend for testing.
|
||||||
|
func kvdbBBolt(t testing.TB, dbPath, fileName string) kvdb.Backend {
|
||||||
cfg := &kvdb.BoltBackendConfig{
|
cfg := &kvdb.BoltBackendConfig{
|
||||||
DBPath: dbPath,
|
DBPath: dbPath,
|
||||||
DBFileName: fileName,
|
DBFileName: fileName,
|
||||||
@@ -252,7 +257,7 @@ func connectBBoltDB(t testing.TB, dbPath, fileName string) V1Store {
|
|||||||
kvStore, err := kvdb.GetBoltBackend(cfg)
|
kvStore, err := kvdb.GetBoltBackend(cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return newKVStore(t, kvStore)
|
return kvStore
|
||||||
}
|
}
|
||||||
|
|
||||||
// newKVStore creates a new KVStore instance for testing using a provided
|
// newKVStore creates a new KVStore instance for testing using a provided
|
||||||
@@ -425,45 +430,122 @@ func TestPopulateDBs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestPopulateViaMigration is a helper test that can be used to populate a
|
// TestPopulateViaMigration is a helper test that can be used to populate a
|
||||||
// local native SQL graph from a kvdb-sql graph using the migration logic.
|
// local native SQL graph from a kvdbgraph using the migration logic.
|
||||||
//
|
//
|
||||||
// NOTE: the testPostgres variable can be set to true to test with a
|
// NOTE: the testPostgres variable can be set to true to test with a
|
||||||
// postgres backend instead of the kvdb-sqlite backend.
|
// postgres backend instead of the kvdb-sqlite backend.
|
||||||
//
|
//
|
||||||
// NOTE: this is a helper test and is not run by default.
|
// NOTE: this is a helper test and is not run by default.
|
||||||
func TestPopulateViaMigration(t *testing.T) {
|
func TestPopulateViaMigration(t *testing.T) {
|
||||||
|
// ======= STEP 0 ===========
|
||||||
|
// Comment out this SKipf line.
|
||||||
t.Skipf("Skipping local helper test")
|
t.Skipf("Skipping local helper test")
|
||||||
|
|
||||||
// Set this to true if you want to test with a postgres backend.
|
const (
|
||||||
// By default, we use a kvdb-sqlite backend.
|
srcBBolt = "kvdb-bbolt"
|
||||||
testPostgres := false
|
srcSQLite = "kvdb-sqlite"
|
||||||
|
srcPostgres = "kvdb-postgres"
|
||||||
|
)
|
||||||
|
|
||||||
ctx := context.Background()
|
// ======= STEP 1 ===========
|
||||||
|
// Set your chosen SOURCE type by uncommenting the corresponding line
|
||||||
|
// below. By default, a kvdb-sqlite source is chosen.
|
||||||
|
srcDB := srcSQLite
|
||||||
|
// srcDB := srcBBolt
|
||||||
|
// srcDB := srcPostgres
|
||||||
|
|
||||||
|
// ======= STEP 2 ============
|
||||||
|
// Set this variable to the correct genesis hash of the source
|
||||||
|
// DB. By default, mainnet is assumed.
|
||||||
|
chain := *chaincfg.MainNetParams.GenesisHash
|
||||||
|
|
||||||
|
// ======= STEP 3 (ignore if source is postgres) ==============
|
||||||
|
// If your source destination is bbolt or sqlite, then set this to the
|
||||||
|
// path where your source database can be found.
|
||||||
|
const sourceDBPath = "testdata"
|
||||||
|
|
||||||
|
// ======= STEP 4 (only if source is bbolt!) ============
|
||||||
|
// If your source destination is bbolt, then set this to the name of
|
||||||
|
// the bbolt file that contains the channel graph data.
|
||||||
|
const sourceBBoltName = "channel.db"
|
||||||
|
|
||||||
|
// ======= STEP 5 (only if source is sqlite!) ============
|
||||||
|
// If your source destination is sqlite, then set this to the name of
|
||||||
|
// the sqlite file that contains the channel graph data.
|
||||||
|
const sourceSQLiteName = "channel.sqlite"
|
||||||
|
|
||||||
|
// ======= STEP 6 (only if source is postgres!) ============
|
||||||
|
// Set the DNS of your kvdb postgres instance below. This should be the
|
||||||
|
// same as what you have set in the config of the LND node that
|
||||||
|
// populated the instance (ie, whatever your --db.postgres.dsn is set
|
||||||
|
// to).
|
||||||
|
const kvdbPostgresDNS = "postgres://user@host/db_name"
|
||||||
|
|
||||||
|
// ======== STEP 7 ========================
|
||||||
|
// Finally, pick your destination DB! You can choose either SQLite or
|
||||||
|
// Postgres.
|
||||||
|
testSQLite := true
|
||||||
|
|
||||||
|
// ======== STEP 8 (only if destination is sqlite) ========
|
||||||
|
// Set the path where you want to create the destination SQLite
|
||||||
|
// database. This should be a directory that exists and is writable.
|
||||||
|
const destSQLitePath = "testdata"
|
||||||
|
|
||||||
|
// ======== STEP 9 (only if destination is sqlite) ========
|
||||||
|
// Pick a name for your destination SQLite database file.
|
||||||
|
// NOTE: if you run this test again, delete the previously created
|
||||||
|
// file first.
|
||||||
|
const destSQLiteFile = "lnd-graph-test.sqlite"
|
||||||
|
|
||||||
|
// ======== STEP 10 (only if destination is postgres) ========
|
||||||
|
// NB: this has some additional steps:
|
||||||
|
// 1. First, connect to your destination postgres instance: example:
|
||||||
|
// $ psql -U ellemouton -d postgres
|
||||||
|
// 2. Now, create the test database:
|
||||||
|
// CREATE DATABASE graphtest;
|
||||||
|
// NOTE: if you restart this test for postgres, it helps to first drop
|
||||||
|
// the new database & recreate it.
|
||||||
|
// NOTE: the database name that you use above must be whatever you will
|
||||||
|
// use in the DNS you set below.
|
||||||
|
const sqlPostgresDNS = "postgres://user@host/graphtest"
|
||||||
|
|
||||||
|
// ======= YOUR WORK IS DONE =============
|
||||||
|
|
||||||
|
// Connect to source database.
|
||||||
|
var srcKVDB kvdb.Backend
|
||||||
|
switch srcDB {
|
||||||
|
case srcBBolt:
|
||||||
|
srcKVDB = kvdbBBolt(t, sourceDBPath, sourceBBoltName)
|
||||||
|
case srcSQLite:
|
||||||
|
srcKVDB = kvdbSqlite(t, sourceDBPath, sourceSQLiteName)
|
||||||
|
case srcPostgres:
|
||||||
|
srcKVDB = kvdbPostgres(t, kvdbPostgresDNS)
|
||||||
|
default:
|
||||||
|
t.Fatalf("Unsupported source database backend: %s", srcDB)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to destination database.
|
||||||
|
cfg := sqldb.DefaultSQLiteConfig()
|
||||||
|
dstSQL := sqlSQLite(t, destSQLitePath, destSQLiteFile)
|
||||||
|
if !testSQLite {
|
||||||
|
cfg = sqldb.DefaultPostgresConfig()
|
||||||
|
dstSQL = sqlPostgres(t, sqlPostgresDNS)
|
||||||
|
}
|
||||||
|
|
||||||
// Set up a logger so we can see the migration progress.
|
// Set up a logger so we can see the migration progress.
|
||||||
logger := btclog.NewDefaultHandler(os.Stdout)
|
logger := btclog.NewDefaultHandler(os.Stdout)
|
||||||
UseLogger(btclog.NewSLogger(logger))
|
UseLogger(btclog.NewSLogger(logger))
|
||||||
log.SetLevel(btclog.LevelDebug)
|
log.SetLevel(btclog.LevelDebug)
|
||||||
|
|
||||||
var (
|
|
||||||
cfg = sqldb.DefaultSQLiteConfig()
|
|
||||||
srcKVDB = kvdbSqlite(t, kvdbSqlitePath, kvdbSqliteFile)
|
|
||||||
dstSQL = sqlSQLite(t, nativeSQLSqlitePath, nativeSQLSqliteFile)
|
|
||||||
)
|
|
||||||
if testPostgres {
|
|
||||||
cfg = sqldb.DefaultPostgresConfig()
|
|
||||||
srcKVDB = kvdbPostgres(t, kvdbPostgresDNS)
|
|
||||||
dstSQL = sqlPostgres(t, nativeSQLPostgresDNS)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use the graph migration to populate the SQL graph from the
|
// Use the graph migration to populate the SQL graph from the
|
||||||
// kvdb graph.
|
// kvdb graph.
|
||||||
|
ctx := context.Background()
|
||||||
err := dstSQL.ExecTx(
|
err := dstSQL.ExecTx(
|
||||||
ctx, sqldb.WriteTxOpt(), func(queries SQLQueries) error {
|
ctx, sqldb.WriteTxOpt(), func(queries SQLQueries) error {
|
||||||
return MigrateGraphToSQL(
|
return MigrateGraphToSQL(
|
||||||
ctx, &SQLStoreConfig{
|
ctx, &SQLStoreConfig{
|
||||||
QueryCfg: cfg,
|
QueryCfg: cfg,
|
||||||
ChainHash: dbTestChain,
|
ChainHash: chain,
|
||||||
}, srcKVDB, queries,
|
}, srcKVDB, queries,
|
||||||
)
|
)
|
||||||
}, func() {},
|
}, func() {},
|
||||||
|
Reference in New Issue
Block a user