2020-05-11 11:01:28 +02:00
|
|
|
package entity
|
2020-05-09 11:00:22 +02:00
|
|
|
|
|
|
|
import "github.com/jinzhu/gorm"
|
|
|
|
|
|
|
|
// UpdatePhotoCounts updates photos count in related tables as needed.
|
|
|
|
func UpdatePhotoCounts() error {
|
2020-05-27 13:40:21 +02:00
|
|
|
// log.Info("index: updating photo counts")
|
2020-05-11 11:01:28 +02:00
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
if err := Db().Table("places").
|
2020-05-09 20:59:45 +02:00
|
|
|
UpdateColumn("photo_count", gorm.Expr("(SELECT COUNT(*) FROM photos ph "+
|
2020-05-25 19:10:44 +02:00
|
|
|
"WHERE places.place_uid = ph.place_uid "+
|
2020-05-09 20:59:45 +02:00
|
|
|
"AND ph.photo_quality >= 0 "+
|
|
|
|
"AND ph.photo_private = 0 "+
|
2020-05-09 11:00:22 +02:00
|
|
|
"AND ph.deleted_at IS NULL)")).Error; err != nil {
|
2020-05-09 20:59:45 +02:00
|
|
|
return err
|
2020-05-09 11:00:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 07:44:45 +02:00
|
|
|
/* See internal/entity/views.go
|
|
|
|
|
2020-05-11 08:57:58 +02:00
|
|
|
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
|
2020-05-11 07:23:23 +02:00
|
|
|
*/
|
|
|
|
|
2020-05-11 07:44:45 +02:00
|
|
|
/* TODO: Requires proper view support in TiDB
|
|
|
|
|
2020-05-11 07:23:23 +02:00
|
|
|
if err := Db().
|
|
|
|
Table("labels").
|
|
|
|
UpdateColumn("photo_count",
|
|
|
|
gorm.Expr("(SELECT photo_count FROM label_counts WHERE label_id = labels.id)")).Error; err != nil {
|
2020-05-11 07:44:45 +02:00
|
|
|
log.Warn(err)
|
|
|
|
} */
|
|
|
|
|
|
|
|
if err := Db().
|
|
|
|
Table("labels").
|
|
|
|
UpdateColumn("photo_count",
|
|
|
|
gorm.Expr(`(SELECT photo_count FROM (
|
|
|
|
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
|
|
|
|
) label_counts WHERE label_id = labels.id)`)).Error; err != nil {
|
2020-05-09 20:59:45 +02:00
|
|
|
return err
|
2020-05-09 11:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|