Migrations: Implement "photoprism migrations ls" command #2216

Lists the status of migrations. Changed "migrate" to "migrations run".
This commit is contained in:
Michael Mayer 2022-04-01 16:02:58 +02:00
parent ca4c2ae199
commit a61470dfc7
11 changed files with 285 additions and 88 deletions

View file

@ -128,7 +128,7 @@ rootshell: root-terminal
root-terminal:
docker-compose exec -u root photoprism bash
migrate:
go run cmd/photoprism/photoprism.go migrate
go run cmd/photoprism/photoprism.go migrations run
generate:
go generate ./pkg/... ./internal/...
go fmt ./pkg/... ./internal/...

View file

@ -72,7 +72,7 @@ func main() {
commands.MomentsCommand,
commands.ConvertCommand,
commands.ThumbsCommand,
commands.MigrateCommand,
commands.MigrationsCommand,
commands.BackupCommand,
commands.RestoreCommand,
commands.ResetCommand,

View file

@ -24,7 +24,7 @@ func configAction(ctx *cli.Context) error {
dbDriver := conf.DatabaseDriver()
fmt.Printf("%-25s VALUE\n", "NAME")
fmt.Printf("%-25s Value\n", "Name")
// Flags.
fmt.Printf("%-25s %t\n", "debug", conf.Debug())

View file

@ -23,7 +23,7 @@ func TestConfigCommand(t *testing.T) {
}
// Expected config command output.
assert.Contains(t, output, "NAME VALUE")
assert.Contains(t, output, "Name Value")
assert.Contains(t, output, "config-file")
assert.Contains(t, output, "darktable-cli")
assert.Contains(t, output, "originals-path")

View file

@ -1,75 +0,0 @@
package commands
import (
"context"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
)
// MigrateCommand registers the "migrate" CLI command.
var MigrateCommand = cli.Command{
Name: "migrate",
Usage: "Updates the index database schema",
ArgsUsage: "[migrations...]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "failed, f",
Usage: "run previously failed migrations",
},
cli.BoolFlag{
Name: "trace, t",
Usage: "show trace logs for debugging",
},
},
Action: migrateAction,
}
// migrateAction initializes and migrates the database.
func migrateAction(ctx *cli.Context) error {
start := time.Now()
conf := config.NewConfig(ctx)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err := conf.Init(); err != nil {
return err
}
defer conf.Shutdown()
if ctx.Bool("trace") {
log.SetLevel(logrus.TraceLevel)
log.Infoln("migrate: enabled trace mode")
}
runFailed := ctx.Bool("failed")
if runFailed {
log.Infoln("migrate: running previously failed migrations")
}
var ids []string
// Check argument for specific migrations to be run.
if migrations := strings.TrimSpace(ctx.Args().First()); migrations != "" {
ids = strings.Fields(migrations)
}
log.Infoln("migrating database schema...")
// Run migrations.
conf.MigrateDb(runFailed, ids)
elapsed := time.Since(start)
log.Infof("migration completed in %s", elapsed)
return nil
}

View file

@ -0,0 +1,156 @@
package commands
import (
"context"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/migrate"
)
// MigrationsCommand registers the "migrations" CLI command.
var MigrationsCommand = cli.Command{
Name: "migrations",
Usage: "Database schema migration subcommands",
Subcommands: []cli.Command{
{
Name: "ls",
Aliases: []string{"status", "show"},
Usage: "Lists the status of schema migrations",
ArgsUsage: "[migrations...]",
Action: migrationsStatusAction,
},
{
Name: "run",
Aliases: []string{"execute", "migrate"},
Usage: "Executes database schema migrations",
ArgsUsage: "[migrations...]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "failed, f",
Usage: "run previously failed migrations",
},
cli.BoolFlag{
Name: "trace, t",
Usage: "show trace logs for debugging",
},
},
Action: migrationsRunAction,
},
},
}
// migrationsStatusAction lists the status of schema migration.
func migrationsStatusAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err := conf.Init(); err != nil {
return err
}
defer conf.Shutdown()
var ids []string
// Check argument for specific migrations to be run.
if migrations := strings.TrimSpace(ctx.Args().First()); migrations != "" {
ids = strings.Fields(migrations)
}
db := conf.Db()
status, err := migrate.Status(db, ids)
if err != nil {
return err
}
// Display value names.
fmt.Printf("%-17s %-9s %-21s %-21s %s\n", "ID", "Dialect", "Started At", "Finished At", "Status")
// Show migrations.
for _, m := range status {
var started, finished, info string
if m.StartedAt.IsZero() {
started = "-"
} else {
started = m.StartedAt.Format("2006-01-02 15:04:05")
}
if m.Finished() {
finished = m.FinishedAt.Format("2006-01-02 15:04:05")
} else {
finished = "-"
}
if m.Error != "" {
info = m.Error
} else if m.Finished() {
info = "OK"
} else if m.StartedAt.IsZero() {
info = "-"
} else if m.Repeat(false) {
info = "Repeat"
} else {
info = "Running?"
}
fmt.Printf("%-17s %-9s %-21s %-21s %s\n", m.ID, m.Dialect, started, finished, info)
}
return nil
}
// migrationsRunAction executes database schema migrations.
func migrationsRunAction(ctx *cli.Context) error {
start := time.Now()
conf := config.NewConfig(ctx)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err := conf.Init(); err != nil {
return err
}
defer conf.Shutdown()
if ctx.Bool("trace") {
log.SetLevel(logrus.TraceLevel)
log.Infoln("migrate: enabled trace mode")
}
runFailed := ctx.Bool("failed")
if runFailed {
log.Infoln("migrate: running previously failed migrations")
}
var ids []string
// Check argument for specific migrations to be run.
if migrations := strings.TrimSpace(ctx.Args().First()); migrations != "" {
ids = strings.Fields(migrations)
}
log.Infoln("migrating database schema...")
// Run migrations.
conf.MigrateDb(runFailed, ids)
elapsed := time.Since(start)
log.Infof("migration completed in %s", elapsed)
return nil
}

