mirror of https://github.com/status-im/go-waku.git
chore: add sqlite default parameters
This commit is contained in:
parent
ca17ac8c91
commit
446e38ceaf
|
@ -218,8 +218,8 @@ var (
|
||||||
}
|
}
|
||||||
StoreMessageDBURL = &cli.StringFlag{
|
StoreMessageDBURL = &cli.StringFlag{
|
||||||
Name: "store-message-db-url",
|
Name: "store-message-db-url",
|
||||||
Usage: "The database connection URL for peristent storage.",
|
Usage: "The database connection URL for persistent storage.",
|
||||||
Value: "sqlite3://store.db?_journal=WAL&_timeout=5000",
|
Value: "sqlite3://store.db",
|
||||||
Destination: &options.Store.DatabaseURL,
|
Destination: &options.Store.DatabaseURL,
|
||||||
}
|
}
|
||||||
StoreResumePeer = &cli.GenericFlag{
|
StoreResumePeer = &cli.GenericFlag{
|
||||||
|
|
|
@ -80,7 +80,7 @@ func freePort() (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateDBUrl(val string) 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 {
|
if !matched || err != nil {
|
||||||
return errors.New("invalid db url option format")
|
return errors.New("invalid db url option format")
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package sqlite
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
|
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
|
||||||
"github.com/waku-org/go-waku/waku/persistence"
|
"github.com/waku-org/go-waku/waku/persistence"
|
||||||
|
@ -85,17 +86,33 @@ func (q Queries) GetSize() string {
|
||||||
return q.getSizeQuery
|
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.
|
// WithDB is a DBOption that lets you use a sqlite3 DBStore.
|
||||||
func WithDB(path string) persistence.DBOption {
|
func WithDB(dburl string) persistence.DBOption {
|
||||||
return persistence.WithDriver("sqlite3", path, persistence.ConnectionPoolOptions{
|
return persistence.WithDriver("sqlite3", addSqliteURLDefaults(dburl), persistence.ConnectionPoolOptions{
|
||||||
// Disable concurrent access as not supported by the driver
|
// Disable concurrent access as not supported by the driver
|
||||||
MaxOpenConnections: 1,
|
MaxOpenConnections: 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDB creates a sqlite3 DB in the specified path
|
// NewDB creates a sqlite3 DB in the specified path
|
||||||
func NewDB(path string) (*sql.DB, error) {
|
func NewDB(dburl string) (*sql.DB, error) {
|
||||||
db, err := sql.Open("sqlite3", path)
|
db, err := sql.Open("sqlite3", addSqliteURLDefaults(dburl))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue