harden document delete and rebuild faiss index

This commit is contained in:
team 1
2026-02-17 14:59:56 +01:00
parent fcd9488a18
commit 47ae0232de
3 changed files with 52 additions and 24 deletions

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20260217135550 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() 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) 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');
}
}

View File

@@ -1,6 +1,5 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
@@ -31,10 +30,18 @@ class Document
#[ORM\Column]
private \DateTimeImmutable $createdAt;
#[ORM\OneToMany(mappedBy: 'document', targetEntity: DocumentVersion::class, cascade: ['persist'], orphanRemoval: true)]
// 🔥 REMOVE ergänzt
#[ORM\OneToMany(
mappedBy: 'document',
targetEntity: DocumentVersion::class,
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
private Collection $versions;
// 🔥 onDelete ergänzt
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?DocumentVersion $currentVersion = null;
public function __construct()
@@ -65,7 +72,6 @@ class Document
return $this;
}
public function getStatus(): string
{
return $this->status;
@@ -94,9 +100,11 @@ class Document
public function addVersion(DocumentVersion $version): void
{
if (!$this->versions->contains($version)) {
$this->versions->add($version);
$version->setDocument($this);
}
}
public function setCurrentVersion(?DocumentVersion $version): void
{

View File

@@ -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();
}