Skip to content

Commit

Permalink
Allowed users to get the config before init
Browse files Browse the repository at this point in the history
This can be useful if other extensions need to get the "final" URL for the db in a way that complies with Nibbler's behavior
  • Loading branch information
markdicksonjr committed Jul 5, 2019
1 parent 7ed009b commit 3da451b
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions database/sql/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ type Extension struct {

Models []interface{}
Db *gorm.DB
}

func NullifyField(db *gorm.DB, field string) *gorm.DB {
return db.Update(field, gorm.Expr("NULL"))
configuration *Configuration
}

type Configuration struct {
Expand All @@ -36,42 +34,38 @@ type Configuration struct {
}

func (s *Extension) Init(app *nibbler.Application) error {
configuration, err := s.getBestConfiguration(app)
var err error
s.configuration, err = s.GetBestConfiguration(app)

if err != nil {
return err
}

if configuration.Scheme == "postgres" {
// our postgres driver initializes in way that we can't just plop in the URL
if s.configuration.Scheme == "postgres" {

// ensure port is numerical
_, err = strconv.Atoi(configuration.Port)

if err != nil {
if _, err = strconv.Atoi(s.configuration.Port); err != nil {
return err
}

// establish the sslmode from the configuration, defaulting to disable
sslMode := configuration.Query.Get("sslmode")
sslMode := s.configuration.Query.Get("sslmode")
if len(sslMode) == 0 {
sslMode = "disable"
}

s.Db, err = gorm.Open(configuration.Scheme,
s.Db, err = gorm.Open(s.configuration.Scheme,
fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s",
configuration.Host,
configuration.Port,
configuration.Username,
configuration.Path,
*configuration.Password,
s.configuration.Host,
s.configuration.Port,
s.configuration.Username,
s.configuration.Path,
*s.configuration.Password,
sslMode,
))
} else if configuration.Scheme == "sqlite3" {
path := ":memory:"
if len(configuration.Path) > 0 {
path = configuration.Path
}
s.Db, err = gorm.Open(configuration.Scheme, path)
} else if s.configuration.Scheme == "sqlite3" {
s.Db, err = gorm.Open(s.configuration.Scheme, s.configuration.Path)
} else {
return errors.New("unknown dialect")
}
Expand All @@ -97,6 +91,10 @@ func (s *Extension) AddRoutes(app *nibbler.Application) error {
return nil
}

func (s *Extension) GetConfiguration() *Configuration {
return s.configuration
}

func (s *Extension) Destroy(app *nibbler.Application) error {
if s.Db != nil {
err := s.Db.Close()
Expand All @@ -110,6 +108,10 @@ func IsRecordNotFoundError(err error) bool {
return gorm.IsRecordNotFoundError(err)
}

func NullifyField(db *gorm.DB, field string) *gorm.DB {
return db.Update(field, gorm.Expr("NULL"))
}

func (s *Extension) getBestDialect(app *nibbler.Application) (*string, error) {
var urlParsed *url.URL = nil
var parseError error = nil
Expand Down Expand Up @@ -138,7 +140,7 @@ func (s *Extension) getBestDialect(app *nibbler.Application) (*string, error) {
}

// TODO: allow attribute Url on Extension to take precedence over all of this
func (s *Extension) getBestConfiguration(app *nibbler.Application) (*Configuration, error) {
func (s *Extension) GetBestConfiguration(app *nibbler.Application) (*Configuration, error) {
var urlParsed *url.URL = nil
var scheme *string

Expand Down Expand Up @@ -242,6 +244,11 @@ func (s *Extension) getBestConfiguration(app *nibbler.Application) (*Configurati
urlParsed.Query(),
}

// for sqlite, default path to ":memory:"
if configuration.Scheme == "sqlite3" && len(configuration.Path) == 0 {
configuration.Path = ":memory:"
}

if !isSet {
configuration.Password = nil
}
Expand Down

0 comments on commit 3da451b

Please sign in to comment.