photoprism/internal/query/photo_counts.go
Michael Mayer 923093042a Purge: Improve photo counts and logging
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-11 07:23:23 +02:00

45 lines
1.4 KiB
Go

package query
import "github.com/jinzhu/gorm"
// UpdatePhotoCounts updates photos count in related tables as needed.
func UpdatePhotoCounts() error {
if err := Db().Table("places").
UpdateColumn("photo_count", gorm.Expr("(SELECT COUNT(*) FROM photos ph "+
"WHERE places.id = ph.place_id "+
"AND ph.photo_quality >= 0 "+
"AND ph.photo_private = 0 "+
"AND ph.deleted_at IS NULL)")).Error; err != nil {
return err
}
/*
CREATE OR REPLACE VIEW label_counts AS
SELECT label_id, SUM(photo_count) AS photo_count FROM (
(SELECT l.id AS label_id, COUNT(*) AS photo_count FROM labels l
JOIN photos_labels pl ON pl.label_id = l.id
JOIN photos ph ON pl.photo_id = ph.id
WHERE pl.uncertainty < 100
AND ph.photo_quality >= 0
AND ph.photo_private = 0
AND ph.deleted_at IS NULL GROUP BY l.id)
UNION ALL
(SELECT l.id AS label_id, COUNT(*) AS photo_count FROM labels l
JOIN categories c ON c.category_id = l.id
JOIN photos_labels pl ON pl.label_id = c.label_id
JOIN photos ph ON pl.photo_id = ph.id
WHERE pl.uncertainty < 100
AND ph.photo_quality >= 0
AND ph.photo_private = 0
AND ph.deleted_at IS NULL GROUP BY l.id)) counts GROUP BY label_id
*/
if err := Db().
Table("labels").
UpdateColumn("photo_count",
gorm.Expr("(SELECT photo_count FROM label_counts WHERE label_id = labels.id)")).Error; err != nil {
return err
}
return nil
}