Luis Serra 2 months ago committed by GitHub
commit 0dffde6199
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -9,6 +9,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/schema"
) )
type PsqlConnInfo struct { type PsqlConnInfo struct {
@ -17,6 +18,7 @@ type PsqlConnInfo struct {
DbName string DbName string
SslMode string SslMode string
Password string Password string
Schema string
} }
func getPsqlConnInfo() (PsqlConnInfo, error) { func getPsqlConnInfo() (PsqlConnInfo, error) {
@ -55,12 +57,21 @@ func getPsqlConnInfo() (PsqlConnInfo, error) {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_PASSWORD env var is required", envVarsPrefix) return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_PASSWORD env var is required", envVarsPrefix)
} }
var schema string
if value := viper.GetString("PGSQL_SCHEMA"); value != "" {
schema = value
} else {
schema = ""
glog.V(2).Info("No schema set.")
}
return PsqlConnInfo{ return PsqlConnInfo{
Host: host, Host: host,
User: user, User: user,
DbName: dbName, DbName: dbName,
SslMode: sslMode, SslMode: sslMode,
Password: password, Password: password,
Schema: schema,
}, nil }, nil
} }
@ -97,7 +108,18 @@ func savePgsql(jsonInfo string) {
exitWithError(err) exitWithError(err)
} }
db, err := gorm.Open(postgres.Open(PsqlConnInfo.toString()), &gorm.Config{}) var db *gorm.DB
if PsqlConnInfo.Schema == "" {
db, err = gorm.Open(postgres.Open(PsqlConnInfo.toString()), &gorm.Config{})
} else {
db, err = gorm.Open(postgres.Open(PsqlConnInfo.toString()), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: fmt.Sprintf("%s.", PsqlConnInfo.Schema), // schema name
SingularTable: false,
}})
}
if err != nil { if err != nil {
exitWithError(fmt.Errorf("received error connecting to database: %s", err)) exitWithError(fmt.Errorf("received error connecting to database: %s", err))
} }

Loading…
Cancel
Save