mirror of
https://github.com/immich-app/immich.git
synced 2026-02-10 19:07:55 +03:00
feat: sync stacks (#19629)
This commit is contained in:
@@ -25,6 +25,7 @@ import { PartnerRepository } from 'src/repositories/partner.repository';
|
||||
import { PersonRepository } from 'src/repositories/person.repository';
|
||||
import { SearchRepository } from 'src/repositories/search.repository';
|
||||
import { SessionRepository } from 'src/repositories/session.repository';
|
||||
import { StackRepository } from 'src/repositories/stack.repository';
|
||||
import { StorageRepository } from 'src/repositories/storage.repository';
|
||||
import { SyncCheckpointRepository } from 'src/repositories/sync-checkpoint.repository';
|
||||
import { SyncRepository } from 'src/repositories/sync.repository';
|
||||
@@ -40,6 +41,7 @@ import { FaceSearchTable } from 'src/schema/tables/face-search.table';
|
||||
import { MemoryTable } from 'src/schema/tables/memory.table';
|
||||
import { PersonTable } from 'src/schema/tables/person.table';
|
||||
import { SessionTable } from 'src/schema/tables/session.table';
|
||||
import { StackTable } from 'src/schema/tables/stack.table';
|
||||
import { UserTable } from 'src/schema/tables/user.table';
|
||||
import { BASE_SERVICE_DEPENDENCIES, BaseService } from 'src/services/base.service';
|
||||
import { SyncService } from 'src/services/sync.service';
|
||||
@@ -133,6 +135,19 @@ export class MediumTestContext<S extends BaseService = BaseService> {
|
||||
return { partner, result };
|
||||
}
|
||||
|
||||
async newStack(dto: Omit<Insertable<StackTable>, 'primaryAssetId'>, assetIds: string[]) {
|
||||
const date = factory.date();
|
||||
const stack = {
|
||||
id: factory.uuid(),
|
||||
createdAt: date,
|
||||
updatedAt: date,
|
||||
...dto,
|
||||
};
|
||||
|
||||
const result = await this.get(StackRepository).create(stack, assetIds);
|
||||
return { stack: { ...stack, primaryAssetId: assetIds[0] }, result };
|
||||
}
|
||||
|
||||
async newAsset(dto: Partial<Insertable<AssetTable>> = {}) {
|
||||
const asset = mediumFactory.assetInsert(dto);
|
||||
const result = await this.get(AssetRepository).create(asset);
|
||||
@@ -252,6 +267,7 @@ const newRealRepository = <T>(key: ClassConstructor<T>, db: Kysely<DB>): T => {
|
||||
case PersonRepository:
|
||||
case SearchRepository:
|
||||
case SessionRepository:
|
||||
case StackRepository:
|
||||
case SyncRepository:
|
||||
case SyncCheckpointRepository:
|
||||
case SystemMetadataRepository:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { LoggingRepository } from 'src/repositories/logging.repository';
|
||||
import { PartnerRepository } from 'src/repositories/partner.repository';
|
||||
import { UserRepository } from 'src/repositories/user.repository';
|
||||
import { partners_delete_audit } from 'src/schema/functions';
|
||||
import { partners_delete_audit, stacks_delete_audit } from 'src/schema/functions';
|
||||
import { BaseService } from 'src/services/base.service';
|
||||
import { MediumTestContext } from 'test/medium.factory';
|
||||
import { getKyselyDB } from 'test/utils';
|
||||
@@ -31,6 +31,20 @@ describe('audit', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe(stacks_delete_audit.name, () => {
|
||||
it('should not cascade user deletes to stacks_audit', async () => {
|
||||
const userRepo = ctx.get(UserRepository);
|
||||
const { user } = await ctx.newUser();
|
||||
const { asset: asset1 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { asset: asset2 } = await ctx.newAsset({ ownerId: user.id });
|
||||
await ctx.newStack({ ownerId: user.id }, [asset1.id, asset2.id]);
|
||||
await userRepo.delete(user, true);
|
||||
await expect(
|
||||
ctx.database.selectFrom('stacks_audit').select(['id']).where('userId', '=', user.id).execute(),
|
||||
).resolves.toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('assets_audit', () => {
|
||||
it('should not cascade user deletes to assets_audit', async () => {
|
||||
const userRepo = ctx.get(UserRepository);
|
||||
|
||||
107
server/test/medium/specs/sync/sync-stack.spec.ts
Normal file
107
server/test/medium/specs/sync/sync-stack.spec.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { SyncEntityType, SyncRequestType } from 'src/enum';
|
||||
import { StackRepository } from 'src/repositories/stack.repository';
|
||||
import { DB } from 'src/schema';
|
||||
import { SyncTestContext } from 'test/medium.factory';
|
||||
import { getKyselyDB } from 'test/utils';
|
||||
|
||||
let defaultDatabase: Kysely<DB>;
|
||||
|
||||
const setup = async (db?: Kysely<DB>) => {
|
||||
const ctx = new SyncTestContext(db || defaultDatabase);
|
||||
const { auth, user, session } = await ctx.newSyncAuthUser();
|
||||
return { auth, user, session, ctx };
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
defaultDatabase = await getKyselyDB();
|
||||
});
|
||||
|
||||
describe(SyncEntityType.StackV1, () => {
|
||||
it('should detect and sync the first stack', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const { asset: asset1 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { asset: asset2 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { stack } = await ctx.newStack({ ownerId: user.id }, [asset1.id, asset2.id]);
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.StacksV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.stringContaining('StackV1'),
|
||||
data: {
|
||||
id: stack.id,
|
||||
createdAt: (stack.createdAt as Date).toISOString(),
|
||||
updatedAt: (stack.updatedAt as Date).toISOString(),
|
||||
primaryAssetId: stack.primaryAssetId,
|
||||
ownerId: stack.ownerId,
|
||||
},
|
||||
type: 'StackV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.StacksV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should detect and sync a deleted stack', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const stackRepo = ctx.get(StackRepository);
|
||||
const { asset: asset1 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { asset: asset2 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { stack } = await ctx.newStack({ ownerId: user.id }, [asset1.id, asset2.id]);
|
||||
await stackRepo.delete(stack.id);
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.StacksV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.stringContaining('StackDeleteV1'),
|
||||
data: { stackId: stack.id },
|
||||
type: 'StackDeleteV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.StacksV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should sync a stack and then an update to that same stack', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const stackRepo = ctx.get(StackRepository);
|
||||
const { asset: asset1 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { asset: asset2 } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { stack } = await ctx.newStack({ ownerId: user.id }, [asset1.id, asset2.id]);
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.StacksV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
await ctx.syncAckAll(auth, response);
|
||||
|
||||
await stackRepo.update(stack.id, { primaryAssetId: asset2.id });
|
||||
const newResponse = await ctx.syncStream(auth, [SyncRequestType.StacksV1]);
|
||||
expect(newResponse).toHaveLength(1);
|
||||
expect(newResponse).toEqual([
|
||||
{
|
||||
ack: expect.stringContaining('StackV1'),
|
||||
data: expect.objectContaining({ id: stack.id, primaryAssetId: asset2.id }),
|
||||
type: 'StackV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, newResponse);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.StacksV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should not sync a stack or stack delete for an unrelated user', async () => {
|
||||
const { auth, ctx } = await setup();
|
||||
const stackRepo = ctx.get(StackRepository);
|
||||
const { user: user2 } = await ctx.newUser();
|
||||
const { asset: asset1 } = await ctx.newAsset({ ownerId: user2.id });
|
||||
const { asset: asset2 } = await ctx.newAsset({ ownerId: user2.id });
|
||||
const { stack } = await ctx.newStack({ ownerId: user2.id }, [asset1.id, asset2.id]);
|
||||
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.StacksV1])).resolves.toEqual([]);
|
||||
await stackRepo.delete(stack.id);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.StacksV1])).resolves.toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -14,7 +14,7 @@ const test_fn = registerFunction({
|
||||
})
|
||||
export class Table1 {}
|
||||
|
||||
export const description = 'should create a trigger ';
|
||||
export const description = 'should create a trigger';
|
||||
export const schema: DatabaseSchema = {
|
||||
name: 'postgres',
|
||||
schemaName: 'public',
|
||||
|
||||
Reference in New Issue
Block a user