From 8940c5e57bb89bffb9d6d82f6af42647f5d56508 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 19 Jan 2024 12:11:36 -0500 Subject: [PATCH 1/3] tapdb: add new build-tag dependent func to make test db This works for both sqlite and postgres. Unlike the existing routines, we don't need to pass in a testing.T and instead want to just return an error. --- tapdb/postgres.go | 19 +++++++++++++++++++ tapdb/sqlite.go | 14 ++++++++++++++ tapdb/sqlutils_test.go | 7 ------- tapdb/test_postgres.go | 11 ++++++++++- tapdb/test_sqlite.go | 9 +++++++++ 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/tapdb/postgres.go b/tapdb/postgres.go index 8dc3c01a0d..e13998b2ab 100644 --- a/tapdb/postgres.go +++ b/tapdb/postgres.go @@ -173,6 +173,25 @@ func NewTestPostgresDB(t *testing.T) *PostgresStore { return store } +// NewPostgresDB is a helper function that creates a Postgres database for +// testing. +func NewPostgresDB() (*PostgresStore, error) { + var t testing.T + sqlFixture := NewTestPgFixture(&t, DefaultPostgresFixtureLifetime, true) + if t.Failed() { + return nil, fmt.Errorf("unable to make postgres db") + } + + store, err := NewPostgresStore(sqlFixture.GetConfig()) + if err != nil { + return nil, err + } + + // sqlFixture.TearDown(t) + + return store, nil +} + // NewTestPostgresDBWithVersion is a helper function that creates a Postgres // database for testing and migrates it to the given version. func NewTestPostgresDBWithVersion(t *testing.T, version uint) *PostgresStore { diff --git a/tapdb/sqlite.go b/tapdb/sqlite.go index aa3fbdb3e1..54a841a3e6 100644 --- a/tapdb/sqlite.go +++ b/tapdb/sqlite.go @@ -196,6 +196,20 @@ func NewTestSqliteDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore { return sqlDB } +// NewSqliteDbHandleFromPath is a helper function that creates a SQLite +// database handle given a database file path. +func NewSqliteDbHandleFromPath(dbPath string) (*SqliteStore, error) { + sqlDB, err := NewSqliteStore(&SqliteConfig{ + DatabaseFileName: dbPath, + SkipMigrations: false, + }) + if err != nil { + return nil, err + } + + return sqlDB, nil +} + // NewTestSqliteDBWithVersion is a helper function that creates an SQLite // database for testing and migrates it to the given version. func NewTestSqliteDBWithVersion(t *testing.T, version uint) *SqliteStore { diff --git a/tapdb/sqlutils_test.go b/tapdb/sqlutils_test.go index a4b51fcb5c..ffdaf71ec9 100644 --- a/tapdb/sqlutils_test.go +++ b/tapdb/sqlutils_test.go @@ -280,13 +280,6 @@ func newDbHandleFromDb(db *BaseDB) *DbHandler { } } -// NewDbHandleFromPath creates a new database store handle given a database file -// path. -func NewDbHandleFromPath(t *testing.T, dbPath string) *DbHandler { - db := NewTestDbHandleFromPath(t, dbPath) - return newDbHandleFromDb(db.BaseDB) -} - // NewDbHandle creates a new database store handle. func NewDbHandle(t *testing.T) *DbHandler { // Create a new test database with the default database file path. diff --git a/tapdb/test_postgres.go b/tapdb/test_postgres.go index 4e284e10b4..8e16cbeebd 100644 --- a/tapdb/test_postgres.go +++ b/tapdb/test_postgres.go @@ -6,17 +6,26 @@ import ( "testing" ) +const activeTestDB = "postgres" + // NewTestDB is a helper function that creates a Postgres database for testing. func NewTestDB(t *testing.T) *PostgresStore { return NewTestPostgresDB(t) } // NewTestDbHandleFromPath is a helper function that creates a new handle to an -// existing SQLite database for testing. +// existing Postgres database for testing. func NewTestDbHandleFromPath(t *testing.T, dbPath string) *PostgresStore { return NewTestPostgresDB(t) } +// NewDbHandleFromPath is a helper function that creates a new handle to an +// existing Postgres database for testing. This version returns an error if an +// an issue is hit during init. +func NewDbHandleFromPath(dbPath string) (*PostgresStore, error) { + return NewPostgresDB() +} + // NewTestDBWithVersion is a helper function that creates a Postgres database // for testing and migrates it to the given version. func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore { diff --git a/tapdb/test_sqlite.go b/tapdb/test_sqlite.go index 40e2b36731..931f9f75c6 100644 --- a/tapdb/test_sqlite.go +++ b/tapdb/test_sqlite.go @@ -6,6 +6,8 @@ import ( "testing" ) +const activeTestDB = "sqlite3" + // NewTestDB is a helper function that creates an SQLite database for testing. func NewTestDB(t *testing.T) *SqliteStore { return NewTestSqliteDB(t) @@ -17,6 +19,13 @@ func NewTestDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore { return NewTestSqliteDbHandleFromPath(t, dbPath) } +// NewDbHandleFromPath is a helper function that creates a new handle to an +// existing SQLite database for testing. This version returns an error if an +// issue is encountered during init. +func NewDbHandleFromPath(dbPath string) (*SqliteStore, error) { + return NewSqliteDbHandleFromPath(dbPath) +} + // NewTestDBWithVersion is a helper function that creates an SQLite database for // testing and migrates it to the given version. func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore { From 4f33f4a2541e23118b22801bcf1c9e8ffbaa2422 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 19 Jan 2024 12:12:41 -0500 Subject: [PATCH 2/3] tapdb: call RegisterTreeStore w/ build tag gated sql db We use the new `activeTestDB` variable to set the proper DB driver name. --- tapdb/mssmt.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tapdb/mssmt.go b/tapdb/mssmt.go index 8a839e4fb0..bbddbb3e58 100644 --- a/tapdb/mssmt.go +++ b/tapdb/mssmt.go @@ -402,3 +402,38 @@ func (t *taprootAssetTreeStoreTx) UpdateRoot(rootNode *mssmt.BranchNode) error { Namespace: t.namespace, }) } + +func init() { + driver := mssmt.TreeStoreDriver{ + Name: activeTestDB, + New: func(args ...interface{}) (mssmt.TreeStore, error) { + dbPath, ok := args[0].(string) + if !ok { + return nil, fmt.Errorf("invalid db path: "+ + "want string, got %T", args[0]) + } + namespace, ok := args[1].(string) + if !ok { + return nil, fmt.Errorf("invalid db path: "+ + "want string, got %T", args[0]) + } + + sqlDB, err := NewDbHandleFromPath(dbPath) + if err != nil { + return nil, err + } + + txCreator := func(tx *sql.Tx) TreeStore { + return sqlDB.WithTx(tx) + } + + treeDB := NewTransactionExecutor(sqlDB, txCreator) + + return NewTaprootAssetTreeStore(treeDB, namespace), nil + }, + } + if err := mssmt.RegisterTreeStore(&driver); err != nil { + panic(fmt.Errorf("failed to register db=%v): %v", + activeTestDB, err)) + } +} From 3a3ce4686d2564f58f7a63b577779e115ae53c5b Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 19 Jan 2024 12:13:10 -0500 Subject: [PATCH 3/3] mssmt: make genTestStores more generic The function will now emit all the registered db drivers vs just special casing for sqlite. --- mssmt/tree_test.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/mssmt/tree_test.go b/mssmt/tree_test.go index c07a10bfca..82bacade87 100644 --- a/mssmt/tree_test.go +++ b/mssmt/tree_test.go @@ -39,25 +39,20 @@ func genTestStores(t *testing.T) map[string]makeTestTreeStoreFunc { constructors := make(map[string]makeTestTreeStoreFunc) for _, driver := range mssmt.RegisteredTreeStores() { - var makeFunc makeTestTreeStoreFunc - if driver.Name == "sqlite3" { - makeFunc = func() (mssmt.TreeStore, error) { - dbFileName := filepath.Join( - t.TempDir(), "tmp.db", - ) - - treeStore, err := driver.New(dbFileName, "test") - if err != nil { - return nil, fmt.Errorf("unable to "+ - "create new sqlite tree "+ - "store: %v", err) - } + constructors[driver.Name] = func() (mssmt.TreeStore, error) { + dbFileName := filepath.Join( + t.TempDir(), "tmp.db", + ) - return treeStore, nil + treeStore, err := driver.New(dbFileName, "test") + if err != nil { + return nil, fmt.Errorf("unable to "+ + "create new sqlite tree "+ + "store: %v", err) } - } - constructors[driver.Name] = makeFunc + return treeStore, nil + } } constructors["default"] = func() (mssmt.TreeStore, error) {