From 4be6c09d4dffe70e9c83bbf0a4860bb666242ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 19 Nov 2024 10:50:46 +0100 Subject: [PATCH] Add unique constraints to prevent person_documnt and accompanyingcourse_document with duplicated object_id This change ensures `object_id` uniqueness within `person_document` and `accompanyingcourse_document` tables. It includes migration scripts and entity annotations to enforce these constraints. This helps maintain data integrity and consistency across the database. --- .../Entity/AccompanyingCourseDocument.php | 1 + .../ChillDocStoreBundle/Entity/Document.php | 1 + .../Entity/PersonDocument.php | 1 + .../migrations/Version20241118151618.php | 41 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/migrations/Version20241118151618.php diff --git a/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php b/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php index d0e72e74f..9ff4c51fd 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/AccompanyingCourseDocument.php @@ -18,6 +18,7 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table('chill_doc.accompanyingcourse_document')] +#[ORM\UniqueConstraint(name: 'acc_course_document_unique_stored_object', columns: ['object_id'])] class AccompanyingCourseDocument extends Document implements HasScopesInterface, HasCentersInterface { #[ORM\ManyToOne(targetEntity: AccompanyingPeriod::class)] diff --git a/src/Bundle/ChillDocStoreBundle/Entity/Document.php b/src/Bundle/ChillDocStoreBundle/Entity/Document.php index 1970d1127..b98159919 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/Document.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/Document.php @@ -40,6 +40,7 @@ class Document implements TrackCreationInterface, TrackUpdateInterface #[Assert\Valid] #[Assert\NotNull(message: 'Upload a document')] #[ORM\ManyToOne(targetEntity: StoredObject::class, cascade: ['persist'])] + #[ORM\JoinColumn(name: 'object_id', referencedColumnName: 'id')] private ?StoredObject $object = null; #[ORM\ManyToOne(targetEntity: DocGeneratorTemplate::class)] diff --git a/src/Bundle/ChillDocStoreBundle/Entity/PersonDocument.php b/src/Bundle/ChillDocStoreBundle/Entity/PersonDocument.php index b6eefc733..95afaee8d 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/PersonDocument.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/PersonDocument.php @@ -19,6 +19,7 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table('chill_doc.person_document')] +#[ORM\UniqueConstraint(name: 'person_document_unique_stored_object', columns: ['object_id'])] class PersonDocument extends Document implements HasCenterInterface, HasScopeInterface { #[ORM\Id] diff --git a/src/Bundle/ChillDocStoreBundle/migrations/Version20241118151618.php b/src/Bundle/ChillDocStoreBundle/migrations/Version20241118151618.php new file mode 100644 index 000000000..b27d57517 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/migrations/Version20241118151618.php @@ -0,0 +1,41 @@ +addSql(<<<'SQL' + WITH ranked AS ( + SELECT id, rank() OVER (PARTITION BY object_id ORDER BY id ASC) FROM chill_doc.accompanyingcourse_document + ) + DELETE FROM chill_doc.accompanyingcourse_document WHERE id IN (SELECT id FROM ranked where "rank" <> 1) + SQL); + $this->addSql('CREATE UNIQUE INDEX acc_course_document_unique_stored_object ON chill_doc.accompanyingcourse_document (object_id)'); + $this->addSql('CREATE UNIQUE INDEX person_document_unique_stored_object ON chill_doc.person_document (object_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('DROP INDEX acc_course_document_unique_stored_object'); + $this->addSql('DROP INDEX person_document_unique_stored_object'); + } +}