2023-07-06 21:40:57 +00:00
|
|
|
package utils
|
2023-01-04 17:58:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2023-10-05 00:20:02 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence"
|
2023-01-04 17:58:14 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence/postgres"
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence/sqlite"
|
2023-08-08 15:46:32 +00:00
|
|
|
"go.uber.org/zap"
|
2023-01-04 17:58:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-08-08 15:46:32 +00:00
|
|
|
// DBSettings hold db specific configuration settings required during the db initialization
|
|
|
|
type DBSettings struct {
|
2023-08-09 17:14:02 +00:00
|
|
|
Vacuum bool
|
2023-08-08 15:46:32 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:40:57 +00:00
|
|
|
// ExtractDBAndMigration will return a database connection, and migration function that should be used depending on a database connection string
|
2023-08-08 15:46:32 +00:00
|
|
|
func ExtractDBAndMigration(databaseURL string, dbSettings DBSettings, logger *zap.Logger) (*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
|
|
|
|
|
2023-08-08 15:46:32 +00:00
|
|
|
logger = logger.Named("db-setup")
|
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
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":
|
2023-08-09 17:23:44 +00:00
|
|
|
db, err = sqlite.NewDB(dbParams, dbSettings.Vacuum, logger)
|
|
|
|
migrationFn = sqlite.Migrations
|
2023-01-04 17:58:14 +00:00
|
|
|
case "postgresql":
|
2023-08-09 17:23:44 +00:00
|
|
|
db, err = postgres.NewDB(dbURL, dbSettings.Vacuum, logger)
|
|
|
|
migrationFn = postgres.Migrations
|
2023-01-04 17:58:14 +00:00
|
|
|
default:
|
|
|
|
err = errors.New("unsupported database engine")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, migrationFn, nil
|
|
|
|
|
|
|
|
}
|
2023-10-05 00:20:02 +00:00
|
|
|
|
|
|
|
func NewQueries(tbl string, db *sql.DB) (*persistence.Queries, error) {
|
|
|
|
switch persistence.GetDriverType(db) {
|
|
|
|
case persistence.SQLiteDriver:
|
|
|
|
return sqlite.NewQueries(tbl, db)
|
|
|
|
case persistence.PostgresDriver:
|
|
|
|
return postgres.NewQueries(tbl, db)
|
|
|
|
}
|
|
|
|
return nil, errors.New("unsupported database engine")
|
|
|
|
}
|