2023-01-04 17:58:14 +00:00
|
|
|
package waku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence/postgres"
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func validateDBUrl(val string) error {
|
|
|
|
matched, err := regexp.Match(`^[\w\+]+:\/\/[\w\/\\\.\:\@]+\?{0,1}.*$`, []byte(val))
|
|
|
|
if !matched || err != nil {
|
|
|
|
return errors.New("invalid db url option format")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
func ExtractDBAndMigration(databaseURL string) (*sql.DB, func(*sql.DB) error, error) {
|
2023-01-04 17:58:14 +00:00
|
|
|
var db *sql.DB
|
|
|
|
var migrationFn func(*sql.DB) error
|
|
|
|
var err error
|
|
|
|
|
|
|
|
dbURL := ""
|
|
|
|
if databaseURL != "" {
|
|
|
|
err := validateDBUrl(databaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
dbURL = databaseURL
|
|
|
|
} else {
|
|
|
|
// In memoryDB
|
2023-02-17 03:03:22 +00:00
|
|
|
dbURL = "sqlite3://:memory:"
|
2023-01-04 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbURLParts := strings.Split(dbURL, "://")
|
|
|
|
dbEngine := dbURLParts[0]
|
|
|
|
dbParams := dbURLParts[1]
|
|
|
|
switch dbEngine {
|
|
|
|
case "sqlite3":
|
|
|
|
db, migrationFn, err = sqlite.NewDB(dbParams)
|
|
|
|
case "postgresql":
|
|
|
|
db, migrationFn, err = postgres.NewDB(dbURL)
|
|
|
|
default:
|
|
|
|
err = errors.New("unsupported database engine")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, migrationFn, nil
|
|
|
|
|
|
|
|
}
|