forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdb.go
81 lines (72 loc) · 2.91 KB
/
testdb.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
package testdb
import (
"database/sql"
"errors"
"fmt"
"net/url"
"strings"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/sqlutil/pg"
)
const (
// PristineDBName is a clean copy of test DB with migrations.
PristineDBName = "chainlink_test_pristine"
// TestDBNamePrefix is a common prefix that will be auto-removed by the dangling DB cleanup process.
TestDBNamePrefix = "chainlink_test_"
)
// CreateOrReplace creates a database named with a common prefix and the given suffix, and returns the URL.
// If the database already exists, it will be dropped and re-created.
// If withTemplate is true, the pristine DB will be used as a template.
func CreateOrReplace(parsed url.URL, suffix string, withTemplate bool) (string, error) {
if parsed.Path == "" {
return "", errors.New("path missing from database URL")
}
// Match the naming schema that our dangling DB cleanup methods expect
dbname := TestDBNamePrefix + suffix
if l := len(dbname); l > 63 {
return "", fmt.Errorf("dbname %v too long (%d), max is 63 bytes. Try a shorter suffix", dbname, l)
}
// Cannot drop test database if we are connected to it, so we must connect
// to a different one. 'postgres' should be present on all postgres installations
parsed.Path = "/postgres"
db, err := sql.Open(string(pgcommon.Postgres), parsed.String())
if err != nil {
return "", fmt.Errorf("in order to drop the test database, we need to connect to a separate database"+
" called 'postgres'. But we are unable to open 'postgres' database: %+v\n", err)
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbname))
if err != nil {
return "", fmt.Errorf("unable to drop postgres migrations test database: %v", err)
}
if withTemplate {
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s WITH TEMPLATE %s", dbname, PristineDBName))
} else {
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname))
}
if err != nil {
return "", fmt.Errorf("unable to create postgres test database with name '%s': %v", dbname, err)
}
parsed.Path = fmt.Sprintf("/%s", dbname)
return parsed.String(), nil
}
// Drop drops the database at the given URL.
func Drop(dbURL url.URL) error {
if dbURL.Path == "" {
return errors.New("path missing from database URL")
}
dbname := strings.TrimPrefix(dbURL.Path, "/")
// Cannot drop test database if we are connected to it, so we must connect
// to a different one. 'postgres' should be present on all postgres installations
dbURL.Path = "/postgres"
db, err := sql.Open(string(pgcommon.Postgres), dbURL.String())
if err != nil {
return fmt.Errorf("in order to drop the test database, we need to connect to a separate database"+
" called 'postgres'. But we are unable to open 'postgres' database: %+v\n", err)
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbname))
if err != nil {
return fmt.Errorf("unable to drop postgres migrations test database: %v", err)
}
return nil
}