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:
Stefan Benten 2019-03-12 14:29:13 +01:00 committed by GitHub
parent 1f4e1a3ab0
commit 12c5f42c30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -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
}

View File

@ -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",