-
Notifications
You must be signed in to change notification settings - Fork 58
/
migrations.go
68 lines (60 loc) · 1.81 KB
/
migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package migrations
import (
"fmt"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/ux"
)
type migrationFunc func(*application.Avalanche, *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"
)
// poor-man's migrations: there are no rollbacks (for now)
func RunMigrations(app *application.Avalanche) error {
runner := &migrationRunner{
showMsg: true,
migrations: map[int]migrationFunc{
// add new migrations here in rising index order
// next one is 2
0: migrateTopLevelFiles,
1: migrateSubnetEVMNames,
},
}
return runner.run(app)
}
func (m *migrationRunner) run(app *application.Avalanche) 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(failedEndMessage)
}
return fmt.Errorf("migration #%d failed: %w", i, err)
}
}
if m.running {
ux.Logger.PrintToUser(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(runMessage)
}
m.showMsg = false
m.running = true
}