Add ParseFileURI and use it when dealing with file URIs (#1088)

* Add ParseFileURI and use it when dealing with file URIs

Fixes #1059

* Missing file

* Linting
main
Kegsay 2020-06-04 11:18:08 +01:00 committed by GitHub
parent d4f9a4bb97
commit e7d1ac84c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 66 additions and 51 deletions

View File

@ -37,7 +37,11 @@ type Database struct {
func NewDatabase(dataSourceName string) (*Database, error) {
var result Database
var err error
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}
if err = result.prepare(); err != nil {

View File

@ -34,7 +34,7 @@ func NewDatabase(
case "postgres":
return nil, fmt.Errorf("Cannot use postgres implementation")
case "file":
return sqlite3.NewDatabase(uri.Path)
return sqlite3.NewDatabase(dataSourceName)
default:
return nil, fmt.Errorf("Cannot use postgres implementation")
}

View File

@ -48,7 +48,11 @@ type Database struct {
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
var db *sql.DB
var err error
if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}
partitions := internal.PartitionOffsetStatements{}

View File

@ -36,7 +36,7 @@ func NewDatabase(
case "postgres":
return nil, fmt.Errorf("Cannot use postgres implementation")
case "file":
return sqlite3.NewDatabase(uri.Path, serverName)
return sqlite3.NewDatabase(dataSourceName, serverName)
default:
return nil, fmt.Errorf("Cannot use postgres implementation")
}

View File

@ -41,7 +41,11 @@ type Database struct {
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
var db *sql.DB
var err error
if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}
d := devicesStatements{}

View File

@ -36,7 +36,7 @@ func NewDatabase(
case "postgres":
return nil, fmt.Errorf("Cannot use postgres implementation")
case "file":
return sqlite3.NewDatabase(uri.Path, serverName)
return sqlite3.NewDatabase(dataSourceName, serverName)
default:
return nil, fmt.Errorf("Cannot use postgres implementation")
}

View File

@ -38,7 +38,11 @@ type Database struct {
func NewDatabase(dataSourceName string) (*Database, error) {
var result Database
var err error
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}
if err = result.prepare(); err != nil {

View File

@ -33,7 +33,7 @@ func NewDatabase(
}
switch uri.Scheme {
case "file":
return sqlite3.NewDatabase(uri.Path)
return sqlite3.NewDatabase(dataSourceName)
case "postgres":
return nil, fmt.Errorf("Cannot use postgres implementation")
default:

View File

@ -277,12 +277,9 @@ func setupNaffka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
uri, err := url.Parse(string(cfg.Database.Naffka))
if err != nil || uri.Scheme == "file" {
var cs string
if uri.Opaque != "" { // file:filename.db
cs = uri.Opaque
} else if uri.Path != "" { // file:///path/to/filename.db
cs = uri.Path
} else {
logrus.Panic("file uri has no filename")
cs, err = sqlutil.ParseFileURI(string(cfg.Database.Naffka))
if err != nil {
logrus.WithError(err).Panic("Failed to parse naffka database file URI")
}
db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
if err != nil {

24
internal/sqlutil/uri.go Normal file
View File

@ -0,0 +1,24 @@
package sqlutil
import (
"fmt"
"net/url"
)
// ParseFileURI returns the filepath in the given file: URI. Specifically, this will handle
// both relative (file:foo.db) and absolute (file:///path/to/foo) paths.
func ParseFileURI(dataSourceName string) (string, error) {
uri, err := url.Parse(dataSourceName)
if err != nil {
return "", err
}
var cs string
if uri.Opaque != "" { // file:filename.db
cs = uri.Opaque
} else if uri.Path != "" { // file:///path/to/filename.db
cs = uri.Path
} else {
return "", fmt.Errorf("invalid file uri: %s", dataSourceName)
}
return cs, nil
}

View File

@ -37,7 +37,11 @@ type Database struct {
func Open(dataSourceName string) (*Database, error) {
var d Database
var err error
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}
if err = d.statements.prepare(d.db); err != nil {

View File

@ -35,7 +35,7 @@ func Open(
case "postgres":
return nil, fmt.Errorf("Cannot use postgres implementation")
case "file":
return sqlite3.Open(uri.Path)
return sqlite3.Open(dataSourceName)
default:
return nil, fmt.Errorf("Cannot use postgres implementation")
}

View File

@ -42,7 +42,11 @@ type attributeValue interface{}
func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatrixserverlib.ServerName) (*PublicRoomsServerDatabase, error) {
var db *sql.DB
var err error
if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}
storage := PublicRoomsServerDatabase{

View File

@ -32,7 +32,7 @@ func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatri
case "postgres":
return nil, fmt.Errorf("Cannot use postgres implementation")
case "file":
return sqlite3.NewPublicRoomsServerDatabase(uri.Path, localServerName)
return sqlite3.NewPublicRoomsServerDatabase(dataSourceName, localServerName)
default:
return nil, fmt.Errorf("Cannot use postgres implementation")
}

View File

@ -18,8 +18,6 @@ package sqlite3
import (
"context"
"database/sql"
"errors"
"net/url"
"github.com/matrix-org/dendrite/internal/sqlutil"
@ -50,18 +48,10 @@ type Database struct {
// nolint: gocyclo
func Open(dataSourceName string) (*Database, error) {
var d Database
uri, err := url.Parse(dataSourceName)
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
var cs string
if uri.Opaque != "" { // file:filename.db
cs = uri.Opaque
} else if uri.Path != "" { // file:///path/to/filename.db
cs = uri.Path
} else {
return nil, errors.New("no filename or path in connect string")
}
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}

View File

@ -17,8 +17,6 @@ package sqlite3
import (
"context"
"errors"
"net/url"
"time"
"golang.org/x/crypto/ed25519"
@ -46,18 +44,10 @@ func NewDatabase(
serverKey ed25519.PublicKey,
serverKeyID gomatrixserverlib.KeyID,
) (*Database, error) {
uri, err := url.Parse(dataSourceName)
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
var cs string
if uri.Opaque != "" { // file:filename.db
cs = uri.Opaque
} else if uri.Path != "" { // file:///path/to/filename.db
cs = uri.Path
} else {
return nil, errors.New("no filename or path in connect string")
}
db, err := sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
if err != nil {
return nil, err

View File

@ -17,8 +17,6 @@ package sqlite3
import (
"database/sql"
"errors"
"net/url"
"github.com/matrix-org/dendrite/internal/sqlutil"
@ -43,18 +41,10 @@ type SyncServerDatasource struct {
// nolint: gocyclo
func NewDatabase(dataSourceName string) (*SyncServerDatasource, error) {
var d SyncServerDatasource
uri, err := url.Parse(dataSourceName)
cs, err := sqlutil.ParseFileURI(dataSourceName)
if err != nil {
return nil, err
}
var cs string
if uri.Opaque != "" { // file:filename.db
cs = uri.Opaque
} else if uri.Path != "" { // file:///path/to/filename.db
cs = uri.Path
} else {
return nil, errors.New("no filename or path in connect string")
}
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
return nil, err
}