@@ -11,33 +11,26 @@ import (
1111 "github.com/pressly/goose/v3/internal/legacystore"
1212)
1313
14- type statusLine struct {
15- Version int64
16- AppliedAt time.Time
17- Pending bool
18- Source string
19- }
20-
21- type statusLines []* statusLine
14+ type (
15+ statusLine struct {
16+ Version int64
17+ AppliedAt time.Time
18+ Pending bool
19+ Source string
20+ }
21+ statusLines struct {
22+ contents []* statusLine
23+ order func (si , sj * statusLine ) bool
24+ }
25+ )
2226
2327// helpers so we can use pkg sort
24- func (s statusLines ) Len () int { return len (s ) }
25- func (s statusLines ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
28+ func (s statusLines ) Len () int { return len (s . contents ) }
29+ func (s statusLines ) Swap (i , j int ) { s . contents [i ], s . contents [j ] = s . contents [j ], s . contents [i ] }
2630func (s statusLines ) Less (i , j int ) bool {
27- lineI := s [i ]
28- lineJ := s [j ]
29- // Pending migrations always come later:
30- if lineI .Pending != lineJ .Pending {
31- // lineJ is pending ---> lineI goes first and return value true means lineI < lineJ
32- return lineJ .Pending
33- }
34- if ! lineI .AppliedAt .Equal (lineJ .AppliedAt ) {
35- return lineI .AppliedAt .Before (lineJ .AppliedAt )
36- }
37- if lineI .Version != lineJ .Version {
38- return lineI .Version < lineJ .Version
39- }
40- return lineI .Source < lineJ .Source
31+ lineI := s .contents [i ]
32+ lineJ := s .contents [j ]
33+ return s .order (lineI , lineJ )
4134}
4235
4336// Status prints the status of all migrations.
@@ -83,7 +76,7 @@ func StatusContext(ctx context.Context, db *sql.DB, dir string, opts ...OptionsF
8376 }
8477
8578 // Gather 1 status line for each migration in the FS, enriched with application timestamp from DB if applied:
86- var statusOutput statusLines
79+ var lines [] * statusLine
8780 for _ , fsM := range fsMigrations {
8881 line := statusLine {
8982 Version : fsM .Version ,
@@ -95,13 +88,14 @@ func StatusContext(ctx context.Context, db *sql.DB, dir string, opts ...OptionsF
9588 line .Pending = false
9689 line .AppliedAt = dbM .Timestamp
9790 }
98- statusOutput = append (statusOutput , & line )
91+ lines = append (lines , & line )
9992 }
100- sort .Sort (statusOutput )
93+
94+ sort .Sort (statusLines {contents : lines , order : lessByVersionOrSource })
10195
10296 log .Printf (" Applied At Migration" )
10397 log .Printf (" =======================================" )
104- for _ , migration := range statusOutput {
98+ for _ , migration := range lines {
10599 appliedAt := "Pending"
106100 if ! migration .Pending {
107101 appliedAt = migration .AppliedAt .Format (time .ANSIC )
@@ -111,3 +105,22 @@ func StatusContext(ctx context.Context, db *sql.DB, dir string, opts ...OptionsF
111105
112106 return nil
113107}
108+
109+ func lessByVersionOrSource (si , sj * statusLine ) bool {
110+ if si .Version != sj .Version {
111+ return si .Version < sj .Version
112+ }
113+ return si .Source < sj .Source
114+ }
115+
116+ func lessByAppliedAt (si , sj * statusLine ) bool {
117+ // Pending migrations always come later:
118+ if si .Pending != sj .Pending {
119+ // lineJ is pending ---> lineI goes first and return value true means lineI < lineJ
120+ return sj .Pending
121+ }
122+ if ! si .AppliedAt .Equal (sj .AppliedAt ) {
123+ return si .AppliedAt .Before (sj .AppliedAt )
124+ }
125+ return lessByVersionOrSource (si , sj )
126+ }
0 commit comments