Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f3d84f8

Browse files
committed
common: Create migrate db connection string from scratch
This fixes a bug in the recent commit to introduce golang-migrate, such that it wasn't able to connect to our production backend.
1 parent d474b97 commit f3d84f8

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

common/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func ReadConfig() error {
172172
}
173173
}
174174

175-
176175
// Set the PostgreSQL configuration values
177176
pgConfig, err = pgxpool.ParseConfig(fmt.Sprintf("host=%s port=%d user= %s password = %s dbname=%s pool_max_conns=%d connect_timeout=10", Conf.Pg.Server, uint16(Conf.Pg.Port), Conf.Pg.Username, Conf.Pg.Password, Conf.Pg.Database, Conf.Pg.NumConnections))
178177
clientTLSConfig := tls.Config{}

common/postgresql.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,19 @@ func ConnectPostgreSQL() (err error) {
490490
return fmt.Errorf("Couldn't connect to PostgreSQL server: %v", err)
491491
}
492492

493-
// Apply any outstanding database migrations
494-
m, err := migrate.New(fmt.Sprintf("file://%s/database/migrations", Conf.Web.BaseDir), "pgx5://dbhub@localhost:5432/dbhub")
493+
// migrate doesn't handle pgx connection strings, so we need to manually create something it can use
494+
var mConnStr string
495+
if Conf.Environment.Environment == "production" {
496+
mConnStr = fmt.Sprintf("pgx5://%s@%s:%d/%s?password=%s&connect_timeout=10", Conf.Pg.Username, Conf.Pg.Server,
497+
uint16(Conf.Pg.Port), Conf.Pg.Database, url.PathEscape(Conf.Pg.Password))
498+
} else {
499+
// Non-production, so probably our Docker test container
500+
mConnStr = "pgx5://dbhub@localhost:5432/dbhub"
501+
}
502+
if Conf.Pg.SSL {
503+
mConnStr += "&sslmode=require"
504+
}
505+
m, err := migrate.New(fmt.Sprintf("file://%s/database/migrations", Conf.Web.BaseDir), mConnStr)
495506
if err != nil {
496507
return
497508
}

0 commit comments

Comments
 (0)