From 8abf9ac5edc4b49f83e550dff5b0a8add5a7d43d Mon Sep 17 00:00:00 2001 From: namhq1989 Date: Wed, 24 Aug 2022 15:46:56 +0700 Subject: [PATCH] change code for new version --- postgresql.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/postgresql.go b/postgresql.go index fc80e4f..f4ad6b4 100644 --- a/postgresql.go +++ b/postgresql.go @@ -23,21 +23,19 @@ type Config struct { ConnectionLifetime time.Duration } -var db *sql.DB - // Connect ... -func Connect(cfg Config, server string) { +func Connect(cfg Config, server string) *sql.DB { uri := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode) // connect - c, err := sql.Open("pgx", uri) + db, err := sql.Open("pgx", uri) if err != nil { panic(err) } // ping - if err = c.Ping(); err != nil { + if err = db.Ping(); err != nil { logger.Error("pgx ping", logger.LogData{ Source: "Connect", Message: err.Error(), @@ -46,9 +44,6 @@ func Connect(cfg Config, server string) { panic(err) } - // assign - db = c - // config if cfg.MaxOpenConnections == 0 { cfg.MaxOpenConnections = 25 @@ -70,9 +65,6 @@ func Connect(cfg Config, server string) { boil.DebugMode = cfg.IsDebug fmt.Printf("⚡️[postgres]: connected to %s:%d \n", cfg.Host, cfg.Port) -} -// GetDB ... -func GetDB() *sql.DB { return db }