0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-07 00:50:23 -05:00

fix: do not use trashed assets as album covers (#12905)

This commit is contained in:
Jason Rasmussen 2024-09-24 17:53:57 -04:00 committed by GitHub
parent b45fce8ddf
commit 05d8c4c132
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 31 deletions

View file

@ -483,16 +483,13 @@ UPDATE "albums"
SET SET
"albumThumbnailAssetId" = ( "albumThumbnailAssetId" = (
SELECT SELECT
"albums_assets2"."assetsId" "album_assets"."assetsId"
FROM FROM
"assets" "assets", "albums_assets_assets" "album_assets"
"albums_assets_assets" "albums_assets2" INNER JOIN "assets" "assets" ON "album_assets"."assetsId" = "assets"."id"
AND "assets"."deletedAt" IS NULL
WHERE WHERE
( "album_assets"."albumsId" = "albums"."id"
"albums_assets2"."assetsId" = "assets"."id"
AND "albums_assets2"."albumsId" = "albums"."id"
)
AND ("assets"."deletedAt" IS NULL)
ORDER BY ORDER BY
"assets"."fileCreatedAt" DESC "assets"."fileCreatedAt" DESC
LIMIT LIMIT
@ -505,17 +502,21 @@ WHERE
SELECT SELECT
1 1
FROM FROM
"albums_assets_assets" "albums_assets" "albums_assets_assets" "album_assets"
INNER JOIN "assets" "assets" ON "album_assets"."assetsId" = "assets"."id"
AND "assets"."deletedAt" IS NULL
WHERE WHERE
"albums"."id" = "albums_assets"."albumsId" "album_assets"."albumsId" = "albums"."id"
) )
OR "albums"."albumThumbnailAssetId" IS NOT NULL OR "albums"."albumThumbnailAssetId" IS NOT NULL
AND NOT EXISTS ( AND NOT EXISTS (
SELECT SELECT
1 1
FROM FROM
"albums_assets_assets" "albums_assets" "albums_assets_assets" "album_assets"
INNER JOIN "assets" "assets" ON "album_assets"."assetsId" = "assets"."id"
AND "assets"."deletedAt" IS NULL
WHERE WHERE
"albums"."id" = "albums_assets"."albumsId" "album_assets"."albumsId" = "albums"."id"
AND "albums"."albumThumbnailAssetId" = "albums_assets"."assetsId" AND "albums"."albumThumbnailAssetId" = "album_assets"."assetsId"
) )

View file

@ -277,32 +277,26 @@ export class AlbumRepository implements IAlbumRepository {
@GenerateSql() @GenerateSql()
async updateThumbnails(): Promise<number | undefined> { async updateThumbnails(): Promise<number | undefined> {
// Subquery for getting a new thumbnail. // Subquery for getting a new thumbnail.
const newThumbnail = this.assetRepository
.createQueryBuilder('assets')
.select('albums_assets2.assetsId')
.addFrom('albums_assets_assets', 'albums_assets2')
.where('albums_assets2.assetsId = assets.id')
.andWhere('albums_assets2.albumsId = "albums"."id"') // Reference to albums.id outside this query
.orderBy('assets.fileCreatedAt', 'DESC')
.limit(1);
// Using dataSource, because there is no direct access to albums_assets_assets. const builder = this.dataSource
const albumHasAssets = this.dataSource .createQueryBuilder('albums_assets_assets', 'album_assets')
.createQueryBuilder() .innerJoin('assets', 'assets', '"album_assets"."assetsId" = "assets"."id"')
.select('1') .where('"album_assets"."albumsId" = "albums"."id"');
.from('albums_assets_assets', 'albums_assets')
.where('"albums"."id" = "albums_assets"."albumsId"');
const albumContainsThumbnail = albumHasAssets const newThumbnail = builder
.clone() .clone()
.andWhere('"albums"."albumThumbnailAssetId" = "albums_assets"."assetsId"'); .select('"album_assets"."assetsId"')
.orderBy('"assets"."fileCreatedAt"', 'DESC')
.limit(1);
const hasAssets = builder.clone().select('1');
const hasInvalidAsset = hasAssets.clone().andWhere('"albums"."albumThumbnailAssetId" = "album_assets"."assetsId"');
const updateAlbums = this.repository const updateAlbums = this.repository
.createQueryBuilder('albums') .createQueryBuilder('albums')
.update(AlbumEntity) .update(AlbumEntity)
.set({ albumThumbnailAssetId: () => `(${newThumbnail.getQuery()})` }) .set({ albumThumbnailAssetId: () => `(${newThumbnail.getQuery()})` })
.where(`"albums"."albumThumbnailAssetId" IS NULL AND EXISTS (${albumHasAssets.getQuery()})`) .where(`"albums"."albumThumbnailAssetId" IS NULL AND EXISTS (${hasAssets.getQuery()})`)
.orWhere(`"albums"."albumThumbnailAssetId" IS NOT NULL AND NOT EXISTS (${albumContainsThumbnail.getQuery()})`); .orWhere(`"albums"."albumThumbnailAssetId" IS NOT NULL AND NOT EXISTS (${hasInvalidAsset.getQuery()})`);
const result = await updateAlbums.execute(); const result = await updateAlbums.execute();