Skip to content

Commit

Permalink
auto migrate older management to sqlite (#2170)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmmbaga authored Jun 20, 2024
1 parent 4a3e78f commit fc15ee6
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions management/server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,38 @@ func getStoreEngineFromEnv() StoreEngine {
return SqliteStoreEngine
}

// getStoreEngine determines the store engine to use
func getStoreEngine(kind StoreEngine) StoreEngine {
// getStoreEngine determines the store engine to use.
// 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 == "" {
kind = getStoreEngineFromEnv()
if kind == "" {
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
}

// 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) {
kind = getStoreEngine(kind)
kind = getStoreEngine(dataDir, kind)

if err := checkFileStoreEngine(kind, dataDir); err != nil {
return nil, err
Expand All @@ -113,7 +131,7 @@ func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (S
log.Info("using Postgres store engine")
return newPostgresStore(metrics)
default:
return handleUnsupportedStoreEngine(kind, dataDir, metrics)
return nil, fmt.Errorf("unsupported kind of store: %s", kind)
}
}

Expand All @@ -128,25 +146,6 @@ func checkFileStoreEngine(kind StoreEngine, dataDir string) error {
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
func migrate(db *gorm.DB) error {
migrations := getMigrations()
Expand Down

0 comments on commit fc15ee6

Please sign in to comment.