2021-08-30 18:58:27 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-23 23:46:17 +02:00
|
|
|
"strings"
|
2021-09-06 11:19:18 +02:00
|
|
|
"time"
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-08-30 18:58:27 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2021-10-01 00:05:49 +02:00
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-10-06 11:50:48 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2021-08-30 18:58:27 +02:00
|
|
|
)
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateAlbumDefaultCovers updates default album cover thumbs.
|
|
|
|
func UpdateAlbumDefaultCovers() (err error) {
|
2021-12-09 02:33:41 +01:00
|
|
|
mutex.Index.Lock()
|
|
|
|
defer mutex.Index.Unlock()
|
2021-10-06 11:50:48 +02:00
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
var res *gorm.DB
|
|
|
|
|
2021-10-01 19:23:08 +02:00
|
|
|
condition := gorm.Expr("album_type = ? AND thumb_src = ?", entity.AlbumDefault, entity.SrcAuto)
|
2021-10-01 15:25:16 +02:00
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
|
|
|
res = Db().Exec(`UPDATE albums LEFT JOIN (
|
|
|
|
SELECT p2.album_uid, f.file_hash FROM files f, (
|
|
|
|
SELECT pa.album_uid, max(p.id) AS photo_id FROM photos p
|
2021-10-01 19:23:08 +02:00
|
|
|
JOIN photos_albums pa ON pa.photo_uid = p.photo_uid AND pa.hidden = 0 AND pa.missing = 0
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-05 18:58:52 +02:00
|
|
|
GROUP BY pa.album_uid) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-10-01 00:05:49 +02:00
|
|
|
) b ON b.album_uid = albums.album_uid
|
2021-10-01 15:25:16 +02:00
|
|
|
SET thumb = b.file_hash WHERE ?`, condition)
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2021-10-01 00:05:49 +02:00
|
|
|
res = Db().Table(entity.Album{}.TableName()).
|
|
|
|
UpdateColumn("thumb", gorm.Expr(`(
|
2021-10-01 15:25:16 +02:00
|
|
|
SELECT f.file_hash FROM files f
|
2021-10-01 19:23:08 +02:00
|
|
|
JOIN photos_albums pa ON pa.album_uid = albums.album_uid AND pa.photo_uid = f.photo_uid AND pa.hidden = 0 AND pa.missing = 0
|
2021-10-01 15:25:16 +02:00
|
|
|
JOIN photos p ON p.id = f.photo_id AND p.photo_private = 0 AND p.deleted_at IS NULL AND p.photo_quality > 0
|
2021-10-05 18:58:52 +02:00
|
|
|
WHERE f.deleted_at IS NULL AND f.file_missing = 0 AND f.file_hash <> '' AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-08-30 18:58:27 +02:00
|
|
|
ORDER BY p.taken_at DESC LIMIT 1
|
2021-10-01 15:25:16 +02:00
|
|
|
) WHERE ?`, condition))
|
2021-10-01 00:05:49 +02:00
|
|
|
default:
|
2021-10-01 16:34:29 +02:00
|
|
|
log.Warnf("sql: unsupported dialect %s", DbDialect())
|
2021-10-01 00:05:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = res.Error
|
2021-09-06 11:19:18 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
if err == nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Debugf("covers: updated %s [%s]", english.Plural(int(res.RowsAffected), "album", "albums"), time.Since(start))
|
2021-09-23 23:46:17 +02:00
|
|
|
} else if strings.Contains(err.Error(), "Error 1054") {
|
2021-10-01 16:44:50 +02:00
|
|
|
log.Errorf("covers: failed updating albums, potentially incompatible database version")
|
2021-09-24 22:46:03 +02:00
|
|
|
log.Errorf("%s see https://jira.mariadb.org/browse/MDEV-25362", err)
|
2021-09-23 23:46:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 11:19:18 +02:00
|
|
|
|
|
|
|
return err
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateAlbumFolderCovers updates folder album cover thumbs.
|
|
|
|
func UpdateAlbumFolderCovers() (err error) {
|
2021-12-09 02:33:41 +01:00
|
|
|
mutex.Index.Lock()
|
|
|
|
defer mutex.Index.Unlock()
|
2021-10-06 11:50:48 +02:00
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
var res *gorm.DB
|
|
|
|
|
2021-10-01 19:23:08 +02:00
|
|
|
condition := gorm.Expr("album_type = ? AND thumb_src = ?", entity.AlbumFolder, entity.SrcAuto)
|
2021-10-01 15:25:16 +02:00
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
|
|
|
res = Db().Exec(`UPDATE albums LEFT JOIN (
|
|
|
|
SELECT p2.photo_path, f.file_hash FROM files f, (
|
|
|
|
SELECT p.photo_path, max(p.id) AS photo_id FROM photos p
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-05 18:58:52 +02:00
|
|
|
GROUP BY p.photo_path) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-10-01 00:05:49 +02:00
|
|
|
) b ON b.photo_path = albums.album_path
|
2021-10-01 15:25:16 +02:00
|
|
|
SET thumb = b.file_hash WHERE ?`, condition)
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2021-10-01 00:05:49 +02:00
|
|
|
res = Db().Table(entity.Album{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
|
|
|
|
SELECT f.file_hash FROM files f,(
|
|
|
|
SELECT p.photo_path, max(p.id) AS photo_id FROM photos p
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-01 00:05:49 +02:00
|
|
|
GROUP BY p.photo_path
|
|
|
|
) b
|
2021-10-05 18:58:52 +02:00
|
|
|
WHERE f.photo_id = b.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-10-01 00:05:49 +02:00
|
|
|
AND b.photo_path = albums.album_path LIMIT 1)
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE ?`, condition))
|
2021-10-01 00:05:49 +02:00
|
|
|
default:
|
2021-10-01 16:34:29 +02:00
|
|
|
log.Warnf("sql: unsupported dialect %s", DbDialect())
|
2021-10-01 00:05:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = res.Error
|
2021-09-06 11:19:18 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
if err == nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Debugf("covers: updated %s [%s]", english.Plural(int(res.RowsAffected), "folder", "folders"), time.Since(start))
|
2021-09-23 23:46:17 +02:00
|
|
|
} else if strings.Contains(err.Error(), "Error 1054") {
|
2021-10-01 16:44:50 +02:00
|
|
|
log.Errorf("covers: failed updating folders, potentially incompatible database version")
|
2021-09-24 22:46:03 +02:00
|
|
|
log.Errorf("%s see https://jira.mariadb.org/browse/MDEV-25362", err)
|
2021-09-23 23:46:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 11:19:18 +02:00
|
|
|
|
|
|
|
return err
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateAlbumMonthCovers updates month album cover thumbs.
|
|
|
|
func UpdateAlbumMonthCovers() (err error) {
|
2021-12-09 02:33:41 +01:00
|
|
|
mutex.Index.Lock()
|
|
|
|
defer mutex.Index.Unlock()
|
2021-10-06 11:50:48 +02:00
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
var res *gorm.DB
|
2021-09-06 11:19:18 +02:00
|
|
|
|
2021-10-01 19:23:08 +02:00
|
|
|
condition := gorm.Expr("album_type = ? AND thumb_src = ?", entity.AlbumMonth, entity.SrcAuto)
|
2021-10-01 15:25:16 +02:00
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
2021-10-01 00:05:49 +02:00
|
|
|
res = Db().Exec(`UPDATE albums LEFT JOIN (
|
|
|
|
SELECT p2.photo_year, p2.photo_month, f.file_hash FROM files f, (
|
|
|
|
SELECT p.photo_year, p.photo_month, max(p.id) AS photo_id FROM photos p
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-05 18:58:52 +02:00
|
|
|
GROUP BY p.photo_year, p.photo_month) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-10-01 00:05:49 +02:00
|
|
|
) b ON b.photo_year = albums.album_year AND b.photo_month = albums.album_month
|
2021-10-01 15:25:16 +02:00
|
|
|
SET thumb = b.file_hash WHERE ?`, condition)
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2021-10-01 00:05:49 +02:00
|
|
|
res = Db().Table(entity.Album{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
|
|
|
|
SELECT f.file_hash FROM files f,(
|
|
|
|
SELECT p.photo_year, p.photo_month, max(p.id) AS photo_id FROM photos p
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-01 00:05:49 +02:00
|
|
|
GROUP BY p.photo_year, p.photo_month
|
|
|
|
) b
|
2021-10-05 18:58:52 +02:00
|
|
|
WHERE f.photo_id = b.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-10-01 00:05:49 +02:00
|
|
|
AND b.photo_year = albums.album_year AND b.photo_month = albums.album_month LIMIT 1)
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE ?`, condition))
|
2021-09-06 11:19:18 +02:00
|
|
|
default:
|
2021-10-01 16:34:29 +02:00
|
|
|
log.Warnf("sql: unsupported dialect %s", DbDialect())
|
2021-09-06 11:19:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-01 00:05:49 +02:00
|
|
|
|
|
|
|
err = res.Error
|
2021-09-23 23:46:17 +02:00
|
|
|
|
|
|
|
if err == nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Debugf("covers: updated %s [%s]", english.Plural(int(res.RowsAffected), "month", "months"), time.Since(start))
|
2021-09-23 23:46:17 +02:00
|
|
|
} else if strings.Contains(err.Error(), "Error 1054") {
|
2021-10-01 16:44:50 +02:00
|
|
|
log.Errorf("covers: failed updating calendar, potentially incompatible database version")
|
2021-09-24 22:46:03 +02:00
|
|
|
log.Errorf("%s see https://jira.mariadb.org/browse/MDEV-25362", err)
|
2021-09-23 23:46:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 11:19:18 +02:00
|
|
|
|
|
|
|
return err
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateAlbumCovers updates album cover thumbs.
|
|
|
|
func UpdateAlbumCovers() (err error) {
|
2021-08-30 18:58:27 +02:00
|
|
|
// Update Default Albums.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err = UpdateAlbumDefaultCovers(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update Folder Albums.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err = UpdateAlbumFolderCovers(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update Monthly Albums.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err = UpdateAlbumMonthCovers(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateLabelCovers updates label cover thumbs.
|
|
|
|
func UpdateLabelCovers() (err error) {
|
2021-12-09 02:33:41 +01:00
|
|
|
mutex.Index.Lock()
|
|
|
|
defer mutex.Index.Unlock()
|
2021-10-06 11:50:48 +02:00
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
var res *gorm.DB
|
|
|
|
|
2021-10-01 19:23:08 +02:00
|
|
|
condition := gorm.Expr("thumb_src = ?", entity.SrcAuto)
|
2021-10-01 15:25:16 +02:00
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
2021-10-01 15:25:16 +02:00
|
|
|
res = Db().Exec(`UPDATE labels LEFT JOIN (
|
2021-10-01 00:05:49 +02:00
|
|
|
SELECT p2.label_id, f.file_hash FROM files f, (
|
|
|
|
SELECT pl.label_id as label_id, max(p.id) AS photo_id FROM photos p
|
|
|
|
JOIN photos_labels pl ON pl.photo_id = p.id AND pl.uncertainty < 100
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-01 00:05:49 +02:00
|
|
|
GROUP BY pl.label_id
|
|
|
|
UNION
|
|
|
|
SELECT c.category_id as label_id, max(p.id) AS photo_id FROM photos p
|
|
|
|
JOIN photos_labels pl ON pl.photo_id = p.id AND pl.uncertainty < 100
|
|
|
|
JOIN categories c ON c.label_id = pl.label_id
|
2021-10-01 15:25:16 +02:00
|
|
|
WHERE p.photo_quality > 0 AND p.photo_private = 0 AND p.deleted_at IS NULL
|
2021-10-01 00:05:49 +02:00
|
|
|
GROUP BY c.category_id
|
2021-10-05 18:58:52 +02:00
|
|
|
) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg' AND f.file_missing = 0
|
2021-10-01 00:05:49 +02:00
|
|
|
) b ON b.label_id = labels.id
|
2021-10-01 15:25:16 +02:00
|
|
|
SET thumb = b.file_hash WHERE ?`, condition)
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2021-10-01 00:05:49 +02:00
|
|
|
res = Db().Table(entity.Label{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
|
|
|
|
SELECT f.file_hash FROM files f
|
2021-08-30 18:58:27 +02:00
|
|
|
JOIN photos_labels pl ON pl.label_id = labels.id AND pl.photo_id = f.photo_id AND pl.uncertainty < 100
|
2021-10-01 15:25:16 +02:00
|
|
|
JOIN photos p ON p.id = f.photo_id AND p.photo_private = 0 AND p.deleted_at IS NULL AND p.photo_quality > 0
|
2021-10-05 18:58:52 +02:00
|
|
|
WHERE f.deleted_at IS NULL AND f.file_hash <> '' AND f.file_missing = 0 AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-08-30 18:58:27 +02:00
|
|
|
ORDER BY p.photo_quality DESC, pl.uncertainty ASC, p.taken_at DESC LIMIT 1
|
2021-10-01 15:25:16 +02:00
|
|
|
) WHERE ?`, condition))
|
2021-09-06 11:19:18 +02:00
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
if res.Error == nil {
|
2021-10-01 15:25:16 +02:00
|
|
|
catRes := Db().Table(entity.Label{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
|
2021-09-03 16:14:09 +02:00
|
|
|
SELECT f.file_hash FROM files f
|
2021-08-30 18:58:27 +02:00
|
|
|
JOIN photos_labels pl ON pl.photo_id = f.photo_id AND pl.uncertainty < 100
|
|
|
|
JOIN categories c ON c.label_id = pl.label_id AND c.category_id = labels.id
|
2021-10-01 15:25:16 +02:00
|
|
|
JOIN photos p ON p.id = f.photo_id AND p.photo_private = 0 AND p.deleted_at IS NULL AND p.photo_quality > 0
|
2021-10-05 18:58:52 +02:00
|
|
|
WHERE f.deleted_at IS NULL AND f.file_hash <> '' AND f.file_missing = 0 AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
|
2021-08-30 18:58:27 +02:00
|
|
|
ORDER BY p.photo_quality DESC, pl.uncertainty ASC, p.taken_at DESC LIMIT 1
|
2021-10-01 00:05:49 +02:00
|
|
|
) WHERE thumb IS NULL`))
|
|
|
|
|
|
|
|
res.RowsAffected += catRes.RowsAffected
|
|
|
|
}
|
|
|
|
default:
|
2021-10-01 16:34:29 +02:00
|
|
|
log.Warnf("sql: unsupported dialect %s", DbDialect())
|
2021-10-01 00:05:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = res.Error
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
if err == nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Debugf("covers: updated %s [%s]", english.Plural(int(res.RowsAffected), "label", "labels"), time.Since(start))
|
2021-09-23 23:46:17 +02:00
|
|
|
} else if strings.Contains(err.Error(), "Error 1054") {
|
2021-10-01 16:44:50 +02:00
|
|
|
log.Errorf("covers: failed updating labels, potentially incompatible database version")
|
2021-09-24 22:46:03 +02:00
|
|
|
log.Errorf("%s see https://jira.mariadb.org/browse/MDEV-25362", err)
|
2021-09-23 23:46:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 11:19:18 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
return err
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateSubjectCovers updates subject cover thumbs.
|
|
|
|
func UpdateSubjectCovers() (err error) {
|
2021-12-09 02:33:41 +01:00
|
|
|
mutex.Index.Lock()
|
|
|
|
defer mutex.Index.Unlock()
|
2021-10-06 11:50:48 +02:00
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
var res *gorm.DB
|
|
|
|
|
2021-10-01 16:34:29 +02:00
|
|
|
subjTable := entity.Subject{}.TableName()
|
2021-10-01 15:25:16 +02:00
|
|
|
markerTable := entity.Marker{}.TableName()
|
|
|
|
|
|
|
|
condition := gorm.Expr(
|
2021-10-01 19:23:08 +02:00
|
|
|
fmt.Sprintf("%s.subj_type = ? AND thumb_src = ?", subjTable),
|
2021-10-01 15:25:16 +02:00
|
|
|
entity.SubjPerson, entity.SrcAuto)
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
// TODO: Avoid using private photos as subject covers.
|
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
|
|
|
res = Db().Exec(`UPDATE ? LEFT JOIN (
|
|
|
|
SELECT m.subj_uid, m.q, MAX(m.thumb) AS marker_thumb FROM ? m
|
|
|
|
WHERE m.subj_uid <> '' AND m.subj_uid IS NOT NULL
|
|
|
|
AND m.marker_invalid = 0 AND m.thumb IS NOT NULL AND m.thumb <> ''
|
|
|
|
GROUP BY m.subj_uid, m.q
|
|
|
|
) b ON b.subj_uid = subjects.subj_uid
|
2021-10-01 16:34:29 +02:00
|
|
|
SET thumb = marker_thumb WHERE ?`, gorm.Expr(subjTable), gorm.Expr(markerTable), condition)
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2021-10-01 16:34:29 +02:00
|
|
|
from := gorm.Expr(fmt.Sprintf("%s m WHERE m.subj_uid = %s.subj_uid ", markerTable, subjTable))
|
2021-10-01 15:25:16 +02:00
|
|
|
res = Db().Table(entity.Subject{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
|
|
|
|
SELECT m.thumb FROM ? AND m.thumb <> '' ORDER BY m.subj_src DESC, m.q DESC LIMIT 1
|
|
|
|
) WHERE ?`, from, condition))
|
2021-10-01 00:05:49 +02:00
|
|
|
default:
|
2021-10-01 16:34:29 +02:00
|
|
|
log.Warnf("sql: unsupported dialect %s", DbDialect())
|
2021-10-01 00:05:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = res.Error
|
2021-09-17 18:51:24 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
if err == nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Debugf("covers: updated %s [%s]", english.Plural(int(res.RowsAffected), "subject", "subjects"), time.Since(start))
|
2021-09-23 23:46:17 +02:00
|
|
|
} else if strings.Contains(err.Error(), "Error 1054") {
|
2021-10-01 16:44:50 +02:00
|
|
|
log.Errorf("covers: failed updating subjects, potentially incompatible database version")
|
2021-09-24 22:46:03 +02:00
|
|
|
log.Errorf("%s see https://jira.mariadb.org/browse/MDEV-25362", err)
|
2021-09-23 23:46:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 11:19:18 +02:00
|
|
|
|
|
|
|
return err
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// UpdateCovers updates album, subject, and label cover thumbs.
|
|
|
|
func UpdateCovers() (err error) {
|
2022-02-19 19:59:28 +01:00
|
|
|
log.Debugf("index: updating covers")
|
2021-10-02 14:24:44 +02:00
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// Update Albums.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err = UpdateAlbumCovers(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:19:18 +02:00
|
|
|
// Update Labels.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err = UpdateLabelCovers(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update Subjects.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err = UpdateSubjectCovers(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|