mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
38b8c9cc58
#15555 Probably the best way to review this is commit by commit: - First commit does the actual moving. - Second commit fixes golangci-lint issues (in the least effort way to avoid refactoring or rearrangement of some of the code). - Third commit moves a printf to before the migration step is executed. In the past some customers hitting migration issues (like migration steps hanging or taking long to execute) and wanted to know which one was it. The only way to know was to look at the repository and looking for the next migration after the last one logged. Checks: - [X] Manual QA for all new/changed functionality Manual tests: - `make fleet && make db-reset`. - Adding a new migration via `make migration name=Foobar` and then running `./build/fleet prepare db`. - Enrolling a new device to Fleet (smoke test).
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package goose
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
func (c *Client) Status(db *sql.DB, dir string) error {
|
|
// collect all migrations
|
|
migrations, err := c.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 := c.GetDBVersion(db); err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println(" Applied At Migration")
|
|
fmt.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 goose_db_version WHERE version_id=%d ORDER BY tstamp DESC LIMIT 1", 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"
|
|
}
|
|
|
|
fmt.Printf(" %-24s -- %v\n", appliedAt, script)
|
|
}
|