status-go/postgres/helpers.go
Igor Sirotin e0eb737c51
fix_: enable tests to run with -count more than 1 (#5757)
* fix(TestProfilingCPU)_: enable run with -count=2

* fix(TestProfilingMem)_: enable run with -count=2

* fix(zaputil)_: register encoder only once

* fix(timesource)_: global variables override in tests

* fix(TestClosingsqlDB)_: delete database from cache

* fix(postgres/helpers)_: drop connections before dropping database

* fix_: linter

* chore_: remove redundant condition
2024-08-23 19:30:58 +00:00

65 lines
1.5 KiB
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
}
defer func() {
_ = db.Close()
}()
// Drop current and prevent any future connections. Used in tests. Details here:
// https://stackoverflow.com/questions/17449420/postgresql-unable-to-drop-database-because-of-some-auto-connections-to-db
_, err = db.Exec("REVOKE CONNECT ON DATABASE postgres FROM public;")
if err != nil {
return err
}
_, err = db.Exec("SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'postgres' AND pid <> pg_backend_pid();")
if err != nil {
return err
}
_, err = db.Exec("DROP DATABASE IF EXISTS postgres;")
if err != nil {
return err
}
_, err = db.Exec("CREATE DATABASE postgres;")
if err != nil {
return err
}
_, err = db.Exec("GRANT CONNECT ON DATABASE postgres TO public;")
if err != nil {
return err
}
return nil
}