2020-08-19 11:35:38 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/manifoldco/promptui"
|
2021-11-28 13:52:27 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2020-08-19 11:35:38 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
)
|
|
|
|
|
2020-12-30 13:33:47 +01:00
|
|
|
// ResetCommand resets the index and removes sidecar files after confirmation.
|
2020-08-19 11:35:38 +02:00
|
|
|
var ResetCommand = cli.Command{
|
2021-11-28 13:52:27 +01:00
|
|
|
Name: "reset",
|
|
|
|
Usage: "Resets the index and removes generated sidecar files",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "index, i",
|
|
|
|
Usage: "reset index database only",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "trace, t",
|
|
|
|
Usage: "show trace logs for debugging",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "yes, y",
|
|
|
|
Usage: "assume \"yes\" as answer to all prompts and run non-interactively",
|
|
|
|
},
|
|
|
|
},
|
2020-08-19 11:35:38 +02:00
|
|
|
Action: resetAction,
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:33:47 +01:00
|
|
|
// resetAction resets the index and removes sidecar files after confirmation.
|
2020-08-19 11:35:38 +02:00
|
|
|
func resetAction(ctx *cli.Context) error {
|
|
|
|
conf := config.NewConfig(ctx)
|
2020-10-08 08:52:03 +02:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2020-08-19 11:35:38 +02:00
|
|
|
defer cancel()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
2020-08-19 11:35:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
defer conf.Shutdown()
|
2020-08-19 11:35:38 +02:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
entity.SetDbProvider(conf)
|
2020-08-19 15:36:31 +02:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if !ctx.Bool("yes") {
|
|
|
|
log.Warnf("This will delete and recreate your index database after confirmation")
|
2020-08-19 15:36:31 +02:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if !ctx.Bool("index") {
|
|
|
|
log.Warnf("You will be asked next if you also want to remove all JSON and YAML backup files")
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if ctx.Bool("trace") {
|
|
|
|
log.SetLevel(logrus.TraceLevel)
|
|
|
|
log.Infoln("reset: enabled trace mode")
|
2020-08-19 15:36:31 +02:00
|
|
|
}
|
2020-08-19 11:35:38 +02:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
resetIndex := ctx.Bool("yes")
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Show prompt?
|
|
|
|
if !resetIndex {
|
|
|
|
removeIndexPrompt := promptui.Prompt{
|
|
|
|
Label: "Delete and recreate index database?",
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if _, err := removeIndexPrompt.Run(); err == nil {
|
|
|
|
resetIndex = true
|
|
|
|
} else {
|
|
|
|
log.Infof("keeping index database")
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Reset index?
|
|
|
|
if resetIndex {
|
|
|
|
resetIndexDb(conf)
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Reset index only?
|
|
|
|
if ctx.Bool("index") {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
removeSidecarJsonPrompt := promptui.Prompt{
|
|
|
|
Label: "Delete all JSON metadata sidecar files?",
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if _, err := removeSidecarJsonPrompt.Run(); err == nil {
|
|
|
|
resetSidecarJson(conf)
|
2020-12-30 13:33:47 +01:00
|
|
|
} else {
|
2021-10-07 13:00:32 +02:00
|
|
|
log.Infof("keeping JSON metadata sidecar files")
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
2020-08-19 11:35:38 +02:00
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
removeSidecarYamlPrompt := promptui.Prompt{
|
2021-11-28 13:52:27 +01:00
|
|
|
Label: "Delete all YAML metadata backup files?",
|
2020-08-19 11:35:38 +02:00
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
if _, err := removeSidecarYamlPrompt.Run(); err == nil {
|
2021-11-28 13:52:27 +01:00
|
|
|
resetSidecarYaml(conf)
|
2020-12-21 14:05:02 +01:00
|
|
|
} else {
|
2021-10-07 13:00:32 +02:00
|
|
|
log.Infof("keeping YAML metadata backups")
|
2020-12-21 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
removeAlbumYamlPrompt := promptui.Prompt{
|
2021-11-28 13:52:27 +01:00
|
|
|
Label: "Delete all YAML album backup files?",
|
2020-12-21 14:05:02 +01:00
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
if _, err := removeAlbumYamlPrompt.Run(); err == nil {
|
2020-12-21 14:05:02 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(conf.AlbumsPath()) + "/**/*.yml")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Infof("%d YAML album backups will be removed", len(matches))
|
2020-12-21 14:05:02 +01:00
|
|
|
|
|
|
|
for _, name := range matches {
|
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
2021-10-07 13:00:32 +02:00
|
|
|
log.Infof("removed all YAML album backups [%s]", time.Since(start))
|
2020-08-19 11:35:38 +02:00
|
|
|
} else {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Infof("found no YAML album backup files")
|
2020-08-19 11:35:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Infof("keeping YAML album backup files")
|
2020-08-19 11:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
// resetIndexDb resets the index database schema.
|
|
|
|
func resetIndexDb(conf *config.Config) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
tables := entity.Entities
|
|
|
|
|
|
|
|
log.Infoln("dropping existing tables")
|
|
|
|
tables.Drop(conf.Db())
|
|
|
|
|
|
|
|
log.Infoln("restoring default schema")
|
|
|
|
entity.MigrateDb(true, false)
|
|
|
|
|
|
|
|
if conf.AdminPassword() != "" {
|
|
|
|
log.Infoln("restoring initial admin password")
|
|
|
|
entity.Admin.InitPassword(conf.AdminPassword())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("database reset completed in %s", time.Since(start))
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetSidecarJson removes generated JSON sidecar files.
|
|
|
|
func resetSidecarJson(conf *config.Config) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.json")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("reset: %s (find json sidecar files)", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
log.Infof("removing %d JSON metadata sidecar files", len(matches))
|
|
|
|
|
|
|
|
for _, name := range matches {
|
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
|
|
|
log.Infof("removed JSON metadata sidecar files [%s]", time.Since(start))
|
|
|
|
} else {
|
|
|
|
log.Infof("found no JSON metadata sidecar files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetSidecarYaml removes generated YAML sidecar files.
|
|
|
|
func resetSidecarYaml(conf *config.Config) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.yml")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("reset: %s (find yaml sidecar files)", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
log.Infof("%d YAML metadata backups will be removed", len(matches))
|
|
|
|
|
|
|
|
for _, name := range matches {
|
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
|
|
|
log.Infof("removed all YAML metadata backups [%s]", time.Since(start))
|
|
|
|
} else {
|
|
|
|
log.Infof("found no YAML metadata backups")
|
|
|
|
}
|
|
|
|
}
|