Skip to content

Commit d4de666

Browse files
jeanbzajba
authored andcommitted
internal/database: allow running db tests under different db name
On a mac, brew install postgresql installs postgres without the postgres user: it uses $USER instead. The current tests don't take that into account. This CL changes that by: - Amending the connecting URI to explicitly specify the db name (the current "" approach makes use of the postgres user, which by coincidence is the same name as the database we want: now instead of we specify the postgres user). - One user test is amended to use the env var provided user if necessary. Also adds some logging to make it more clear why failures around the URI are happening. Change-Id: I03d5b40a62d7034e00f45ba8151292688ab61de4 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/685995 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 1d37c52 commit d4de666

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

internal/database/dbutil.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func DBConnURI(dbName string) string {
3636
host = serverconfig.GetEnv("GO_DISCOVERY_DATABASE_HOST", "localhost")
3737
port = serverconfig.GetEnv("GO_DISCOVERY_DATABASE_PORT", "5432")
3838
)
39-
cs := fmt.Sprintf("postgres://%s/%s?sslmode=disable&user=%s&password=%s&port=%s&timezone=UTC",
40-
host, dbName, url.QueryEscape(user), url.QueryEscape(password), url.QueryEscape(port))
41-
return cs
39+
// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS
40+
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable&timezone=UTC",
41+
url.QueryEscape(user), url.QueryEscape(password), url.QueryEscape(host), url.QueryEscape(port), dbName)
4242
}
4343

4444
// MultiErr can be used to combine one or more errors into a single error.
@@ -79,17 +79,21 @@ func ConnectAndExecute(uri string, dbFunc func(*sql.DB) error) (outerErr error)
7979

8080
// CreateDB creates a new database dbName.
8181
func CreateDB(dbName string) error {
82-
return ConnectAndExecute(DBConnURI(""), func(pg *sql.DB) error {
82+
uri := DBConnURI("postgres")
83+
err := ConnectAndExecute(uri, func(pg *sql.DB) error {
8384
if _, err := pg.Exec(fmt.Sprintf(`
8485
CREATE DATABASE %q
8586
TEMPLATE=template0
8687
LC_COLLATE='C'
8788
LC_CTYPE='C';`, dbName)); err != nil {
8889
return fmt.Errorf("error creating %q: %v", dbName, err)
8990
}
90-
9191
return nil
9292
})
93+
if err != nil {
94+
return fmt.Errorf("error connecting to %q: %v", uri, err)
95+
}
96+
return nil
9397
}
9498

9599
// DropDB drops the database named dbName.
@@ -118,7 +122,8 @@ func CreateDBIfNotExists(dbName string) error {
118122
func checkIfDBExists(dbName string) (bool, error) {
119123
var exists bool
120124

121-
err := ConnectAndExecute(DBConnURI(""), func(pg *sql.DB) error {
125+
uri := DBConnURI("postgres")
126+
err := ConnectAndExecute(uri, func(pg *sql.DB) error {
122127
rows, err := pg.Query("SELECT 1 from pg_database WHERE datname = $1 LIMIT 1", dbName)
123128
if err != nil {
124129
return err
@@ -132,8 +137,11 @@ func checkIfDBExists(dbName string) (bool, error) {
132137

133138
return rows.Err()
134139
})
140+
if err != nil {
141+
return false, fmt.Errorf("error connecting to %q: %w", uri, err)
142+
}
135143

136-
return exists, err
144+
return exists, nil
137145
}
138146

139147
// TryToMigrate attempts to migrate the database named dbName to the latest

internal/postgres/postgres_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"os"
1112
"testing"
1213
"time"
1314

@@ -118,7 +119,11 @@ func TestGetUserInfo(t *testing.T) {
118119
testDB, release := acquire(t)
119120
defer release()
120121

121-
got, err := testDB.GetUserInfo(ctx, "postgres")
122+
dbUser := "postgres"
123+
if u := os.Getenv("GO_DISCOVERY_DATABASE_USER"); u != "" {
124+
dbUser = u
125+
}
126+
got, err := testDB.GetUserInfo(ctx, dbUser)
122127
if err != nil {
123128
t.Fatal(err)
124129
}

0 commit comments

Comments
 (0)