update with databaseURL option

dev-test-db
Jeff Malnick 6 years ago
parent 3036114882
commit 9e0aac89b2

@ -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)

@ -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

@ -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
}
}

Loading…
Cancel
Save