fix(server): query fixes (#15509)

This commit is contained in:
Mert 2025-01-22 15:17:42 -05:00 committed by GitHub
parent 7b882b35e5
commit 49a6961ec6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 275 additions and 165 deletions

View file

@ -82,11 +82,31 @@ select
where
"shared_links"."albumId" = "albums"."id"
) as agg
) as "sharedLinks"
) as "sharedLinks",
(
select
json_agg("asset") as "assets"
from
(
select
"assets".*,
to_json("exif") as "exifInfo"
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
inner join "albums_assets_assets" on "albums_assets_assets"."assetsId" = "assets"."id"
where
"albums_assets_assets"."albumsId" = "albums"."id"
and "assets"."deletedAt" is null
and "assets"."isArchived" = $1
order by
"assets"."fileCreatedAt" desc
) as "asset"
) as "assets"
from
"albums"
where
"albums"."id" = $1
"albums"."id" = $2
and "albums"."deletedAt" is null
-- AlbumRepository.getByAssetId

View file

@ -23,7 +23,7 @@ with
)
select
"a".*,
to_jsonb("exif") as "exifInfo"
to_json("exif") as "exifInfo"
from
"today"
inner join lateral (
@ -56,7 +56,7 @@ select
(
(now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date
) / 365 as "yearsAgo",
jsonb_agg("res") as "assets"
json_agg("res") as "assets"
from
"res"
group by
@ -109,34 +109,28 @@ select
"assets"."id" = "tag_asset"."assetsId"
) as agg
) as "tags",
to_jsonb("exif") as "exifInfo",
to_jsonb("stacked_assets") as "stack"
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
left join lateral (
select
"asset_stack".*,
"s"."assets"
array_agg("stacked") as "assets"
from
"asset_stack"
inner join lateral (
select
array_agg("stacked") as "assets"
from
"assets" as "stacked"
where
"asset_stack"."id" = "stacked"."stackId"
and "asset_stack"."primaryAssetId" != "stacked"."id"
) as "s" on (
"asset_stack"."primaryAssetId" = "assets"."id"
or "assets"."stackId" is null
)
"assets" as "stacked"
where
"assets"."stackId" = "asset_stack"."id"
) as "stacked_assets" on true
"stacked"."stackId" = "asset_stack"."id"
and "stacked"."id" != "asset_stack"."primaryAssetId"
and "stacked"."deletedAt" is null
and "stacked"."isArchived" = $1
group by
"asset_stack"."id"
) as "stacked_assets" on "asset_stack"."id" is not null
where
"assets"."id" = any ($1::uuid [])
"assets"."id" = any ($2::uuid [])
-- AssetRepository.deleteAll
delete from "assets"
@ -278,14 +272,33 @@ order by
-- AssetRepository.getTimeBucket
select
"assets".*,
to_jsonb("exif") as "exifInfo"
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
left join lateral (
select
"asset_stack".*,
count("stacked") as "assetCount"
from
"assets" as "stacked"
where
"stacked"."stackId" = "asset_stack"."id"
and "stacked"."deletedAt" is null
and "stacked"."isArchived" = $1
group by
"asset_stack"."id"
) as "stacked_assets" on "asset_stack"."id" is not null
where
"assets"."deletedAt" is null
and "assets"."isVisible" = $1
and date_trunc($2, "localDateTime" at time zone 'UTC') at time zone 'UTC' = $3
(
"asset_stack"."primaryAssetId" = "assets"."id"
or "assets"."stackId" is null
)
and "assets"."deletedAt" is null
and "assets"."isVisible" = $2
and date_trunc($3, "localDateTime" at time zone 'UTC') at time zone 'UTC' = $4
order by
"assets"."localDateTime" desc
@ -368,25 +381,23 @@ limit
-- AssetRepository.getAllForUserFullSync
select
"assets".*,
to_jsonb("exif") as "exifInfo",
to_jsonb("stacked_assets") as "stack"
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
left join lateral (
select
"asset_stack".*,
(
select
count(*) as "assetCount"
where
"asset_stack"."id" = "assets"."stackId"
) as "assetCount"
count("stacked") as "assetCount"
from
"asset_stack"
"assets" as "stacked"
where
"assets"."stackId" = "asset_stack"."id"
) as "stacked_assets" on true
"stacked"."stackId" = "asset_stack"."id"
group by
"asset_stack"."id"
) as "stacked_assets" on "asset_stack"."id" is not null
where
"assets"."ownerId" = $1::uuid
and "isVisible" = $2
@ -400,25 +411,23 @@ limit
-- AssetRepository.getChangedDeltaSync
select
"assets".*,
to_jsonb("exif") as "exifInfo",
to_jsonb("stacked_assets") as "stack"
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
left join lateral (
select
"asset_stack".*,
(
select
count(*) as "assetCount"
where
"asset_stack"."id" = "assets"."stackId"
) as "assetCount"
count("stacked") as "assetCount"
from
"asset_stack"
"assets" as "stacked"
where
"assets"."stackId" = "asset_stack"."id"
) as "stacked_assets" on true
"stacked"."stackId" = "asset_stack"."id"
group by
"asset_stack"."id"
) as "stacked_assets" on "asset_stack"."id" is not null
where
"assets"."ownerId" = any ($1::uuid [])
and "isVisible" = $2

View file

@ -112,7 +112,7 @@ order by
-- LibraryRepository.getStatistics
select
count("assets"."id") filter (
count(*) filter (
where
(
"assets"."type" = $1
@ -130,8 +130,17 @@ select
from
"libraries"
inner join "assets" on "assets"."libraryId" = "libraries"."id"
inner join "exif" on "exif"."assetId" = "assets"."id"
left join "exif" on "exif"."assetId" = "assets"."id"
where
"libraries"."id" = $6
group by
"libraries"."id"
select
0::int as "photos",
0::int as "videos",
0::int as "usage",
0::int as "total"
from
"libraries"
where
"libraries"."id" = $1

View file

@ -14,7 +14,7 @@ where
-- ViewRepository.getAssetsByOriginalPath
select
"assets".*,
to_jsonb("exif") as "exifInfo"
to_json("exif") as "exifInfo"
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"