@@ -12,33 +12,42 @@ 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 {
23+ func initDB (db * gorm.DB , pgConfig * PostgreSQLConfig , globalPgConfig * PostgreSQLConfig ) {
24+ if db != nil && pgConfig == globalPgConfig {
2525 // reuse connection
2626 check (DB )
2727 return
2828 }
2929 globalPgConfig = pgConfig
30- DB = openPostgreSQL (pgConfig )
31- check (DB )
30+ db = openPostgreSQL (pgConfig )
31+ check (db )
32+ }
33+
34+ func InitAdminDB () {
35+ pgConfig := createPostgreSQLPrimaryAdminConfig ()
36+ initDB (DB , pgConfig , globalPgConfig )
37+ }
38+
39+ func InitUserDB () {
40+ pgConfig := createPostgreSQLPrimaryUserConfig ()
41+ initDB (DB , pgConfig , globalPgConfig )
3242 if utils .CoreCfg .DBReadReplicaEnabled {
33- pgConfig := loadEnvPostgreSQLConfig (ReadReplicaConfigured ())
34- DBReadReplica = openPostgreSQL (pgConfig )
35- check (DBReadReplica )
43+ pgConfig := createPostgreSQLReplicaConfig ()
44+ initDB (DBReadReplica , pgConfig , globalPgReadReplicaConfig )
3645 }
3746}
3847
3948// Configure Configure database, PostgreSQL or SQLite connection
4049func Configure () {
41- InitDB ()
50+ InitUserDB ()
4251 loadAdditionalParamsFromDB ()
4352}
4453
@@ -104,19 +113,11 @@ func check(db *gorm.DB) {
104113}
105114
106115// 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- }
116+ func createPostgreSQLPrimaryConfig () * PostgreSQLConfig {
114117 config := PostgreSQLConfig {
115- User : utils .CoreCfg .DBUser ,
116- Host : host ,
117- Port : port ,
118+ Host : utils .CoreCfg .DBHost ,
119+ Port : utils .CoreCfg .DBPort ,
118120 Database : utils .CoreCfg .DBName ,
119- Passwd : utils .CoreCfg .DBPassword ,
120121 SSLMode : utils .CoreCfg .DBSslMode ,
121122 SSLRootCert : utils .CoreCfg .DBSslRootCert ,
122123 Debug : utils .CoreCfg .DBDebug ,
@@ -128,6 +129,29 @@ func loadEnvPostgreSQLConfig(useReadReplica bool) *PostgreSQLConfig {
128129 return & config
129130}
130131
132+ func createPostgreSQLReplicaConfig () * PostgreSQLConfig {
133+ config := createPostgreSQLPrimaryConfig ()
134+ config .User = utils .CoreCfg .DBUser
135+ config .Passwd = utils .CoreCfg .DBPassword
136+ config .Host = utils .CoreCfg .DBReadReplicaHost
137+ config .Port = utils .CoreCfg .DBReadReplicaPort
138+ return config
139+ }
140+
141+ func createPostgreSQLPrimaryUserConfig () * PostgreSQLConfig {
142+ config := createPostgreSQLPrimaryConfig ()
143+ config .User = utils .CoreCfg .DBUser
144+ config .Passwd = utils .CoreCfg .DBPassword
145+ return config
146+ }
147+
148+ func createPostgreSQLPrimaryAdminConfig () * PostgreSQLConfig {
149+ config := createPostgreSQLPrimaryConfig ()
150+ config .User = utils .CoreCfg .DBAdminUser
151+ config .Passwd = utils .CoreCfg .DBAdminPassword
152+ return config
153+ }
154+
131155// create "data source" config string needed for database connection opening
132156func dataSourceName (dbConfig * PostgreSQLConfig ) string {
133157 dbsource := fmt .Sprintf ("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s statement_timeout=%d" ,
0 commit comments