Add Application Name to PostgreSQL Connection String (#1451)
* Add Application Name to PostgreSQL Connection String, if not existing already * Relocate Check * Handle Application Name only for Postgres * URL Encode * Relocate Function into pgutil * Fix Error, when ApplicationName is set * Rename parameter * Rename Check Value * Straightline Comment * Remove fmt Dependency * Fix Linting Recommendation
This commit is contained in:
parent
1f4e1a3ab0
commit
12c5f42c30
@ -5,6 +5,7 @@ package pgutil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
@ -98,3 +99,17 @@ func QuerySnapshot(db dbschema.Queryer) (*dbschema.Snapshot, error) {
|
||||
Data: data,
|
||||
}, err
|
||||
}
|
||||
|
||||
//CheckApplicationName ensures that the Connection String contains an application name
|
||||
func CheckApplicationName(s string) (r string) {
|
||||
if !strings.Contains(s, "application_name") {
|
||||
if !strings.Contains(s, "?") {
|
||||
r = s + "?application_name=Satellite"
|
||||
return
|
||||
}
|
||||
r = s + "&application_name=Satellite"
|
||||
return
|
||||
}
|
||||
//return source as is if application_name is set
|
||||
return s
|
||||
}
|
||||
|
@ -41,7 +41,9 @@ func New(log *zap.Logger, databaseURL string) (satellite.DB, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if driver == "postgres" {
|
||||
source = pgutil.CheckApplicationName(source)
|
||||
}
|
||||
db, err := dbx.Open(driver, source)
|
||||
if err != nil {
|
||||
return nil, Error.New("failed opening database %q, %q: %v",
|
||||
|
Loading…
Reference in New Issue
Block a user