update sql

This commit is contained in:
mertalev 2025-05-05 09:38:24 -04:00
parent b20440e4d5
commit f7712c332e
No known key found for this signature in database
GPG Key ID: DF6ABC77AAD98C95
3 changed files with 90 additions and 50 deletions

View File

@ -236,12 +236,12 @@ limit
with with
"assets" as ( "assets" as (
select select
date_trunc($1, "localDateTime" at time zone 'UTC') at time zone 'UTC' as "timeBucket" date_trunc('MONTH', "localDateTime" at time zone 'UTC') at time zone 'UTC' as "timeBucket"
from from
"assets" "assets"
where where
"assets"."deletedAt" is null "assets"."deletedAt" is null
and "assets"."isVisible" = $2 and "assets"."isVisible" = $1
) )
select select
"timeBucket", "timeBucket",
@ -254,53 +254,95 @@ order by
"timeBucket" desc "timeBucket" desc
-- AssetRepository.getTimeBucket -- AssetRepository.getTimeBucket
with
"cte" as (
select select
"assets"."id" as "id", "assets"."duration",
"assets"."id",
assets."isArchived"::int as "isArchived",
assets."isFavorite"::int as "isFavorite",
(assets.type = 'IMAGE')::int as "isImage",
(assets."deletedAt" is null)::int as "isTrashed",
(assets.type = 'VIDEO')::int as "isVideo",
"assets"."livePhotoVideoId",
"assets"."localDateTime",
"assets"."ownerId", "assets"."ownerId",
"assets"."status", "assets"."status",
"deletedAt", encode("assets"."thumbhash", 'base64') as "thumbhash",
"type", "exif"."city",
"duration", "exif"."country",
"isFavorite",
"isArchived",
"thumbhash",
"localDateTime",
"livePhotoVideoId",
"exif"."exifImageHeight" as "height",
"exifImageWidth" as "width",
"exif"."orientation",
"exif"."projectionType", "exif"."projectionType",
"exif"."city" as "city", coalesce(
"exif"."country" as "country", case
to_json("localDateTime" at time zone 'UTC') #>> '{}' as "localDateTime", when exif."exifImageHeight" = 0
to_json("stacked_assets") as "stack" or exif."exifImageWidth" = 0 then 1
when "exif"."orientation" in ('5', '6', '7', '8', '-90', '90') then round(
exif."exifImageHeight"::numeric / exif."exifImageWidth"::numeric,
3
)
else round(
exif."exifImageWidth"::numeric / exif."exifImageHeight"::numeric,
3
)
end,
1
) as "ratio",
"stack"
from from
"assets" "assets"
left join "exif" on "assets"."id" = "exif"."assetId" inner join "exif" on "assets"."id" = "exif"."assetId"
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
left join lateral ( left join lateral (
select select
"asset_stack".*, json_build_array(stacked."stackId", count('stacked')) as "stack"
count("stacked") as "assetCount"
from from
"assets" as "stacked" "assets" as "stacked"
where where
"stacked"."stackId" = "asset_stack"."id" "stacked"."stackId" = "assets"."stackId"
and "stacked"."deletedAt" is null and "stacked"."deletedAt" is null
and "stacked"."isArchived" = $1 and "stacked"."isArchived" = $1
group by group by
"asset_stack"."id" "stacked"."stackId"
) as "stacked_assets" on "asset_stack"."id" is not null ) as "stacked_assets" on true
where where
( "assets"."deletedAt" is null
"asset_stack"."primaryAssetId" = "assets"."id"
or "assets"."stackId" is null
)
and "assets"."deletedAt" is null
and "assets"."isVisible" = $2 and "assets"."isVisible" = $2
and date_trunc($3, "localDateTime" at time zone 'UTC') at time zone 'UTC' = $4 and date_trunc('MONTH', "localDateTime" at time zone 'UTC') at time zone 'UTC' = $3
and not exists (
select
from
"asset_stack"
where
"asset_stack"."id" = "assets"."stackId"
and "asset_stack"."primaryAssetId" != "assets"."id"
)
order by order by
"assets"."localDateTime" desc "assets"."localDateTime" desc
),
"agg" as (
select
coalesce(array_agg("city"), '{}') as "city",
coalesce(array_agg("country"), '{}') as "country",
coalesce(array_agg("duration"), '{}') as "duration",
coalesce(array_agg("id"), '{}') as "id",
coalesce(array_agg("isArchived"), '{}') as "isArchived",
coalesce(array_agg("isFavorite"), '{}') as "isFavorite",
coalesce(array_agg("isImage"), '{}') as "isImage",
coalesce(array_agg("isTrashed"), '{}') as "isTrashed",
coalesce(array_agg("livePhotoVideoId"), '{}') as "livePhotoVideoId",
coalesce(array_agg("localDateTime"), '{}') as "localDateTime",
coalesce(array_agg("ownerId"), '{}') as "ownerId",
coalesce(array_agg("projectionType"), '{}') as "projectionType",
coalesce(array_agg("ratio"), '{}') as "ratio",
coalesce(array_agg("status"), '{}') as "status",
coalesce(array_agg("thumbhash"), '{}') as "thumbhash",
coalesce(array_agg("stack"), '{}') as "stack"
from
"cte"
)
select
to_json(agg)::text as "assets"
from
"agg"
-- AssetRepository.getDuplicates -- AssetRepository.getDuplicates
with with

View File

@ -17,8 +17,6 @@ VACUUM
ANALYZE asset_faces, ANALYZE asset_faces,
face_search, face_search,
person person
REINDEX TABLE asset_faces
REINDEX TABLE person
-- PersonRepository.delete -- PersonRepository.delete
delete from "person" delete from "person"
@ -33,8 +31,6 @@ VACUUM
ANALYZE asset_faces, ANALYZE asset_faces,
face_search, face_search,
person person
REINDEX TABLE asset_faces
REINDEX TABLE person
-- PersonRepository.getAllWithoutFaces -- PersonRepository.getAllWithoutFaces
select select

View File

@ -691,7 +691,9 @@ export class AssetRepository {
eb.fn.coalesce(eb.fn('array_agg', ['status']), sql.lit('{}')).as('status'), eb.fn.coalesce(eb.fn('array_agg', ['status']), sql.lit('{}')).as('status'),
eb.fn.coalesce(eb.fn('array_agg', ['thumbhash']), sql.lit('{}')).as('thumbhash'), eb.fn.coalesce(eb.fn('array_agg', ['thumbhash']), sql.lit('{}')).as('thumbhash'),
]) ])
.$if(!!options.withStacked, (qb) => qb.select((eb) => eb.fn('array_agg', ['stack']).as('stack'))), .$if(!!options.withStacked, (qb) =>
qb.select((eb) => eb.fn.coalesce(eb.fn('array_agg', ['stack']), sql.lit('{}')).as('stack')),
),
) )
.selectFrom('agg') .selectFrom('agg')
.select(sql<string>`to_json(agg)::text`.as('assets')); .select(sql<string>`to_json(agg)::text`.as('assets'));