-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathcockroach.go
More file actions
169 lines (150 loc) · 3.67 KB
/
Copy pathcockroach.go
File metadata and controls
169 lines (150 loc) · 3.67 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cockroach
import (
"context"
"database/sql"
"fmt"
"strconv"
"github.com/coreos/go-semver/semver"
"github.com/interuss/stacktrace"
)
var (
UnknownVersion = &semver.Version{}
)
type (
// Credentials models connect credentials.
Credentials struct {
Username string
Password string
}
// SSL models SSL configuration parameters.
SSL struct {
Mode string
Dir string
}
// ConnectParameters bundles up parameters used for connecting to a CRDB instance.
ConnectParameters struct {
ApplicationName string
Host string
Port int
DBName string
Credentials Credentials
SSL SSL
}
)
func parsePortOrDefault(port string, defaultPort int64) int64 {
p, err := strconv.ParseInt(port, 10, 16)
if err != nil {
p = defaultPort
}
return p
}
// connectParametersFromMap constructs a ConnectParameters instance from m.
func connectParametersFromMap(m map[string]string) ConnectParameters {
return ConnectParameters{
ApplicationName: m["application_name"],
DBName: m["db_name"],
Host: m["host"],
Port: int(parsePortOrDefault(m["port"], 0)),
Credentials: Credentials{
Username: m["user"],
},
SSL: SSL{
Mode: m["ssl_mode"],
Dir: m["ssl_dir"],
},
}
}
// BuildURI returns a URI built from p.
func (p ConnectParameters) BuildURI() (string, error) {
an := p.ApplicationName
if an == "" {
an = "dss"
}
h := p.Host
if h == "" {
return "", stacktrace.NewError("Missing crdb hostname")
}
port := p.Port
if port == 0 {
return "", stacktrace.NewError("Missing crdb port")
}
u := p.Credentials.Username
if u == "" {
return "", stacktrace.NewError("Missing crdb user")
}
ssl := p.SSL.Mode
if ssl == "" {
return "", stacktrace.NewError("Missing crdb ssl_mode")
}
db := p.DBName
if db != "" {
db = fmt.Sprintf("/%s", db)
}
if ssl == "disable" {
return fmt.Sprintf("postgresql://%s@%s:%d%s?application_name=%s&sslmode=disable", u, h, port, db, an), nil
}
dir := p.SSL.Dir
if dir == "" {
return "", stacktrace.NewError("Missing crdb ssl_dir")
}
return fmt.Sprintf(
"postgresql://%s@%s:%d%s?application_name=%s&sslmode=%s&sslrootcert=%s/ca.crt&sslcert=%s/client.%s.crt&sslkey=%s/client.%s.key",
u, h, port, db, an, ssl, dir, dir, u, dir, u,
), nil
}
// DB models a connection to a CRDB instance.
type DB struct {
*sql.DB
}
// Dial returns a DB instance connected to a cockroach instance available at
// "uri".
// https://www.cockroachlabs.com/docs/stable/connection-parameters.html
func Dial(uri string) (*DB, error) {
db, err := sql.Open("postgres", uri)
if err != nil {
return nil, err
}
return &DB{
DB: db,
}, nil
}
// GetVersion returns the Schema Version of the requested DB Name
func (db *DB) GetVersion(ctx context.Context, dbName string) (*semver.Version, error) {
const query = `
SELECT EXISTS (
SELECT
*
FROM
information_schema.tables
WHERE
table_name = 'schema_versions'
AND
table_catalog = $1
)
`
var (
exists bool
getVersionQuery = fmt.Sprintf(`
SELECT
schema_version
FROM
%s.schema_versions
WHERE
onerow_enforcer = TRUE`, dbName)
)
if err := db.QueryRowContext(ctx, query, dbName).Scan(&exists); err != nil {
return nil, stacktrace.Propagate(err, "Error scanning table listing row")
}
if !exists {
// Database has not been bootstrapped using DB Schema Manager
return UnknownVersion, nil
}
var dbVersion string
if err := db.QueryRowContext(ctx, getVersionQuery).Scan(&dbVersion); err != nil {
return nil, stacktrace.Propagate(err, "Error scanning version row")
}
if len(dbVersion) > 0 && dbVersion[0] == 'v' {
dbVersion = dbVersion[1:]
}
return semver.NewVersion(dbVersion)
}