-
Notifications
You must be signed in to change notification settings - Fork 2.2k
graph/db: expand TestPopulateViaMigration for easy testing #10158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+100
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -240,6 +240,11 @@ func connectKVDBSqlite(t testing.TB, dbPath, fileName string) V1Store { | |
|
|
||
| // connectBBoltDB creates a new BBolt database connection for testing. | ||
| 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{ | ||
| DBPath: dbPath, | ||
| DBFileName: fileName, | ||
|
|
@@ -252,7 +257,7 @@ func connectBBoltDB(t testing.TB, dbPath, fileName string) V1Store { | |
| kvStore, err := kvdb.GetBoltBackend(cfg) | ||
| require.NoError(t, err) | ||
|
|
||
| return newKVStore(t, kvStore) | ||
| return kvStore | ||
| } | ||
|
|
||
| // 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 | ||
| // 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 | ||
| // postgres backend instead of the kvdb-sqlite backend. | ||
| // | ||
| // NOTE: this is a helper test and is not run by default. | ||
| func TestPopulateViaMigration(t *testing.T) { | ||
| // ======= STEP 0 =========== | ||
| // Comment out this SKipf line. | ||
| t.Skipf("Skipping local helper test") | ||
|
|
||
| // Set this to true if you want to test with a postgres backend. | ||
| // By default, we use a kvdb-sqlite backend. | ||
| testPostgres := false | ||
| const ( | ||
| srcBBolt = "kvdb-bbolt" | ||
| 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 ============= | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😂 |
||
|
|
||
| // 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. | ||
| logger := btclog.NewDefaultHandler(os.Stdout) | ||
| UseLogger(btclog.NewSLogger(logger)) | ||
| 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 | ||
| // kvdb graph. | ||
| ctx := context.Background() | ||
| err := dstSQL.ExecTx( | ||
| ctx, sqldb.WriteTxOpt(), func(queries SQLQueries) error { | ||
| return MigrateGraphToSQL( | ||
| ctx, &SQLStoreConfig{ | ||
| QueryCfg: cfg, | ||
| ChainHash: dbTestChain, | ||
| ChainHash: chain, | ||
| }, srcKVDB, queries, | ||
| ) | ||
| }, func() {}, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.