Make SQLite default for new installations (#1529)

* Make SQLite default for new installations

* if var is not set, return empty string

this allows getStoreEngineFromDatadir to detect json store files

---------

Co-authored-by: Maycon Santos <mlsmaycon@gmail.com>
This commit is contained in:
Yury Gargay
2024-02-20 15:06:32 +01:00
committed by GitHub
parent 51f133fdc6
commit 0fbf72434e
5 changed files with 27 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package server
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
@@ -50,10 +51,10 @@ const (
)
func getStoreEngineFromEnv() StoreEngine {
// NETBIRD_STORE_ENGINE supposed to be used in tests. Otherwise rely on the config file.
// NETBIRD_STORE_ENGINE supposed to be used in tests. Otherwise, rely on the config file.
kind, ok := os.LookupEnv("NETBIRD_STORE_ENGINE")
if !ok {
return FileStoreEngine
return ""
}
value := StoreEngine(strings.ToLower(kind))
@@ -62,13 +63,26 @@ func getStoreEngineFromEnv() StoreEngine {
return value
}
return SqliteStoreEngine
}
func getStoreEngineFromDatadir(dataDir string) StoreEngine {
storeFile := filepath.Join(dataDir, storeFileName)
if _, err := os.Stat(storeFile); err != nil {
// json file not found then use sqlite as default
return SqliteStoreEngine
}
return FileStoreEngine
}
func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
if kind == "" {
// fallback to env. Normally this only should be used from tests
// if store engine is not set in the config we first try to evaluate NETBIRD_STORE_ENGINE
kind = getStoreEngineFromEnv()
if kind == "" {
// NETBIRD_STORE_ENGINE is not set we evaluate default based on dataDir
kind = getStoreEngineFromDatadir(dataDir)
}
}
switch kind {
case FileStoreEngine:
@@ -82,15 +96,14 @@ func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (S
}
}
// NewStoreFromJson is only used in tests
func NewStoreFromJson(dataDir string, metrics telemetry.AppMetrics) (Store, error) {
fstore, err := NewFileStore(dataDir, nil)
if err != nil {
return nil, err
}
kind := getStoreEngineFromEnv()
switch kind {
switch kind := getStoreEngineFromEnv(); kind {
case FileStoreEngine:
return fstore, nil
case SqliteStoreEngine: