-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstatus.go
51 lines (45 loc) · 914 Bytes
/
status.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
package migrate
import (
"context"
"log"
"strconv"
"github.com/johngibb/migrate/db"
"github.com/johngibb/migrate/source"
)
// Status displays every migration, and whether it's been applied yet.
func Status(ctx context.Context, src *source.Source, db *db.Client) error {
migrations, err := src.FindMigrations()
if err != nil {
return err
}
applied, err := db.GetMigrations(ctx)
if err != nil {
return err
}
isApplied := func(name string) bool {
for _, a := range applied {
if a.Name == name {
return true
}
}
return false
}
w := maxNameWidth(migrations)
for _, m := range migrations {
status := "pending"
if isApplied(m.Name) {
status = "applied"
}
log.Printf("%-"+strconv.Itoa(w)+"s %s\n", m.Name, status)
}
return nil
}
func maxNameWidth(mm []*source.Migration) int {
w := 0
for _, m := range mm {
if n := len(m.Name); n > w {
w = n
}
}
return w
}