Fall back to postgres when database connection string parsing fails (#842)

* Fall back to postgres when parsing the database connection string for a URI schema fails

* Fix behaviour so that it really tries postgres when URL parsing fails and it complains about unknown schema if it succeeds
This commit is contained in:
Neil Alexander 2020-01-09 17:03:36 +00:00 committed by GitHub
parent f7faf74528
commit 714959126b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 7 deletions

View file

@ -33,7 +33,9 @@ type Database interface {
func NewDatabase(dataSourceName string) (Database, error) { func NewDatabase(dataSourceName string) (Database, error) {
uri, err := url.Parse(dataSourceName) uri, err := url.Parse(dataSourceName)
if err != nil { if err != nil {
return nil, err // if the scheme doesn't match, fall back to postgres in case the config has
// postgres key=value connection strings
return postgres.NewDatabase(dataSourceName)
} }
switch uri.Scheme { switch uri.Scheme {
case "postgres": case "postgres":

View file

@ -34,12 +34,14 @@ type Database interface {
func NewDatabase(dataSourceName string) (Database, error) { func NewDatabase(dataSourceName string) (Database, error) {
uri, err := url.Parse(dataSourceName) uri, err := url.Parse(dataSourceName)
if err != nil { if err != nil {
return nil, err // if the scheme doesn't match, fall back to postgres in case the config has
// postgres key=value connection strings
return postgres.NewDatabase(dataSourceName)
} }
switch uri.Scheme { switch uri.Scheme {
case "postgres": case "postgres":
return postgres.NewDatabase(dataSourceName) return postgres.NewDatabase(dataSourceName)
default: default:
return nil, errors.New("unknown schema") return errors.New("unknown schema")
} }
} }

View file

@ -36,7 +36,9 @@ type Database interface {
func Open(dataSourceName string) (Database, error) { func Open(dataSourceName string) (Database, error) {
uri, err := url.Parse(dataSourceName) uri, err := url.Parse(dataSourceName)
if err != nil { if err != nil {
return nil, err // if the scheme doesn't match, fall back to postgres in case the config has
// postgres key=value connection strings
return postgres.Open(dataSourceName)
} }
switch uri.Scheme { switch uri.Scheme {
case "postgres": case "postgres":

View file

@ -39,7 +39,9 @@ type Database interface {
func NewPublicRoomsServerDatabase(dataSourceName string) (Database, error) { func NewPublicRoomsServerDatabase(dataSourceName string) (Database, error) {
uri, err := url.Parse(dataSourceName) uri, err := url.Parse(dataSourceName)
if err != nil { if err != nil {
return nil, err // if the scheme doesn't match, fall back to postgres in case the config has
// postgres key=value connection strings
return postgres.NewPublicRoomsServerDatabase(dataSourceName)
} }
switch uri.Scheme { switch uri.Scheme {
case "postgres": case "postgres":

View file

@ -61,7 +61,9 @@ type Database interface {
func Open(dataSourceName string) (Database, error) { func Open(dataSourceName string) (Database, error) {
uri, err := url.Parse(dataSourceName) uri, err := url.Parse(dataSourceName)
if err != nil { if err != nil {
return nil, err // if the scheme doesn't match, fall back to postgres in case the config has
// postgres key=value connection strings
return postgres.Open(dataSourceName)
} }
switch uri.Scheme { switch uri.Scheme {
case "postgres": case "postgres":

View file

@ -52,7 +52,9 @@ type Database interface {
func NewSyncServerDatasource(dataSourceName string) (Database, error) { func NewSyncServerDatasource(dataSourceName string) (Database, error) {
uri, err := url.Parse(dataSourceName) uri, err := url.Parse(dataSourceName)
if err != nil { if err != nil {
return nil, err // if the scheme doesn't match, fall back to postgres in case the config has
// postgres key=value connection strings
return postgres.NewSyncServerDatasource(dataSourceName)
} }
switch uri.Scheme { switch uri.Scheme {
case "postgres": case "postgres":