@@ -6,14 +6,12 @@ import (
66 "errors"
77 "fmt"
88 "net"
9- "os"
109 "path"
1110 "sync"
1211 "testing"
1312 "time"
1413
1514 "github.com/btcsuite/btcd/chaincfg"
16- "github.com/btcsuite/btclog/v2"
1715 "github.com/lightningnetwork/lnd/batch"
1816 "github.com/lightningnetwork/lnd/fn/v2"
1917 "github.com/lightningnetwork/lnd/graph/db/models"
@@ -430,137 +428,6 @@ func TestPopulateDBs(t *testing.T) {
430428 }
431429}
432430
433- // TestPopulateViaMigration is a helper test that can be used to populate a
434- // local native SQL graph from a kvdbgraph using the migration logic.
435- //
436- // NOTE: the testPostgres variable can be set to true to test with a
437- // postgres backend instead of the kvdb-sqlite backend.
438- //
439- // NOTE: you will need to set the following build tags in order to run this
440- // test:
441- //
442- // test_native_sql
443- // kvdb_sqlite // If your source is kvdb-sqlite
444- // kvdb_postgres // If your source is kvdb-postgres
445- //
446- // NOTE: this is a helper test and is not run by default.
447- func TestPopulateViaMigration (t * testing.T ) {
448- // ======= STEP 0 ===========
449- // Comment out this SKipf line.
450- t .Skipf ("Skipping local helper test" )
451-
452- const (
453- srcBBolt = "kvdb-bbolt"
454- srcSQLite = "kvdb-sqlite"
455- srcPostgres = "kvdb-postgres"
456- )
457-
458- // ======= STEP 1 ===========
459- // Set your chosen SOURCE type by uncommenting the corresponding line
460- // below. By default, a kvdb-sqlite source is chosen.
461- srcDB := srcSQLite
462- // srcDB := srcBBolt
463- // srcDB := srcPostgres
464-
465- // ======= STEP 2 ============
466- // Set this variable to the correct genesis hash of the source
467- // DB. By default, mainnet is assumed.
468- chain := * chaincfg .MainNetParams .GenesisHash
469-
470- // ======= STEP 3 (ignore if source is postgres) ==============
471- // If your source destination is bbolt or sqlite, then set this to the
472- // path where your source database can be found.
473- const sourceDBPath = "testdata"
474-
475- // ======= STEP 4 (only if source is bbolt!) ============
476- // If your source destination is bbolt, then set this to the name of
477- // the bbolt file that contains the channel graph data.
478- const sourceBBoltName = "channel.db"
479-
480- // ======= STEP 5 (only if source is sqlite!) ============
481- // If your source destination is sqlite, then set this to the name of
482- // the sqlite file that contains the channel graph data.
483- const sourceSQLiteName = "channel.sqlite"
484-
485- // ======= STEP 6 (only if source is postgres!) ============
486- // Set the DNS of your kvdb postgres instance below. This should be the
487- // same as what you have set in the config of the LND node that
488- // populated the instance (ie, whatever your --db.postgres.dsn is set
489- // to).
490- const kvdbPostgresDNS = "postgres://user@host/db_name"
491-
492- // ======== STEP 7 ========================
493- // Finally, pick your destination DB! You can choose either SQLite or
494- // Postgres.
495- testSQLite := true
496-
497- // ======== STEP 8 (only if destination is sqlite) ========
498- // Set the path where you want to create the destination SQLite
499- // database. This should be a directory that exists and is writable.
500- const destSQLitePath = "testdata"
501-
502- // ======== STEP 9 (only if destination is sqlite) ========
503- // Pick a name for your destination SQLite database file.
504- // NOTE: if you run this test again, delete the previously created
505- // file first.
506- const destSQLiteFile = "lnd-graph-test.sqlite"
507-
508- // ======== STEP 10 (only if destination is postgres) ========
509- // NB: this has some additional steps:
510- // 1. First, connect to your destination postgres instance: example:
511- // $ psql -U ellemouton -d postgres
512- // 2. Now, create the test database:
513- // CREATE DATABASE graphtest;
514- // NOTE: if you restart this test for postgres, it helps to first drop
515- // the new database & recreate it.
516- // NOTE: the database name that you use above must be whatever you will
517- // use in the DNS you set below.
518- const sqlPostgresDNS = "postgres://user@host/graphtest"
519-
520- // ======= YOUR WORK IS DONE =============
521-
522- // Connect to source database.
523- var srcKVDB kvdb.Backend
524- switch srcDB {
525- case srcBBolt :
526- srcKVDB = kvdbBBolt (t , sourceDBPath , sourceBBoltName )
527- case srcSQLite :
528- srcKVDB = kvdbSqlite (t , sourceDBPath , sourceSQLiteName )
529- case srcPostgres :
530- srcKVDB = kvdbPostgres (t , kvdbPostgresDNS )
531- default :
532- t .Fatalf ("Unsupported source database backend: %s" , srcDB )
533- }
534-
535- // Connect to destination database.
536- cfg := sqldb .DefaultSQLiteConfig ()
537- dstSQL := sqlSQLite (t , destSQLitePath , destSQLiteFile )
538- if ! testSQLite {
539- cfg = sqldb .DefaultPostgresConfig ()
540- dstSQL = sqlPostgres (t , sqlPostgresDNS )
541- }
542-
543- // Set up a logger so we can see the migration progress.
544- logger := btclog .NewDefaultHandler (os .Stdout )
545- UseLogger (btclog .NewSLogger (logger ))
546- log .SetLevel (btclog .LevelDebug )
547-
548- // Use the graph migration to populate the SQL graph from the
549- // kvdb graph.
550- ctx := t .Context ()
551- err := dstSQL .ExecTx (
552- ctx , sqldb .WriteTxOpt (), func (queries SQLQueries ) error {
553- return MigrateGraphToSQL (
554- ctx , & SQLStoreConfig {
555- QueryCfg : cfg ,
556- ChainHash : chain ,
557- }, srcKVDB , queries ,
558- )
559- }, func () {},
560- )
561- require .NoError (t , err )
562- }
563-
564431// syncGraph synchronizes the source graph with the destination graph by
565432// copying all nodes and channels from the source to the destination.
566433func syncGraph (t * testing.T , src , dest * ChannelGraph ) {
0 commit comments