fix(server): avoid duplicate rows in album queries (#15670)

* avoid duplicate rows

* left join, handle null vs. undefined

* update sql
This commit is contained in:
Mert 2025-01-25 23:37:19 -05:00 committed by GitHub
parent 4f725b95e1
commit 05a446c259
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 103 additions and 130 deletions

View file

@ -90,7 +90,7 @@ select
(
select
"assets".*,
to_json("exif") as "exifInfo"
"exif" as "exifInfo"
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
@ -180,19 +180,20 @@ select
) as "albumUsers"
from
"albums"
left join "albums_assets_assets" as "album_assets" on "album_assets"."albumsId" = "albums"."id"
left join "albums_shared_users_users" as "album_users" on "album_users"."albumsId" = "albums"."id"
inner join "albums_assets_assets" as "album_assets" on "album_assets"."albumsId" = "albums"."id"
where
(
(
"albums"."ownerId" = $1
and "album_assets"."assetsId" = $2
)
or (
"album_users"."usersId" = $3
and "album_assets"."assetsId" = $4
"albums"."ownerId" = $1
or exists (
select
from
"albums_shared_users_users" as "album_users"
where
"album_users"."albumsId" = "albums"."id"
and "album_users"."usersId" = $2
)
)
and "album_assets"."assetsId" = $3
and "albums"."deletedAt" is null
order by
"albums"."createdAt" desc,
@ -200,10 +201,10 @@ order by
-- AlbumRepository.getMetadataForIds
select
"albums"."id",
"albums"."id" as "albumId",
min("assets"."fileCreatedAt") as "startDate",
max("assets"."fileCreatedAt") as "endDate",
count("assets"."id") as "assetCount"
count("assets"."id")::int as "assetCount"
from
"albums"
left join "albums_assets_assets" as "album_assets" on "album_assets"."albumsId" = "albums"."id"
@ -306,8 +307,8 @@ order by
"albums"."createdAt" desc
-- AlbumRepository.getShared
select distinct
on ("albums"."createdAt") "albums".*,
select
"albums".*,
(
select
coalesce(json_agg(agg), '[]')
@ -390,15 +391,26 @@ select distinct
) as "sharedLinks"
from
"albums"
left join "albums_shared_users_users" as "shared_albums" on "shared_albums"."albumsId" = "albums"."id"
left join "shared_links" on "shared_links"."albumId" = "albums"."id"
where
(
"shared_albums"."usersId" = $1
or "shared_links"."userId" = $2
or (
"albums"."ownerId" = $3
and "shared_albums"."usersId" is not null
exists (
select
from
"albums_shared_users_users" as "album_users"
where
"album_users"."albumsId" = "albums"."id"
and (
"albums"."ownerId" = $1
or "album_users"."usersId" = $2
)
)
or exists (
select
from
"shared_links"
where
"shared_links"."albumId" = "albums"."id"
and "shared_links"."userId" = $3
)
)
and "albums"."deletedAt" is null
@ -406,48 +418,8 @@ order by
"albums"."createdAt" desc
-- AlbumRepository.getNotShared
select distinct
on ("albums"."createdAt") "albums".*,
(
select
coalesce(json_agg(agg), '[]')
from
(
select
"album_users".*,
(
select
to_json(obj)
from
(
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"profileChangedAt"
from
"users"
where
"users"."id" = "album_users"."usersId"
) as obj
) as "user"
from
"albums_shared_users_users" as "album_users"
where
"album_users"."albumsId" = "albums"."id"
) as agg
) as "albumUsers",
select
"albums".*,
(
select
to_json(obj)
@ -474,29 +446,26 @@ select distinct
where
"users"."id" = "albums"."ownerId"
) as obj
) as "owner",
(
select
coalesce(json_agg(agg), '[]')
from
(
select
*
from
"shared_links"
where
"shared_links"."albumId" = "albums"."id"
) as agg
) as "sharedLinks"
) as "owner"
from
"albums"
left join "albums_shared_users_users" as "shared_albums" on "shared_albums"."albumsId" = "albums"."id"
left join "shared_links" on "shared_links"."albumId" = "albums"."id"
where
"albums"."ownerId" = $1
and "shared_albums"."usersId" is null
and "shared_links"."userId" is null
and "albums"."deletedAt" is null
and not exists (
select
from
"albums_shared_users_users" as "album_users"
where
"album_users"."albumsId" = "albums"."id"
)
and not exists (
select
from
"shared_links"
where
"shared_links"."albumId" = "albums"."id"
)
order by
"albums"."createdAt" desc