Skip to content

Commit 3cdc684

Browse files
fix: automatic media location migration without internal assets (#20489)
1 parent c3263e5 commit 3cdc684

File tree

4 files changed

+8
-44
lines changed

4 files changed

+8
-44
lines changed

server/src/queries/asset.repository.sql

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,10 @@ where
170170

171171
-- AssetRepository.getFileSamples
172172
select
173-
"asset"."id",
174-
"asset"."originalPath",
175-
"asset"."sidecarPath",
176-
"asset"."encodedVideoPath",
177-
(
178-
select
179-
coalesce(json_agg(agg), '[]')
180-
from
181-
(
182-
select
183-
"path"
184-
from
185-
"asset_file"
186-
where
187-
"asset"."id" = "asset_file"."assetId"
188-
) as agg
189-
) as "files"
173+
"assetId",
174+
"path"
190175
from
191-
"asset"
192-
where
193-
"asset"."libraryId" is null
176+
"asset_file"
194177
limit
195178
3
196179

server/src/repositories/asset.repository.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Injectable } from '@nestjs/common';
22
import { Insertable, Kysely, NotNull, Selectable, UpdateResult, Updateable, sql } from 'kysely';
3-
import { jsonArrayFrom } from 'kysely/helpers/postgres';
43
import { isEmpty, isUndefined, omitBy } from 'lodash';
54
import { InjectKysely } from 'nestjs-kysely';
65
import { Stack } from 'src/database';
@@ -338,20 +337,7 @@ export class AssetRepository {
338337

339338
@GenerateSql()
340339
getFileSamples() {
341-
return this.db
342-
.selectFrom('asset')
343-
.select((eb) => [
344-
'asset.id',
345-
'asset.originalPath',
346-
'asset.sidecarPath',
347-
'asset.encodedVideoPath',
348-
jsonArrayFrom(eb.selectFrom('asset_file').select('path').whereRef('asset.id', '=', 'asset_file.assetId')).as(
349-
'files',
350-
),
351-
])
352-
.where('asset.libraryId', 'is', null)
353-
.limit(sql.lit(3))
354-
.execute();
340+
return this.db.selectFrom('asset_file').select(['assetId', 'path']).limit(sql.lit(3)).execute();
355341
}
356342

357343
@GenerateSql({ params: [DummyValue.UUID] })

server/src/services/cli.service.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ export class CliService extends BaseService {
8686
}
8787

8888
for (const asset of assets) {
89-
paths.push(
90-
asset.originalPath,
91-
asset.sidecarPath,
92-
asset.encodedVideoPath,
93-
...asset.files.map((file) => file.path),
94-
);
89+
paths.push(asset.path);
9590
}
9691

9792
return paths.filter(Boolean) as string[];

server/src/services/storage.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ export class StorageService extends BaseService {
9797
const current = StorageCore.getMediaLocation();
9898
const samples = await this.assetRepository.getFileSamples();
9999
if (samples.length > 0) {
100-
const originalPath = samples[0].originalPath;
100+
const path = samples[0].path;
101101
const savedValue = await this.systemMetadataRepository.get(SystemMetadataKey.MediaLocation);
102102
let previous = savedValue?.location || '';
103103

104104
if (!previous) {
105-
previous = originalPath.startsWith('upload/') ? 'upload' : '/usr/src/app/upload';
105+
previous = path.startsWith('upload/') ? 'upload' : '/usr/src/app/upload';
106106
}
107107

108108
if (previous !== current) {
109109
this.logger.log(`Media location changed (from=${previous}, to=${current})`);
110110

111-
if (!originalPath.startsWith(previous)) {
111+
if (!path.startsWith(previous)) {
112112
throw new Error(
113113
'Detected an inconsistent media location. For more information, see https://immich.app/errors#inconsistent-media-location',
114114
);

0 commit comments

Comments
 (0)