Browse Source

Log opening connection too

Volodymyr Tkach 2 years ago
parent
commit
6b3380b524
3 changed files with 22 additions and 10 deletions
  1. 16 4
      gosql/common/common.go
  2. 3 3
      gosql/common/common_test.go
  3. 3 3
      gosql/engine/engine.go

+ 16 - 4
gosql/common/common.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"net/url"
 	"net/url"
+	"os"
 	"regexp"
 	"regexp"
 	"strings"
 	"strings"
 	"time"
 	"time"
@@ -104,7 +105,7 @@ func ParseUrl(dbURL string) (*url.URL, error) {
 	return databaseURL, nil
 	return databaseURL, nil
 }
 }
 
 
-func OpenDB(databaseURL *url.URL, migrationsDir string) (*sql.DB, error) {
+func OpenDB(databaseURL *url.URL, migrationsDir string, debug bool) (*sql.DB, error) {
 	mate := dbmate.New(databaseURL)
 	mate := dbmate.New(databaseURL)
 
 
 	mate.AutoDumpSchema = false
 	mate.AutoDumpSchema = false
@@ -122,9 +123,20 @@ func OpenDB(databaseURL *url.URL, migrationsDir string) (*sql.DB, error) {
 		return nil, fmt.Errorf("DB migration error: %w", err)
 		return nil, fmt.Errorf("DB migration error: %w", err)
 	}
 	}
 
 
-	db, err := driver.Open()
-	if err != nil {
-		return nil, fmt.Errorf("DB open error: %w", err)
+	var db *sql.DB
+
+	if debug {
+		t := time.Now()
+		db, err = driver.Open()
+		log(os.Stdout, "[func Open]", t, err, false, "")
+		if err != nil {
+			return nil, fmt.Errorf("DB open error: %w", err)
+		}
+	} else {
+		db, err = driver.Open()
+		if err != nil {
+			return nil, fmt.Errorf("DB open error: %w", err)
+		}
 	}
 	}
 
 
 	return db, nil
 	return db, nil

+ 3 - 3
gosql/common/common_test.go

@@ -160,7 +160,7 @@ var _ = Describe("common", func() {
 			// 	databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
 			// 	databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
 			// 	Expect(err).To(Succeed())
 			// 	Expect(err).To(Succeed())
 
 
-			// 	db, err := common.OpenDB(databaseURL, migrationsDir)
+			// 	db, err := common.OpenDB(databaseURL, migrationsDir, false)
 			// 	Expect(err).To(Succeed())
 			// 	Expect(err).To(Succeed())
 			// 	Expect(db.Close()).To(Succeed())
 			// 	Expect(db.Close()).To(Succeed())
 			// })
 			// })
@@ -170,7 +170,7 @@ var _ = Describe("common", func() {
 			// 	databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
 			// 	databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
 			// 	Expect(err).To(Succeed())
 			// 	Expect(err).To(Succeed())
 
 
-			// 	db, err := common.OpenDB(databaseURL, migrationsDir)
+			// 	db, err := common.OpenDB(databaseURL, migrationsDir, false)
 			// 	Expect(err).To(Succeed())
 			// 	Expect(err).To(Succeed())
 			// 	Expect(db.Close()).To(Succeed())
 			// 	Expect(db.Close()).To(Succeed())
 			// })
 			// })
@@ -183,7 +183,7 @@ var _ = Describe("common", func() {
 				databaseURL, err := url.Parse("sqlite://" + f.Name())
 				databaseURL, err := url.Parse("sqlite://" + f.Name())
 				Expect(err).To(Succeed())
 				Expect(err).To(Succeed())
 
 
-				db, err := common.OpenDB(databaseURL, migrationsDir)
+				db, err := common.OpenDB(databaseURL, migrationsDir, false)
 				Expect(err).To(Succeed())
 				Expect(err).To(Succeed())
 				Expect(db.Close()).To(Succeed())
 				Expect(db.Close()).To(Succeed())
 			})
 			})

+ 3 - 3
gosql/engine/engine.go

@@ -13,7 +13,7 @@ type mysql struct {
 }
 }
 
 
 func NewMySQL(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
 func NewMySQL(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
-	db, err := common.OpenDB(dbURL, migrationsDir)
+	db, err := common.OpenDB(dbURL, migrationsDir, debug)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -34,7 +34,7 @@ type postgresql struct {
 }
 }
 
 
 func NewPostgreSQL(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
 func NewPostgreSQL(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
-	db, err := common.OpenDB(dbURL, migrationsDir)
+	db, err := common.OpenDB(dbURL, migrationsDir, debug)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -55,7 +55,7 @@ type sqlite struct {
 }
 }
 
 
 func NewSQLite(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
 func NewSQLite(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
-	db, err := common.OpenDB(dbURL, migrationsDir)
+	db, err := common.OpenDB(dbURL, migrationsDir, debug)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}