11package main
22
33import (
4+ "encoding/json"
45 "flag"
56 "fmt"
67 "os"
@@ -14,6 +15,12 @@ import (
1415
1516type StatusCommand struct {}
1617
18+ type statusRow struct {
19+ Id string
20+ Migrated bool
21+ AppliedAt time.Time
22+ }
23+
1724func (* StatusCommand ) Help () string {
1825 helpText := `
1926Usage: 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}
0 commit comments