@@ -11,34 +11,48 @@ import (
1111 "gorm.io/gorm/logger"
1212)
1313
14+ type DBMode int
15+
16+ const (
17+ User DBMode = iota
18+ Admin
19+ Replica
20+ )
21+
1422var (
1523 DB * gorm.DB
1624 DBReadReplica * gorm.DB
1725 OtherAdvisoryTypes []string
1826 AdvisoryTypes map [int ]string
19- globalPgConfig * PostgreSQLConfig
27+ globalPgConfig map [ DBMode ] PostgreSQLConfig
2028)
2129
22- func InitDB () {
23- pgConfig := loadEnvPostgreSQLConfig (false )
24- if DB != nil && pgConfig == globalPgConfig {
25- // reuse connection
26- check (DB )
27- return
30+ func initDB (mode DBMode , db * gorm.DB , pgConfig PostgreSQLConfig ) * gorm.DB {
31+ if db == nil || pgConfig != globalPgConfig [mode ] {
32+ globalPgConfig [mode ] = pgConfig
33+ db = openPostgreSQL (& pgConfig )
2834 }
29- globalPgConfig = pgConfig
30- DB = openPostgreSQL (pgConfig )
31- check (DB )
32- if utils .CoreCfg .DBReadReplicaEnabled {
33- pgConfig := loadEnvPostgreSQLConfig (ReadReplicaConfigured ())
34- DBReadReplica = openPostgreSQL (pgConfig )
35- check (DBReadReplica )
35+ check (db )
36+ return db
37+ }
38+
39+ func InitDB (mode DBMode ) {
40+ if mode == Replica {
41+ if utils .CoreCfg .DBReadReplicaEnabled && ReadReplicaConfigured () {
42+ pgConfig := createPostgreSQLConfig (mode )
43+ DBReadReplica = initDB (mode , DBReadReplica , pgConfig )
44+ }
45+ return
3646 }
47+
48+ pgConfig := createPostgreSQLConfig (mode )
49+ DB = initDB (mode , DB , pgConfig )
3750}
3851
3952// Configure Configure database, PostgreSQL or SQLite connection
4053func Configure () {
41- InitDB ()
54+ InitDB (User )
55+ InitDB (Replica )
4256 loadAdditionalParamsFromDB ()
4357}
4458
@@ -104,17 +118,11 @@ func check(db *gorm.DB) {
104118}
105119
106120// load database config from environment vars using inserted prefix
107- func loadEnvPostgreSQLConfig (useReadReplica bool ) * PostgreSQLConfig {
108- host := utils .CoreCfg .DBHost
109- port := utils .CoreCfg .DBPort
110- if useReadReplica {
111- host = utils .CoreCfg .DBReadReplicaHost
112- port = utils .CoreCfg .DBReadReplicaPort
113- }
121+ func createPostgreSQLConfig (mode DBMode ) PostgreSQLConfig {
114122 config := PostgreSQLConfig {
115123 User : utils .CoreCfg .DBUser ,
116- Host : host ,
117- Port : port ,
124+ Host : utils . CoreCfg . DBHost ,
125+ Port : utils . CoreCfg . DBPort ,
118126 Database : utils .CoreCfg .DBName ,
119127 Passwd : utils .CoreCfg .DBPassword ,
120128 SSLMode : utils .CoreCfg .DBSslMode ,
@@ -125,7 +133,17 @@ func loadEnvPostgreSQLConfig(useReadReplica bool) *PostgreSQLConfig {
125133 MaxIdleConnections : utils .CoreCfg .DBMaxIdleConnections ,
126134 MaxConnectionLifetimeS : utils .CoreCfg .DBMaxConnectionLifetimeS ,
127135 }
128- return & config
136+ switch mode {
137+ case User :
138+ // noop
139+ case Admin :
140+ config .User = utils .CoreCfg .DBAdminUser
141+ config .Passwd = utils .CoreCfg .DBAdminPassword
142+ case Replica :
143+ config .Host = utils .CoreCfg .DBReadReplicaHost
144+ config .Port = utils .CoreCfg .DBReadReplicaPort
145+ }
146+ return config
129147}
130148
131149// create "data source" config string needed for database connection opening
0 commit comments