/
print.go
91 lines (71 loc) · 1.85 KB
/
print.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package migration
import (
"fmt"
"os"
"path"
"sort"
"github.com/Sirupsen/logrus"
"github.com/olekukonko/tablewriter"
migrate "github.com/rubenv/sql-migrate"
)
func (st *Step) print(log *logrus.Entry, admin bool) (int, error) {
folder := st.folder
tableName := st.tableUser
caption := fmt.Sprintf("%s (%s)", st.datasource.GetName(), "user")
if admin {
folder = path.Join(st.folder, "admin")
caption = fmt.Sprintf("%s (%s)", st.datasource.GetName(), "admin")
tableName = st.tableAdmin
}
db, err := st.datasource.OpenDatabase(log, admin, false)
if err != nil {
return 0, err
}
defer st.datasource.CloseDatabase(log, admin, false) //nolint: errcheck
migSet := migrate.MigrationSet{TableName: tableName, SchemaName: st.schema}
source := migrate.FileMigrationSource{Dir: folder}
files, err := source.FindMigrations()
if err != nil {
log.Error(err)
return 0, err
}
records, err := migSet.GetMigrationRecords(db, st.dialect)
if err != nil {
log.Error(err)
return 0, err
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Migration", "Applied"})
table.SetColWidth(60) //nolint:gomnd // Due to what is displayed this value ie more adapted
rows := make(map[string]string)
ids := make([]string, 0, len(files))
// Initialize table with migrations files
for _, f := range files {
rows[f.Id] = "no"
}
//Find the applying date
for _, r := range records {
if _, present := rows[r.Id]; !present {
log.Errorf("Applied migration %s not exists", r.Id)
}
rows[r.Id] = r.AppliedAt.String()
}
remaining := 0
for id := range rows {
ids = append(ids, id)
}
sort.Strings(ids)
for _, id := range ids {
table.Append([]string{
id,
rows[id],
})
if rows[id] == "no" {
remaining++
}
}
fmt.Printf("\n ---------- %s ----------\n", caption)
table.Render()
fmt.Println()
return remaining, nil
}