From 446e38ceaf2a97e8acd981fee8202340b77e7f57 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sat, 10 Dec 2022 07:25:26 -0400 Subject: [PATCH] chore: add sqlite default parameters --- cmd/waku/flags.go | 4 ++-- waku/node.go | 2 +- waku/persistence/sqlite/sqlite.go | 25 +++++++++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cmd/waku/flags.go b/cmd/waku/flags.go index 6e8e2288..bbe3b825 100644 --- a/cmd/waku/flags.go +++ b/cmd/waku/flags.go @@ -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{ diff --git a/waku/node.go b/waku/node.go index 3662623a..fcf5a0e5 100644 --- a/waku/node.go +++ b/waku/node.go @@ -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") } diff --git a/waku/persistence/sqlite/sqlite.go b/waku/persistence/sqlite/sqlite.go index b80c59c4..851b091b 100644 --- a/waku/persistence/sqlite/sqlite.go +++ b/waku/persistence/sqlite/sqlite.go @@ -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 }