@@ -12,33 +12,41 @@ import (
1212)
1313
1414var (
15- DB * gorm.DB
16- DBReadReplica * gorm.DB
17- OtherAdvisoryTypes []string
18- AdvisoryTypes map [int ]string
19- globalPgConfig * PostgreSQLConfig
15+ DB * gorm.DB
16+ DBReadReplica * gorm.DB
17+ OtherAdvisoryTypes []string
18+ AdvisoryTypes map [int ]string
19+ globalPgConfig * PostgreSQLConfig
20+ globalPgReadReplicaConfig * PostgreSQLConfig
2021)
2122
22- func InitDB () {
23- pgConfig := loadEnvPostgreSQLConfig (false )
24- if DB != nil && pgConfig == globalPgConfig {
25- // reuse connection
26- check (DB )
27- return
23+ func initDB (db * gorm.DB , pgConfig * PostgreSQLConfig , globalPgConfig * PostgreSQLConfig ) * gorm.DB {
24+ if db == nil || pgConfig != globalPgConfig {
25+ // create new connection if not already exists
26+ globalPgConfig = pgConfig
27+ db = openPostgreSQL (pgConfig )
2828 }
29- globalPgConfig = pgConfig
30- DB = openPostgreSQL (pgConfig )
31- check (DB )
29+ check (db )
30+ return db
31+ }
32+
33+ func InitAdminDB () {
34+ pgConfig := createPostgreSQLPrimaryAdminConfig ()
35+ DB = initDB (DB , pgConfig , globalPgConfig )
36+ }
37+
38+ func InitUserDB () {
39+ pgConfig := createPostgreSQLPrimaryUserConfig ()
40+ DB = initDB (DB , pgConfig , globalPgConfig )
3241 if utils .CoreCfg .DBReadReplicaEnabled {
33- pgConfig := loadEnvPostgreSQLConfig (ReadReplicaConfigured ())
34- DBReadReplica = openPostgreSQL (pgConfig )
35- check (DBReadReplica )
42+ pgConfig := createPostgreSQLReplicaConfig ()
43+ DBReadReplica = initDB (DBReadReplica , pgConfig , globalPgReadReplicaConfig )
3644 }
3745}
3846
3947// Configure Configure database, PostgreSQL or SQLite connection
4048func Configure () {
41- InitDB ()
49+ InitUserDB ()
4250 loadAdditionalParamsFromDB ()
4351}
4452
@@ -104,19 +112,11 @@ func check(db *gorm.DB) {
104112}
105113
106114// 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- }
115+ func createPostgreSQLPrimaryConfig () * PostgreSQLConfig {
114116 config := PostgreSQLConfig {
115- User : utils .CoreCfg .DBUser ,
116- Host : host ,
117- Port : port ,
117+ Host : utils .CoreCfg .DBHost ,
118+ Port : utils .CoreCfg .DBPort ,
118119 Database : utils .CoreCfg .DBName ,
119- Passwd : utils .CoreCfg .DBPassword ,
120120 SSLMode : utils .CoreCfg .DBSslMode ,
121121 SSLRootCert : utils .CoreCfg .DBSslRootCert ,
122122 Debug : utils .CoreCfg .DBDebug ,
@@ -128,6 +128,29 @@ func loadEnvPostgreSQLConfig(useReadReplica bool) *PostgreSQLConfig {
128128 return & config
129129}
130130
131+ func createPostgreSQLReplicaConfig () * PostgreSQLConfig {
132+ config := createPostgreSQLPrimaryConfig ()
133+ config .User = utils .CoreCfg .DBUser
134+ config .Passwd = utils .CoreCfg .DBPassword
135+ config .Host = utils .CoreCfg .DBReadReplicaHost
136+ config .Port = utils .CoreCfg .DBReadReplicaPort
137+ return config
138+ }
139+
140+ func createPostgreSQLPrimaryUserConfig () * PostgreSQLConfig {
141+ config := createPostgreSQLPrimaryConfig ()
142+ config .User = utils .CoreCfg .DBUser
143+ config .Passwd = utils .CoreCfg .DBPassword
144+ return config
145+ }
146+
147+ func createPostgreSQLPrimaryAdminConfig () * PostgreSQLConfig {
148+ config := createPostgreSQLPrimaryConfig ()
149+ config .User = utils .CoreCfg .DBAdminUser
150+ config .Passwd = utils .CoreCfg .DBAdminPassword
151+ return config
152+ }
153+
131154// create "data source" config string needed for database connection opening
132155func dataSourceName (dbConfig * PostgreSQLConfig ) string {
133156 dbsource := fmt .Sprintf ("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s statement_timeout=%d" ,
0 commit comments