99 lines
2.4 KiB
Go
99 lines
2.4 KiB
Go
/*
|
|
* Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* @author Alex aka mailoman <alex@webz.asia>
|
|
* @copyright Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
|
|
* @since 08.01.2020
|
|
*
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose"
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
var statusCmd = &Command{
|
|
Name: "status",
|
|
Usage: "",
|
|
Summary: "dump the migration status for the current DB",
|
|
Help: `status extended help here...`,
|
|
Run: statusRun,
|
|
}
|
|
|
|
type StatusData struct {
|
|
Source string
|
|
Status string
|
|
}
|
|
|
|
func statusRun(cmd *Command, args ...string) {
|
|
|
|
conf, err := dbConfFromFlags()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// collect all migrations
|
|
min := int64(0)
|
|
max := int64((1 << 63) - 1)
|
|
migrations, e := goose.CollectMigrations(conf.MigrationsDir, min, max)
|
|
if e != nil {
|
|
log.Fatal(e)
|
|
}
|
|
|
|
db, e := goose.OpenDBFromDBConf(conf)
|
|
if e != nil {
|
|
log.Fatal("couldn't open DB:", e)
|
|
}
|
|
defer db.Close()
|
|
|
|
// must ensure that the version table exists if we're running on a pristine DB
|
|
if _, e := goose.EnsureDBVersion(conf, db); e != nil {
|
|
log.Fatal(e)
|
|
}
|
|
|
|
fmt.Printf("goose: status for environment '%v'\n", conf.Env)
|
|
fmt.Println(" Applied At Migration")
|
|
fmt.Println(" =======================================")
|
|
for _, m := range migrations {
|
|
printMigrationStatus(db, m.Version, filepath.Base(m.Source))
|
|
}
|
|
}
|
|
|
|
func printMigrationStatus(db *gorm.DB, version int64, script string) {
|
|
row := goose.MigrationRecord{}
|
|
result := db.Where("version_id = ?", version).Order("t_stamp desc").First(&row)
|
|
|
|
if result.Error != nil && !result.RecordNotFound() {
|
|
log.Fatal(result.Error)
|
|
}
|
|
|
|
var appliedAt string
|
|
|
|
if row.IsApplied {
|
|
appliedAt = row.TStamp.Format(time.ANSIC)
|
|
} else {
|
|
appliedAt = "Pending"
|
|
}
|
|
|
|
fmt.Printf(" %-24s -- %v\n", appliedAt, script)
|
|
}
|