Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema): add database schema config #1129

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

type PsqlConnInfo struct {
Expand All @@ -17,8 +18,11 @@ type PsqlConnInfo struct {
DbName string
SslMode string
Password string
Schema string
}

var db *gorm.DB

serrovsky marked this conversation as resolved.
Show resolved Hide resolved
func getPsqlConnInfo() (PsqlConnInfo, error) {
var host string
if value := viper.GetString("PGSQL_HOST"); value != "" {
Expand All @@ -38,7 +42,7 @@ func getPsqlConnInfo() (PsqlConnInfo, error) {
if value := viper.GetString("PGSQL_DBNAME"); value != "" {
dbName = value
} else {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_USER env var is required", envVarsPrefix)
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_DBNAME env var is required", envVarsPrefix)
}

var sslMode string
Expand All @@ -55,12 +59,21 @@ func getPsqlConnInfo() (PsqlConnInfo, error) {
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 = ""
fmt.Printf("No schema set.")
serrovsky marked this conversation as resolved.
Show resolved Hide resolved
}

return PsqlConnInfo{
Host: host,
User: user,
DbName: dbName,
SslMode: sslMode,
Password: password,
Schema: schema,
}, nil
}

Expand Down Expand Up @@ -97,7 +110,16 @@ func savePgsql(jsonInfo string) {
exitWithError(err)
}

db, err := gorm.Open(postgres.Open(PsqlConnInfo.toString()), &gorm.Config{})
if PsqlConnInfo.Schema == "" {
serrovsky marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if PsqlConnInfo.Schema == "" {
if PsqlConnInfo.Schema == "" {
glog.V(2).Info("No schema set.")

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 {
exitWithError(fmt.Errorf("received error connecting to database: %s", err))
}
Expand Down