26 lines
488 B
Go
26 lines
488 B
Go
package backfill
|
|
|
|
import (
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
)
|
|
|
|
// BackfillPhotoCounts recomputes the cached Likes and Comment counts on photos.
|
|
func BackfillPhotoCounts() error {
|
|
res := models.DB.Exec(`
|
|
UPDATE photos
|
|
SET like_count = (
|
|
SELECT count(id)
|
|
FROM likes
|
|
WHERE table_name='photos'
|
|
AND table_id=photos.id
|
|
),
|
|
comment_count = (
|
|
SELECT count(id)
|
|
FROM comments
|
|
WHERE table_name='photos'
|
|
AND table_id=photos.id
|
|
);
|
|
`)
|
|
return res.Error
|
|
}
|