-
Notifications
You must be signed in to change notification settings - Fork 2
/
cockroach.go
51 lines (45 loc) · 1.49 KB
/
cockroach.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
package coresql
import (
"log"
"os"
"github.com/golang-migrate/migrate/v4"
)
const (
// DefaultCockroachURL is the default url to a CockroachDB database.
DefaultCockroachURL = "tcp(127.0.0.1:26257)/defaultdb"
)
// CockroachURLFromEnv tries to retrieve the cockroach url from the environment.
func CockroachURLFromEnv() string {
url := os.Getenv("COCKROACH_URL")
if url == "" {
url = DefaultCockroachURL
}
return url
}
// MustOpenCockroachWithMigration opens a cockroach database connection with an associated migration instance
// and craches if the connection cannot be obtained.
// This assumes you use a postgres driver like https://github.com/lib/pq to interact with your postgres database.
func MustOpenCockroachWithMigration(dsn, sourceURL string) (*DB, *migrate.Migrate) {
database, migration, err := OpenCockroachWithMigration(dsn, sourceURL)
if err != nil {
log.Fatalln(err)
}
return database, migration
}
// OpenCockroachWithMigration opens a cockroach database connection with an associated migration instance.
// This assumes you use a postgres driver like https://github.com/lib/pq to interact with your postgres database.
func OpenCockroachWithMigration(dsn, sourceURL string) (*DB, *migrate.Migrate, error) {
const (
migDriver = "cockroach"
dbDriver = "postgres"
)
migration, err := OpenMigration(migDriver, dsn, sourceURL)
if err != nil {
return nil, nil, err
}
database, err := Open(dbDriver, dsn)
if err != nil {
return nil, nil, err
}
return database, migration, nil
}