refact: Add db.DB wrapper and refact all test fixtures to use it. (#1535)
* refact: Changes required for gorm v2 * refact: Add db.DB wrapper and refact all test fixtures to use it Stop leaking the gorm abstraction within our unit tests. * refact: Migrate direct calls to pq to pgx driver The refactor will remove the remaining direct dependencies on the pq driver which has been deprecated in favor of the pgx driver. It includes the addition of common.SqlOpen which is a wrapper around sql.Open, which is needed to deal with the driver name changing from "postgres" to "pgx" but in our code base the dialect and driver are synonyms.
This commit is contained in:
77 files changed
+441
-321
No files matched your search
@@ -12,7 +12,7 @@
|
||||
// require.NoError(t, c())
|
||||
// })
|
||||
// // use u to get a connection to the new database
|
||||
// dBase, err := sql.Open("postgres", u)
|
||||
// dBase, err := common.SqlOpen("postgres", u)
|
||||
//
|
||||
// By default this uses a template that already has all of the boundary
|
||||
// migrations run. If a test needs a database without any migrations, like in
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package dbtest
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
// postgres dialect
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
)
|
||||
|
||||
var testDBPort = "5432"
|
||||
@@ -75,7 +76,10 @@ func startUsingTemplate(dialect string, opt ...Option) (cleanup func() error, re
|
||||
return func() error { return nil }, "", "", fmt.Errorf("unsupported dialect: %s", dialect)
|
||||
}
|
||||
|
||||
db, err := sql.Open(dialect, fmt.Sprintf("postgres://boundary:boundary@127.0.0.1:%s/boundary?sslmode=disable", testDBPort))
|
||||
if dialect == "postgres" {
|
||||
dialect = "pgx"
|
||||
}
|
||||
db, err := common.SqlOpen(dialect, fmt.Sprintf("postgres://boundary:boundary@127.0.0.1:%s/boundary?sslmode=disable", testDBPort))
|
||||
if err != nil {
|
||||
return func() error { return nil }, "", "", fmt.Errorf("could not connect to source database: %w", err)
|
||||
}
|
||||
@@ -103,7 +107,7 @@ func startUsingTemplate(dialect string, opt ...Option) (cleanup func() error, re
|
||||
return dropDatabase(dialect, dbname)
|
||||
}
|
||||
|
||||
tdb, err := sql.Open(dialect, url)
|
||||
tdb, err := common.SqlOpen(dialect, url)
|
||||
if err != nil {
|
||||
return func() error { return nil }, "", "", fmt.Errorf("could not create test database: %w", err)
|
||||
}
|
||||
@@ -130,7 +134,7 @@ func dropDatabase(dialect, dbname string) error {
|
||||
return fmt.Errorf("empty dbname")
|
||||
}
|
||||
|
||||
db, err := sql.Open(dialect, fmt.Sprintf("postgres://boundary:boundary@127.0.0.1:%s/boundary?sslmode=disable", testDBPort))
|
||||
db, err := common.SqlOpen(dialect, fmt.Sprintf("postgres://boundary:boundary@127.0.0.1:%s/boundary?sslmode=disable", testDBPort))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package dbtest_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/testing/dbtest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -56,7 +56,7 @@ func TestStartUsingTemplate(t *testing.T) {
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := sql.Open(tt.dialect, u)
|
||||
db, err := common.SqlOpen(tt.dialect, u)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.Ping())
|
||||
db.Close()
|
||||
@@ -64,7 +64,7 @@ func TestStartUsingTemplate(t *testing.T) {
|
||||
require.NoError(t, c())
|
||||
|
||||
// ensure that database is gone after calling c
|
||||
db, _ = sql.Open(tt.dialect, u)
|
||||
db, _ = common.SqlOpen(tt.dialect, u)
|
||||
require.Error(t, db.Ping())
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user