ci: add TEST_DB_HOST env var for service container connectivity
Backend Tests / test (push) Failing after 58s

This commit is contained in:
2026-06-25 00:47:18 +01:00
parent ab06c7e596
commit f3a41585d2
2 changed files with 13 additions and 4 deletions
+12 -3
View File
@@ -29,6 +29,13 @@ const (
adminDatabaseDSN = "postgres://myuser:mypassword@localhost:5432/mydb?sslmode=disable"
)
func dbHost() string {
if h := os.Getenv("TEST_DB_HOST"); h != "" {
return h
}
return "localhost"
}
// CreateTestDatabase creates a fresh isolated test database, runs the schema migration,
// and returns a pool connected to it. If a database with the same name exists, it's
// dropped first. This enables parallel test execution across packages since each
@@ -36,7 +43,8 @@ const (
func CreateTestDatabase(dbName string) *pgxpool.Pool {
ctx := context.Background()
adminPool, err := pgxpool.New(ctx, adminDatabaseDSN)
adminDSN := fmt.Sprintf("postgres://myuser:mypassword@%s:5432/mydb?sslmode=disable", dbHost())
adminPool, err := pgxpool.New(ctx, adminDSN)
if err != nil {
log.Fatalf("testdb: failed to connect to admin database: %v", err)
}
@@ -68,7 +76,7 @@ func CreateTestDatabase(dbName string) *pgxpool.Pool {
}
adminPool.Close()
testDSN := fmt.Sprintf("postgres://myuser:mypassword@localhost:5432/%s?sslmode=disable", dbName)
testDSN := fmt.Sprintf("postgres://myuser:mypassword@%s:5432/%s?sslmode=disable", dbHost(), dbName)
poolCfg, err := pgxpool.ParseConfig(testDSN)
if err != nil {
log.Fatalf("testdb: failed to parse config: %v", err)
@@ -105,7 +113,8 @@ func DestroyTestDatabase(pool *pgxpool.Pool, dbName string) {
}
ctx := context.Background()
adminPool, err := pgxpool.New(ctx, adminDatabaseDSN)
adminDSN := fmt.Sprintf("postgres://myuser:mypassword@%s:5432/mydb?sslmode=disable", dbHost())
adminPool, err := pgxpool.New(ctx, adminDSN)
if err != nil {
log.Printf("testdb: warning: failed to connect to drop %s: %v", dbName, err)
return