Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically migrate older management store engine to SQLite #2170

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading