mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-19 17:31:39 +02:00
auto migrate older management to sqlite (#2170)
This commit is contained in:
parent
4a3e78fb0f
commit
fc15ee6351
@ -86,20 +86,38 @@ func getStoreEngineFromEnv() StoreEngine {
|
|||||||
return SqliteStoreEngine
|
return SqliteStoreEngine
|
||||||
}
|
}
|
||||||
|
|
||||||
// getStoreEngine determines the store engine to use
|
// getStoreEngine determines the store engine to use.
|
||||||
func getStoreEngine(kind StoreEngine) StoreEngine {
|
// If no engine is specified, it attempts to retrieve it from the environment.
|
||||||
|
// If still not specified, it defaults to using SQLite.
|
||||||
|
// Additionally, it handles the migration from a JSON store file to SQLite if applicable.
|
||||||
|
func getStoreEngine(dataDir string, kind StoreEngine) StoreEngine {
|
||||||
if kind == "" {
|
if kind == "" {
|
||||||
kind = getStoreEngineFromEnv()
|
kind = getStoreEngineFromEnv()
|
||||||
if kind == "" {
|
if kind == "" {
|
||||||
kind = SqliteStoreEngine
|
kind = SqliteStoreEngine
|
||||||
|
|
||||||
|
// Migrate if it is the first run with a JSON file existing and no SQLite file present
|
||||||
|
jsonStoreFile := filepath.Join(dataDir, storeFileName)
|
||||||
|
sqliteStoreFile := filepath.Join(dataDir, storeSqliteFileName)
|
||||||
|
|
||||||
|
if util.FileExists(jsonStoreFile) && !util.FileExists(sqliteStoreFile) {
|
||||||
|
log.Warnf("unsupported store engine specified, but found %s. Automatically migrating to SQLite.", jsonStoreFile)
|
||||||
|
|
||||||
|
// Attempt to migrate from JSON store to SQLite
|
||||||
|
if err := MigrateFileStoreToSqlite(dataDir); err != nil {
|
||||||
|
log.Errorf("failed to migrate filestore to SQLite: %v", err)
|
||||||
|
kind = FileStoreEngine
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return kind
|
return kind
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStore creates a new store based on the provided engine type, data directory, and telemetry metrics
|
// NewStore creates a new store based on the provided engine type, data directory, and telemetry metrics
|
||||||
func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
||||||
kind = getStoreEngine(kind)
|
kind = getStoreEngine(dataDir, kind)
|
||||||
|
|
||||||
if err := checkFileStoreEngine(kind, dataDir); err != nil {
|
if err := checkFileStoreEngine(kind, dataDir); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -113,7 +131,7 @@ func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (S
|
|||||||
log.Info("using Postgres store engine")
|
log.Info("using Postgres store engine")
|
||||||
return newPostgresStore(metrics)
|
return newPostgresStore(metrics)
|
||||||
default:
|
default:
|
||||||
return handleUnsupportedStoreEngine(kind, dataDir, metrics)
|
return nil, fmt.Errorf("unsupported kind of store: %s", kind)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,25 +146,6 @@ func checkFileStoreEngine(kind StoreEngine, dataDir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleUnsupportedStoreEngine handles cases where the store engine is unsupported
|
|
||||||
func handleUnsupportedStoreEngine(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
|
||||||
jsonStoreFile := filepath.Join(dataDir, storeFileName)
|
|
||||||
sqliteStoreFile := filepath.Join(dataDir, storeSqliteFileName)
|
|
||||||
|
|
||||||
if util.FileExists(jsonStoreFile) && !util.FileExists(sqliteStoreFile) {
|
|
||||||
log.Warnf("unsupported store engine, but found %s. Automatically migrating to SQLite.", jsonStoreFile)
|
|
||||||
|
|
||||||
if err := MigrateFileStoreToSqlite(dataDir); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to migrate data to SQLite store: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("using SQLite store engine")
|
|
||||||
return NewSqliteStore(dataDir, metrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("unsupported kind of store: %s", kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
// migrate migrates the SQLite database to the latest schema
|
// migrate migrates the SQLite database to the latest schema
|
||||||
func migrate(db *gorm.DB) error {
|
func migrate(db *gorm.DB) error {
|
||||||
migrations := getMigrations()
|
migrations := getMigrations()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user