mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
38b8c9cc58
#15555 Probably the best way to review this is commit by commit: - First commit does the actual moving. - Second commit fixes golangci-lint issues (in the least effort way to avoid refactoring or rearrangement of some of the code). - Third commit moves a printf to before the migration step is executed. In the past some customers hitting migration issues (like migration steps hanging or taking long to execute) and wanted to know which one was it. The only way to know was to look at the repository and looking for the next migration after the last one logged. Checks: - [X] Manual QA for all new/changed functionality Manual tests: - `make fleet && make db-reset`. - Adding a new migration via `make migration name=Foobar` and then running `./build/fleet prepare db`. - Enrolling a new device to Fleet (smoke test).
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package goose
|
|
|
|
import "testing"
|
|
|
|
func newMigration(v int64, src string) *Migration {
|
|
return &Migration{Version: v, Previous: -1, Next: -1, Source: src}
|
|
}
|
|
|
|
func TestMigrationSort(t *testing.T) {
|
|
|
|
ms := Migrations{}
|
|
|
|
// insert in any order
|
|
ms = append(ms, newMigration(20120000, "test"))
|
|
ms = append(ms, newMigration(20128000, "test"))
|
|
ms = append(ms, newMigration(20129000, "test"))
|
|
ms = append(ms, newMigration(20127000, "test"))
|
|
|
|
ms = sortAndConnectMigrations(ms)
|
|
|
|
sorted := []int64{20120000, 20127000, 20128000, 20129000}
|
|
|
|
validateMigrationSort(t, ms, sorted)
|
|
}
|
|
|
|
func validateMigrationSort(t *testing.T, ms Migrations, sorted []int64) {
|
|
|
|
for i, m := range ms {
|
|
if sorted[i] != m.Version {
|
|
t.Error("incorrect sorted version")
|
|
}
|
|
|
|
var next, prev int64
|
|
|
|
if i == 0 {
|
|
prev = -1
|
|
next = ms[i+1].Version
|
|
} else if i == len(ms)-1 {
|
|
prev = ms[i-1].Version
|
|
next = -1
|
|
} else {
|
|
prev = ms[i-1].Version
|
|
next = ms[i+1].Version
|
|
}
|
|
|
|
if m.Next != next {
|
|
t.Errorf("mismatched Next. v: %v, got %v, wanted %v\n", m, m.Next, next)
|
|
}
|
|
|
|
if m.Previous != prev {
|
|
t.Errorf("mismatched Previous v: %v, got %v, wanted %v\n", m, m.Previous, prev)
|
|
}
|
|
}
|
|
|
|
t.Log(ms)
|
|
}
|