diff --git a/internal/photoprism/files.go b/internal/photoprism/files.go index ffc72d7d6..7d3d44867 100644 --- a/internal/photoprism/files.go +++ b/internal/photoprism/files.go @@ -35,8 +35,8 @@ func (m *Files) Init() error { return nil } - if err := query.CleanDuplicates(); err != nil { - return fmt.Errorf("%s (clean duplicates)", err.Error()) + if err := query.PurgeDuplicates(); err != nil { + return fmt.Errorf("%s (purge duplicates)", err.Error()) } files, err := query.IndexedFiles() diff --git a/internal/photoprism/purge.go b/internal/photoprism/purge.go index 0a914dedf..7927d5dd9 100644 --- a/internal/photoprism/purge.go +++ b/internal/photoprism/purge.go @@ -242,7 +242,7 @@ func (w *Purge) Start(opt PurgeOptions) (purgedFiles map[string]bool, purgedPhot log.Info("purge: searching index for unassigned primary files") if err := query.FixPrimaries(); err != nil { - log.Errorf("purge: %s (find unassigned primaries)", err.Error()) + log.Errorf("purge: %s (fix primary files)", err.Error()) } log.Info("purge: searching index for hidden media files") @@ -251,16 +251,20 @@ func (w *Purge) Start(opt PurgeOptions) (purgedFiles map[string]bool, purgedPhot return purgedFiles, purgedPhotos, err } + if err := query.PurgeDuplicates(); err != nil { + log.Errorf("purge: %s (duplicates)", err) + } + + if err := query.PurgeUnusedCountries(); err != nil { + log.Errorf("purge: %s (countries)", err) + } + if err := query.UpdateMissingAlbumEntries(); err != nil { - log.Errorf("purge: %s (update albums)", err.Error()) + log.Errorf("purge: %s (album entries)", err) } if err := entity.UpdatePhotoCounts(); err != nil { - log.Errorf("purge: %s (update photo counts)", err) - } - - if err := query.CleanDuplicates(); err != nil { - log.Errorf("purge: %s (clean duplicates)", err) + log.Errorf("purge: %s (photo counts)", err) } return purgedFiles, purgedPhotos, nil diff --git a/internal/query/countries.go b/internal/query/countries.go new file mode 100644 index 000000000..b8a51a51f --- /dev/null +++ b/internal/query/countries.go @@ -0,0 +1,9 @@ +package query + +// PurgeUnusedCountries removes countries without any photos. +func PurgeUnusedCountries() error { + switch DbDialect() { + default: + return UnscopedDb().Exec(`DELETE FROM countries WHERE id NOT IN (SELECT photo_country FROM photos)`).Error + } +} diff --git a/internal/query/countries_test.go b/internal/query/countries_test.go new file mode 100644 index 000000000..94f859516 --- /dev/null +++ b/internal/query/countries_test.go @@ -0,0 +1,11 @@ +package query + +import ( + "testing" +) + +func TestPurgeUnusedCountries(t *testing.T) { + if err := PurgeUnusedCountries(); err != nil { + t.Fatal(err) + } +} diff --git a/internal/query/duplicates.go b/internal/query/duplicates.go index 1d69f7651..16c4fd2d6 100644 --- a/internal/query/duplicates.go +++ b/internal/query/duplicates.go @@ -6,8 +6,8 @@ import ( "github.com/photoprism/photoprism/internal/entity" ) -// CleanDuplicates removes all files from the duplicates table that don't exist in the files table. -func CleanDuplicates() error { +// PurgeDuplicates deletes all files from the duplicates table that don't exist in the files table. +func PurgeDuplicates() error { if err := UnscopedDb().Delete(entity.Duplicate{}, "file_hash IN (SELECT d.file_hash FROM duplicates d LEFT JOIN files f ON d.file_hash = f.file_hash AND f.file_missing = 0 AND f.deleted_at IS NULL WHERE f.file_hash IS NULL)").Error; err == nil { return nil } @@ -16,7 +16,7 @@ func CleanDuplicates() error { return UnscopedDb().Delete(entity.Duplicate{}, "file_hash IN (SELECT file_hash FROM (SELECT d.file_hash FROM duplicates d LEFT JOIN files f ON d.file_hash = f.file_hash AND f.file_missing = 0 AND f.deleted_at IS NULL WHERE f.file_hash IS NULL) AS tmp)").Error } -// Duplicates returns duplicate files in the range of limit and offset sorted by file name. +// Duplicates finds duplicate files in the range of limit and offset sorted by file name. func Duplicates(limit, offset int, pathName string) (files entity.Duplicates, err error) { if strings.HasPrefix(pathName, "/") { pathName = pathName[1:] diff --git a/internal/query/duplicates_test.go b/internal/query/duplicates_test.go index 52cda05d0..507f79c41 100644 --- a/internal/query/duplicates_test.go +++ b/internal/query/duplicates_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCleanDuplicates(t *testing.T) { +func TestPurgeDuplicates(t *testing.T) { fileName := "hd89e5yhb8p9h.jpg" if err := entity.AddDuplicate( @@ -27,7 +27,7 @@ func TestCleanDuplicates(t *testing.T) { t.Fatal(err) } - err := CleanDuplicates() + err := PurgeDuplicates() assert.NoError(t, err)