2024-12-18 18:54:48 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2024-12-24 17:13:48 +00:00
|
|
|
"queryorchestration/internal/env"
|
2024-12-18 18:54:48 +00:00
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-08 18:14:15 +00:00
|
|
|
type config struct {
|
|
|
|
|
user string
|
|
|
|
|
password string
|
|
|
|
|
host string
|
|
|
|
|
port string
|
|
|
|
|
name string
|
2025-01-10 19:17:20 +00:00
|
|
|
disableSSL bool
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustGetConfig() *config {
|
|
|
|
|
user := env.GetPanic("DB_USER")
|
|
|
|
|
pass := env.GetPanic("DB_PASS")
|
|
|
|
|
host := env.GetPanic("DB_HOST")
|
|
|
|
|
port := env.GetPanic("DB_PORT")
|
|
|
|
|
name := env.GetPanic("DB_NAME")
|
|
|
|
|
|
|
|
|
|
disableSSL, _ := env.Get("DB_NOSSL")
|
|
|
|
|
|
|
|
|
|
return &config{
|
|
|
|
|
user: user,
|
|
|
|
|
password: pass,
|
|
|
|
|
host: host,
|
|
|
|
|
port: port,
|
|
|
|
|
name: name,
|
2025-01-10 19:17:20 +00:00
|
|
|
disableSSL: disableSSL == "1",
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustGetConnectionString() string {
|
2024-12-18 18:54:48 +00:00
|
|
|
driver := "postgres"
|
2025-01-08 18:14:15 +00:00
|
|
|
conf := mustGetConfig()
|
|
|
|
|
opts := ""
|
|
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
if conf.disableSSL {
|
2025-01-08 18:14:15 +00:00
|
|
|
opts += "sslmode=disable"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s://%s:%s@%s:%s/%s?%s", driver, conf.user, conf.password, conf.host, conf.port, conf.name, opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPoolConfig() (*pgxpool.Config, error) {
|
|
|
|
|
connStr := mustGetConnectionString()
|
|
|
|
|
|
|
|
|
|
config, err := pgxpool.ParseConfig(connStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustGetPoolConfig() *pgxpool.Config {
|
|
|
|
|
config, err := getPoolConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicf("Unable to create database config: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return config
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetDBPool(ctx context.Context) *pgxpool.Pool {
|
2025-01-08 18:14:15 +00:00
|
|
|
config := mustGetPoolConfig()
|
2024-12-18 18:54:48 +00:00
|
|
|
|
2025-01-08 18:14:15 +00:00
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, config)
|
2024-12-18 18:54:48 +00:00
|
|
|
if err != nil {
|
2024-12-24 18:11:25 +00:00
|
|
|
log.Panicf("Unable to create database pool: %v\n", err)
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetDBConn(ctx context.Context) *pgx.Conn {
|
2025-01-08 18:14:15 +00:00
|
|
|
config := mustGetConnectionString()
|
2024-12-18 18:54:48 +00:00
|
|
|
|
2025-01-08 18:14:15 +00:00
|
|
|
conn, err := pgx.Connect(ctx, config)
|
2024-12-18 18:54:48 +00:00
|
|
|
if err != nil {
|
2024-12-24 18:11:25 +00:00
|
|
|
log.Panicf("Unable to connect to database: %v\n", err)
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conn
|
|
|
|
|
}
|