@@ -12,33 +12,40 @@ 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 ) {
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+ }
31+
32+ func InitAdminDB () {
33+ pgConfig := createPostgreSQLPrimaryAdminConfig ()
34+ initDB (DB , pgConfig , globalPgConfig )
35+ }
36+
37+ func InitUserDB () {
38+ pgConfig := createPostgreSQLPrimaryUserConfig ()
39+ initDB (DB , pgConfig , globalPgConfig )
3240 if utils .CoreCfg .DBReadReplicaEnabled {
33- pgConfig := loadEnvPostgreSQLConfig (ReadReplicaConfigured ())
34- DBReadReplica = openPostgreSQL (pgConfig )
35- check (DBReadReplica )
41+ pgConfig := createPostgreSQLReplicaConfig ()
42+ initDB (DBReadReplica , pgConfig , globalPgReadReplicaConfig )
3643 }
3744}
3845
3946// Configure Configure database, PostgreSQL or SQLite connection
4047func Configure () {
41- InitDB ()
48+ InitUserDB ()
4249 loadAdditionalParamsFromDB ()
4350}
4451
@@ -104,19 +111,11 @@ func check(db *gorm.DB) {
104111}
105112
106113// 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- }
114+ func createPostgreSQLPrimaryConfig () * PostgreSQLConfig {
114115 config := PostgreSQLConfig {
115- User : utils .CoreCfg .DBUser ,
116- Host : host ,
117- Port : port ,
116+ Host : utils .CoreCfg .DBHost ,
117+ Port : utils .CoreCfg .DBPort ,
118118 Database : utils .CoreCfg .DBName ,
119- Passwd : utils .CoreCfg .DBPassword ,
120119 SSLMode : utils .CoreCfg .DBSslMode ,
121120 SSLRootCert : utils .CoreCfg .DBSslRootCert ,
122121 Debug : utils .CoreCfg .DBDebug ,
@@ -128,6 +127,29 @@ func loadEnvPostgreSQLConfig(useReadReplica bool) *PostgreSQLConfig {
128127 return & config
129128}
130129
130+ func createPostgreSQLReplicaConfig () * PostgreSQLConfig {
131+ config := createPostgreSQLPrimaryConfig ()
132+ config .User = utils .CoreCfg .DBUser
133+ config .Passwd = utils .CoreCfg .DBPassword
134+ config .Host = utils .CoreCfg .DBReadReplicaHost
135+ config .Port = utils .CoreCfg .DBReadReplicaPort
136+ return config
137+ }
138+
139+ func createPostgreSQLPrimaryUserConfig () * PostgreSQLConfig {
140+ config := createPostgreSQLPrimaryConfig ()
141+ config .User = utils .CoreCfg .DBUser
142+ config .Passwd = utils .CoreCfg .DBPassword
143+ return config
144+ }
145+
146+ func createPostgreSQLPrimaryAdminConfig () * PostgreSQLConfig {
147+ config := createPostgreSQLPrimaryConfig ()
148+ config .User = utils .CoreCfg .DBAdminUser
149+ config .Passwd = utils .CoreCfg .DBAdminPassword
150+ return config
151+ }
152+
131153// create "data source" config string needed for database connection opening
132154func dataSourceName (dbConfig * PostgreSQLConfig ) string {
133155 dbsource := fmt .Sprintf ("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s statement_timeout=%d" ,
0 commit comments