From 47ae0232de64534f76b7c0cab0ae648f54ab19e0 Mon Sep 17 00:00:00 2001 From: team 1 Date: Tue, 17 Feb 2026 14:59:56 +0100 Subject: [PATCH] harden document delete and rebuild faiss index --- migrations/Version20260217135550.php | 39 ++++++++++++++++++++++++++++ src/Entity/Document.php | 18 +++++++++---- src/Service/DocumentService.php | 19 -------------- 3 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 migrations/Version20260217135550.php diff --git a/migrations/Version20260217135550.php b/migrations/Version20260217135550.php new file mode 100644 index 0000000..24fe10d --- /dev/null +++ b/migrations/Version20260217135550.php @@ -0,0 +1,39 @@ +addSql('ALTER TABLE document DROP FOREIGN KEY `FK_D8698A769407EE77`'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A769407EE77 FOREIGN KEY (current_version_id) REFERENCES document_version (id) ON DELETE SET NULL'); + $this->addSql('ALTER TABLE document_version DROP FOREIGN KEY `FK_1B73751FC33F7837`'); + $this->addSql('ALTER TABLE document_version ADD CONSTRAINT FK_1B73751FC33F7837 FOREIGN KEY (document_id) REFERENCES document (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE ingest_profile CHANGE id id BINARY(16) NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A769407EE77'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT `FK_D8698A769407EE77` FOREIGN KEY (current_version_id) REFERENCES document_version (id)'); + $this->addSql('ALTER TABLE document_version DROP FOREIGN KEY FK_1B73751FC33F7837'); + $this->addSql('ALTER TABLE document_version ADD CONSTRAINT `FK_1B73751FC33F7837` FOREIGN KEY (document_id) REFERENCES document (id)'); + $this->addSql('ALTER TABLE ingest_profile CHANGE id id VARBINARY(16) NOT NULL'); + } +} diff --git a/src/Entity/Document.php b/src/Entity/Document.php index 106b7c4..f49730e 100644 --- a/src/Entity/Document.php +++ b/src/Entity/Document.php @@ -1,6 +1,5 @@ status; @@ -94,8 +100,10 @@ class Document public function addVersion(DocumentVersion $version): void { - $this->versions->add($version); - $version->setDocument($this); + if (!$this->versions->contains($version)) { + $this->versions->add($version); + $version->setDocument($this); + } } public function setCurrentVersion(?DocumentVersion $version): void diff --git a/src/Service/DocumentService.php b/src/Service/DocumentService.php index c71f934..9f1a86a 100644 --- a/src/Service/DocumentService.php +++ b/src/Service/DocumentService.php @@ -97,27 +97,8 @@ class DocumentService $this->em->flush(); } - /** - * HARD DELETE - * - * Entfernt das Dokument vollstÀndig aus der Datenbank. - * Chunks und Vector-Index werden NICHT hier behandelt, - * sondern im Ingest-Job (TYPE_DOCUMENT_DELETE). - */ public function delete(Document $document): void { - // 1. FK-Zyklus auflösen - $document->setCurrentVersion(null); - $this->em->flush(); // <-- WICHTIG: zuerst FK nullen! - - // 2. Versionen entfernen (falls kein cascade remove existiert) - foreach ($document->getVersions() as $version) { - $this->em->remove($version); - } - - $this->em->flush(); // <-- Versionen löschen - - // 3. Dokument löschen $this->em->remove($document); $this->em->flush(); }