diff --git a/internal/query/photo.go b/internal/query/photo.go index d33cb6657..45f7143b3 100644 --- a/internal/query/photo.go +++ b/internal/query/photo.go @@ -73,9 +73,7 @@ func PhotoPreloadByUID(photoUID string) (photo entity.Photo, err error) { func PhotosMissing(limit int, offset int) (entities entity.Photos, err error) { err = Db(). Select("photos.*"). - Joins("JOIN files a ON photos.id = a.photo_id "). - Joins("LEFT JOIN files b ON a.photo_id = b.photo_id AND a.id != b.id AND b.file_missing = 0 AND b.file_root = '/'"). - Where("a.file_missing = 1 AND b.id IS NULL"). + Where("id NOT IN (SELECT photo_id FROM files WHERE file_missing = 0 AND file_root = '/' AND deleted_at IS NULL)"). Where("photos.photo_type <> ?", entity.TypeText). Group("photos.id"). Limit(limit).Offset(offset).Find(&entities).Error @@ -85,16 +83,8 @@ func PhotosMissing(limit int, offset int) (entities entity.Photos, err error) { // ResetPhotoQuality resets the quality of photos without primary file to -1. func ResetPhotoQuality() error { - if err := Db().Table("photos"). - Where("id IN (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id)"). - Where("id IN (SELECT id FROM (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id) AS tmp)"). - Update("photo_quality", -1).Error; err == nil { - return nil - } - - // MySQL fallback, see https://github.com/photoprism/photoprism/issues/599 return Db().Table("photos"). - Where("id IN (SELECT id FROM (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id) AS tmp)"). + Where("id NOT IN (SELECT photo_id FROM files WHERE file_primary = 1 AND file_missing = 0 AND deleted_at IS NULL)"). Update("photo_quality", -1).Error } diff --git a/internal/query/photo_test.go b/internal/query/photo_test.go index e85aab9cc..482d5c99c 100644 --- a/internal/query/photo_test.go +++ b/internal/query/photo_test.go @@ -58,25 +58,28 @@ func TestPreloadPhotoByUID(t *testing.T) { } func TestMissingPhotos(t *testing.T) { - r, err := PhotosMissing(15, 0) + result, err := PhotosMissing(15, 0) + if err != nil { t.Fatal(err) } - assert.LessOrEqual(t, 1, len(r)) + + assert.LessOrEqual(t, 1, len(result)) } func TestResetPhotosQuality(t *testing.T) { - err := ResetPhotoQuality() - if err != nil { + if err := ResetPhotoQuality(); err != nil { t.Fatal(err) } } func TestPhotosCheck(t *testing.T) { result, err := PhotosCheck(10, 0, time.Second) + if err != nil { t.Fatal(err) } + assert.IsType(t, entity.Photos{}, result) } @@ -88,6 +91,4 @@ func TestOrphanPhotos(t *testing.T) { } assert.IsType(t, entity.Photos{}, result) - - t.Logf("ORPHANS: %#v", result) } diff --git a/internal/query/purge.go b/internal/query/purge.go index dda79073f..a60913a67 100644 --- a/internal/query/purge.go +++ b/internal/query/purge.go @@ -22,12 +22,9 @@ func PurgeOrphans() error { // PurgeOrphanDuplicates deletes all files from the duplicates table that don't exist in the files table. func PurgeOrphanDuplicates() 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 - } - - // MySQL fallback, see https://github.com/photoprism/photoprism/issues/599 - 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 + return UnscopedDb().Delete( + entity.Duplicate{}, + "file_hash NOT IN (SELECT file_hash FROM files WHERE file_missing = 0 AND deleted_at IS NULL)").Error } // PurgeOrphanCountries removes countries without any photos.