From 714959126beca3a85bac0bd0e8be2e20e8cf8c84 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 9 Jan 2020 17:03:36 +0000 Subject: [PATCH] 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 --- common/keydb/keydb.go | 4 +++- federationsender/storage/storage.go | 6 ++++-- mediaapi/storage/storage.go | 4 +++- publicroomsapi/storage/storage.go | 4 +++- roomserver/storage/storage.go | 4 +++- syncapi/storage/storage.go | 4 +++- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/common/keydb/keydb.go b/common/keydb/keydb.go index b9fa884e..04692e98 100644 --- a/common/keydb/keydb.go +++ b/common/keydb/keydb.go @@ -33,7 +33,9 @@ type Database interface { func NewDatabase(dataSourceName string) (Database, error) { uri, err := url.Parse(dataSourceName) 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 { case "postgres": diff --git a/federationsender/storage/storage.go b/federationsender/storage/storage.go index 8cffdbf1..9f2805cf 100644 --- a/federationsender/storage/storage.go +++ b/federationsender/storage/storage.go @@ -34,12 +34,14 @@ type Database interface { func NewDatabase(dataSourceName string) (Database, error) { uri, err := url.Parse(dataSourceName) 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 { case "postgres": return postgres.NewDatabase(dataSourceName) default: - return nil, errors.New("unknown schema") + return errors.New("unknown schema") } } diff --git a/mediaapi/storage/storage.go b/mediaapi/storage/storage.go index 0f39c1d0..ca9f69f8 100644 --- a/mediaapi/storage/storage.go +++ b/mediaapi/storage/storage.go @@ -36,7 +36,9 @@ type Database interface { func Open(dataSourceName string) (Database, error) { uri, err := url.Parse(dataSourceName) 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 { case "postgres": diff --git a/publicroomsapi/storage/storage.go b/publicroomsapi/storage/storage.go index a6a39d52..d611686b 100644 --- a/publicroomsapi/storage/storage.go +++ b/publicroomsapi/storage/storage.go @@ -39,7 +39,9 @@ type Database interface { func NewPublicRoomsServerDatabase(dataSourceName string) (Database, error) { uri, err := url.Parse(dataSourceName) 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 { case "postgres": diff --git a/roomserver/storage/storage.go b/roomserver/storage/storage.go index 325d96e9..62e8b64c 100644 --- a/roomserver/storage/storage.go +++ b/roomserver/storage/storage.go @@ -61,7 +61,9 @@ type Database interface { func Open(dataSourceName string) (Database, error) { uri, err := url.Parse(dataSourceName) 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 { case "postgres": diff --git a/syncapi/storage/storage.go b/syncapi/storage/storage.go index eedb42f0..4d896531 100644 --- a/syncapi/storage/storage.go +++ b/syncapi/storage/storage.go @@ -52,7 +52,9 @@ type Database interface { func NewSyncServerDatasource(dataSourceName string) (Database, error) { uri, err := url.Parse(dataSourceName) 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 { case "postgres":