Explicitly set the table name during service init (#1497)
This commit is contained in:
parent
2ea5b33955
commit
71538adfdc
@ -106,12 +106,9 @@ func (s *service) Init(opts ...Option) {
|
|||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the store has no Table set, fallback to the
|
// Explicitly set the table name to the service name
|
||||||
// services name
|
|
||||||
if len(store.DefaultStore.Options().Table) == 0 {
|
|
||||||
name := s.opts.Cmd.App().Name
|
name := s.opts.Cmd.App().Name
|
||||||
store.DefaultStore.Init(store.Table(name))
|
store.DefaultStore.Init(store.Table(name))
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: replace Cmd.Init with config.Load
|
// TODO: replace Cmd.Init with config.Load
|
||||||
// Right now we're just going to load a token
|
// Right now we're just going to load a token
|
||||||
|
@ -14,11 +14,10 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultNamespace is the namespace that the sql store
|
// DefaultDatabase is the namespace that the sql store
|
||||||
// will use if no namespace is provided.
|
// will use if no namespace is provided.
|
||||||
var (
|
var (
|
||||||
DefaultNamespace = "micro"
|
DefaultDatabase = "micro"
|
||||||
DefaultPrefix = "micro"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type sqlStore struct {
|
type sqlStore struct {
|
||||||
@ -296,26 +295,26 @@ func (s *sqlStore) initDB() error {
|
|||||||
|
|
||||||
func (s *sqlStore) configure() error {
|
func (s *sqlStore) configure() error {
|
||||||
if len(s.options.Nodes) == 0 {
|
if len(s.options.Nodes) == 0 {
|
||||||
s.options.Nodes = []string{"localhost:26257"}
|
s.options.Nodes = []string{"postgresql://root@localhost:26257"}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := s.options.Database
|
database := s.options.Database
|
||||||
if len(namespace) == 0 {
|
if len(database) == 0 {
|
||||||
namespace = DefaultNamespace
|
database = DefaultDatabase
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := s.options.Table
|
if len(s.options.Table) == 0 {
|
||||||
if len(prefix) == 0 {
|
return errors.New("no table set")
|
||||||
prefix = DefaultPrefix
|
|
||||||
}
|
}
|
||||||
|
table := s.options.Table
|
||||||
|
|
||||||
// store.namespace must only contain letters, numbers and underscores
|
// store.namespace must only contain letters, numbers and underscores
|
||||||
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("error compiling regex for namespace")
|
return errors.New("error compiling regex for namespace")
|
||||||
}
|
}
|
||||||
namespace = reg.ReplaceAllString(namespace, "_")
|
database = reg.ReplaceAllString(database, "_")
|
||||||
prefix = reg.ReplaceAllString(prefix, "_")
|
table = reg.ReplaceAllString(table, "_")
|
||||||
|
|
||||||
source := s.options.Nodes[0]
|
source := s.options.Nodes[0]
|
||||||
// check if it is a standard connection string eg: host=%s port=%d user=%s password=%s dbname=%s sslmode=disable
|
// check if it is a standard connection string eg: host=%s port=%d user=%s password=%s dbname=%s sslmode=disable
|
||||||
@ -343,8 +342,8 @@ func (s *sqlStore) configure() error {
|
|||||||
|
|
||||||
// save the values
|
// save the values
|
||||||
s.db = db
|
s.db = db
|
||||||
s.database = namespace
|
s.database = database
|
||||||
s.table = prefix
|
s.table = table
|
||||||
|
|
||||||
// initialise the database
|
// initialise the database
|
||||||
return s.initDB()
|
return s.initDB()
|
||||||
|
Loading…
Reference in New Issue
Block a user