From bb8e0a9d84643e98a488f34f0d0086d463b8e30e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Karlsson?= Date: Tue, 28 Jul 2015 16:29:56 +0200 Subject: [PATCH 1/4] Fixed issue https://github.com/jinzhu/gorm/issues/151 where postgresql connection problems fail silently --- main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main.go b/main.go index aba51fc4..1490b43d 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,13 @@ func Open(dialect string, args ...interface{}) (DB, error) { driver = "postgres" // FoundationDB speaks a postgres-compatible protocol. } dbSql, err = sql.Open(driver, source) + + // For some reason, postgres does not throw an connection error. + // Sending a ping request after the connection is made is a quick workaround for this + if driver == "postgres" && err != nil { + err = db.DB().Ping() + } + case sqlCommon: source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String() dbSql = value From 68400171ac726ebc7d155397bf928112f713705b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Karlsson?= Date: Wed, 29 Jul 2015 17:58:33 +0200 Subject: [PATCH 2/4] typo --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 1490b43d..d4a201ac 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func Open(dialect string, args ...interface{}) (DB, error) { // For some reason, postgres does not throw an connection error. // Sending a ping request after the connection is made is a quick workaround for this - if driver == "postgres" && err != nil { + if driver == "postgres" && err == nil { err = db.DB().Ping() } From bdf410d4d1576d3b0e0b191704d61160e71f64e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Karlsson?= Date: Sun, 2 Aug 2015 01:18:22 +0200 Subject: [PATCH 3/4] Added generic ping check to probe for active db connection --- main.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d4a201ac..bdaf0d71 100644 --- a/main.go +++ b/main.go @@ -60,10 +60,8 @@ func Open(dialect string, args ...interface{}) (DB, error) { } dbSql, err = sql.Open(driver, source) - // For some reason, postgres does not throw an connection error. - // Sending a ping request after the connection is made is a quick workaround for this - if driver == "postgres" && err == nil { - err = db.DB().Ping() + if err == nil { + err = db.DB().Ping() // Send a ping to make sure the database connection is alive. } case sqlCommon: From 335f09749bcbb05e9e1935ab57dfd03cc222c3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=88r=20Karlsson?= Date: Sun, 2 Aug 2015 01:35:22 +0200 Subject: [PATCH 4/4] Fixed a bug when using sqlite --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 4c5cad11..fa68e52b 100644 --- a/main.go +++ b/main.go @@ -60,10 +60,6 @@ func Open(dialect string, args ...interface{}) (DB, error) { } dbSql, err = sql.Open(driver, source) - if err == nil { - err = db.DB().Ping() // Send a ping to make sure the database connection is alive. - } - case sqlCommon: source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String() dbSql = value @@ -80,6 +76,10 @@ func Open(dialect string, args ...interface{}) (DB, error) { db.parent = &db } + if err == nil { + err = db.DB().Ping() // Send a ping to make sure the database connection is alive. + } + return db, err }