Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Owncloudsql psql #4521

Open
wants to merge 1 commit into
base: edge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (s *svc) ListStorageSpaces(ctx context.Context, req *provider.ListStorageSp
res, err := c.ListStorageProviders(ctx, listReq)
if err != nil {
return &provider.ListStorageSpacesResponse{
Status: status.NewStatusFromErrType(ctx, "gateway could not call ListStorageSpaces", err),
Status: status.NewStatusFromErrType(ctx, "gateway could not call ListStorageProviders", err),
}, nil
}
if res.Status.Code != rpc.Code_CODE_OK {
Expand Down
24 changes: 17 additions & 7 deletions pkg/storage/fs/owncloudsql/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

// Provides mysql drivers
_ "github.com/go-sql-driver/mysql"

_ "github.com/lib/pq"
)

// Cache represents a oc10-style file cache
Expand Down Expand Up @@ -86,11 +88,11 @@ type Scannable interface {
Scan(...interface{}) error
}

// NewMysql returns a new Cache instance connecting to a MySQL database
func NewMysql(dsn string) (*Cache, error) {
sqldb, err := sql.Open("mysql", dsn)
// NewSqlConnect returns a new Cache instance connecting to a database specified by the driver
func NewSqlConnect(driver, dsn string) (*Cache, error) {
sqldb, err := sql.Open(driver, dsn)
if err != nil {
return nil, errors.Wrap(err, "error connecting to the database")
return nil, errors.Wrap(err, "error connecting to database")
}

// FIXME make configurable
Expand All @@ -104,7 +106,7 @@ func NewMysql(dsn string) (*Cache, error) {
return nil, errors.Wrap(err, "error connecting to the database")
}

return New("mysql", sqldb)
return New(driver, sqldb)
}

// New returns a new Cache instance connecting to the given sql.DB
Expand All @@ -123,8 +125,12 @@ func (c *Cache) ListStorages(ctx context.Context, onlyHome bool) ([]*Storage, er
mountPointConcat := ""
if c.driver == "mysql" {
mountPointConcat = "m.mount_point = CONCAT('/', m.user_id, '/')"
} else { // sqlite3
} else if c.driver == "sqlite3" { // sqlite3
mountPointConcat = "m.mount_point = '/' || m.user_id || '/'"
} else if c.driver == "postgres" {
mountPointConcat = "m.mount_point = '/' + m.user_id + '/'"
} else {
return nil, errors.New("Unsupported driver")
}

query = "SELECT s.id, s.numeric_id FROM oc_storages s JOIN oc_mounts m ON s.numeric_id = m.storage_id WHERE " + mountPointConcat
Expand Down Expand Up @@ -477,8 +483,12 @@ func (c *Cache) doInsertOrUpdate(ctx context.Context, tx *sql.Tx, storage interf
}
if c.driver == "mysql" { // mysql upsert
query += " ON DUPLICATE KEY UPDATE "
} else { // sqlite3 upsert
} else if c.driver == "postgres" {
query += " ON CONFLICT (id) DO UPDATE SET "
} else if c.driver == "sqlite3" { // sqlite3 upsert
query += " ON CONFLICT(storage,path_hash) DO UPDATE SET "
} else {
return -1, errors.New("unsupported database driver")
}
query += strings.Join(updates, ",")

Expand Down
12 changes: 10 additions & 2 deletions pkg/storage/fs/owncloudsql/owncloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,16 @@ func New(m map[string]interface{}, _ events.Stream) (storage.FS, error) {
Msg("could not create uploadinfo dir")
}

dbSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.DbUsername, c.DbPassword, c.DbHost, c.DbPort, c.DbName)
filecache, err := filecache.NewMysql(dbSource)
// use MySQL
// driver := "mysql"
// dbSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.DbUsername, c.DbPassword, c.DbHost, c.DbPort, c.DbName)

// Use PSql
driver := "postgres"
dbSource := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
c.DbHost, c.DbPort, c.DbUsername, c.DbPassword, c.DbName)

filecache, err := filecache.NewSqlConnect(driver, dbSource)
if err != nil {
return nil, err
}
Expand Down
Loading