perf(server): improve people query performance (#32405) fix(server): slow people query (cherry picked from commit 6b79a82800cb984a755b163bca0cf689b486ad9a) Co-authored-by: Zack Pollard <zack@futo.org>
1309 lines
31 KiB
SQL
1309 lines
31 KiB
SQL
-- NOTE: This file is auto generated by ./sql-generator
|
|
|
|
-- PersonRepository.reassignFaces
|
|
update "asset_face"
|
|
set
|
|
"personGroupId" = $1
|
|
from
|
|
"asset"
|
|
where
|
|
"asset_face"."assetId" = "asset"."id"
|
|
and "asset_face"."personGroupId" = $2
|
|
|
|
-- PersonRepository.updateGroupId
|
|
begin
|
|
update "asset_face"
|
|
set
|
|
"personGroupId" = $1
|
|
from
|
|
"asset"
|
|
where
|
|
"asset_face"."assetId" = "asset"."id"
|
|
and "asset_face"."personGroupId" = $2
|
|
and "asset"."ownerId" = $3
|
|
rollback
|
|
|
|
-- PersonRepository.unassignFaces
|
|
update "asset_face"
|
|
set
|
|
"personGroupId" = $1
|
|
from
|
|
"asset"
|
|
inner join "user" on "user"."id" = "asset"."ownerId"
|
|
where
|
|
"asset_face"."assetId" = "asset"."id"
|
|
and "asset_face"."sourceType" = $2
|
|
and "user"."clusterGroupId" = $3
|
|
|
|
-- PersonRepository.delete
|
|
delete from "person"
|
|
where
|
|
"ownerId" = $1
|
|
and "person"."personGroupId" in ($2)
|
|
returning
|
|
"personGroupId",
|
|
"ownerId",
|
|
"thumbnailPath"
|
|
|
|
-- PersonRepository.deleteGroups
|
|
delete from "person_group"
|
|
where
|
|
"person_group"."id" in ($1)
|
|
|
|
-- PersonRepository.deleteEmptyGroups
|
|
delete from "person_group"
|
|
where
|
|
not exists (
|
|
select
|
|
"person"."personGroupId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
)
|
|
|
|
-- PersonRepository.deleteOrphanedClusterGroups
|
|
delete from "cluster_group"
|
|
where
|
|
not exists (
|
|
select
|
|
"user"."id"
|
|
from
|
|
"user"
|
|
where
|
|
"user"."clusterGroupId" = "cluster_group"."id"
|
|
)
|
|
|
|
-- PersonRepository.getAllFaces
|
|
select
|
|
"asset_face".*
|
|
from
|
|
"asset_face"
|
|
inner join "asset" on "asset"."id" = "asset_face"."assetId"
|
|
inner join "user" on "user"."id" = "asset"."ownerId"
|
|
where
|
|
"asset_face"."personGroupId" is null
|
|
and "asset_face"."sourceType" = $1
|
|
and "user"."clusterGroupId" = $2
|
|
and "asset_face"."deletedAt" is null
|
|
and "asset_face"."isVisible" is true
|
|
|
|
-- PersonRepository.forBirthdayMemories
|
|
select
|
|
"person"."personGroupId",
|
|
"person"."name",
|
|
date_part('year', person."birthDate")::int as "birthYear",
|
|
date_part('month', person."birthDate")::int as "birthMonth",
|
|
date_part('day', person."birthDate")::int as "birthDay"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $1
|
|
and "person"."isHidden" = $2
|
|
and "person"."name" != $3
|
|
and "person"."birthDate" is not null
|
|
and (
|
|
date_part('month', person."birthDate")::int = $4
|
|
and date_part('day', person."birthDate")::int = $5
|
|
)
|
|
and date_part('year', person."birthDate")::int < $6
|
|
|
|
-- PersonRepository.forBirthdayMemories (leap day fallback)
|
|
select
|
|
"person"."personGroupId",
|
|
"person"."name",
|
|
date_part('year', person."birthDate")::int as "birthYear",
|
|
date_part('month', person."birthDate")::int as "birthMonth",
|
|
date_part('day', person."birthDate")::int as "birthDay"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $1
|
|
and "person"."isHidden" = $2
|
|
and "person"."name" != $3
|
|
and "person"."birthDate" is not null
|
|
and (
|
|
(
|
|
date_part('month', person."birthDate")::int = $4
|
|
and date_part('day', person."birthDate")::int = $5
|
|
)
|
|
or (
|
|
date_part('month', person."birthDate")::int = $6
|
|
and date_part('day', person."birthDate")::int = $7
|
|
)
|
|
)
|
|
and date_part('year', person."birthDate")::int < $8
|
|
|
|
-- PersonRepository.getFileSamples
|
|
select
|
|
"ownerId",
|
|
"personGroupId",
|
|
"thumbnailPath"
|
|
from
|
|
"person"
|
|
where
|
|
"thumbnailPath" != ''
|
|
limit
|
|
3
|
|
|
|
-- PersonRepository.getAllForUser
|
|
with
|
|
"similarity_threshold" as (
|
|
select
|
|
set_config('pg_trgm.word_similarity_threshold', '0.5', true) as "thresh"
|
|
)
|
|
select
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"person".*
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
and "person"."ownerId" = $1
|
|
) as obj
|
|
) as "ownedPerson",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $2
|
|
where
|
|
"other"."personGroupId" = "person_group"."id"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person_group"."id"
|
|
and "person_user"."sharedWithId" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person_group"."id"
|
|
and "person_user"."sharedById" = $4
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"similarity_threshold",
|
|
"person_group"
|
|
inner join "person" as "owned" on "owned"."personGroupId" = "person_group"."id"
|
|
and "owned"."ownerId" = $5
|
|
inner join (
|
|
select
|
|
"asset_face"."personGroupId",
|
|
count("asset"."id") as "faceCount"
|
|
from
|
|
"asset_face"
|
|
inner join "asset" on "asset"."id" = "asset_face"."assetId"
|
|
and "asset"."visibility" = 'timeline'
|
|
and "asset"."deletedAt" is null
|
|
and (
|
|
"asset"."ownerId" = any ($6::uuid[])
|
|
or exists (
|
|
select
|
|
1 as "exists"
|
|
from
|
|
"album_asset"
|
|
inner join "album" on "album"."id" = "album_asset"."albumId"
|
|
and "album"."deletedAt" is null
|
|
inner join "album_user" on "album_user"."albumId" = "album"."id"
|
|
and "album_user"."userId" = $7::uuid
|
|
where
|
|
"album_asset"."assetId" = "asset"."id"
|
|
)
|
|
)
|
|
where
|
|
"asset_face"."personGroupId" = any (
|
|
array(
|
|
(
|
|
select
|
|
"person"."personGroupId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $8
|
|
)
|
|
)
|
|
)
|
|
and "asset_face"."deletedAt" is null
|
|
and "asset_face"."isVisible" is true
|
|
group by
|
|
"asset_face"."personGroupId"
|
|
) as "faces" on "faces"."personGroupId" = "person_group"."id"
|
|
where
|
|
(
|
|
"owned"."name" != $9
|
|
or "faces"."faceCount" >= COALESCE(
|
|
(
|
|
SELECT
|
|
value -> 'people' ->> 'minimumFaces'
|
|
FROM
|
|
user_metadata
|
|
WHERE
|
|
"userId" = $10
|
|
AND key = 'preferences'
|
|
),
|
|
'3'
|
|
)::int
|
|
)
|
|
and "owned"."isHidden" = $11
|
|
and 1 = 1
|
|
order by
|
|
"owned"."isHidden" asc,
|
|
"owned"."isFavorite" desc,
|
|
NULLIF("owned"."name", '') is null asc,
|
|
"faces"."faceCount" desc,
|
|
NULLIF("owned"."name", '') asc nulls last,
|
|
"owned"."createdAt"
|
|
limit
|
|
$12
|
|
offset
|
|
$13
|
|
|
|
-- PersonRepository.getAllWithoutFaces
|
|
select
|
|
"person".*
|
|
from
|
|
"person"
|
|
left join "asset_face" on "asset_face"."personGroupId" = "person"."personGroupId"
|
|
where
|
|
"asset_face"."deletedAt" is null
|
|
and (
|
|
"asset_face"."isVisible" is null
|
|
or "asset_face"."isVisible" = $1
|
|
)
|
|
group by
|
|
"person"."ownerId",
|
|
"person"."personGroupId"
|
|
having
|
|
count("asset_face"."assetId") = $2
|
|
|
|
-- PersonRepository.getFaces
|
|
select
|
|
"asset_face".*,
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"person".*,
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $1
|
|
where
|
|
"other"."personGroupId" = "person"."personGroupId"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedWithId" = $2
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedById" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "asset_face"."personGroupId"
|
|
and "person"."ownerId" = $4
|
|
) as obj
|
|
) as "person"
|
|
from
|
|
"asset_face"
|
|
where
|
|
"asset_face"."assetId" = $5
|
|
and "asset_face"."deletedAt" is null
|
|
and "asset_face"."isVisible" = $6
|
|
order by
|
|
"asset_face"."boundingBoxX1" asc
|
|
|
|
-- PersonRepository.getFaceById
|
|
select
|
|
"asset_face".*,
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"person".*,
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $1
|
|
where
|
|
"other"."personGroupId" = "person"."personGroupId"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedWithId" = $2
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedById" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "asset_face"."personGroupId"
|
|
and "person"."ownerId" = $4
|
|
) as obj
|
|
) as "person"
|
|
from
|
|
"asset_face"
|
|
where
|
|
"asset_face"."id" = $5
|
|
and "asset_face"."deletedAt" is null
|
|
|
|
-- PersonRepository.getFaceForFacialRecognitionJob
|
|
select
|
|
"asset_face"."id",
|
|
"asset_face"."personGroupId",
|
|
"asset_face"."sourceType",
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"asset"."ownerId",
|
|
"asset"."visibility",
|
|
"asset"."fileCreatedAt",
|
|
"user"."clusterGroupId"
|
|
from
|
|
"asset"
|
|
inner join "user" on "user"."id" = "asset"."ownerId"
|
|
where
|
|
"asset"."id" = "asset_face"."assetId"
|
|
) as obj
|
|
) as "asset",
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"face_search".*
|
|
from
|
|
"face_search"
|
|
where
|
|
"face_search"."faceId" = "asset_face"."id"
|
|
) as obj
|
|
) as "faceSearch"
|
|
from
|
|
"asset_face"
|
|
where
|
|
"asset_face"."id" = $1
|
|
and "asset_face"."deletedAt" is null
|
|
|
|
-- PersonRepository.getDataForThumbnailGenerationJob
|
|
select
|
|
"person"."ownerId",
|
|
"asset_face"."boundingBoxX1" as "x1",
|
|
"asset_face"."boundingBoxY1" as "y1",
|
|
"asset_face"."boundingBoxX2" as "x2",
|
|
"asset_face"."boundingBoxY2" as "y2",
|
|
"asset_face"."imageWidth" as "oldWidth",
|
|
"asset_face"."imageHeight" as "oldHeight",
|
|
"asset"."type",
|
|
"asset"."originalPath",
|
|
"asset_exif"."orientation" as "exifOrientation",
|
|
(
|
|
select
|
|
"asset_file"."path"
|
|
from
|
|
"asset_file"
|
|
where
|
|
"asset_file"."assetId" = "asset"."id"
|
|
and "asset_file"."type" = 'preview'
|
|
and "asset_file"."isEdited" = false
|
|
) as "previewPath"
|
|
from
|
|
"person"
|
|
inner join "asset_face" on "asset_face"."id" = "person"."faceAssetId"
|
|
inner join "asset" on "asset_face"."assetId" = "asset"."id"
|
|
left join "asset_exif" on "asset_exif"."assetId" = "asset"."id"
|
|
where
|
|
"person"."ownerId" = $1
|
|
and "person"."personGroupId" = $2
|
|
and "asset_face"."deletedAt" is null
|
|
|
|
-- PersonRepository.reassignFace
|
|
update "asset_face"
|
|
set
|
|
"personGroupId" = $1
|
|
where
|
|
"asset_face"."id" = $2
|
|
|
|
-- PersonRepository.getForUser
|
|
select
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"person".*
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
and "person"."ownerId" = $1
|
|
) as obj
|
|
) as "ownedPerson",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $2
|
|
where
|
|
"other"."personGroupId" = "person_group"."id"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person_group"."id"
|
|
and "person_user"."sharedWithId" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person_group"."id"
|
|
and "person_user"."sharedById" = $4
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"person_group"
|
|
where
|
|
"person_group"."id" = $5
|
|
and exists (
|
|
select
|
|
"person"."ownerId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
and "person"."ownerId" = $6
|
|
)
|
|
|
|
-- PersonRepository.getByGroupId
|
|
select
|
|
"person".*,
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $1
|
|
where
|
|
"other"."personGroupId" = "person"."personGroupId"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedWithId" = $2
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedById" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = $4
|
|
and "person"."ownerId" = $5
|
|
|
|
-- PersonRepository.getForThumbnail
|
|
select
|
|
(
|
|
select
|
|
"person"."thumbnailPath"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
and "person"."ownerId" = $1
|
|
) as "thumbnailPath",
|
|
(
|
|
select
|
|
"person"."thumbnailPath"
|
|
from
|
|
"person"
|
|
inner join "person_user" on "person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedById" = "person"."ownerId"
|
|
and "person_user"."sharedWithId" = $2
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
and "person"."thumbnailPath" != $3
|
|
limit
|
|
$4
|
|
) as "sharedThumbnailPath"
|
|
from
|
|
"person_group"
|
|
where
|
|
"person_group"."id" = $5
|
|
|
|
-- PersonRepository.getByName
|
|
with
|
|
"similarity_threshold" as (
|
|
select
|
|
set_config('pg_trgm.word_similarity_threshold', '0.5', true) as "thresh"
|
|
)
|
|
select
|
|
"person".*,
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $1
|
|
where
|
|
"other"."personGroupId" = "person"."personGroupId"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedWithId" = $2
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedById" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"similarity_threshold",
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $4
|
|
and f_unaccent ("person"."name") %> f_unaccent ($5)
|
|
order by
|
|
f_unaccent ("person"."name") <->>> f_unaccent ($6)
|
|
limit
|
|
$7
|
|
|
|
-- PersonRepository.getDistinctNames
|
|
select distinct
|
|
on (lower("person"."name")) "person"."personGroupId",
|
|
"person"."name"
|
|
from
|
|
"person"
|
|
where
|
|
(
|
|
"person"."ownerId" = $1
|
|
and "person"."name" != $2
|
|
)
|
|
|
|
-- PersonRepository.getNumberOfPeople
|
|
select
|
|
coalesce(count(*), 0) as "total",
|
|
coalesce(
|
|
count(*) filter (
|
|
where
|
|
"people"."isHidden" = $1
|
|
),
|
|
0
|
|
) as "hidden"
|
|
from
|
|
(
|
|
with
|
|
"similarity_threshold" as (
|
|
select
|
|
set_config('pg_trgm.word_similarity_threshold', '0.5', true) as "thresh"
|
|
)
|
|
select
|
|
"owned"."isHidden"
|
|
from
|
|
"similarity_threshold",
|
|
"person_group"
|
|
inner join "person" as "owned" on "owned"."personGroupId" = "person_group"."id"
|
|
and "owned"."ownerId" = $2
|
|
inner join (
|
|
select
|
|
"asset_face"."personGroupId",
|
|
count("asset"."id") as "faceCount"
|
|
from
|
|
"asset_face"
|
|
inner join "asset" on "asset"."id" = "asset_face"."assetId"
|
|
and "asset"."visibility" = 'timeline'
|
|
and "asset"."deletedAt" is null
|
|
and (
|
|
"asset"."ownerId" = any ($3::uuid[])
|
|
or exists (
|
|
select
|
|
1 as "exists"
|
|
from
|
|
"album_asset"
|
|
inner join "album" on "album"."id" = "album_asset"."albumId"
|
|
and "album"."deletedAt" is null
|
|
inner join "album_user" on "album_user"."albumId" = "album"."id"
|
|
and "album_user"."userId" = $4::uuid
|
|
where
|
|
"album_asset"."assetId" = "asset"."id"
|
|
)
|
|
)
|
|
where
|
|
"asset_face"."personGroupId" = any (
|
|
array(
|
|
(
|
|
select
|
|
"person"."personGroupId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $5
|
|
)
|
|
)
|
|
)
|
|
and "asset_face"."deletedAt" is null
|
|
and "asset_face"."isVisible" is true
|
|
group by
|
|
"asset_face"."personGroupId"
|
|
) as "faces" on "faces"."personGroupId" = "person_group"."id"
|
|
where
|
|
(
|
|
"owned"."name" != $6
|
|
or "faces"."faceCount" >= COALESCE(
|
|
(
|
|
SELECT
|
|
value -> 'people' ->> 'minimumFaces'
|
|
FROM
|
|
user_metadata
|
|
WHERE
|
|
"userId" = $7
|
|
AND key = 'preferences'
|
|
),
|
|
'3'
|
|
)::int
|
|
)
|
|
and 1 = 1
|
|
) as "people"
|
|
|
|
-- PersonRepository.createGroup
|
|
insert into
|
|
"person_group" ("clusterGroupId")
|
|
select
|
|
"user"."clusterGroupId"
|
|
from
|
|
"user"
|
|
where
|
|
"user"."id" = $1
|
|
returning
|
|
*
|
|
|
|
-- PersonRepository.reassignCluster
|
|
begin
|
|
update "person_group"
|
|
set
|
|
"clusterGroupId" = $1
|
|
where
|
|
"person_group"."id" in (
|
|
select
|
|
"person"."personGroupId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $2
|
|
)
|
|
and not exists (
|
|
select
|
|
"person"."personGroupId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "person_group"."id"
|
|
and "person"."ownerId" != $3
|
|
)
|
|
with
|
|
"shared" as (
|
|
select distinct
|
|
"person"."personGroupId" as "oldId"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."ownerId" = $1
|
|
and exists (
|
|
select
|
|
"other"."personGroupId"
|
|
from
|
|
"person" as "other"
|
|
where
|
|
"other"."personGroupId" = "person"."personGroupId"
|
|
and "other"."ownerId" != $2
|
|
)
|
|
),
|
|
"mapping" as materialized (
|
|
select
|
|
"shared"."oldId",
|
|
uuid_generate_v4 () as "newId"
|
|
from
|
|
"shared"
|
|
),
|
|
"created" as (
|
|
insert into
|
|
"person_group" ("id", "clusterGroupId")
|
|
select
|
|
"mapping"."newId",
|
|
$3 as "clusterGroupId"
|
|
from
|
|
"mapping"
|
|
)
|
|
select
|
|
"mapping"."oldId",
|
|
"mapping"."newId"
|
|
from
|
|
"mapping"
|
|
commit
|
|
|
|
-- PersonRepository.createGroups
|
|
insert into
|
|
"person_group" (
|
|
"0",
|
|
"1",
|
|
"2",
|
|
"3",
|
|
"4",
|
|
"5",
|
|
"6",
|
|
"7",
|
|
"8",
|
|
"9",
|
|
"10",
|
|
"11",
|
|
"12",
|
|
"13",
|
|
"14",
|
|
"15",
|
|
"16",
|
|
"17",
|
|
"18",
|
|
"19",
|
|
"20",
|
|
"21",
|
|
"22",
|
|
"23",
|
|
"24",
|
|
"25",
|
|
"26",
|
|
"27",
|
|
"28",
|
|
"29",
|
|
"30",
|
|
"31",
|
|
"32",
|
|
"33",
|
|
"34",
|
|
"35"
|
|
)
|
|
values
|
|
(
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10,
|
|
$11,
|
|
$12,
|
|
$13,
|
|
$14,
|
|
$15,
|
|
$16,
|
|
$17,
|
|
$18,
|
|
$19,
|
|
$20,
|
|
$21,
|
|
$22,
|
|
$23,
|
|
$24,
|
|
$25,
|
|
$26,
|
|
$27,
|
|
$28,
|
|
$29,
|
|
$30,
|
|
$31,
|
|
$32,
|
|
$33,
|
|
$34,
|
|
$35,
|
|
$36
|
|
)
|
|
returning
|
|
*
|
|
|
|
-- PersonRepository.refreshFaces
|
|
with
|
|
"added_embeddings" as (
|
|
insert into
|
|
"face_search" ("faceId", "embedding")
|
|
values
|
|
($1, $2)
|
|
)
|
|
select
|
|
from
|
|
(
|
|
select
|
|
1
|
|
) as "dummy"
|
|
|
|
-- PersonRepository.updateForWritableOwners
|
|
update "person"
|
|
set
|
|
"name" = $1
|
|
where
|
|
"person"."personGroupId" = $2
|
|
and "person"."ownerId" in (
|
|
select
|
|
"person_user"."sharedById"
|
|
from
|
|
"person_user"
|
|
where
|
|
"person_user"."personGroupId" = $3
|
|
and "person_user"."sharedWithId" = $4
|
|
and "person_user"."role" in ($5, $6)
|
|
)
|
|
|
|
-- PersonRepository.getFacesByIds
|
|
select
|
|
"asset_face".*,
|
|
(
|
|
select
|
|
to_json(obj)
|
|
from
|
|
(
|
|
select
|
|
"person".*,
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"person_user"."sharedById",
|
|
"person_user"."role",
|
|
"other"."name",
|
|
"other"."birthDate"
|
|
from
|
|
"person" as "other"
|
|
inner join "person_user" on "person_user"."personGroupId" = "other"."personGroupId"
|
|
and "person_user"."sharedById" = "other"."ownerId"
|
|
and "person_user"."sharedWithId" = $1
|
|
where
|
|
"other"."personGroupId" = "person"."personGroupId"
|
|
) as agg
|
|
) as "otherPeople",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedById"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedWithId" = $2
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedBy",
|
|
(
|
|
select
|
|
coalesce(json_agg(agg), '[]')
|
|
from
|
|
(
|
|
select
|
|
"id",
|
|
"name",
|
|
"email",
|
|
"avatarColor",
|
|
"profileImagePath",
|
|
"profileChangedAt",
|
|
"person_user"."role"
|
|
from
|
|
"person_user"
|
|
inner join "user" on "user"."id" = "person_user"."sharedWithId"
|
|
and "user"."deletedAt" is null
|
|
where
|
|
"person_user"."personGroupId" = "person"."personGroupId"
|
|
and "person_user"."sharedById" = $3
|
|
order by
|
|
"user"."name"
|
|
) as agg
|
|
) as "sharedWith"
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" = "asset_face"."personGroupId"
|
|
and "person"."ownerId" = $4
|
|
) as obj
|
|
) as "person"
|
|
from
|
|
"asset_face"
|
|
where
|
|
"asset_face"."assetId" in ($5)
|
|
and "asset_face"."personGroupId" in ($6)
|
|
and "asset_face"."deletedAt" is null
|
|
|
|
-- PersonRepository.getRandomFace
|
|
select
|
|
"asset_face".*
|
|
from
|
|
"asset_face"
|
|
where
|
|
"asset_face"."personGroupId" = $1
|
|
and "asset_face"."deletedAt" is null
|
|
and "asset_face"."isVisible" is true
|
|
|
|
-- PersonRepository.getLatestFaceDate
|
|
select
|
|
max("asset_job_status"."facesRecognizedAt")::text as "latestDate"
|
|
from
|
|
"asset_job_status"
|
|
|
|
-- PersonRepository.deleteAssetFace
|
|
delete from "asset_face"
|
|
where
|
|
"asset_face"."id" = $1
|
|
|
|
-- PersonRepository.softDeleteAssetFaces
|
|
update "asset_face"
|
|
set
|
|
"deletedAt" = $1
|
|
where
|
|
"asset_face"."id" = $2
|
|
|
|
-- PersonRepository.getForFeatureFaceUpdate
|
|
select
|
|
"asset_face"."id"
|
|
from
|
|
"asset_face"
|
|
inner join "asset" on "asset"."id" = "asset_face"."assetId"
|
|
and "asset"."isOffline" = $1
|
|
where
|
|
"asset_face"."assetId" = $2
|
|
and "asset_face"."personGroupId" = $3
|
|
and "asset_face"."deletedAt" is null
|
|
|
|
-- PersonRepository.getForMergePerson
|
|
select
|
|
"person".*
|
|
from
|
|
"person"
|
|
where
|
|
"person"."personGroupId" in ($1)
|