chore: add sqlite default parameters

This commit is contained in:
Richard Ramos 2022-12-10 07:25:26 -04:00
parent ca17ac8c91
commit 446e38ceaf
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
3 changed files with 24 additions and 7 deletions

View File

@ -218,8 +218,8 @@ var (
}
StoreMessageDBURL = &cli.StringFlag{
Name: "store-message-db-url",
Usage: "The database connection URL for peristent storage.",
Value: "sqlite3://store.db?_journal=WAL&_timeout=5000",
Usage: "The database connection URL for persistent storage.",
Value: "sqlite3://store.db",
Destination: &options.Store.DatabaseURL,
}
StoreResumePeer = &cli.GenericFlag{

View File

@ -80,7 +80,7 @@ func freePort() (int, error) {
}
func validateDBUrl(val string) error {
matched, err := regexp.Match(`^[\w\+]+:\/\/[\w\/\\\.\:\@]+\?.*$`, []byte(val))
matched, err := regexp.Match(`^[\w\+]+:\/\/[\w\/\\\.\:\@]+\?{0,1}.*$`, []byte(val))
if !matched || err != nil {
return errors.New("invalid db url option format")
}

View File

@ -3,6 +3,7 @@ package sqlite
import (
"database/sql"
"fmt"
"strings"
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
"github.com/waku-org/go-waku/waku/persistence"
@ -85,17 +86,33 @@ func (q Queries) GetSize() string {
return q.getSizeQuery
}
func addSqliteURLDefaults(dburl string) string {
if !strings.Contains(dburl, "?") {
dburl += "?"
}
if !strings.Contains(dburl, "_journal=") {
dburl += "&_journal=WAL"
}
if !strings.Contains(dburl, "_timeout=") {
dburl += "&_timeout=5000"
}
return dburl
}
// WithDB is a DBOption that lets you use a sqlite3 DBStore.
func WithDB(path string) persistence.DBOption {
return persistence.WithDriver("sqlite3", path, persistence.ConnectionPoolOptions{
func WithDB(dburl string) persistence.DBOption {
return persistence.WithDriver("sqlite3", addSqliteURLDefaults(dburl), persistence.ConnectionPoolOptions{
// Disable concurrent access as not supported by the driver
MaxOpenConnections: 1,
})
}
// NewDB creates a sqlite3 DB in the specified path
func NewDB(path string) (*sql.DB, error) {
db, err := sql.Open("sqlite3", path)
func NewDB(dburl string) (*sql.DB, error) {
db, err := sql.Open("sqlite3", addSqliteURLDefaults(dburl))
if err != nil {
return nil, err
}