-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_connect.go
50 lines (43 loc) · 1.21 KB
/
collection_connect.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
package gorpheus
import (
"fmt"
"os"
"github.com/gobuffalo/fizz/translators"
"github.com/jmoiron/sqlx"
"github.com/xo/dburl"
)
func (c *Collection) connectToDb(params *MigrationParams) (*sqlx.DB, error) {
var connectionURL string = params.Connection.ConnectionURL
var dbConn *sqlx.DB
var url *dburl.URL
var err error
if connectionURL == "" && params.Connection.Conn == nil {
connectionURL = os.Getenv(params.Connection.EnvKeyName)
if connectionURL == "" {
return nil, fmt.Errorf("`%s` environment variable is empty", params.Connection.EnvKeyName)
}
}
// let's parse the database URL
url, err = dburl.Parse(connectionURL)
if err != nil {
return nil, fmt.Errorf("unable to parse database URL: %v", err)
}
if params.Connection.Conn != nil {
dbConn = sqlx.NewDb(params.Connection.Conn, url.Driver)
} else {
dbConn, err = sqlx.Open(url.Driver, url.DSN)
}
if url.Driver == "" {
return nil, fmt.Errorf("unable to detect database driver")
}
switch url.Driver {
case "sqlite3":
c.SetTranslator(translators.NewSQLite(url.DSN))
case "postgres":
c.SetTranslator(translators.NewPostgres())
case "mysql":
c.SetTranslator(translators.NewMySQL(url.DSN, url.Path))
default:
}
return dbConn, err
}