Fix merging: update the thirdparty related column and not the other one

This commit is contained in:
Julien Fastré 2025-04-14 15:49:51 +02:00
parent d9d151aa89
commit e701e96187
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 39 additions and 28 deletions

View File

@ -25,10 +25,10 @@ class ThirdpartyMergeService
$conn->beginTransaction(); $conn->beginTransaction();
try { try {
$queries = array_merge( $queries = [
$this->updateReferences($toKeep, $toDelete), ...$this->updateReferences($toKeep, $toDelete),
$this->removeThirdparty($toDelete) ...$this->removeThirdparty($toKeep, $toDelete),
); ];
foreach ($queries as $query) { foreach ($queries as $query) {
$conn->executeStatement($query['sql'], $query['params']); $conn->executeStatement($query['sql'], $query['params']);
@ -53,31 +53,26 @@ class ThirdpartyMergeService
$tableName = $meta->getTableName(); $tableName = $meta->getTableName();
foreach ($meta->getAssociationMappings() as $assoc) { foreach ($meta->getAssociationMappings() as $assoc) {
if (ThirdParty::class !== $assoc['targetEntity'] && ThirdParty::class !== $assoc['sourceEntity']) { if (ThirdParty::class !== $assoc['targetEntity']) {
continue; continue;
} }
// phpstan wants boolean for if condition // phpstan wants boolean for if condition
if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) { if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) {
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
$suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : ''; $suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : '';
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn) {
$queries[] = [
'sql' => "UPDATE {$suffix}{$tableName} SET parent_id = (SELECT parent_id FROM chill_3party.third_party WHERE id = :toDelete) WHERE id = :toKeep",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
];
} else {
$queries[] = [ $queries[] = [
'sql' => "UPDATE {$suffix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", 'sql' => "UPDATE {$suffix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
]; ];
} } elseif (ClassMetadata::MANY_TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) {
} elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
$joinTable = $assoc['joinTable']['name']; $joinTable = $assoc['joinTable']['name'];
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; $prefix = null !== ($assoc['joinTable']['schema'] ?? null) ? $assoc['joinTable']['schema'].'.' : '';
$joinColumn = $assoc['joinTable']['inverseJoinColumns'][0]['name'];
$queries[] = [ $queries[] = [
'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete AND NOT EXISTS (SELECT 1 FROM {$joinTable} WHERE {$joinColumn} = :toKeep)", 'sql' => "UPDATE {$prefix}{$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete AND NOT EXISTS (SELECT 1 FROM {$prefix}{$joinTable} WHERE {$joinColumn} = :toKeep)",
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()], 'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
]; ];
@ -92,11 +87,21 @@ class ThirdpartyMergeService
return $queries; return $queries;
} }
public function removeThirdparty(ThirdParty $toDelete): array public function removeThirdparty(ThirdParty $toKeep, ThirdParty $toDelete): array
{ {
return [[ return [
[
'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', 'sql' => 'DELETE FROM chill_3party.third_party WHERE id = :toDelete',
'params' => ['toDelete' => $toDelete->getId()], 'params' => ['toDelete' => $toDelete->getId()],
]]; ],
];
} }
} }

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\ThirdPartyBundle\Tests\Service; namespace Chill\ThirdPartyBundle\Tests\Service;
use Chill\ActivityBundle\Entity\Activity;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory; use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService; use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
@ -49,9 +50,8 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
// Create a related entity with TO_ONE relation (thirdparty parent) // Create a related entity with TO_ONE relation (thirdparty parent)
$relatedToOneEntity = new ThirdParty(); $relatedToOneEntity = new ThirdParty();
$relatedToOneEntity->setName('RelatedToOne thirdparty'); $relatedToOneEntity->setName('RelatedToOne thirdparty');
$relatedToOneEntity->setParent($toDelete);
$this->em->persist($relatedToOneEntity); $this->em->persist($relatedToOneEntity);
$toDelete->setParent($relatedToOneEntity);
$this->em->persist($toDelete);
// Create a related entity with TO_MANY relation (thirdparty category) // Create a related entity with TO_MANY relation (thirdparty category)
$thirdpartyCategory = new ThirdPartyCategory(); $thirdpartyCategory = new ThirdPartyCategory();
@ -60,14 +60,20 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
$toDelete->addCategory($thirdpartyCategory); $toDelete->addCategory($thirdpartyCategory);
$this->em->persist($toDelete); $this->em->persist($toDelete);
$activity = new Activity();
$activity->setDate(new \DateTime());
$activity->addThirdParty($toDelete);
$this->em->persist($activity);
$this->em->flush(); $this->em->flush();
// Run merge // Run merge
$this->service->merge($toKeep, $toDelete); $this->service->merge($toKeep, $toDelete);
$this->em->refresh($toKeep); $this->em->refresh($toKeep);
$this->em->refresh($relatedToOneEntity);
// Check that references were updated // Check that references were updated
$this->assertEquals($toKeep->getParent()->getId(), $relatedToOneEntity->getId(), 'The parent thirdparty was succesfully merged'); $this->assertEquals($toKeep->getId(), $relatedToOneEntity->getParent()->getId(), 'The parent thirdparty was succesfully merged');
$updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $thirdpartyCategory->getId()); $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 was found in the toKeep entity');