Skip to content

Commit 9f54568

Browse files
committed
feat(status_command): add json output option
Signed-off-by: Nicolas Payart <npayart@gmail.com>
1 parent e2b42d1 commit 9f54568

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

sql-migrate/command_status.go

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
67
"os"
@@ -14,6 +15,12 @@ import (
1415

1516
type StatusCommand struct{}
1617

18+
type statusRow struct {
19+
Id string
20+
Migrated bool
21+
AppliedAt time.Time
22+
}
23+
1724
func (*StatusCommand) Help() string {
1825
helpText := `
1926
Usage: sql-migrate status [options] ...
@@ -24,6 +31,7 @@ Options:
2431
2532
-config=dbconfig.yml Configuration file to use.
2633
-env="development" Environment.
34+
-output="json" Print output in JSON format (default is table format).
2735
2836
`
2937
return strings.TrimSpace(helpText)
@@ -70,10 +78,21 @@ func (c *StatusCommand) Run(args []string) int {
7078
return 1
7179
}
7280

73-
table := tablewriter.NewWriter(os.Stdout)
74-
table.SetHeader([]string{"Migration", "Applied"})
75-
table.SetColWidth(60)
81+
statusRows := buildStatusRows(migrations, records)
82+
83+
if OutputFormat == "json" {
84+
if err := printStatusJSON(statusRows); err != nil {
85+
ui.Error(fmt.Sprintf("Could not encode JSON: %s", err))
86+
return 1
87+
}
88+
} else {
89+
printStatusTable(statusRows)
90+
}
91+
92+
return 0
93+
}
7694

95+
func buildStatusRows(migrations []*migrate.Migration, records []*migrate.MigrationRecord) []*statusRow {
7796
rows := make(map[string]*statusRow)
7897

7998
for _, m := range migrations {
@@ -84,36 +103,60 @@ func (c *StatusCommand) Run(args []string) int {
84103
}
85104

86105
for _, r := range records {
87-
if rows[r.Id] == nil {
106+
if row, ok := rows[r.Id]; ok {
107+
row.Migrated = true
108+
row.AppliedAt = r.AppliedAt
109+
} else {
88110
ui.Warn(fmt.Sprintf("Could not find migration file: %v", r.Id))
89-
continue
90111
}
91-
92-
rows[r.Id].Migrated = true
93-
rows[r.Id].AppliedAt = r.AppliedAt
94112
}
95113

114+
var result []*statusRow
96115
for _, m := range migrations {
97-
if rows[m.Id] != nil && rows[m.Id].Migrated {
98-
table.Append([]string{
99-
m.Id,
100-
rows[m.Id].AppliedAt.String(),
101-
})
102-
} else {
103-
table.Append([]string{
104-
m.Id,
105-
"no",
106-
})
116+
result = append(result, rows[m.Id])
117+
}
118+
return result
119+
}
120+
121+
func printStatusJSON(rows []*statusRow) error {
122+
type jsonRow struct {
123+
Migration string `json:"migration"`
124+
Applied string `json:"applied"`
125+
}
126+
127+
var output []jsonRow
128+
for _, r := range rows {
129+
applied := "no"
130+
if r.Migrated {
131+
applied = r.AppliedAt.Format(time.RFC3339)
107132
}
133+
output = append(output, jsonRow{
134+
Migration: r.Id,
135+
Applied: applied,
136+
})
108137
}
109138

110-
table.Render()
139+
data, err := json.MarshalIndent(output, "", " ")
140+
if err != nil {
141+
return err
142+
}
111143

112-
return 0
144+
fmt.Println(string(data))
145+
return nil
113146
}
114147

115-
type statusRow struct {
116-
Id string
117-
Migrated bool
118-
AppliedAt time.Time
148+
func printStatusTable(rows []*statusRow) {
149+
table := tablewriter.NewWriter(os.Stdout)
150+
table.SetHeader([]string{"Migration", "Applied"})
151+
table.SetColWidth(60)
152+
153+
for _, r := range rows {
154+
applied := "no"
155+
if r.Migrated {
156+
applied = r.AppliedAt.Format("2006-01-02 15:04:05")
157+
}
158+
table.Append([]string{r.Id, applied})
159+
}
160+
161+
table.Render()
119162
}

sql-migrate/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ var dialects = map[string]gorp.Dialect{
2727
var (
2828
ConfigFile string
2929
ConfigEnvironment string
30+
OutputFormat string
3031
)
3132

3233
func ConfigFlags(f *flag.FlagSet) {
3334
f.StringVar(&ConfigFile, "config", "dbconfig.yml", "Configuration file to use.")
3435
f.StringVar(&ConfigEnvironment, "env", "development", "Environment to use.")
36+
f.StringVar(&OutputFormat, "output", "table", "Output format.")
3537
}
3638

3739
type Environment struct {

0 commit comments

Comments
 (0)