-
Notifications
You must be signed in to change notification settings - Fork 22
/
status.go
75 lines (61 loc) · 2.01 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package migrations
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
"unicode/utf8"
)
func (m *migrator) status(w io.Writer) error {
// sort the registered migrations by name (which will sort by the
// timestamp in their names)
sort.Slice(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})
// look at the migrations table to see the already run migrations
completed, err := m.getCompletedMigrations()
if err != nil {
return err
}
// diff the completed migrations from the registered migrations to find
// the migrations we still need to run
uncompleted := filterMigrations(migrations, completed, false)
return writeStatusTable(w, completed, uncompleted)
}
func writeStatusTable(w io.Writer, completed []*migration, uncompleted []*migration) error {
if len(completed)+len(uncompleted) == 0 {
_, err := fmt.Fprintln(w, "No migrations found")
return err
}
maxNameLength := 20
for _, m := range completed {
maxNameLength = maxInt(maxNameLength, utf8.RuneCountInString(m.Name))
}
for _, m := range uncompleted {
maxNameLength = maxInt(maxNameLength, utf8.RuneCountInString(m.Name))
}
bf := bytes.NewBuffer(nil)
// write header
bf.WriteString("+---------+" + strings.Repeat("-", maxNameLength+2) + "+-------+\n")
bf.WriteString("| Applied | Migration" + strings.Repeat(" ", maxNameLength-8) + "| Batch |\n")
bf.WriteString("+---------+" + strings.Repeat("-", maxNameLength+2) + "+-------+\n")
// write completed migrations
for _, m := range completed {
bf.WriteString("| √ | " + m.Name + strings.Repeat(" ", maxNameLength-len(m.Name)) + " | " + fmt.Sprintf("%5d", m.Batch) + " |\n")
}
// write uncompleted migrations
for _, m := range uncompleted {
bf.WriteString("| | " + m.Name + strings.Repeat(" ", maxNameLength-len(m.Name)) + " | |\n")
}
// write footer
bf.WriteString("+---------+" + strings.Repeat("-", maxNameLength+2) + "+-------+\n")
_, err := bf.WriteTo(w)
return err
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}