-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand_status.go
106 lines (85 loc) · 1.8 KB
/
command_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/olekukonko/tablewriter"
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/viper"
)
type StatusCommand struct {
}
func (c *StatusCommand) Help() string {
helpText := `
Usage: evedbtool status [options] ...
Show migration status.
Options:
`
return strings.TrimSpace(helpText)
}
func (c *StatusCommand) Synopsis() string {
return "Show migration status"
}
func (c *StatusCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
migrate.SetTable("migrations")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
db := getDB()
dialect := "mysql"
source := migrate.FileMigrationSource{
Dir: viper.GetString("migrations-dir"),
}
migrations, err := source.FindMigrations()
if err != nil {
ui.Error(err.Error())
return 1
}
records, err := migrate.GetMigrationRecords(db, dialect)
if err != nil {
ui.Error(err.Error())
return 1
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Migration", "Applied"})
table.SetColWidth(60)
rows := make(map[string]*statusRow)
for _, m := range migrations {
rows[m.Id] = &statusRow{
Id: m.Id,
Migrated: false,
}
}
for _, r := range records {
if rows[r.Id] == nil {
ui.Warn(fmt.Sprintf("Could not find migration file: %v", r.Id))
continue
}
rows[r.Id].Migrated = true
rows[r.Id].AppliedAt = r.AppliedAt
}
for _, m := range migrations {
if rows[m.Id] != nil && rows[m.Id].Migrated {
table.Append([]string{
m.Id,
rows[m.Id].AppliedAt.String(),
})
} else {
table.Append([]string{
m.Id,
"no",
})
}
}
table.Render()
return 0
}
type statusRow struct {
Id string
Migrated bool
AppliedAt time.Time
}