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
@@ -2,10 +2,10 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/db/schema"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/mitchellh/cli"
|
||||
@@ -20,7 +20,7 @@ func migrateDatabase(ctx context.Context, ui cli.Ui, dialect, u string, initiali
|
||||
noop := func() {}
|
||||
// This database is used to keep an exclusive lock on the database for the
|
||||
// remainder of the command
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Errorf("Error establishing db connection: %w", err).Error())
|
||||
return noop, 2
|
||||
|
||||
@@ -2,10 +2,10 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/db/schema"
|
||||
"github.com/hashicorp/boundary/testing/dbtest"
|
||||
"github.com/mitchellh/cli"
|
||||
@@ -49,7 +49,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
earlyMigrationVersion := 2000
|
||||
@@ -75,7 +75,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
earlyMigrationVersion := 2000
|
||||
@@ -100,7 +100,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
createStmt := `create table if not exists schema_migrations (version bigint primary key, dirty boolean not null)`
|
||||
@@ -120,7 +120,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
createStmt := `create table if not exists schema_migrations (version bigint primary key, dirty boolean not null)`
|
||||
@@ -154,7 +154,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
@@ -177,7 +177,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
@@ -217,7 +217,7 @@ func TestVerifyOplogIsEmpty(t *testing.T) {
|
||||
})
|
||||
_ = c
|
||||
|
||||
dBase, err := sql.Open(dialect, u)
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
@@ -228,7 +228,7 @@ func TestVerifyOplogIsEmpty(t *testing.T) {
|
||||
cmd.srv = base.NewServer(&base.Command{UI: cmd.UI})
|
||||
|
||||
cmd.srv.DatabaseUrl = u
|
||||
require.NoError(t, cmd.srv.ConnectToDatabase(dialect))
|
||||
require.NoError(t, cmd.srv.ConnectToDatabase(ctx, dialect))
|
||||
|
||||
assert.NoError(t, cmd.verifyOplogIsEmpty())
|
||||
assert.NoError(t, cmd.verifyOplogIsEmpty(ctx))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
@@ -164,6 +165,7 @@ func (c *InitCommand) AutocompleteFlags() complete.Flags {
|
||||
}
|
||||
|
||||
func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
ctx := context.Background()
|
||||
if result := c.ParseFlagsAndConfig(args); result > 0 {
|
||||
return result
|
||||
}
|
||||
@@ -277,11 +279,11 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
return base.CommandUserError
|
||||
}
|
||||
// Everything after is done with normal database URL and is affecting actual data
|
||||
if err := c.srv.ConnectToDatabase(dialect); err != nil {
|
||||
if err := c.srv.ConnectToDatabase(ctx, dialect); err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error connecting to database after migrations: %w", err).Error())
|
||||
return base.CommandCliError
|
||||
}
|
||||
if err := c.verifyOplogIsEmpty(); err != nil {
|
||||
if err := c.verifyOplogIsEmpty(ctx); err != nil {
|
||||
c.UI.Error(fmt.Sprintf("The database appears to have already been initialized: %v", err))
|
||||
return base.CommandCliError
|
||||
}
|
||||
@@ -491,9 +493,9 @@ func (c *InitCommand) ParseFlagsAndConfig(args []string) int {
|
||||
return base.CommandSuccess
|
||||
}
|
||||
|
||||
func (c *InitCommand) verifyOplogIsEmpty() error {
|
||||
func (c *InitCommand) verifyOplogIsEmpty(ctx context.Context) error {
|
||||
const op = "database.(InitCommand).verifyOplogIsEmpty"
|
||||
underlyingDB, err := c.srv.Database.DB()
|
||||
underlyingDB, err := c.srv.Database.SqlDB(ctx)
|
||||
if err != nil {
|
||||
return errors.NewDeprecated(errors.Internal, op, "unable to retreive db", errors.WithWrap(err))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user