Purge: Fix and simplify database queries #599 #1010

This commit is contained in:
Michael Mayer 2021-02-08 14:09:58 +01:00
parent 9e10ba6900
commit 1db5b36dde
3 changed files with 12 additions and 24 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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.