From 780358979dfa8e3bdfdf088018490c13433a36cd Mon Sep 17 00:00:00 2001 From: Jacob LeGrone Date: Thu, 16 Sep 2021 20:29:55 -0400 Subject: [PATCH] add CLI flag to pre-create namespaces --- README.md | 9 +++++++++ cmd/temporalite/main.go | 9 +++++++++ .../common/persistence/sql/sqlplugin/sqlite/plugin.go | 10 ++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2b12de58..eb3cac05 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,15 @@ Use the help flag to see all available options: temporalite start -h ``` +### Namespace Registration + +Namespaces can be pre-registered at startup so they're available to use right away: +```bash +temporalite start --namespace foo --namespace bar +``` + +Registering namespaces the old-fashioned way via `tctl --namespace foo namespace register` works too! + ### Persistence Modes #### File on Disk diff --git a/cmd/temporalite/main.go b/cmd/temporalite/main.go index 1c41c9cb..8dae3f02 100644 --- a/cmd/temporalite/main.go +++ b/cmd/temporalite/main.go @@ -29,6 +29,7 @@ const ( dbPathFlag = "filename" portFlag = "port" logFormatFlag = "log-format" + namespaceFlag = "namespace" ) func init() { @@ -76,6 +77,13 @@ func buildCLI() *cli.App { EnvVars: nil, Value: "json", }, + &cli.StringSliceFlag{ + Name: namespaceFlag, + Aliases: []string{"n"}, + Usage: `specify namespaces that should be pre-created`, + EnvVars: nil, + Value: nil, + }, }, Before: func(c *cli.Context) error { if c.Args().Len() > 0 { @@ -95,6 +103,7 @@ func buildCLI() *cli.App { opts := []temporalite.ServerOption{ temporalite.WithFrontendPort(c.Int(portFlag)), temporalite.WithDatabaseFilePath(c.String(dbPathFlag)), + temporalite.WithNamespaces(c.StringSlice(namespaceFlag)...), } if c.Bool(ephemeralFlag) { opts = append(opts, temporalite.WithPersistenceDisabled()) diff --git a/internal/common/persistence/sql/sqlplugin/sqlite/plugin.go b/internal/common/persistence/sql/sqlplugin/sqlite/plugin.go index c237ba44..fd7e81a3 100644 --- a/internal/common/persistence/sql/sqlplugin/sqlite/plugin.go +++ b/internal/common/persistence/sql/sqlplugin/sqlite/plugin.go @@ -88,10 +88,12 @@ func (p *plugin) CreateDB( p.mainDB = newDB(dbKind, cfg.DatabaseName, conn, nil) // Ensure namespaces exist - namespaces := strings.Split(cfg.ConnectAttributes["preCreateNamespaces"], ",") - for _, ns := range namespaces { - if err := createNamespaceIfNotExists(p.mainDB, ns); err != nil { - return nil, fmt.Errorf("error ensuring namespace exists: %w", err) + if nsConfig := cfg.ConnectAttributes["preCreateNamespaces"]; nsConfig != "" { + namespaces := strings.Split(cfg.ConnectAttributes["preCreateNamespaces"], ",") + for _, ns := range namespaces { + if err := createNamespaceIfNotExists(p.mainDB, ns); err != nil { + return nil, fmt.Errorf("error ensuring namespace exists: %w", err) + } } } }