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

This commit is contained in:
Igor Sirotin 2024-08-21 14:03:49 +01:00
parent 690a62debe
commit 33d3885960
No known key found for this signature in database
GPG Key ID: 425E227CAAB81F95
1 changed files with 25 additions and 1 deletions

View File

@ -29,6 +29,21 @@ func ResetDefaultTestPostgresDB() error {
if err != nil { if err != nil {
return err 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;") _, err = db.Exec("DROP DATABASE IF EXISTS postgres;")
if err != nil { if err != nil {
@ -36,5 +51,14 @@ func ResetDefaultTestPostgresDB() error {
} }
_, err = db.Exec("CREATE DATABASE postgres;") _, err = db.Exec("CREATE DATABASE postgres;")
return err if err != nil {
return err
}
_, err = db.Exec("GRANT CONNECT ON DATABASE postgres TO public;")
if err != nil {
return err
}
return nil
} }