fastpastebin/vendor/github.com/pressly/goose/status.go

55 lines
1.4 KiB
Go
Raw Normal View History

2018-04-30 18:42:17 +05:00
package goose
import (
"database/sql"
"fmt"
"path/filepath"
"time"
2019-10-13 13:55:38 +05:00
"github.com/pkg/errors"
2018-04-30 18:42:17 +05:00
)
// 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 {
2019-10-13 13:55:38 +05:00
return errors.Wrap(err, "failed to collect migrations")
2018-04-30 18:42:17 +05:00
}
// must ensure that the version table exists if we're running on a pristine DB
if _, err := EnsureDBVersion(db); err != nil {
2019-10-13 13:55:38 +05:00
return errors.Wrap(err, "failed to ensure DB version")
2018-04-30 18:42:17 +05:00
}
2019-10-13 13:55:38 +05:00
log.Println(" Applied At Migration")
log.Println(" =======================================")
2018-04-30 18:42:17 +05:00
for _, migration := range migrations {
2019-10-13 13:55:38 +05:00
if err := printMigrationStatus(db, migration.Version, filepath.Base(migration.Source)); err != nil {
return errors.Wrap(err, "failed to print status")
}
2018-04-30 18:42:17 +05:00
}
return nil
}
2019-10-13 13:55:38 +05:00
func printMigrationStatus(db *sql.DB, version int64, script string) error {
q := fmt.Sprintf("SELECT tstamp, is_applied FROM %s WHERE version_id=%d ORDER BY tstamp DESC LIMIT 1", TableName(), version)
2018-04-30 18:42:17 +05:00
2019-10-13 13:55:38 +05:00
var row MigrationRecord
err := db.QueryRow(q).Scan(&row.TStamp, &row.IsApplied)
if err != nil && err != sql.ErrNoRows {
return errors.Wrap(err, "failed to query the latest migration")
2018-04-30 18:42:17 +05:00
}
var appliedAt string
if row.IsApplied {
appliedAt = row.TStamp.Format(time.ANSIC)
} else {
appliedAt = "Pending"
}
2019-10-13 13:55:38 +05:00
log.Printf(" %-24s -- %v\n", appliedAt, script)
return nil
2018-04-30 18:42:17 +05:00
}