2019-12-04 03:36:21 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pgutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2020-04-27 20:34:42 +01:00
|
|
|
"storj.io/storj/private/dbutil/pgtest"
|
2019-12-04 03:36:21 +00:00
|
|
|
"storj.io/storj/private/dbutil/tempdb"
|
2020-01-19 14:41:23 +00:00
|
|
|
"storj.io/storj/private/tagsql"
|
2019-12-04 03:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTempPostgresDB(t *testing.T) {
|
2020-04-27 20:34:42 +01:00
|
|
|
connstr := pgtest.PickPostgres(t)
|
|
|
|
|
2019-12-04 03:36:21 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
prefix := "name#spaced/Test/DB"
|
2020-04-27 20:34:42 +01:00
|
|
|
testDB, err := tempdb.OpenUnique(ctx, connstr, prefix)
|
2019-12-04 03:36:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert new test db exists and can be connected to again
|
2020-01-19 14:41:23 +00:00
|
|
|
otherConn, err := tagsql.Open(testDB.Driver, testDB.ConnStr)
|
2019-12-04 03:36:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(otherConn.Close)
|
|
|
|
|
|
|
|
// verify the name matches expectation
|
|
|
|
var name *string
|
2020-01-19 14:41:23 +00:00
|
|
|
row := otherConn.QueryRowContext(ctx, `SELECT current_schema()`)
|
2019-12-04 03:36:21 +00:00
|
|
|
err = row.Scan(&name)
|
|
|
|
require.NoErrorf(t, err, "connStr=%q", testDB.ConnStr)
|
|
|
|
require.NotNilf(t, name, "PG has no current_schema, which means the one we asked for doesn't exist. connStr=%q", testDB.ConnStr)
|
|
|
|
require.Truef(t, strings.HasPrefix(*name, prefix), "Expected prefix of %q for current db name, but found %q", prefix, name)
|
|
|
|
|
|
|
|
// verify there is an entry in pg_namespace with such a name
|
|
|
|
var count int
|
2020-01-19 14:41:23 +00:00
|
|
|
row = otherConn.QueryRowContext(ctx, `SELECT COUNT(*) FROM pg_namespace WHERE nspname = current_schema`)
|
2019-12-04 03:36:21 +00:00
|
|
|
err = row.Scan(&count)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equalf(t, 1, count, "Expected 1 schema with matching name, but counted %d", count)
|
|
|
|
|
|
|
|
// close testDB but leave otherConn open
|
|
|
|
err = testDB.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert new test schema was deleted
|
2020-01-19 14:41:23 +00:00
|
|
|
row = otherConn.QueryRowContext(ctx, `SELECT COUNT(*) FROM pg_namespace WHERE nspname = current_schema`)
|
2019-12-04 03:36:21 +00:00
|
|
|
err = row.Scan(&count)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equalf(t, 0, count, "Expected 0 schemas with matching name, but counted %d (deletion failure?)", count)
|
|
|
|
}
|