Skip to content

Commit 5ec646a

Browse files
committed
RHINENG-22333: allow app to connect as admin
1 parent d8f273c commit 5ec646a

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

base/database/setup.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var (
1919
globalPgConfig *PostgreSQLConfig
2020
)
2121

22-
func InitDB() {
23-
pgConfig := loadEnvPostgreSQLConfig(false)
22+
func InitDB(useAdmin bool) {
23+
pgConfig := loadEnvPostgreSQLConfig(useAdmin, false)
2424
if DB != nil && pgConfig == globalPgConfig {
2525
// reuse connection
2626
check(DB)
@@ -30,18 +30,22 @@ func InitDB() {
3030
DB = openPostgreSQL(pgConfig)
3131
check(DB)
3232
if utils.CoreCfg.DBReadReplicaEnabled {
33-
pgConfig := loadEnvPostgreSQLConfig(ReadReplicaConfigured())
33+
pgConfig := loadEnvPostgreSQLConfig(useAdmin, ReadReplicaConfigured())
3434
DBReadReplica = openPostgreSQL(pgConfig)
3535
check(DBReadReplica)
3636
}
3737
}
3838

3939
// Configure Configure database, PostgreSQL or SQLite connection
40-
func Configure() {
41-
InitDB()
40+
func ConfigureAdmin(useAdmin bool) {
41+
InitDB(useAdmin)
4242
loadAdditionalParamsFromDB()
4343
}
4444

45+
func Configure() {
46+
ConfigureAdmin(false)
47+
}
48+
4549
// PostgreSQLConfig PostgreSQL database config
4650
type PostgreSQLConfig struct {
4751
Host string
@@ -104,19 +108,25 @@ func check(db *gorm.DB) {
104108
}
105109

106110
// load database config from environment vars using inserted prefix
107-
func loadEnvPostgreSQLConfig(useReadReplica bool) *PostgreSQLConfig {
111+
func loadEnvPostgreSQLConfig(useAdmin bool, useReadReplica bool) *PostgreSQLConfig {
112+
user := utils.CoreCfg.DBUser
113+
passwd := utils.CoreCfg.DBPassword
114+
if useAdmin {
115+
user = utils.CoreCfg.DBAdminUser
116+
passwd = utils.CoreCfg.DBAdminPassword
117+
}
108118
host := utils.CoreCfg.DBHost
109119
port := utils.CoreCfg.DBPort
110120
if useReadReplica {
111121
host = utils.CoreCfg.DBReadReplicaHost
112122
port = utils.CoreCfg.DBReadReplicaPort
113123
}
114124
config := PostgreSQLConfig{
115-
User: utils.CoreCfg.DBUser,
125+
User: user,
116126
Host: host,
117127
Port: port,
118128
Database: utils.CoreCfg.DBName,
119-
Passwd: utils.CoreCfg.DBPassword,
129+
Passwd: passwd,
120130
SSLMode: utils.CoreCfg.DBSslMode,
121131
SSLRootCert: utils.CoreCfg.DBSslRootCert,
122132
Debug: utils.CoreCfg.DBDebug,

0 commit comments

Comments
 (0)