-
Notifications
You must be signed in to change notification settings - Fork 401
/
db.go
91 lines (75 loc) · 2.48 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package cockroachutil
import (
"context"
"crypto/rand"
"encoding/hex"
"net/url"
"strings"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"storj.io/storj/private/dbutil"
"storj.io/storj/private/dbutil/pgutil"
"storj.io/storj/private/tagsql"
)
var mon = monkit.Package()
// CreateRandomTestingSchemaName creates a random schema name string.
func CreateRandomTestingSchemaName(n int) string {
data := make([]byte, n)
_, _ = rand.Read(data)
return hex.EncodeToString(data)
}
// 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.
func OpenUnique(ctx context.Context, connStr string, schemaPrefix string) (db *dbutil.TempDatabase, err error) {
if !strings.HasPrefix(connStr, "cockroach://") {
return nil, errs.New("expected a cockroachDB URI, but got %q", connStr)
}
schemaName := schemaPrefix + "-" + CreateRandomTestingSchemaName(8)
masterDB, err := tagsql.Open(ctx, "cockroach", connStr)
if err != nil {
return nil, errs.Wrap(err)
}
defer func() {
err = errs.Combine(err, masterDB.Close())
}()
err = masterDB.PingContext(ctx)
if err != nil {
return nil, errs.New("Could not open masterDB at conn %q: %w", connStr, err)
}
_, err = masterDB.Exec(ctx, "CREATE DATABASE "+pgutil.QuoteIdentifier(schemaName))
if err != nil {
return nil, errs.Wrap(err)
}
cleanup := func(cleanupDB tagsql.DB) error {
_, err := cleanupDB.Exec(context.TODO(), "DROP DATABASE "+pgutil.QuoteIdentifier(schemaName))
return errs.Wrap(err)
}
modifiedConnStr, err := changeDBTargetInConnStr(connStr, schemaName)
if err != nil {
return nil, errs.Combine(err, cleanup(masterDB))
}
sqlDB, err := tagsql.Open(ctx, "cockroach", modifiedConnStr)
if err != nil {
return nil, errs.Combine(errs.Wrap(err), cleanup(masterDB))
}
dbutil.Configure(ctx, sqlDB, "tmp_cockroach", mon)
return &dbutil.TempDatabase{
DB: sqlDB,
ConnStr: modifiedConnStr,
Schema: schemaName,
Driver: "cockroach",
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
}