status-go/postgres/helpers.go
Jakub Sokołowski 494c6707ba
ci: parametrize DB port for unit tests
Otherwise we can't run tests in parallel on the same host. Also the
container name has to be different depending on executor.

Resolves errors like:
```
docker: Error response from daemon: Conflict.
    The container name "/status-go-test-db" is already in use by container "123...".
    You have to remove (or rename) that container to be able to reuse that name.
```
Resolves: https://github.com/status-im/status-go/issues/4040

Signed-off-by: Jakub Sokołowski <jakub@status.im>
2023-09-18 19:54:21 +02:00

40 lines
913 B
Go

package postgres
import (
"database/sql"
"fmt"
"os"
// Import postgres driver
_ "github.com/lib/pq"
)
var (
DefaultTestDBHost = GetEnvDefault("TEST_POSTGRES_HOST", "localhost")
DefaultTestDBPort = GetEnvDefault("TEST_POSTGRES_PORT", "5432")
DefaultTestURI = fmt.Sprintf("postgres://postgres@%s:%s/postgres?sslmode=disable", DefaultTestDBHost, DefaultTestDBPort)
DropTableURI = fmt.Sprintf("postgres://postgres@%s:%s/template1?sslmode=disable", DefaultTestDBHost, DefaultTestDBPort)
)
func GetEnvDefault(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
func ResetDefaultTestPostgresDB() error {
db, err := sql.Open("postgres", DropTableURI)
if err != nil {
return err
}
_, err = db.Exec("DROP DATABASE IF EXISTS postgres;")
if err != nil {
return err
}
_, err = db.Exec("CREATE DATABASE postgres;")
return err
}