immich/server/src/queries/person.repository.sql
2026-08-13 13:16:58 -04:00

579 lines
10 KiB
SQL

-- NOTE: This file is auto generated by ./sql-generator
-- PersonRepository.reassignFaces
update "asset_face"
set
"personGroupId" = $1
where
"asset_face"."personGroupId" = $2
-- PersonRepository.delete
delete from "person"
where
(
"person"."ownerId" = $1
and "person"."personGroupId" = $2
)
-- 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.getFileSamples
select
"ownerId",
"personGroupId",
"thumbnailPath"
from
"person"
where
"thumbnailPath" != ''
limit
3
-- PersonRepository.getAllForUser
select
"person".*
from
"person"
inner join "asset_face" on "asset_face"."personGroupId" = "person"."personGroupId"
inner join "asset" on "asset_face"."assetId" = "asset"."id"
and "asset"."ownerId" = "person"."ownerId"
and "asset"."visibility" = 'timeline'
and "asset"."deletedAt" is null
where
"person"."ownerId" = $1
and "asset_face"."deletedAt" is null
and "asset_face"."isVisible" is true
and "person"."isHidden" = $2
group by
"person"."ownerId",
"person"."personGroupId"
having
(
"person"."name" != $3
or count("asset_face"."assetId") >= COALESCE(
(
SELECT
value -> 'people' ->> 'minimumFaces'
FROM
user_metadata
WHERE
"userId" = $4
AND key = 'preferences'
),
'3'
)::int
)
order by
"person"."isHidden" asc,
"person"."isFavorite" desc,
NULLIF(person.name, '') is null asc,
count("asset_face"."assetId") desc,
NULLIF(person.name, '') asc nulls last,
"person"."createdAt"
limit
$5
offset
$6
-- 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 true
group by
"person"."ownerId",
"person"."personGroupId"
having
count("asset_face"."assetId") = $1
-- PersonRepository.getFaces
select
"asset_face".*,
(
select
to_json(obj)
from
(
select
"person".*
from
"person"
where
"person"."personGroupId" = "asset_face"."personGroupId"
and "person"."ownerId" = $1
) as obj
) as "person"
from
"asset_face"
where
"asset_face"."assetId" = $2
and "asset_face"."deletedAt" is null
and "asset_face"."isVisible" = $3
order by
"asset_face"."boundingBoxX1" asc
-- PersonRepository.getFaceById
select
"asset_face".*,
(
select
to_json(obj)
from
(
select
"person".*
from
"person"
where
"person"."personGroupId" = "asset_face"."personGroupId"
and "person"."ownerId" = $1
) as obj
) as "person"
from
"asset_face"
where
"asset_face"."id" = $2
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.getByGroupId
select
"person".*
from
"person"
where
"person"."personGroupId" = $1
and "person"."ownerId" = $2
-- PersonRepository.getByName
with
"similarity_threshold" as (
select
set_config('pg_trgm.word_similarity_threshold', '0.5', true) as "thresh"
)
select
"person".*
from
"similarity_threshold",
"person"
where
"person"."ownerId" = $1
and f_unaccent ("person"."name") %> f_unaccent ($2)
order by
f_unaccent ("person"."name") <->>> f_unaccent ($3)
limit
$4
-- PersonRepository.getDistinctNames
select distinct
on (lower("person"."name")) "person"."personGroupId",
"person"."name"
from
"person"
where
(
"person"."ownerId" = $1
and "person"."name" != $2
)
-- PersonRepository.getStatistics
select
count(distinct ("asset"."id")) as "count"
from
"asset_face"
left join "asset" on "asset"."id" = "asset_face"."assetId"
and "asset"."visibility" = 'timeline'
and "asset"."deletedAt" is null
where
"asset_face"."deletedAt" is null
and "asset_face"."isVisible" is true
and "asset_face"."personGroupId" = $1
-- PersonRepository.getNumberOfPeople
select
coalesce(count(*), 0) as "total",
coalesce(
count(*) filter (
where
"isHidden" = $1
),
0
) as "hidden"
from
"person"
where
exists (
select
from
"asset_face"
where
"asset_face"."personGroupId" = "person"."personGroupId"
and "asset_face"."deletedAt" is null
and "asset_face"."isVisible" = $2
and exists (
select
from
"asset"
where
"asset"."id" = "asset_face"."assetId"
and "asset"."visibility" = 'timeline'
and "asset"."deletedAt" is null
)
)
and "person"."ownerId" = $3
-- 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.getFacesByIds
select
"asset_face".*,
(
select
to_json(obj)
from
(
select
"person".*
from
"person"
where
"person"."personGroupId" = "asset_face"."personGroupId"
and "person"."ownerId" = $1
) as obj
) as "person"
from
"asset_face"
where
"asset_face"."assetId" in ($2)
and "asset_face"."personGroupId" in ($3)
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.getForPeopleDelete
select
"person"."ownerId",
"person"."personGroupId",
"person"."thumbnailPath"
from
"person"
where
"person"."personGroupId" in ($1)
-- 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