-
Notifications
You must be signed in to change notification settings - Fork 2
/
reload_db.go
42 lines (35 loc) · 961 Bytes
/
reload_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
package zeroweb
import (
"github.com/go-pg/pg"
"github.com/google/go-cmp/cmp"
"github.com/rs/zerolog/log"
)
func (zeroweb *Zeroweb) reloadDB() error {
if !zeroweb.Config.GetBool("db.Enabled") {
return nil
}
var config pg.Options
var oldconfig *pg.Options = nil
if zeroweb.DB != nil {
oldconfig = zeroweb.DB.Options()
}
err := zeroweb.Config.UnmarshalKey("db", &config)
if err != nil {
zeroweb.Log.Error().Err(err).Msg("unable to decode db config into struct")
return err
}
if oldconfig != nil && cmp.Equal(config, oldconfig) {
return nil // config didn't change for DB
}
//config.DialFunc //TODO cache dns, reuseport
db := pg.Connect(&config)
if db == nil {
if zeroweb.DB == nil {
log.Fatal().Err(err).Interface("config", config).Msg("connection to DB failed")
}
log.Error().Err(err).Interface("config", config).Msg("connection to DB failed (keeping old db connection)")
return err
}
zeroweb.DB = db
return nil
}