From 57bd36d3dda1a9d630c6344e6d9448f684478ff6 Mon Sep 17 00:00:00 2001 From: l3uddz Date: Sat, 1 Feb 2020 22:06:26 +0000 Subject: [PATCH] change(missing): only remove media items when count > 0 --- cmd/missing.go | 22 +++++++++++++--------- cmd/root.go | 9 +++++---- database/count.go | 7 +++++++ 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 database/count.go diff --git a/cmd/missing.go b/cmd/missing.go index 74a4ece8..4b7d1828 100644 --- a/cmd/missing.go +++ b/cmd/missing.go @@ -39,7 +39,8 @@ var missingCmd = &cobra.Command{ defer database.Close() // retrieve missing records from pvr and stash in database - if flagRefreshCache { + existingItemsCount := database.GetItemsCount(lowerPvrName, "missing") + if flagRefreshCache || existingItemsCount < 1 { log.Infof("Retrieving missing media from %s: %q", capitalise.First(pvrConfig.Type), pvrName) missingRecords, err := pvr.GetWantedMissing() @@ -50,22 +51,24 @@ var missingCmd = &cobra.Command{ // stash missing media in database log.Debug("Stashing media items in database...") - if err := database.SetMediaItems(strings.ToLower(pvrName), "missing", missingRecords); err != nil { + if err := database.SetMediaItems(lowerPvrName, "missing", missingRecords); err != nil { log.WithError(err).Fatal("Failed stashing media items in database") } log.Info("Stashed media items") // remove media no longer missing - log.Debug("Removing media items from database that are no longer missing...") + if existingItemsCount >= 1 { + log.Debug("Removing media items from database that are no longer missing...") - removedItems, err := database.DeleteMissingItems(strings.ToLower(pvrName), "missing", missingRecords) - if err != nil { - log.WithError(err).Fatal("Failed removing media items from database that are no longer missing...") - } + removedItems, err := database.DeleteMissingItems(lowerPvrName, "missing", missingRecords) + if err != nil { + log.WithError(err).Fatal("Failed removing media items from database that are no longer missing...") + } - log.WithField("removed_items", removedItems). - Info("Removed media items from database that are no longer missing") + log.WithField("removed_items", removedItems). + Info("Removed media items from database that are no longer missing") + } //if err := database.SetMediaItems(pvrName, "missing", missingRecords); err != nil { // log.WithError(err).Fatal("Failed stashing media items in database...") @@ -149,6 +152,7 @@ func parseValidateInputs(args []string) error { // validate pvr exists in config pvrName = args[0] + lowerPvrName = strings.ToLower(pvrName) pvrConfig, ok = config.Config.Pvr[pvrName] if !ok { return fmt.Errorf("no pvr configuration found for: %q", pvrName) diff --git a/cmd/root.go b/cmd/root.go index 7ef915f3..4b3e6cf4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,10 +24,11 @@ var ( flagRefreshCache = false // Global vars - pvrName string - pvrConfig *config.Pvr - pvr pvrObj.Interface - log *logrus.Entry + pvrName string + lowerPvrName string + pvrConfig *config.Pvr + pvr pvrObj.Interface + log *logrus.Entry ) // rootCmd represents the base command when called without any subcommands diff --git a/database/count.go b/database/count.go new file mode 100644 index 00000000..bea661ba --- /dev/null +++ b/database/count.go @@ -0,0 +1,7 @@ +package database + +func GetItemsCount(pvrName string, wantedType string) int { + itemCount := 0 + db.Model(&MediaItem{}).Where("pvr_name = ? AND wanted_type = ?", pvrName, wantedType).Count(&itemCount) + return itemCount +}