From 90331d171e2a0409df8826b65aaa5ec238d0195f Mon Sep 17 00:00:00 2001 From: Yee Hing Tong Date: Mon, 20 Mar 2023 12:59:19 -0700 Subject: [PATCH] add a copy of postgresDsn that's exported Signed-off-by: Yee Hing Tong --- database/postgres.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/database/postgres.go b/database/postgres.go index 1344517..1777e9d 100644 --- a/database/postgres.go +++ b/database/postgres.go @@ -49,6 +49,17 @@ func getPostgresDsn(ctx context.Context, pgConfig PostgresConfig) string { pgConfig.Host, pgConfig.Port, pgConfig.DbName, pgConfig.User, password, pgConfig.ExtraOptions) } +func PostgresDsn(ctx context.Context, pgConfig PostgresConfig) string { + password := resolvePassword(ctx, pgConfig.Password, pgConfig.PasswordPath) + if len(password) == 0 { + // The password-less case is included for development environments. + return fmt.Sprintf("host=%s port=%d dbname=%s user=%s sslmode=disable", + pgConfig.Host, pgConfig.Port, pgConfig.DbName, pgConfig.User) + } + return fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s %s", + pgConfig.Host, pgConfig.Port, pgConfig.DbName, pgConfig.User, password, pgConfig.ExtraOptions) +} + // CreatePostgresDbIfNotExists creates DB if it doesn't exist for the passed in config func CreatePostgresDbIfNotExists(ctx context.Context, gormConfig *gorm.Config, pgConfig PostgresConfig) (*gorm.DB, error) { dialector := postgres.Open(getPostgresDsn(ctx, pgConfig))