diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 3d4e6cf4d6..df3b831549 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -61,7 +61,7 @@ type Server struct { DefaultOrgId string - DevDatabaseUrl string + DatabaseUrl string DevDatabaseCleanupFunc func() error Database *gorm.DB @@ -378,39 +378,35 @@ func (b *Server) RunShutdownFuncs() error { } func (b *Server) initDBInDocker(dialect string) error { - - return nil -} - -func (b *Server) initDB(dialect string) error { - - return nil -} - -// Add a string for discerning where the DB is created. Proposing: docker, basic. -func (b *Server) CreateDevDatabase(dialect string) error { - // Move the following to initDBInDocker c, url, container, err := db.InitDbInDocker(dialect) if err != nil { c() return fmt.Errorf("unable to start dev database with dialect %s: %w", dialect, err) } - - b.DevDatabaseCleanupFunc = c - b.DevDatabaseUrl = url - // end moved code - - // Add switch on DB creation type to choose initDB() for the basic type or - // initDBInDocker() for the docker type. - b.InfoKeys = append(b.InfoKeys, "dev database url") - b.Info["dev database url"] = b.DevDatabaseUrl + b.Info["dev database url"] = b.DatabaseUrl if container != "" { b.InfoKeys = append(b.InfoKeys, "dev database container") b.Info["dev database container"] = strings.TrimPrefix(container, "/") } - dbase, err := gorm.Open(dialect, url) + b.DevDatabaseCleanupFunc = c + b.DatabaseUrl = url + + return nil +} + +// Add a string for discerning where the DB is created. Proposing: docker, basic. +func (b *Server) CreateDevDatabase(dialect string) error { + // if the database URL isn't pre-set, assume no config exists for it and init start + // PG in docker + if b.DatabaseUrl == "" { + if err := b.initDBInDocker(dialect); err != nil { + return err + } + } + + dbase, err := gorm.Open(dialect, b.DatabaseUrl) if err != nil { c() return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err) diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index 01d9152935..be7c25fcfe 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -68,6 +68,7 @@ type Config struct { DevController bool `hcl:"-"` DefaultOrgId string `hcl:"default_org_id"` PassthroughDirectory string `hcl:"-"` + DatabaseURL string `hcl:"database_url"` } // DevWorker is a Config that is used for dev mode of Watchtower diff --git a/testing/controller/controller.go b/testing/controller/controller.go index d9a6442a98..f2d4449bb5 100644 --- a/testing/controller/controller.go +++ b/testing/controller/controller.go @@ -91,6 +91,7 @@ func WithDefaultOrgId(id string) Option { } // WithDatabaseURL sets the database URL if running externally +// Should be passed with DisableDatabaseCreation(). func WithDatabaseURL(url string) Option { return func(c *option) error { if c.setDatabaseURL { @@ -98,6 +99,7 @@ func WithDatabaseURL(url string) Option { } c.setDatabaseURL = true c.tcOptions.DatabaseURL = url + return nil } }