From 71538adfdcb00cb8e240c3b57471c55e74756e82 Mon Sep 17 00:00:00 2001 From: Jake Sanders Date: Tue, 7 Apr 2020 13:00:05 +0100 Subject: [PATCH] Explicitly set the table name during service init (#1497) --- service.go | 9 +++------ store/cockroach/cockroach.go | 27 +++++++++++++-------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/service.go b/service.go index 7e16ed11..5b34f2ae 100644 --- a/service.go +++ b/service.go @@ -106,12 +106,9 @@ func (s *service) Init(opts ...Option) { logger.Fatal(err) } - // If the store has no Table set, fallback to the - // services name - if len(store.DefaultStore.Options().Table) == 0 { - name := s.opts.Cmd.App().Name - store.DefaultStore.Init(store.Table(name)) - } + // Explicitly set the table name to the service name + name := s.opts.Cmd.App().Name + store.DefaultStore.Init(store.Table(name)) // TODO: replace Cmd.Init with config.Load // Right now we're just going to load a token diff --git a/store/cockroach/cockroach.go b/store/cockroach/cockroach.go index e34324f8..5f242810 100644 --- a/store/cockroach/cockroach.go +++ b/store/cockroach/cockroach.go @@ -14,11 +14,10 @@ import ( "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. var ( - DefaultNamespace = "micro" - DefaultPrefix = "micro" + DefaultDatabase = "micro" ) type sqlStore struct { @@ -296,26 +295,26 @@ func (s *sqlStore) initDB() error { func (s *sqlStore) configure() error { if len(s.options.Nodes) == 0 { - s.options.Nodes = []string{"localhost:26257"} + s.options.Nodes = []string{"postgresql://root@localhost:26257"} } - namespace := s.options.Database - if len(namespace) == 0 { - namespace = DefaultNamespace + database := s.options.Database + if len(database) == 0 { + database = DefaultDatabase } - prefix := s.options.Table - if len(prefix) == 0 { - prefix = DefaultPrefix + if len(s.options.Table) == 0 { + return errors.New("no table set") } + table := s.options.Table // store.namespace must only contain letters, numbers and underscores reg, err := regexp.Compile("[^a-zA-Z0-9]+") if err != nil { return errors.New("error compiling regex for namespace") } - namespace = reg.ReplaceAllString(namespace, "_") - prefix = reg.ReplaceAllString(prefix, "_") + database = reg.ReplaceAllString(database, "_") + table = reg.ReplaceAllString(table, "_") 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 @@ -343,8 +342,8 @@ func (s *sqlStore) configure() error { // save the values s.db = db - s.database = namespace - s.table = prefix + s.database = database + s.table = table // initialise the database return s.initDB()