feat(server): people sync (#19854)

* chore: fix missing usage of deleteType for syncMemoriesV1

* chore: add src path for proper absolute imports in jetbrains

* feat: people sync
This commit is contained in:
Zack Pollard 2025-07-10 16:32:42 +01:00 committed by GitHub
parent feff1899ee
commit b19884d01e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 675 additions and 5 deletions

View file

@ -203,3 +203,16 @@ export const stacks_delete_audit = registerFunction({
RETURN NULL;
END`,
});
export const person_delete_audit = registerFunction({
name: 'person_delete_audit',
returnType: 'TRIGGER',
language: 'PLPGSQL',
body: `
BEGIN
INSERT INTO person_audit ("personId", "ownerId")
SELECT "id", "ownerId"
FROM OLD;
RETURN NULL;
END`,
});

View file

@ -11,6 +11,7 @@ import {
memories_delete_audit,
memory_assets_delete_audit,
partners_delete_audit,
person_delete_audit,
stacks_delete_audit,
updated_at,
users_delete_audit,
@ -42,6 +43,7 @@ import { NaturalEarthCountriesTable } from 'src/schema/tables/natural-earth-coun
import { NotificationTable } from 'src/schema/tables/notification.table';
import { PartnerAuditTable } from 'src/schema/tables/partner-audit.table';
import { PartnerTable } from 'src/schema/tables/partner.table';
import { PersonAuditTable } from 'src/schema/tables/person-audit.table';
import { PersonTable } from 'src/schema/tables/person.table';
import { SessionTable } from 'src/schema/tables/session.table';
import { SharedLinkAssetTable } from 'src/schema/tables/shared-link-asset.table';
@ -92,6 +94,7 @@ export class ImmichDatabase {
PartnerAuditTable,
PartnerTable,
PersonTable,
PersonAuditTable,
SessionTable,
SharedLinkAssetTable,
SharedLinkTable,
@ -124,6 +127,7 @@ export class ImmichDatabase {
memories_delete_audit,
memory_assets_delete_audit,
stacks_delete_audit,
person_delete_audit,
];
enum = [assets_status_enum, asset_face_source_type, asset_visibility_enum];
@ -166,6 +170,7 @@ export interface DB {
partners_audit: PartnerAuditTable;
partners: PartnerTable;
person: PersonTable;
person_audit: PersonAuditTable;
sessions: SessionTable;
session_sync_checkpoints: SessionSyncCheckpointTable;
shared_link__asset: SharedLinkAssetTable;

View file

@ -0,0 +1,41 @@
import { Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await sql`CREATE OR REPLACE FUNCTION person_delete_audit()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
INSERT INTO person_audit ("personId", "ownerId")
SELECT "id", "ownerId"
FROM OLD;
RETURN NULL;
END
$$;`.execute(db);
await sql`CREATE TABLE "person_audit" (
"id" uuid NOT NULL DEFAULT immich_uuid_v7(),
"personId" uuid NOT NULL,
"ownerId" uuid NOT NULL,
"deletedAt" timestamp with time zone NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT "PK_46c1ad23490b9312ffaa052aa59" PRIMARY KEY ("id")
);`.execute(db);
await sql`CREATE INDEX "IDX_person_audit_person_id" ON "person_audit" ("personId");`.execute(db);
await sql`CREATE INDEX "IDX_person_audit_owner_id" ON "person_audit" ("ownerId");`.execute(db);
await sql`CREATE INDEX "IDX_person_audit_deleted_at" ON "person_audit" ("deletedAt");`.execute(db);
await sql`CREATE OR REPLACE TRIGGER "person_delete_audit"
AFTER DELETE ON "person"
REFERENCING OLD TABLE AS "old"
FOR EACH STATEMENT
WHEN (pg_trigger_depth() = 0)
EXECUTE FUNCTION person_delete_audit();`.execute(db);
await sql`INSERT INTO "migration_overrides" ("name", "value") VALUES ('function_person_delete_audit', '{"type":"function","name":"person_delete_audit","sql":"CREATE OR REPLACE FUNCTION person_delete_audit()\\n RETURNS TRIGGER\\n LANGUAGE PLPGSQL\\n AS $$\\n BEGIN\\n INSERT INTO person_audit (\\"personId\\", \\"ownerId\\")\\n SELECT \\"id\\", \\"ownerId\\"\\n FROM OLD;\\n RETURN NULL;\\n END\\n $$;"}'::jsonb);`.execute(db);
await sql`INSERT INTO "migration_overrides" ("name", "value") VALUES ('trigger_person_delete_audit', '{"type":"trigger","name":"person_delete_audit","sql":"CREATE OR REPLACE TRIGGER \\"person_delete_audit\\"\\n AFTER DELETE ON \\"person\\"\\n REFERENCING OLD TABLE AS \\"old\\"\\n FOR EACH STATEMENT\\n WHEN (pg_trigger_depth() = 0)\\n EXECUTE FUNCTION person_delete_audit();"}'::jsonb);`.execute(db);
}
export async function down(db: Kysely<any>): Promise<void> {
await sql`DROP TRIGGER "person_delete_audit" ON "person";`.execute(db);
await sql`DROP TABLE "person_audit";`.execute(db);
await sql`DROP FUNCTION person_delete_audit;`.execute(db);
await sql`DELETE FROM "migration_overrides" WHERE "name" = 'function_person_delete_audit';`.execute(db);
await sql`DELETE FROM "migration_overrides" WHERE "name" = 'trigger_person_delete_audit';`.execute(db);
}

View file

@ -0,0 +1,17 @@
import { PrimaryGeneratedUuidV7Column } from 'src/decorators';
import { Column, CreateDateColumn, Generated, Table, Timestamp } from 'src/sql-tools';
@Table('person_audit')
export class PersonAuditTable {
@PrimaryGeneratedUuidV7Column()
id!: Generated<string>;
@Column({ type: 'uuid', indexName: 'IDX_person_audit_person_id' })
personId!: string;
@Column({ type: 'uuid', indexName: 'IDX_person_audit_owner_id' })
ownerId!: string;
@CreateDateColumn({ default: () => 'clock_timestamp()', indexName: 'IDX_person_audit_deleted_at' })
deletedAt!: Generated<Timestamp>;
}

View file

@ -1,7 +1,9 @@
import { UpdatedAtTrigger, UpdateIdColumn } from 'src/decorators';
import { person_delete_audit } from 'src/schema/functions';
import { AssetFaceTable } from 'src/schema/tables/asset-face.table';
import { UserTable } from 'src/schema/tables/user.table';
import {
AfterDeleteTrigger,
Check,
Column,
CreateDateColumn,
@ -15,6 +17,12 @@ import {
@Table('person')
@UpdatedAtTrigger('person_updated_at')
@AfterDeleteTrigger({
scope: 'statement',
function: person_delete_audit,
referencingOldTableAs: 'old',
when: 'pg_trigger_depth() = 0',
})
@Check({ name: 'CHK_b0f82b0ed662bfc24fbb58bb45', expression: `"birthDate" <= CURRENT_DATE` })
export class PersonTable {
@PrimaryGeneratedColumn('uuid')