immich/server/src/queries/duplicate.repository.sql
2025-07-14 10:13:06 -04:00

120 lines
2.2 KiB
SQL

-- NOTE: This file is auto generated by ./sql-generator
-- DuplicateRepository.getAll
with
"duplicates" as (
select
"asset"."duplicateId",
json_agg(
"asset2"
order by
"asset"."localDateTime" asc
) as "assets"
from
"asset"
left join lateral (
select
"asset".*,
"asset_exif" as "exifInfo"
from
"asset_exif"
where
"asset_exif"."assetId" = "asset"."id"
) as "asset2" on true
where
"asset"."visibility" in ('archive', 'timeline')
and "asset"."ownerId" = $1::uuid
and "asset"."duplicateId" is not null
and "asset"."deletedAt" is null
and "asset"."stackId" is null
group by
"asset"."duplicateId"
),
"unique" as (
select
"duplicateId"
from
"duplicates"
where
json_array_length("assets") = $2
),
"removed_unique" as (
update "asset"
set
"duplicateId" = $3
from
"unique"
where
"asset"."duplicateId" = "unique"."duplicateId"
)
select
*
from
"duplicates"
where
not exists (
select
from
"unique"
where
"unique"."duplicateId" = "duplicates"."duplicateId"
)
-- DuplicateRepository.delete
update "asset"
set
"duplicateId" = $1
where
"ownerId" = $2
and "duplicateId" = $3
-- DuplicateRepository.deleteAll
update "asset"
set
"duplicateId" = $1
where
"ownerId" = $2
and "duplicateId" in ($3)
-- DuplicateRepository.search
begin
set
local vchordrq.probes = 1
with
"cte" as (
select
"asset"."id" as "assetId",
"asset"."duplicateId",
smart_search.embedding <=> $1 as "distance"
from
"asset"
inner join "smart_search" on "asset"."id" = "smart_search"."assetId"
where
"asset"."visibility" in ('archive', 'timeline')
and "asset"."ownerId" = any ($2::uuid[])
and "asset"."deletedAt" is null
and "asset"."type" = $3
and "asset"."id" != $4::uuid
and "asset"."stackId" is null
order by
"distance"
limit
$5
)
select
*
from
"cte"
where
"cte"."distance" <= $6
commit
-- DuplicateRepository.merge
update "asset"
set
where
(
"duplicateId" = any ($1::uuid[])
or "id" = any ($2::uuid[])
)