mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-11-07 20:58:24 +00:00
Fix the fusion of thirdparty properties that are located in another schema...
This commit is contained in:
6
.changes/unreleased/Fixed-20251106-123641.yaml
Normal file
6
.changes/unreleased/Fixed-20251106-123641.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
kind: Fixed
|
||||
body: Fix the fusion of thirdparty properties that are located in another schema than public for TO_ONE relations + add extra loop for MANY_TO_MANY relations where thirdparty is the source instead of the target
|
||||
time: 2025-11-06T12:36:41.555991969+01:00
|
||||
custom:
|
||||
Issue: "457"
|
||||
SchemaChange: No schema change
|
||||
@@ -69,10 +69,11 @@ readonly class ThirdpartyMergeService
|
||||
if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) {
|
||||
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
||||
|
||||
$suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : '';
|
||||
$schema = $meta->getSchemaName();
|
||||
$prefix = null !== $schema && '' !== $schema ? $schema.'.' : '';
|
||||
|
||||
$queries[] = [
|
||||
'sql' => "UPDATE {$suffix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
||||
'sql' => "UPDATE {$prefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
];
|
||||
} elseif (ClassMetadata::MANY_TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) {
|
||||
@@ -85,13 +86,36 @@ readonly class ThirdpartyMergeService
|
||||
];
|
||||
|
||||
$queries[] = [
|
||||
'sql' => "DELETE FROM {$joinTable} WHERE {$joinColumn} = :toDelete",
|
||||
'sql' => "DELETE FROM {$prefix}{$joinTable} WHERE {$joinColumn} = :toDelete",
|
||||
'params' => ['toDelete' => $toDelete->getId()],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Also handle many-to-many where ThirdParty is the source
|
||||
$thirdPartyMeta = $this->em->getClassMetadata(ThirdParty::class);
|
||||
foreach ($thirdPartyMeta->getAssociationMappings() as $assoc) {
|
||||
if (ClassMetadata::MANY_TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) {
|
||||
$joinTable = $assoc['joinTable']['name'];
|
||||
$prefix = null !== ($assoc['joinTable']['schema'] ?? null) ? $assoc['joinTable']['schema'].'.' : '';
|
||||
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; // Note: joinColumns, not inverseJoinColumns
|
||||
|
||||
// Get the other column name to build proper duplicate check
|
||||
$otherColumn = $assoc['joinTable']['inverseJoinColumns'][0]['name'];
|
||||
|
||||
$queries[] = [
|
||||
'sql' => "UPDATE {$prefix}{$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete AND NOT EXISTS (SELECT 1 FROM {$prefix}{$joinTable} AS t2 WHERE t2.{$joinColumn} = :toKeep AND t2.{$otherColumn} = {$prefix}{$joinTable}.{$otherColumn})",
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
];
|
||||
|
||||
$queries[] = [
|
||||
'sql' => "DELETE FROM {$prefix}{$joinTable} WHERE {$joinColumn} = :toDelete",
|
||||
'params' => ['toDelete' => $toDelete->getId()],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $queries;
|
||||
}
|
||||
|
||||
@@ -102,10 +126,6 @@ readonly class ThirdpartyMergeService
|
||||
'sql' => 'UPDATE chill_3party.third_party SET parent_id = :toKeep WHERE parent_id = :toDelete',
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
],
|
||||
[
|
||||
'sql' => 'UPDATE chill_3party.thirdparty_category SET thirdparty_id = :toKeep WHERE thirdparty_id = :toDelete AND NOT EXISTS (SELECT 1 FROM chill_3party.thirdparty_category WHERE thirdparty_id = :toKeep)',
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
],
|
||||
[
|
||||
'sql' => 'DELETE FROM chill_3party.third_party WHERE id = :toDelete',
|
||||
'params' => ['toDelete' => $toDelete->getId()],
|
||||
|
||||
@@ -12,6 +12,7 @@ declare(strict_types=1);
|
||||
namespace Chill\ThirdPartyBundle\Tests\Service;
|
||||
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
|
||||
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
|
||||
@@ -47,19 +48,20 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
$toDelete->setName('Thirdparty to delete');
|
||||
$this->em->persist($toDelete);
|
||||
|
||||
// Create a related entity with TO_ONE relation (thirdparty parent)
|
||||
// Create a related entity with TO_ONE relation (thirdparty parent) - tests schema handling for TO_ONE
|
||||
$relatedToOneEntity = new ThirdParty();
|
||||
$relatedToOneEntity->setName('RelatedToOne thirdparty');
|
||||
$relatedToOneEntity->setParent($toDelete);
|
||||
$this->em->persist($relatedToOneEntity);
|
||||
|
||||
// Create a related entity with TO_MANY relation (thirdparty category)
|
||||
// Create a related entity with MANY_TO_MANY relation (thirdparty category) - tests schema handling for MANY_TO_MANY where ThirdParty is target
|
||||
$thirdpartyCategory = new ThirdPartyCategory();
|
||||
$thirdpartyCategory->setName(['fr' => 'Thirdparty category']);
|
||||
$this->em->persist($thirdpartyCategory);
|
||||
$toDelete->addCategory($thirdpartyCategory);
|
||||
$this->em->persist($toDelete);
|
||||
|
||||
// Test MANY_TO_MANY relation from another bundle (Activity) - tests cross-bundle schema handling
|
||||
$activity = new Activity();
|
||||
$activity->setDate(new \DateTime());
|
||||
$activity->addThirdParty($toDelete);
|
||||
@@ -73,14 +75,55 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
$this->em->refresh($relatedToOneEntity);
|
||||
|
||||
// Check that references were updated
|
||||
$this->assertEquals($toKeep->getId(), $relatedToOneEntity->getParent()->getId(), 'The parent thirdparty was succesfully merged');
|
||||
|
||||
// Test TO_ONE relation in chill_3party schema was properly handled
|
||||
$this->assertEquals($toKeep->getId(), $relatedToOneEntity->getParent()->getId(), 'The parent thirdparty in chill_3party schema was successfully merged');
|
||||
|
||||
// Test MANY_TO_MANY relation in chill_3party schema was properly handled
|
||||
$updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $thirdpartyCategory->getId());
|
||||
$this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity');
|
||||
$this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category in chill_3party schema was found in the toKeep entity');
|
||||
|
||||
// Test MANY_TO_MANY relation from different schema (Activity bundle) was properly handled
|
||||
$this->em->refresh($activity);
|
||||
$this->assertContains($toKeep, $activity->getThirdParties(), 'The activity relation from different schema was successfully merged');
|
||||
$this->assertNotContains($toDelete, $activity->getThirdParties(), 'The toDelete thirdparty was removed from activity relation');
|
||||
|
||||
// Check that toDelete was removed
|
||||
$this->em->clear();
|
||||
$deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId());
|
||||
$this->assertNull($deletedThirdParty);
|
||||
$this->assertNull($deletedThirdParty, 'The toDelete thirdparty was successfully removed');
|
||||
}
|
||||
|
||||
public function testMergeWithSharedCenterDoesNotCauseUniqueConstraintViolation(): void
|
||||
{
|
||||
// Create a center that will be shared by both thirdparties
|
||||
$sharedCenter = new Center();
|
||||
$sharedCenter->setName('Shared Center');
|
||||
$this->em->persist($sharedCenter);
|
||||
|
||||
// Create ThirdParty entities
|
||||
$toKeep = new ThirdParty();
|
||||
$toKeep->setName('Thirdparty to keep');
|
||||
$toKeep->addCenter($sharedCenter); // Both thirdparties linked to same center
|
||||
$this->em->persist($toKeep);
|
||||
|
||||
$toDelete = new ThirdParty();
|
||||
$toDelete->setName('Thirdparty to delete');
|
||||
$toDelete->addCenter($sharedCenter); // Both thirdparties linked to same center
|
||||
$this->em->persist($toDelete);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// This should not throw a unique constraint violation
|
||||
$this->service->merge($toKeep, $toDelete);
|
||||
|
||||
// Verify that toKeep still has the shared center
|
||||
$this->em->refresh($toKeep);
|
||||
$this->assertContains($sharedCenter, $toKeep->getCenters(), 'The shared center is still linked to the kept thirdparty');
|
||||
|
||||
// Verify that toDelete was removed
|
||||
$this->em->clear();
|
||||
$deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId());
|
||||
$this->assertNull($deletedThirdParty, 'The toDelete thirdparty was successfully removed');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user