diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 8f24868d73..13a4bda17e 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -462,5 +462,7 @@ func (b *Server) DestroyDevDatabase() error { if b.DevDatabaseCleanupFunc != nil { return b.DevDatabaseCleanupFunc() } - return errors.New("no dev database cleanup function found") + // allow the use of out-of-tree databases such as external dev + // database instances not forked by the underlying testing lib + return nil } diff --git a/internal/servers/controller/testing.go b/internal/servers/controller/testing.go index b73c0b1d83..7df478512a 100644 --- a/internal/servers/controller/testing.go +++ b/internal/servers/controller/testing.go @@ -114,6 +114,10 @@ type TestControllerOpts struct { // DisableDatabaseCreation can be set true to disable creating a dev // database DisableDatabaseCreation bool + + // DatabaseURL can be optionally set to point the dev server at an external + // database instance. This is overwritten if DisableDatabaseCreation is false. + DatabaseURL string } func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController { @@ -170,6 +174,10 @@ func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController { t.Fatal(err) } + if opts.DatabaseURL != "" { + tc.b.DevDatabaseUrl = opts.DatabaseURL + } + if !opts.DisableDatabaseCreation { if err := tc.b.CreateDevDatabase("postgres"); err != nil { t.Fatal(err) diff --git a/testing/controller/controller.go b/testing/controller/controller.go index 43f16b965b..d9a6442a98 100644 --- a/testing/controller/controller.go +++ b/testing/controller/controller.go @@ -29,6 +29,7 @@ type option struct { setWithConfigText bool setDisableDatabaseCreation bool setDefaultOrgId bool + setDatabaseURL bool } type Option func(*option) error @@ -89,6 +90,18 @@ func WithDefaultOrgId(id string) Option { } } +// WithDatabaseURL sets the database URL if running externally +func WithDatabaseURL(url string) Option { + return func(c *option) error { + if c.setDatabaseURL { + return fmt.Errorf("WithDatabaseURL provided more than once.") + } + c.setDatabaseURL = true + c.tcOptions.DatabaseURL = url + return nil + } +} + // NewTestController blocks until a new TestController is created, returns the url for the TestController and a function // that can be called to tear down the controller after it has been used for testing. func NewTestController(t *testing.T, opt ...Option) *TestController {