View file

@ -49,7 +49,7 @@ func startAction(ctx *cli.Context) error {
service.SetConfig(conf)
if ctx.IsSet("config") {
fmt.Printf("NAME VALUE\n")
fmt.Printf("Name Value\n")
fmt.Printf("detach-server %t\n", conf.DetachServer())
fmt.Printf("http-host %s\n", conf.HttpHost())

View file

@ -6,7 +6,7 @@ import (
"github.com/jinzhu/gorm"
)
// Auto automatically migrates the database provided.
// Auto automatically migrates the schema of the database passed as argument.
func Auto(db *gorm.DB, runFailed bool, ids []string) error {
if db == nil {
return fmt.Errorf("migrate: database connection required")

View file

@ -23,20 +23,71 @@ func (Migration) TableName() string {
return "migrations"
}
// Fail marks the migration as failed by adding an error message.
// Finished tests if the migration has been finished yet.
func (m *Migration) Finished() bool {
if m.FinishedAt == nil {
return false
}
return !m.FinishedAt.IsZero()
}
// RunDuration returns the run duration of started migrations.
func (m *Migration) RunDuration() time.Duration {
if m.Error != "" || m.StartedAt.IsZero() {
return time.Duration(0)
}
if m.Finished() {
return m.FinishedAt.UTC().Sub(m.StartedAt.UTC())
}
return time.Now().UTC().Sub(m.StartedAt.UTC())
}
// Repeat tests if the migration should be repeated.
func (m *Migration) Repeat(runFailed bool) bool {
if runFailed && m.Error != "" {
return true
}
// Don't repeat if finished.
if m.Finished() {
return false
}
// Repeat not started yet.
if m.StartedAt.IsZero() {
return true
}
// Repeat if "running" for more than 60 minutes.
return m.RunDuration().Minutes() >= 60
}
// Fail marks the migration as failed by adding an error message and removing the FinishedAt timestamp.
func (m *Migration) Fail(err error, db *gorm.DB) {
if err == nil {
return
}
m.Error = err.Error()
m.FinishedAt = nil
db.Model(m).Updates(Values{"Error": m.Error})
if err.Error() == "" {
m.Error = "unknown error"
} else {
m.Error = err.Error()
}
db.Model(m).Updates(Values{"FinishedAt": m.FinishedAt, "Error": m.Error})
}
// Finish updates the FinishedAt timestamp when the migration was successful.
// Finish updates the FinishedAt timestamp and removes the error message when the migration was successful.
func (m *Migration) Finish(db *gorm.DB) error {
return db.Model(m).Updates(Values{"FinishedAt": time.Now().UTC()}).Error
finished := time.Now().UTC().Round(time.Second)
m.FinishedAt = &finished
m.Error = ""
return db.Model(m).Updates(Values{"FinishedAt": m.FinishedAt, "Error": m.Error}).Error
}
// Execute runs the migration.

View file

@ -71,8 +71,8 @@ func (m *Migrations) Start(db *gorm.DB, runFailed bool, ids []string) {
// Already executed?
if done, ok := executed[migration.ID]; ok {
// Try to run failed migrations again?
if (!runFailed || done.Error == "") && !list.Contains(ids, migration.ID) {
// Repeat?
if !done.Repeat(runFailed) && !list.Contains(ids, migration.ID) {
log.Debugf("migrate: %s skipped", migration.ID)
continue
}

View file

@ -0,0 +1,65 @@
package migrate
import (
"fmt"
"github.com/dustin/go-humanize/english"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/pkg/list"
)
// Status returns the current status of schema migrations.
func Status(db *gorm.DB, ids []string) (status Migrations, err error) {
status = Migrations{}
if db == nil {
return status, fmt.Errorf("migrate: database connection required")
}
name := db.Dialect().GetName()
if name == "" {
return status, fmt.Errorf("migrate: database has no dialect name")
}
if err := db.AutoMigrate(&Migration{}).Error; err != nil {
return status, fmt.Errorf("migrate: %s (create migrations table)", err)
}
migrations, ok := Dialects[name]
if !ok && len(migrations) == 0 {
return status, fmt.Errorf("migrate: no migrations found for %s", name)
}
// Find previously executed migrations.
executed := Existing(db)
if prev := len(executed); prev == 0 {
log.Infof("migrate: no previously executed migrations")
} else {
log.Debugf("migrate: found %s", english.Plural(len(executed), "previous migration", "previous migrations"))
}
for _, migration := range migrations {
// Excluded?
if list.Excludes(ids, migration.ID) {
continue
}
// Already executed?
if done, ok := executed[migration.ID]; ok {
migration.Dialect = done.Dialect
migration.Error = done.Error
migration.Source = done.Source
migration.StartedAt = done.StartedAt
migration.FinishedAt = done.FinishedAt
status = append(status, migration)
} else {
// Should not happen.
status = append(status, migration)
}
}
return status, nil
}