mirror of
https://github.com/immich-app/immich
synced 2025-10-17 18:19:27 +00:00
remove transaction and format:fix
This commit is contained in:
parent
c503422309
commit
d7cdde0153
3 changed files with 64 additions and 53 deletions
|
|
@ -163,28 +163,26 @@ export class TagRepository {
|
|||
}
|
||||
|
||||
async deleteEmptyTags() {
|
||||
await this.db.transaction().execute(async (tx) => {
|
||||
const result = await tx
|
||||
.deleteFrom('tag')
|
||||
.where('id', 'not in', (eb) => eb.selectFrom('tag_asset').select('tagsId'))
|
||||
.where('id', 'not in', (eb) =>
|
||||
eb
|
||||
.selectFrom('tag as child')
|
||||
.select('child.parentId')
|
||||
.where('child.parentId', 'is not', null)
|
||||
.where((eb2) =>
|
||||
eb2.or([
|
||||
eb2('child.id', 'in', (eb3) => eb3.selectFrom('tag_asset').select('tagsId')),
|
||||
eb2('child.id', 'not in', (eb3) => eb3.selectFrom('tag_asset').select('tagsId')),
|
||||
]),
|
||||
),
|
||||
)
|
||||
.executeTakeFirst();
|
||||
const result = await this.db
|
||||
.deleteFrom('tag')
|
||||
.where('id', 'not in', (eb) => eb.selectFrom('tag_asset').select('tagsId'))
|
||||
.where('id', 'not in', (eb) =>
|
||||
eb
|
||||
.selectFrom('tag as child')
|
||||
.select('child.parentId')
|
||||
.where('child.parentId', 'is not', null)
|
||||
.where((eb2) =>
|
||||
eb2.or([
|
||||
eb2('child.id', 'in', (eb3) => eb3.selectFrom('tag_asset').select('tagsId')),
|
||||
eb2('child.id', 'not in', (eb3) => eb3.selectFrom('tag_asset').select('tagsId')),
|
||||
]),
|
||||
),
|
||||
)
|
||||
.executeTakeFirst();
|
||||
|
||||
const deletedRows = Number(result.numDeletedRows);
|
||||
if (deletedRows > 0) {
|
||||
this.logger.log(`Deleted ${deletedRows} empty tags`);
|
||||
}
|
||||
});
|
||||
const deletedRows = Number(result.numDeletedRows);
|
||||
if (deletedRows > 0) {
|
||||
this.logger.log(`Deleted ${deletedRows} empty tags`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,8 +39,8 @@ import { StorageRepository } from 'src/repositories/storage.repository';
|
|||
import { SyncCheckpointRepository } from 'src/repositories/sync-checkpoint.repository';
|
||||
import { SyncRepository } from 'src/repositories/sync.repository';
|
||||
import { SystemMetadataRepository } from 'src/repositories/system-metadata.repository';
|
||||
import { TelemetryRepository } from 'src/repositories/telemetry.repository';
|
||||
import { TagRepository } from 'src/repositories/tag.repository';
|
||||
import { TelemetryRepository } from 'src/repositories/telemetry.repository';
|
||||
import { UserRepository } from 'src/repositories/user.repository';
|
||||
import { VersionHistoryRepository } from 'src/repositories/version-history.repository';
|
||||
import { DB } from 'src/schema';
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { Kysely } from "kysely";
|
||||
import { JobStatus } from "src/enum";
|
||||
import { AccessRepository } from "src/repositories/access.repository";
|
||||
import { LoggingRepository } from "src/repositories/logging.repository";
|
||||
import { TagRepository } from "src/repositories/tag.repository";
|
||||
import { DB } from "src/schema";
|
||||
import { TagService } from "src/services/tag.service";
|
||||
import { newMediumService } from "test/medium.factory";
|
||||
import { getKyselyDB } from "test/utils";
|
||||
import { Kysely } from 'kysely';
|
||||
import { JobStatus } from 'src/enum';
|
||||
import { AccessRepository } from 'src/repositories/access.repository';
|
||||
import { LoggingRepository } from 'src/repositories/logging.repository';
|
||||
import { TagRepository } from 'src/repositories/tag.repository';
|
||||
import { DB } from 'src/schema';
|
||||
import { TagService } from 'src/services/tag.service';
|
||||
import { newMediumService } from 'test/medium.factory';
|
||||
import { getKyselyDB } from 'test/utils';
|
||||
|
||||
let defaultDatabase: Kysely<DB>;
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ describe(TagService.name, () => {
|
|||
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id }));
|
||||
await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
|
||||
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id }));
|
||||
})
|
||||
});
|
||||
|
||||
it('hierarchical tag exists, and the parent is connected to an asset, and the child is deleted', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
|
|
@ -59,12 +59,18 @@ describe(TagService.name, () => {
|
|||
|
||||
await ctx.newTagAsset({ tagIds: [parentTag.id], assetIds: [asset.id] });
|
||||
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id }));
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(
|
||||
expect.objectContaining({ id: parentTag.id }),
|
||||
);
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(
|
||||
expect.objectContaining({ id: childrenTag.id }),
|
||||
);
|
||||
await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(
|
||||
expect.objectContaining({ id: parentTag.id }),
|
||||
);
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toBeUndefined();
|
||||
})
|
||||
});
|
||||
|
||||
it('hierarchical tag exists, and only the child is connected to an asset, and nothing is deleted', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
|
|
@ -76,12 +82,19 @@ describe(TagService.name, () => {
|
|||
|
||||
await ctx.newTagAsset({ tagIds: [childrenTag.id], assetIds: [asset.id] });
|
||||
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id }));
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(
|
||||
expect.objectContaining({ id: parentTag.id }),
|
||||
);
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(
|
||||
expect.objectContaining({ id: childrenTag.id }),
|
||||
);
|
||||
await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id }));
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(
|
||||
expect.objectContaining({ id: parentTag.id }),
|
||||
);
|
||||
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(
|
||||
expect.objectContaining({ id: childrenTag.id }),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue