From a43f8be23195e00dcfe116b85e3c09fe477f44db Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 7 Dec 2020 17:51:35 +0100 Subject: [PATCH] Stacks: Repair merge query #681 #667 #593 --- internal/entity/photo.go | 6 +++--- internal/photoprism/index_mediafile.go | 22 +++++++++------------- internal/query/photo.go | 6 +++--- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/internal/entity/photo.go b/internal/entity/photo.go index 86c288535..7223eaa3c 100644 --- a/internal/entity/photo.go +++ b/internal/entity/photo.go @@ -1023,13 +1023,13 @@ func (m *Photo) Stack(meta, uuid bool) (identical Photos, err error) { switch { case meta && uuid: - stmt.Where("(taken_at = ? AND taken_src = 'meta' AND cell_id = ? AND camera_serial = ? AND camera_id = ?) OR (uuid <> '' AND uuid = ?)", + stmt = stmt.Where("(taken_at = ? AND taken_src = 'meta' AND cell_id = ? AND camera_serial = ? AND camera_id = ?) OR (uuid <> '' AND uuid = ?)", m.TakenAt, m.CellID, m.CameraSerial, m.CameraID, m.UUID) case meta: - stmt.Where("taken_at = ? AND taken_src = 'meta' AND cell_id = ? AND camera_serial = ? AND camera_id = ?", + stmt = stmt.Where("taken_at = ? AND taken_src = 'meta' AND cell_id = ? AND camera_serial = ? AND camera_id = ?", m.TakenAt, m.CellID, m.CameraSerial, m.CameraID) case uuid: - stmt.Where("uuid <> '' AND uuid = ?", m.UUID) + stmt = stmt.Where("uuid <> '' AND uuid = ?", m.UUID) } if err := stmt.Find(&identical).Error; err != nil { diff --git a/internal/photoprism/index_mediafile.go b/internal/photoprism/index_mediafile.go index 98126e7ee..9a32c2a7c 100644 --- a/internal/photoprism/index_mediafile.go +++ b/internal/photoprism/index_mediafile.go @@ -161,25 +161,21 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName string) ( photoQuery = entity.UnscopedDb().First(&photo, "photo_path = ? AND photo_name = ?", filePath, fileBase) // Stack file based on matching location and time metadata? - if Config().Settings().StackMeta() { - if photoQuery.Error != nil && m.MetaData().HasTimeAndPlace() { - metaData = m.MetaData() - photoQuery = entity.UnscopedDb().First(&photo, "photo_lat = ? AND photo_lng = ? AND taken_at = ? AND taken_src = 'meta' AND camera_serial = ?", metaData.Lat, metaData.Lng, metaData.TakenAt, metaData.CameraSerial) + if photoQuery.Error != nil && Config().Settings().StackMeta() && m.MetaData().HasTimeAndPlace() { + metaData = m.MetaData() + photoQuery = entity.UnscopedDb().First(&photo, "photo_lat = ? AND photo_lng = ? AND taken_at = ? AND taken_src = 'meta' AND camera_serial = ?", metaData.Lat, metaData.Lng, metaData.TakenAt, metaData.CameraSerial) - if photoQuery.Error == nil { - fileStacked = true - } + if photoQuery.Error == nil { + fileStacked = true } } // Stack file based on the same unique ID? - if Config().Settings().StackUUID() { - if photoQuery.Error != nil && m.MetaData().HasDocumentID() { - photoQuery = entity.UnscopedDb().First(&photo, "uuid <> '' AND uuid = ?", m.MetaData().DocumentID) + if photoQuery.Error != nil && Config().Settings().StackUUID() && m.MetaData().HasDocumentID() { + photoQuery = entity.UnscopedDb().First(&photo, "uuid <> '' AND uuid = ?", m.MetaData().DocumentID) - if photoQuery.Error == nil { - fileStacked = true - } + if photoQuery.Error == nil { + fileStacked = true } } } else { diff --git a/internal/query/photo.go b/internal/query/photo.go index 9bcfbe62a..0f5018b66 100644 --- a/internal/query/photo.go +++ b/internal/query/photo.go @@ -121,7 +121,7 @@ func MatchingPhotos(meta, uuid bool) (entities entity.Photos, err error) { switch { case meta && uuid: - stmt.Joins(`JOIN photos dup ON photos.id < dup.id + stmt = stmt.Joins(`JOIN photos dup ON photos.id < dup.id AND photos.photo_single = 0 AND dup.photo_single = 0 AND photos.deleted_at IS NULL AND dup.deleted_at IS NULL AND ((photos.taken_src = 'meta' AND dup.taken_src = 'meta' @@ -132,7 +132,7 @@ func MatchingPhotos(meta, uuid bool) (entities entity.Photos, err error) { AND photos.camera_serial = dup.camera_serial) OR (photos.uuid <> '' AND photos.uuid = dup.uuid))`) case meta: - stmt.Joins(`JOIN photos dup ON photos.id < dup.id + stmt = stmt.Joins(`JOIN photos dup ON photos.id < dup.id AND photos.photo_single = 0 AND dup.photo_single = 0 AND photos.deleted_at IS NULL AND dup.deleted_at IS NULL @@ -143,7 +143,7 @@ func MatchingPhotos(meta, uuid bool) (entities entity.Photos, err error) { AND photos.camera_id = dup.camera_id AND photos.camera_serial = dup.camera_serial))`) case uuid: - stmt.Joins(`JOIN photos dup ON photos.id < dup.id + stmt = stmt.Joins(`JOIN photos dup ON photos.id < dup.id AND photos.photo_single = 0 AND dup.photo_single = 0 AND photos.deleted_at IS NULL AND dup.deleted_at IS NULL AND (photos.uuid <> '' AND photos.uuid = dup.uuid)`)