mirror of
https://github.com/luxfi/cli.git
synced 2026-10-30 17:01:20 +00:00
- Update github.com/luxfi/netrunner v1.15.3 → v1.15.4 - Update github.com/luxfi/node v1.22.77 → v1.22.80 - Remove local replace directive for netrunner - Use published package versions for reproducible builds
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
// Copyright (C) 2022-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
// Package migrations handles internal data migrations for the CLI.
|
|
package migrations
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/luxfi/cli/pkg/application"
|
|
"github.com/luxfi/cli/pkg/ux"
|
|
)
|
|
|
|
type migrationFunc func(*application.Lux, *migrationRunner) error
|
|
|
|
type migrationRunner struct {
|
|
showMsg bool
|
|
running bool
|
|
migrations map[int]migrationFunc
|
|
}
|
|
|
|
var (
|
|
runMessage = "The tool needs to apply some internal updates first..."
|
|
endMessage = "Update process successfully completed"
|
|
failedEndMessage = "Some updates succeeded - others failed. Check output for hints"
|
|
)
|
|
|
|
// RunMigrations executes all pending migrations.
|
|
// Note: there are no rollbacks (for now).
|
|
func RunMigrations(app *application.Lux) error {
|
|
runner := &migrationRunner{
|
|
showMsg: true,
|
|
migrations: map[int]migrationFunc{
|
|
// add new migrations here in rising index order
|
|
// next one is 2
|
|
0: migrateTopLevelFiles,
|
|
1: migrateEVMNames,
|
|
},
|
|
}
|
|
return runner.run(app)
|
|
}
|
|
|
|
func (m *migrationRunner) run(app *application.Lux) error {
|
|
// by using an int index we can sort of "enforce" an order
|
|
// with just an array it could easily happen that someone
|
|
// prepends a new migration at the front instead of the bottom
|
|
for i := 0; i < len(m.migrations); i++ {
|
|
err := m.migrations[i](app, m)
|
|
if err != nil {
|
|
if m.running {
|
|
ux.Logger.PrintToUser("%s", failedEndMessage)
|
|
}
|
|
return fmt.Errorf("migration #%d failed: %w", i, err)
|
|
}
|
|
}
|
|
if m.running {
|
|
ux.Logger.PrintToUser("%s", endMessage)
|
|
m.running = false
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Every migration should first check if there are migrations to be run.
|
|
// If yes, should run this function to print a message only once
|
|
func (m *migrationRunner) printMigrationMessage() {
|
|
if m.showMsg {
|
|
ux.Logger.PrintToUser("%s", runMessage)
|
|
}
|
|
m.showMsg = false
|
|
m.running = true
|
|
}
|