31 lines
585 B
Go
31 lines
585 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
func CreateDBConnection(connectionString string) *sql.DB {
|
|
sqlDB, err := sql.Open("postgres", connectionString)
|
|
if err != nil {
|
|
panic(fmt.Errorf("db: cannot open connection: %w", err))
|
|
}
|
|
|
|
return sqlDB
|
|
}
|
|
|
|
func CloseConnection(SQLDB *sql.DB) {
|
|
err := SQLDB.Close()
|
|
if err != nil {
|
|
log.Fatalf("Error Closing DB Connection: %s", err)
|
|
}
|
|
}
|
|
|
|
func AssertSuccessfulConnection(ctx context.Context, SQLDB *sql.DB) {
|
|
if err := SQLDB.PingContext(ctx); err != nil {
|
|
panic(fmt.Errorf("db: cannot connect: %w", err))
|
|
}
|
|
}
|