forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
50 lines (40 loc) · 1.18 KB
/
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
package goose
import (
"database/sql"
"fmt"
"path/filepath"
"time"
)
// Status prints the status of all migrations.
func Status(db *sql.DB, dir string) error {
// collect all migrations
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
}
// must ensure that the version table exists if we're running on a pristine DB
if _, err := EnsureDBVersion(db); err != nil {
return err
}
log.Println(" Applied At Migration")
log.Println(" =======================================")
for _, migration := range migrations {
printMigrationStatus(db, migration.Version, filepath.Base(migration.Source))
}
return nil
}
func printMigrationStatus(db *sql.DB, version int64, script string) {
var row MigrationRecord
q := fmt.Sprintf("SELECT tstamp, is_applied FROM %s WHERE version_id=%d ORDER BY tstamp DESC LIMIT 1", TableName(), version)
e := db.QueryRow(q).Scan(&row.TStamp, &row.IsApplied)
if e != nil && e != sql.ErrNoRows {
log.Fatal(e)
}
var appliedAt string
if row.IsApplied {
appliedAt = row.TStamp.Format(time.ANSIC)
} else {
appliedAt = "Pending"
}
log.Printf(" %-24s -- %v\n", appliedAt, script)
}