forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
85 lines (79 loc) · 2.44 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
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"errors"
"fmt"
"log"
)
// DBConfig holds configuration information to connect to a database.
// Parameters for the config.
// * dbname - The name of the database to connect to
// * user - The user to sign in as
// * password - The user's password
// * host - The host to connect to. Values that start with / are for unix domain sockets.
// (default is localhost)
// * port - The port to bind to. (default is 5432)
// * sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)
// Valid SSL modes:
// * disable - No SSL
// * require - Always SSL (skip verification)
// * verify-full - Always SSL (require verification)
type DBConfig struct {
DbType string
Url string
Username string
Password string
DbName string
Sslmode string
Port int64 // Is int64 to match the type that rds.Endpoint.Port is in the AWS RDS SDK.
}
// DBinit is a generic helper function that will try to connect to a database with the config in the input.
// Supported DB types:
// * postgres
// * sqlite3
func DBInit(dbConfig *DBConfig) (*gorm.DB, error) {
var DB gorm.DB
var err error
switch dbConfig.DbType {
case "postgres":
conn := "dbname=%s user=%s password=%s host=%s sslmode=%s port=%d"
conn = fmt.Sprintf(conn,
dbConfig.DbName,
dbConfig.Username,
dbConfig.Password,
dbConfig.Url,
dbConfig.Sslmode,
dbConfig.Port)
DB, err = gorm.Open("postgres", conn)
case "sqlite3":
DB, err = gorm.Open("sqlite3", dbConfig.DbName)
default:
errorString := "Cannot connect. Unsupported DB type: (" + dbConfig.DbType + ")"
log.Println(errorString)
return nil, errors.New(errorString)
}
if err != nil {
log.Println("Error!")
return nil, err
}
if err = DB.DB().Ping(); err != nil {
log.Println("Unable to verify connection to database")
return nil, err
}
return &DB, nil
}
// InternalDBInit initializes the internal database connection that the service broker will use.
// In addition to calling DBInit(), it also makes sure that the tables are setup for Instance and DBConfig structs.
func InternalDBInit(dbConfig *DBConfig) (*gorm.DB, error) {
db, err := DBInit(dbConfig)
if err == nil {
db.DB().SetMaxOpenConns(10)
log.Println("Migrating")
// Automigrate!
db.AutoMigrate(Instance{}) // Add all your models here to help setup the database tables.
log.Println("Migrated")
}
return db, err
}