From b7c692878b07b312ab7012367bab13e63a57109f Mon Sep 17 00:00:00 2001 From: Daniel <dhaavi@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:43:29 +0100 Subject: [PATCH] Switch core and cache databases to use sqlite when bbold db is not present --- base/database/dbmodule/db.go | 5 +++++ service/core/base/databases.go | 34 +++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/base/database/dbmodule/db.go b/base/database/dbmodule/db.go index 51d121fd..5da80da7 100644 --- a/base/database/dbmodule/db.go +++ b/base/database/dbmodule/db.go @@ -35,6 +35,11 @@ func SetDatabaseLocation(dir string) { } } +// GetDatabaseLocation returns the initialized database location. +func GetDatabaseLocation() string { + return databasesRootDir +} + func prep() error { SetDatabaseLocation(filepath.Join(module.instance.DataDir(), "databases")) if databasesRootDir == "" { diff --git a/service/core/base/databases.go b/service/core/base/databases.go index c0e9dca4..98c587a1 100644 --- a/service/core/base/databases.go +++ b/service/core/base/databases.go @@ -1,43 +1,51 @@ package base import ( + "path/filepath" + "github.com/safing/portmaster/base/database" - _ "github.com/safing/portmaster/base/database/dbmodule" + "github.com/safing/portmaster/base/database/dbmodule" _ "github.com/safing/portmaster/base/database/storage/bbolt" + "github.com/safing/portmaster/base/utils" ) // Default Values (changeable for testing). var ( - DefaultDatabaseStorageType = "bbolt" + DefaultDatabaseStorageType = "sqlite" ) func registerDatabases() error { + // If there is an existing bbolt core database, use it instead. + coreStorageType := DefaultDatabaseStorageType + if utils.PathExists(filepath.Join(dbmodule.GetDatabaseLocation(), "core", "bbolt")) { + coreStorageType = "bbolt" + } + + // Register core database. _, err := database.Register(&database.Database{ Name: "core", Description: "Holds core data, such as settings and profiles", - StorageType: DefaultDatabaseStorageType, + StorageType: coreStorageType, }) if err != nil { return err } + // If there is an existing cache bbolt database, use it instead. + cacheStorageType := DefaultDatabaseStorageType + if utils.PathExists(filepath.Join(dbmodule.GetDatabaseLocation(), "cache", "bbolt")) { + cacheStorageType = "bbolt" + } + + // Register cache database. _, err = database.Register(&database.Database{ Name: "cache", Description: "Cached data, such as Intelligence and DNS Records", - StorageType: DefaultDatabaseStorageType, + StorageType: cacheStorageType, }) if err != nil { return err } - // _, err = database.Register(&database.Database{ - // Name: "history", - // Description: "Historic event data", - // StorageType: DefaultDatabaseStorageType, - // }) - // if err != nil { - // return err - // } - return nil }