Skip to content

Commit

Permalink
Fix SQLite URL parsing on Windows (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m authored Jan 22, 2019
1 parent adadabd commit 60b907a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
26 changes: 16 additions & 10 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ var dialectX = regexp.MustCompile(`\S+://`)
// URL parser if exists.
func (cd *ConnectionDetails) withURL() error {
ul := cd.URL
if cd.Dialect != "" && !dialectX.MatchString(ul) {
if cd.Dialect == "" {
if dialectX.MatchString(ul) {
// Guess the dialect from the scheme
dialect := ul[:strings.Index(ul, ":")]
cd.Dialect = normalizeSynonyms(dialect)
} else {
return errors.New("no dialect provided, and could not guess it from URL")
}
} else if !dialectX.MatchString(ul) {
ul = cd.Dialect + "://" + ul
}
u, err := url.Parse(ul)
if err != nil {
return errors.Wrapf(err, "couldn't parse %s", ul)
}

//! dialect should not be overrided here (especially for cockroach)
if cd.Dialect == "" {
cd.Dialect = normalizeSynonyms(u.Scheme)
}
if !DialectSupported(cd.Dialect) {
return errors.Errorf("unsupported dialect '%v'", cd.Dialect)
return errors.Errorf("unsupported dialect '%s'", cd.Dialect)
}

// warning message is required to prevent confusion
Expand All @@ -77,6 +77,11 @@ func (cd *ConnectionDetails) withURL() error {
return up(cd)
}

// Fallback on generic parsing if no URL parser was found for the dialect.
u, err := url.Parse(ul)
if err != nil {
return errors.Wrapf(err, "couldn't parse %s", ul)
}
cd.Database = strings.TrimPrefix(u.Path, "/")

hp := strings.Split(u.Host, ":")
Expand All @@ -103,6 +108,7 @@ func (cd *ConnectionDetails) Finalize() error {
cd.Options = make(map[string]string)
}

// Process the database connection string, if provided.
if cd.URL != "" {
if err := cd.withURL(); err != nil {
return err
Expand Down
9 changes: 2 additions & 7 deletions dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package pop
import (
"fmt"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -227,11 +226,7 @@ func newSQLite(deets *ConnectionDetails) (dialect, error) {
}

func urlParserSQLite3(cd *ConnectionDetails) error {
u, err := url.Parse(cd.URL)
if err != nil {
return errors.Wrapf(err, "could not parse url '%v'", cd.URL)
}
cd.Database = u.Path

db := strings.TrimPrefix(cd.URL, "sqlite://")
cd.Database = strings.TrimPrefix(db, "sqlite3://")
return nil
}

0 comments on commit 60b907a

Please sign in to comment.