immich/server/src/queries/asset.repository.sql

465 lines
9.5 KiB
MySQL
Raw Normal View History

-- NOTE: This file is auto generated by ./sql-generator
-- AssetRepository.updateAllExif
update "exif"
set
"model" = $1
where
"assetId" in ($2)
-- AssetRepository.getByDayOfYear
2025-01-09 11:15:41 -05:00
with
"res" as (
with
"today" as (
select
make_date(year::int, $1::int, $2::int) as "date"
from
generate_series(
(
select
date_part(
'year',
min(("localDateTime" at time zone 'UTC')::date)
)::int
from
assets
),
date_part('year', current_date)::int - 1
) as "year"
)
select
"a".*,
2025-01-22 15:17:42 -05:00
to_json("exif") as "exifInfo"
2025-01-09 11:15:41 -05:00
from
"today"
inner join lateral (
select
"assets".*
from
"assets"
inner join "asset_job_status" on "assets"."id" = "asset_job_status"."assetId"
where
"asset_job_status"."previewAt" is not null
and (assets."localDateTime" at time zone 'UTC')::date = today.date
and "assets"."ownerId" = any ($3::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isVisible" = $4
and "assets"."isArchived" = $5
and exists (
select
from
"asset_files"
where
"assetId" = "assets"."id"
and "asset_files"."type" = $6
)
and "assets"."deletedAt" is null
order by
(assets."localDateTime" at time zone 'UTC')::date desc
2025-01-09 11:15:41 -05:00
limit
$7
) as "a" on true
inner join "exif" on "a"."id" = "exif"."assetId"
)
2025-01-09 11:15:41 -05:00
select
2025-02-28 13:51:28 -05:00
date_part(
'year',
("localDateTime" at time zone 'UTC')::date
)::int as "year",
2025-01-22 15:17:42 -05:00
json_agg("res") as "assets"
2025-01-09 11:15:41 -05:00
from
"res"
group by
("localDateTime" at time zone 'UTC')::date
order by
("localDateTime" at time zone 'UTC')::date desc
-- AssetRepository.getByIds
2025-01-09 11:15:41 -05:00
select
"assets".*
from
"assets"
where
"assets"."id" = any ($1::uuid[])
-- AssetRepository.getByIdsWithAllRelations
2025-01-09 11:15:41 -05:00
select
"assets".*,
(
select
coalesce(json_agg(agg), '[]')
2025-01-09 11:15:41 -05:00
from
(
select
"asset_faces".*,
"person" as "person"
from
"asset_faces"
left join lateral (
select
"person".*
from
"person"
where
"asset_faces"."personId" = "person"."id"
) as "person" on true
where
"asset_faces"."assetId" = "assets"."id"
and "asset_faces"."deletedAt" is null
) as agg
2025-01-09 11:15:41 -05:00
) as "faces",
(
select
coalesce(json_agg(agg), '[]')
from
(
select
"tags"."id",
"tags"."value",
"tags"."createdAt",
"tags"."updatedAt",
"tags"."color",
"tags"."parentId"
2025-01-09 11:15:41 -05:00
from
"tags"
inner join "tag_asset" on "tags"."id" = "tag_asset"."tagsId"
where
"assets"."id" = "tag_asset"."assetsId"
) as agg
) as "tags",
2025-01-22 15:17:42 -05:00
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
2025-01-09 11:15:41 -05:00
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
2025-01-22 15:17:42 -05:00
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
2025-01-09 11:15:41 -05:00
left join lateral (
select
"asset_stack".*,
2025-01-22 15:17:42 -05:00
array_agg("stacked") as "assets"
2025-01-09 11:15:41 -05:00
from
2025-01-22 15:17:42 -05:00
"assets" as "stacked"
2025-01-09 11:15:41 -05:00
where
2025-01-22 15:17:42 -05:00
"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
2025-01-09 11:15:41 -05:00
where
"assets"."id" = any ($2::uuid[])
-- AssetRepository.deleteAll
2025-01-09 11:15:41 -05:00
delete from "assets"
where
"ownerId" = $1
-- AssetRepository.getByLibraryIdAndOriginalPath
2025-01-09 11:15:41 -05:00
select
"assets".*
from
"assets"
where
"libraryId" = $1::uuid
and "originalPath" = $2
limit
$3
-- AssetRepository.getAllByDeviceId
2025-01-09 11:15:41 -05:00
select
"deviceAssetId"
from
"assets"
where
"ownerId" = $1::uuid
and "deviceId" = $2
and "isVisible" = $3
and "deletedAt" is null
-- AssetRepository.getLivePhotoCount
2025-01-09 11:15:41 -05:00
select
count(*) as "count"
from
"assets"
where
"livePhotoVideoId" = $1::uuid
-- AssetRepository.getById
2025-01-09 11:15:41 -05:00
select
"assets".*
from
"assets"
where
"assets"."id" = $1::uuid
limit
$2
-- AssetRepository.updateAll
2025-01-09 11:15:41 -05:00
update "assets"
set
"deviceId" = $1
where
"id" = any ($2::uuid[])
feat(server): near-duplicate detection (#8228) * duplicate detection job, entity, config * queueing * job panel, update api * use embedding in db instead of fetching * disable concurrency * only queue visible assets * handle multiple duplicateIds * update concurrent queue check * add provider * add web placeholder, server endpoint, migration, various fixes * update sql * select embedding by default * rename variable * simplify * remove separate entity, handle re-running with different threshold, set default back to 0.02 * fix tests * add tests * add index to entity * formatting * update asset mock * fix `upsertJobStatus` signature * update sql * formatting * default to 0.03 * optimize clustering * use asset's `duplicateId` if present * update sql * update tests * expose admin setting * refactor * formatting * skip if ml is disabled * debug trash e2e * remove from web * remove from sidebar * test if ml is disabled * update sql * separate duplicate detection from clip in config, disable by default for now * fix doc * lower minimum `maxDistance` * update api * Add and Use Duplicate Detection Feature Flag (#9364) * Add Duplicate Detection Flag * Use Duplicate Detection Flag * Attempt Fixes for Failing Checks * lower minimum `maxDistance` * fix tests --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> * chore: fixes and additions after rebase * chore: update api (remove new Role enum) * fix: left join smart search so getAll works without machine learning * test: trash e2e go back to checking length of assets is zero * chore: regen api after rebase * test: fix tests after rebase * redundant join --------- Co-authored-by: Nicholas Flamy <30300649+NicholasFlamy@users.noreply.github.com> Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack@futo.org>
2024-05-16 13:08:37 -04:00
-- AssetRepository.updateDuplicates
2025-01-09 11:15:41 -05:00
update "assets"
set
"duplicateId" = $1
where
(
"duplicateId" = any ($2::uuid[])
or "id" = any ($3::uuid[])
)
2025-01-09 11:15:41 -05:00
-- AssetRepository.getByChecksum
select
"assets".*
from
"assets"
where
"ownerId" = $1::uuid
and "checksum" = $2
and "libraryId" = $3::uuid
limit
$4
-- AssetRepository.getByChecksums
2025-01-09 11:15:41 -05:00
select
"id",
"checksum",
"deletedAt"
from
"assets"
where
"ownerId" = $1::uuid
and "checksum" in ($2)
-- AssetRepository.getUploadAssetIdByChecksum
2025-01-09 11:15:41 -05:00
select
"id"
from
"assets"
where
"ownerId" = $1::uuid
and "checksum" = $2
and "libraryId" is null
limit
$3
-- AssetRepository.getWithout (sidecar)
2025-01-09 11:15:41 -05:00
select
"assets".*
from
"assets"
where
(
2025-01-09 11:15:41 -05:00
"assets"."sidecarPath" = $1
or "assets"."sidecarPath" is null
)
2025-01-09 11:15:41 -05:00
and "assets"."isVisible" = $2
and "deletedAt" is null
order by
"createdAt"
limit
$3
offset
$4
2024-01-03 23:32:52 -05:00
-- AssetRepository.getTimeBuckets
2025-01-09 11:15:41 -05:00
with
"assets" as (
select
date_trunc($1, "localDateTime" at time zone 'UTC') at time zone 'UTC' as "timeBucket"
from
"assets"
where
"assets"."deletedAt" is null
and "assets"."isVisible" = $2
)
select
"timeBucket",
count(*) as "count"
from
"assets"
group by
"timeBucket"
order by
"timeBucket" desc
2024-01-03 23:32:52 -05:00
-- AssetRepository.getTimeBucket
2025-01-09 11:15:41 -05:00
select
"assets".*,
2025-01-22 15:17:42 -05:00
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
2025-01-09 11:15:41 -05:00
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
2025-01-22 15:17:42 -05:00
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
2025-01-09 11:15:41 -05:00
where
2025-01-22 15:17:42 -05:00
(
"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
2025-01-09 11:15:41 -05:00
order by
"assets"."localDateTime" desc
2024-01-03 23:32:52 -05:00
feat(server): near-duplicate detection (#8228) * duplicate detection job, entity, config * queueing * job panel, update api * use embedding in db instead of fetching * disable concurrency * only queue visible assets * handle multiple duplicateIds * update concurrent queue check * add provider * add web placeholder, server endpoint, migration, various fixes * update sql * select embedding by default * rename variable * simplify * remove separate entity, handle re-running with different threshold, set default back to 0.02 * fix tests * add tests * add index to entity * formatting * update asset mock * fix `upsertJobStatus` signature * update sql * formatting * default to 0.03 * optimize clustering * use asset's `duplicateId` if present * update sql * update tests * expose admin setting * refactor * formatting * skip if ml is disabled * debug trash e2e * remove from web * remove from sidebar * test if ml is disabled * update sql * separate duplicate detection from clip in config, disable by default for now * fix doc * lower minimum `maxDistance` * update api * Add and Use Duplicate Detection Feature Flag (#9364) * Add Duplicate Detection Flag * Use Duplicate Detection Flag * Attempt Fixes for Failing Checks * lower minimum `maxDistance` * fix tests --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> * chore: fixes and additions after rebase * chore: update api (remove new Role enum) * fix: left join smart search so getAll works without machine learning * test: trash e2e go back to checking length of assets is zero * chore: regen api after rebase * test: fix tests after rebase * redundant join --------- Co-authored-by: Nicholas Flamy <30300649+NicholasFlamy@users.noreply.github.com> Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack@futo.org>
2024-05-16 13:08:37 -04:00
-- AssetRepository.getDuplicates
2025-01-09 11:15:41 -05:00
with
"duplicates" as (
select
"assets"."duplicateId",
jsonb_agg("asset") as "assets"
2025-01-09 11:15:41 -05:00
from
"assets"
left join lateral (
select
"assets".*,
"exif" as "exifInfo"
from
"exif"
where
"exif"."assetId" = "assets"."id"
) as "asset" on true
2025-01-09 11:15:41 -05:00
where
"assets"."ownerId" = $1::uuid
and "assets"."duplicateId" is not null
and "assets"."deletedAt" is null
and "assets"."isVisible" = $2
and "assets"."stackId" is null
2025-01-09 11:15:41 -05:00
group by
"assets"."duplicateId"
2025-01-09 11:15:41 -05:00
),
"unique" as (
select
"duplicateId"
from
"duplicates"
where
jsonb_array_length("assets") = $3
),
"removed_unique" as (
update "assets"
set
"duplicateId" = $4
from
"unique"
where
"assets"."duplicateId" = "unique"."duplicateId"
)
select
*
from
"duplicates"
where
not exists (
select
from
"unique"
where
"unique"."duplicateId" = "duplicates"."duplicateId"
feat(server): near-duplicate detection (#8228) * duplicate detection job, entity, config * queueing * job panel, update api * use embedding in db instead of fetching * disable concurrency * only queue visible assets * handle multiple duplicateIds * update concurrent queue check * add provider * add web placeholder, server endpoint, migration, various fixes * update sql * select embedding by default * rename variable * simplify * remove separate entity, handle re-running with different threshold, set default back to 0.02 * fix tests * add tests * add index to entity * formatting * update asset mock * fix `upsertJobStatus` signature * update sql * formatting * default to 0.03 * optimize clustering * use asset's `duplicateId` if present * update sql * update tests * expose admin setting * refactor * formatting * skip if ml is disabled * debug trash e2e * remove from web * remove from sidebar * test if ml is disabled * update sql * separate duplicate detection from clip in config, disable by default for now * fix doc * lower minimum `maxDistance` * update api * Add and Use Duplicate Detection Feature Flag (#9364) * Add Duplicate Detection Flag * Use Duplicate Detection Flag * Attempt Fixes for Failing Checks * lower minimum `maxDistance` * fix tests --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> * chore: fixes and additions after rebase * chore: update api (remove new Role enum) * fix: left join smart search so getAll works without machine learning * test: trash e2e go back to checking length of assets is zero * chore: regen api after rebase * test: fix tests after rebase * redundant join --------- Co-authored-by: Nicholas Flamy <30300649+NicholasFlamy@users.noreply.github.com> Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack@futo.org>
2024-05-16 13:08:37 -04:00
)
2023-12-08 11:15:46 -05:00
-- AssetRepository.getAssetIdByCity
2025-01-09 11:15:41 -05:00
with
"cities" as (
select
"city"
from
"exif"
where
"city" is not null
group by
"city"
having
count("assetId") >= $1
2023-12-08 11:15:46 -05:00
)
2025-01-09 11:15:41 -05:00
select distinct
on ("exif"."city") "assetId" as "data",
"exif"."city" as "value"
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
inner join "cities" on "exif"."city" = "cities"."city"
where
"ownerId" = $2::uuid
and "isVisible" = $3
and "isArchived" = $4
and "type" = $5
and "deletedAt" is null
limit
$6
2023-12-08 11:15:46 -05:00
-- AssetRepository.getAllForUserFullSync
2025-01-09 11:15:41 -05:00
select
"assets".*,
2025-01-22 15:17:42 -05:00
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
2025-01-09 11:15:41 -05:00
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
2025-01-22 15:17:42 -05:00
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
2025-01-09 11:15:41 -05:00
left join lateral (
select
"asset_stack".*,
2025-01-22 15:17:42 -05:00
count("stacked") as "assetCount"
2025-01-09 11:15:41 -05:00
from
2025-01-22 15:17:42 -05:00
"assets" as "stacked"
2025-01-09 11:15:41 -05:00
where
2025-01-22 15:17:42 -05:00
"stacked"."stackId" = "asset_stack"."id"
group by
"asset_stack"."id"
) as "stacked_assets" on "asset_stack"."id" is not null
2025-01-09 11:15:41 -05:00
where
"assets"."ownerId" = $1::uuid
and "assets"."isVisible" = $2
and "assets"."updatedAt" <= $3
2025-01-09 11:15:41 -05:00
and "assets"."id" > $4
order by
"assets"."id"
limit
$5
-- AssetRepository.getChangedDeltaSync
2025-01-09 11:15:41 -05:00
select
"assets".*,
2025-01-22 15:17:42 -05:00
to_json("exif") as "exifInfo",
to_json("stacked_assets") as "stack"
2025-01-09 11:15:41 -05:00
from
"assets"
left join "exif" on "assets"."id" = "exif"."assetId"
2025-01-22 15:17:42 -05:00
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
2025-01-09 11:15:41 -05:00
left join lateral (
select
"asset_stack".*,
2025-01-22 15:17:42 -05:00
count("stacked") as "assetCount"
2025-01-09 11:15:41 -05:00
from
2025-01-22 15:17:42 -05:00
"assets" as "stacked"
2025-01-09 11:15:41 -05:00
where
2025-01-22 15:17:42 -05:00
"stacked"."stackId" = "asset_stack"."id"
group by
"asset_stack"."id"
) as "stacked_assets" on "asset_stack"."id" is not null
2025-01-09 11:15:41 -05:00
where
"assets"."ownerId" = any ($1::uuid[])
and "assets"."isVisible" = $2
and "assets"."updatedAt" > $3
2025-01-09 11:15:41 -05:00
limit
$4