check if the db conn is nil before doing anything (#1652)

This commit is contained in:
Asim Aslam 2020-05-20 14:03:38 +01:00 committed by GitHub
parent a29676b86a
commit 6a661fd08c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/lib/pq" "github.com/lib/pq"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/store"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -87,6 +88,10 @@ func (s *sqlStore) createDB(database, table string) error {
} }
func (s *sqlStore) initDB(database, table string) error { func (s *sqlStore) initDB(database, table string) error {
if s.db == nil {
return errors.New("Database connection not initialised")
}
// Create the namespace's database // Create the namespace's database
_, err := s.db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s;", database)) _, err := s.db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s;", database))
if err != nil { if err != nil {
@ -456,7 +461,11 @@ func NewStore(opts ...store.Option) store.Store {
// mark known databases // mark known databases
s.databases = make(map[string]bool) s.databases = make(map[string]bool)
// best-effort configure the store // best-effort configure the store
s.configure() if err := s.configure(); err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error("Error configuring store ", err)
}
}
// return store // return store
return s return s