mirror of https://github.com/immich-app/immich.git
feat: sync stacks (#19629)
parent
095ace8687
commit
181a7e115f
@ -0,0 +1,99 @@
|
||||
//
|
||||
// AUTO-GENERATED FILE, DO NOT MODIFY!
|
||||
//
|
||||
// @dart=2.18
|
||||
|
||||
// ignore_for_file: unused_element, unused_import
|
||||
// ignore_for_file: always_put_required_named_parameters_first
|
||||
// ignore_for_file: constant_identifier_names
|
||||
// ignore_for_file: lines_longer_than_80_chars
|
||||
|
||||
part of openapi.api;
|
||||
|
||||
class SyncStackDeleteV1 {
|
||||
/// Returns a new [SyncStackDeleteV1] instance.
|
||||
SyncStackDeleteV1({
|
||||
required this.stackId,
|
||||
});
|
||||
|
||||
String stackId;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => identical(this, other) || other is SyncStackDeleteV1 &&
|
||||
other.stackId == stackId;
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
// ignore: unnecessary_parenthesis
|
||||
(stackId.hashCode);
|
||||
|
||||
@override
|
||||
String toString() => 'SyncStackDeleteV1[stackId=$stackId]';
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = <String, dynamic>{};
|
||||
json[r'stackId'] = this.stackId;
|
||||
return json;
|
||||
}
|
||||
|
||||
/// Returns a new [SyncStackDeleteV1] instance and imports its values from
|
||||
/// [value] if it's a [Map], null otherwise.
|
||||
// ignore: prefer_constructors_over_static_methods
|
||||
static SyncStackDeleteV1? fromJson(dynamic value) {
|
||||
upgradeDto(value, "SyncStackDeleteV1");
|
||||
if (value is Map) {
|
||||
final json = value.cast<String, dynamic>();
|
||||
|
||||
return SyncStackDeleteV1(
|
||||
stackId: mapValueOfType<String>(json, r'stackId')!,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static List<SyncStackDeleteV1> listFromJson(dynamic json, {bool growable = false,}) {
|
||||
final result = <SyncStackDeleteV1>[];
|
||||
if (json is List && json.isNotEmpty) {
|
||||
for (final row in json) {
|
||||
final value = SyncStackDeleteV1.fromJson(row);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList(growable: growable);
|
||||
}
|
||||
|
||||
static Map<String, SyncStackDeleteV1> mapFromJson(dynamic json) {
|
||||
final map = <String, SyncStackDeleteV1>{};
|
||||
if (json is Map && json.isNotEmpty) {
|
||||
json = json.cast<String, dynamic>(); // ignore: parameter_assignments
|
||||
for (final entry in json.entries) {
|
||||
final value = SyncStackDeleteV1.fromJson(entry.value);
|
||||
if (value != null) {
|
||||
map[entry.key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// maps a json object with a list of SyncStackDeleteV1-objects as value to a dart map
|
||||
static Map<String, List<SyncStackDeleteV1>> mapListFromJson(dynamic json, {bool growable = false,}) {
|
||||
final map = <String, List<SyncStackDeleteV1>>{};
|
||||
if (json is Map && json.isNotEmpty) {
|
||||
// ignore: parameter_assignments
|
||||
json = json.cast<String, dynamic>();
|
||||
for (final entry in json.entries) {
|
||||
map[entry.key] = SyncStackDeleteV1.listFromJson(entry.value, growable: growable,);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/// The list of required keys that must be present in a JSON.
|
||||
static const requiredKeys = <String>{
|
||||
'stackId',
|
||||
};
|
||||
}
|
||||
|
||||
@ -0,0 +1,131 @@
|
||||
//
|
||||
// AUTO-GENERATED FILE, DO NOT MODIFY!
|
||||
//
|
||||
// @dart=2.18
|
||||
|
||||
// ignore_for_file: unused_element, unused_import
|
||||
// ignore_for_file: always_put_required_named_parameters_first
|
||||
// ignore_for_file: constant_identifier_names
|
||||
// ignore_for_file: lines_longer_than_80_chars
|
||||
|
||||
part of openapi.api;
|
||||
|
||||
class SyncStackV1 {
|
||||
/// Returns a new [SyncStackV1] instance.
|
||||
SyncStackV1({
|
||||
required this.createdAt,
|
||||
required this.id,
|
||||
required this.ownerId,
|
||||
required this.primaryAssetId,
|
||||
required this.updatedAt,
|
||||
});
|
||||
|
||||
DateTime createdAt;
|
||||
|
||||
String id;
|
||||
|
||||
String ownerId;
|
||||
|
||||
String primaryAssetId;
|
||||
|
||||
DateTime updatedAt;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => identical(this, other) || other is SyncStackV1 &&
|
||||
other.createdAt == createdAt &&
|
||||
other.id == id &&
|
||||
other.ownerId == ownerId &&
|
||||
other.primaryAssetId == primaryAssetId &&
|
||||
other.updatedAt == updatedAt;
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
// ignore: unnecessary_parenthesis
|
||||
(createdAt.hashCode) +
|
||||
(id.hashCode) +
|
||||
(ownerId.hashCode) +
|
||||
(primaryAssetId.hashCode) +
|
||||
(updatedAt.hashCode);
|
||||
|
||||
@override
|
||||
String toString() => 'SyncStackV1[createdAt=$createdAt, id=$id, ownerId=$ownerId, primaryAssetId=$primaryAssetId, updatedAt=$updatedAt]';
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = <String, dynamic>{};
|
||||
json[r'createdAt'] = this.createdAt.toUtc().toIso8601String();
|
||||
json[r'id'] = this.id;
|
||||
json[r'ownerId'] = this.ownerId;
|
||||
json[r'primaryAssetId'] = this.primaryAssetId;
|
||||
json[r'updatedAt'] = this.updatedAt.toUtc().toIso8601String();
|
||||
return json;
|
||||
}
|
||||
|
||||
/// Returns a new [SyncStackV1] instance and imports its values from
|
||||
/// [value] if it's a [Map], null otherwise.
|
||||
// ignore: prefer_constructors_over_static_methods
|
||||
static SyncStackV1? fromJson(dynamic value) {
|
||||
upgradeDto(value, "SyncStackV1");
|
||||
if (value is Map) {
|
||||
final json = value.cast<String, dynamic>();
|
||||
|
||||
return SyncStackV1(
|
||||
createdAt: mapDateTime(json, r'createdAt', r'')!,
|
||||
id: mapValueOfType<String>(json, r'id')!,
|
||||
ownerId: mapValueOfType<String>(json, r'ownerId')!,
|
||||
primaryAssetId: mapValueOfType<String>(json, r'primaryAssetId')!,
|
||||
updatedAt: mapDateTime(json, r'updatedAt', r'')!,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static List<SyncStackV1> listFromJson(dynamic json, {bool growable = false,}) {
|
||||
final result = <SyncStackV1>[];
|
||||
if (json is List && json.isNotEmpty) {
|
||||
for (final row in json) {
|
||||
final value = SyncStackV1.fromJson(row);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList(growable: growable);
|
||||
}
|
||||
|
||||
static Map<String, SyncStackV1> mapFromJson(dynamic json) {
|
||||
final map = <String, SyncStackV1>{};
|
||||
if (json is Map && json.isNotEmpty) {
|
||||
json = json.cast<String, dynamic>(); // ignore: parameter_assignments
|
||||
for (final entry in json.entries) {
|
||||
final value = SyncStackV1.fromJson(entry.value);
|
||||
if (value != null) {
|
||||
map[entry.key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// maps a json object with a list of SyncStackV1-objects as value to a dart map
|
||||
static Map<String, List<SyncStackV1>> mapListFromJson(dynamic json, {bool growable = false,}) {
|
||||
final map = <String, List<SyncStackV1>>{};
|
||||
if (json is Map && json.isNotEmpty) {
|
||||
// ignore: parameter_assignments
|
||||
json = json.cast<String, dynamic>();
|
||||
for (final entry in json.entries) {
|
||||
map[entry.key] = SyncStackV1.listFromJson(entry.value, growable: growable,);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/// The list of required keys that must be present in a JSON.
|
||||
static const requiredKeys = <String>{
|
||||
'createdAt',
|
||||
'id',
|
||||
'ownerId',
|
||||
'primaryAssetId',
|
||||
'updatedAt',
|
||||
};
|
||||
}
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
import { Kysely, sql } from 'kysely';
|
||||
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await sql`CREATE TABLE "stacks_audit" ("id" uuid NOT NULL DEFAULT immich_uuid_v7(), "stackId" uuid NOT NULL, "userId" uuid NOT NULL, "deletedAt" timestamp with time zone NOT NULL DEFAULT clock_timestamp());`.execute(db);
|
||||
await sql`ALTER TABLE "asset_stack" ADD "createdAt" timestamp with time zone NOT NULL DEFAULT now();`.execute(db);
|
||||
await sql`ALTER TABLE "asset_stack" ADD "updatedAt" timestamp with time zone NOT NULL DEFAULT now();`.execute(db);
|
||||
await sql`ALTER TABLE "asset_stack" ADD "updateId" uuid NOT NULL DEFAULT immich_uuid_v7();`.execute(db);
|
||||
await sql`ALTER TABLE "stacks_audit" ADD CONSTRAINT "PK_dbe4ec648fa032e8973297de07e" PRIMARY KEY ("id");`.execute(db);
|
||||
await sql`CREATE INDEX "IDX_stacks_audit_deleted_at" ON "stacks_audit" ("deletedAt")`.execute(db);
|
||||
await sql`CREATE OR REPLACE FUNCTION stacks_delete_audit()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE PLPGSQL
|
||||
AS $$
|
||||
BEGIN
|
||||
INSERT INTO stacks_audit ("stackId", "userId")
|
||||
SELECT "id", "ownerId"
|
||||
FROM OLD;
|
||||
RETURN NULL;
|
||||
END
|
||||
$$;`.execute(db);
|
||||
await sql`CREATE OR REPLACE TRIGGER "stacks_delete_audit"
|
||||
AFTER DELETE ON "asset_stack"
|
||||
REFERENCING OLD TABLE AS "old"
|
||||
FOR EACH STATEMENT
|
||||
WHEN (pg_trigger_depth() = 0)
|
||||
EXECUTE FUNCTION stacks_delete_audit();`.execute(db);
|
||||
await sql`CREATE OR REPLACE TRIGGER "stacks_updated_at"
|
||||
BEFORE UPDATE ON "asset_stack"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION updated_at();`.execute(db);
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await sql`DROP TRIGGER "stacks_delete_audit" ON "asset_stack";`.execute(db);
|
||||
await sql`DROP TRIGGER "stacks_updated_at" ON "asset_stack";`.execute(db);
|
||||
await sql`DROP INDEX "IDX_stacks_audit_deleted_at";`.execute(db);
|
||||
await sql`ALTER TABLE "stacks_audit" DROP CONSTRAINT "PK_dbe4ec648fa032e8973297de07e";`.execute(db);
|
||||
await sql`ALTER TABLE "asset_stack" DROP COLUMN "createdAt";`.execute(db);
|
||||
await sql`ALTER TABLE "asset_stack" DROP COLUMN "updatedAt";`.execute(db);
|
||||
await sql`ALTER TABLE "asset_stack" DROP COLUMN "updateId";`.execute(db);
|
||||
await sql`DROP TABLE "stacks_audit";`.execute(db);
|
||||
await sql`DROP FUNCTION stacks_delete_audit;`.execute(db);
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
import 'src/schema/tables/activity.table';
|
||||
import 'src/schema/tables/album-asset.table';
|
||||
import 'src/schema/tables/album-user.table';
|
||||
import 'src/schema/tables/album.table';
|
||||
import 'src/schema/tables/api-key.table';
|
||||
import 'src/schema/tables/asset-audit.table';
|
||||
import 'src/schema/tables/asset-face.table';
|
||||
import 'src/schema/tables/asset-files.table';
|
||||
import 'src/schema/tables/asset-job-status.table';
|
||||
import 'src/schema/tables/asset.table';
|
||||
import 'src/schema/tables/audit.table';
|
||||
import 'src/schema/tables/exif.table';
|
||||
import 'src/schema/tables/face-search.table';
|
||||
import 'src/schema/tables/geodata-places.table';
|
||||
import 'src/schema/tables/library.table';
|
||||
import 'src/schema/tables/memory-asset.table';
|
||||
import 'src/schema/tables/memory.table';
|
||||
import 'src/schema/tables/move.table';
|
||||
import 'src/schema/tables/natural-earth-countries.table';
|
||||
import 'src/schema/tables/partner-audit.table';
|
||||
import 'src/schema/tables/partner.table';
|
||||
import 'src/schema/tables/person.table';
|
||||
import 'src/schema/tables/session.table';
|
||||
import 'src/schema/tables/shared-link-asset.table';
|
||||
import 'src/schema/tables/shared-link.table';
|
||||
import 'src/schema/tables/smart-search.table';
|
||||
import 'src/schema/tables/stack.table';
|
||||
import 'src/schema/tables/sync-checkpoint.table';
|
||||
import 'src/schema/tables/system-metadata.table';
|
||||
import 'src/schema/tables/tag-asset.table';
|
||||
import 'src/schema/tables/tag-closure.table';
|
||||
import 'src/schema/tables/user-audit.table';
|
||||
import 'src/schema/tables/user-metadata.table';
|
||||
import 'src/schema/tables/user.table';
|
||||
import 'src/schema/tables/version-history.table';
|
||||
@ -0,0 +1,17 @@
|
||||
import { PrimaryGeneratedUuidV7Column } from 'src/decorators';
|
||||
import { Column, CreateDateColumn, Generated, Table, Timestamp } from 'src/sql-tools';
|
||||
|
||||
@Table('stacks_audit')
|
||||
export class StackAuditTable {
|
||||
@PrimaryGeneratedUuidV7Column()
|
||||
id!: Generated<string>;
|
||||
|
||||
@Column({ type: 'uuid' })
|
||||
stackId!: string;
|
||||
|
||||
@Column({ type: 'uuid' })
|
||||
userId!: string;
|
||||
|
||||
@CreateDateColumn({ default: () => 'clock_timestamp()', indexName: 'IDX_stacks_audit_deleted_at' })
|
||||
deletedAt!: Generated<Timestamp>;
|
||||
}
|
||||
@ -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([]);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue