2019-12-04 03:36:21 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package cockroachutil
|
|
|
|
|
|
|
|
import (
|
2020-01-13 13:57:47 +00:00
|
|
|
"context"
|
2020-01-16 20:53:22 +00:00
|
|
|
"crypto/rand"
|
2019-12-20 19:03:58 +00:00
|
|
|
"encoding/hex"
|
2019-12-04 03:36:21 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-12-04 03:36:21 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/private/dbutil"
|
2020-06-28 04:56:29 +01:00
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
2020-01-19 14:41:23 +00:00
|
|
|
"storj.io/storj/private/tagsql"
|
2019-12-04 03:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-12-20 19:03:58 +00:00
|
|
|
// CreateRandomTestingSchemaName creates a random schema name string.
|
|
|
|
func CreateRandomTestingSchemaName(n int) string {
|
|
|
|
data := make([]byte, n)
|
|
|
|
_, _ = rand.Read(data)
|
|
|
|
return hex.EncodeToString(data)
|
|
|
|
}
|
|
|
|
|
2019-12-04 03:36:21 +00:00
|
|
|
// OpenUnique opens a temporary unique CockroachDB database that will be cleaned up when closed.
|
|
|
|
// It is expected that this should normally be used by way of
|
|
|
|
// "storj.io/storj/private/dbutil/tempdb".OpenUnique() instead of calling it directly.
|
2020-01-13 13:57:47 +00:00
|
|
|
func OpenUnique(ctx context.Context, connStr string, schemaPrefix string) (db *dbutil.TempDatabase, err error) {
|
2019-12-04 03:36:21 +00:00
|
|
|
if !strings.HasPrefix(connStr, "cockroach://") {
|
|
|
|
return nil, errs.New("expected a cockroachDB URI, but got %q", connStr)
|
|
|
|
}
|
2019-12-20 19:03:58 +00:00
|
|
|
|
|
|
|
schemaName := schemaPrefix + "-" + CreateRandomTestingSchemaName(8)
|
|
|
|
|
2020-01-19 14:41:23 +00:00
|
|
|
masterDB, err := tagsql.Open("cockroach", connStr)
|
2019-12-04 03:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, masterDB.Close())
|
|
|
|
}()
|
2020-01-19 14:41:23 +00:00
|
|
|
|
|
|
|
err = masterDB.PingContext(ctx)
|
2019-12-04 03:36:21 +00:00
|
|
|
if err != nil {
|
2020-01-14 11:41:23 +00:00
|
|
|
return nil, errs.New("Could not open masterDB at conn %q: %w", connStr, err)
|
2019-12-04 03:36:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 04:56:29 +01:00
|
|
|
_, err = masterDB.Exec(ctx, "CREATE DATABASE "+pgutil.QuoteIdentifier(schemaName))
|
2019-12-04 03:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-01-19 14:41:23 +00:00
|
|
|
cleanup := func(cleanupDB tagsql.DB) error {
|
2020-06-28 04:56:29 +01:00
|
|
|
_, err := cleanupDB.Exec(context.TODO(), "DROP DATABASE "+pgutil.QuoteIdentifier(schemaName))
|
2019-12-04 03:36:21 +00:00
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiedConnStr, err := changeDBTargetInConnStr(connStr, schemaName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, cleanup(masterDB))
|
|
|
|
}
|
|
|
|
|
2020-01-19 14:41:23 +00:00
|
|
|
sqlDB, err := tagsql.Open("cockroach", modifiedConnStr)
|
2019-12-04 03:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(errs.Wrap(err), cleanup(masterDB))
|
|
|
|
}
|
|
|
|
|
2020-03-23 19:58:36 +00:00
|
|
|
dbutil.Configure(sqlDB, "tmp_cockroach", mon)
|
2019-12-04 03:36:21 +00:00
|
|
|
return &dbutil.TempDatabase{
|
|
|
|
DB: sqlDB,
|
|
|
|
ConnStr: modifiedConnStr,
|
|
|
|
Schema: schemaName,
|
2019-12-11 19:04:09 +00:00
|
|
|
Driver: "cockroach",
|
2019-12-04 03:36:21 +00:00
|
|
|
Implementation: dbutil.Cockroach,
|
|
|
|
Cleanup: cleanup,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func changeDBTargetInConnStr(connStr string, newDBName string) (string, error) {
|
|
|
|
connURL, err := url.Parse(connStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", errs.Wrap(err)
|
|
|
|
}
|
|
|
|
connURL.Path = newDBName
|
|
|
|
return connURL.String(), nil
|
|
|
|
}
|