WIP started center_history query for duplicate person merge

This commit is contained in:
Julie Lenaerts 2023-07-17 07:50:10 +02:00
parent 860e076b89
commit bb187f5463
4 changed files with 79 additions and 33 deletions

View File

@ -2,9 +2,11 @@
namespace Chill\PersonBundle\Actions\Remove\Handler;
use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use Chill\PersonBundle\Entity\Person;
class PersonMoveAccompanyingPeriodParticipationHandler
class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlHandlerInterface
{
public function supports(string $className, string $field): bool
{
@ -13,6 +15,7 @@ class PersonMoveAccompanyingPeriodParticipationHandler
public function getSqls(string $className, string $field, Person $from, Person $to): array
{
var_dump(__METHOD__);
$insertSql = sprintf(<<<SQL
INSERT INTO chill_person_accompanying_period_participation (person_id, accompanyingperiod_id, id, startdate, enddate)
SELECT %d, accompanyingperiod_id, nextval('chill_person_person_center_history_id_seq'), startdate, enddate
@ -22,7 +25,7 @@ class PersonMoveAccompanyingPeriodParticipationHandler
SELECT 1 FROM chill_person_accompanying_period_participation cpapp2
WHERE
person_id = %d
AND daterange(cpapp.startdate, cpapp.enddate) && daterange(cpapp2.startdate, cpapp2.enddate)
AND (cpapp.startdate, COALESCE(cpapp.enddate, 'infinity'::date)) OVERLAPS (cpapp2.startdate, COALESCE(cpapp2.enddate, 'infinity'::date))
)
SQL, $to->getId(), $from->getId(), $to->getId());
@ -30,6 +33,6 @@ class PersonMoveAccompanyingPeriodParticipationHandler
DELETE FROM chill_person_accompanying_period_participation WHERE person_id = %d
SQL, $from->getId());
return [$sqlInsert, $deleteSql];
return [$insertSql, $deleteSql];
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace Chill\PersonBundle\Actions\Remove\Handler;
use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface;
use Chill\PersonBundle\Entity\Person;
class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
{
public function supports(string $className, string $field): bool
{
return $className === Person\PersonCenterHistory::class;
}
public function getSqls(string $className, string $field, Person $from, Person $to): array
{
$insertSql = sprintf(<<<SQL
INSERT INTO chill_person_person_center_history (id, person_id, center_id, startdate, enddate, createdat, updatedat, createdby_id, updatedby_id)
SELECT nextval('chill_person_person_center_history_id_seq'), %d, center_id, startdate, enddate, createdat, updatedat, createdby_id, updatedby_id
FROM chill_person_person_center_history cppch
WHERE person_id = %d
AND NOT EXISTS (
SELECT 1 FROM chill_person_person_center_history cppch2
WHERE
person_id = %d
AND daterange(cppch.startdate, cppch.enddate) && daterange(cppch2.startdate, cppch2.enddate)
)
SQL, $to->getId(), $from->getId(), $to->getId());
$updateSql = sprintf(<<<SQL
UPDATE chill_person_person_center_history cppch
SET startdate =
WHERE person_id = %d
AND EXISTS (
SELECT 1 FROM chill_person_person_center_history cppch2
WHERE
person_id = %d
AND daterange(cppch.startdate, cppch.enddate) && daterange(cppch2.startdate, cppch2.enddate)
)
SQL, $to->getId());
}
}

View File

@ -78,9 +78,9 @@ class PersonMove
foreach ($metadata->getAssociationMappings() as $field => $mapping) {
if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) {
$sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to));
continue;
if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) {
$sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to));
continue;
}
if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities(), true)) {
@ -152,29 +152,28 @@ class PersonMove
$tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '')
. $mapping['joinTable']['name'];
$sqlInsert = sprintf(
"INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING",
$tableName,
$mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id
$mapping['joinTable']['joinColumns'][0]['name'], // something_else_id
$to->getId(),
$mapping['joinTable']['joinColumns'][0]['name'], // something_else_id
$tableName,
$mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id
$from->getId()
$sqlInsert = sprintf(
"INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING",
$tableName,
$mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id
$mapping['joinTable']['joinColumns'][0]['name'], // something_else_id
$to->getId(),
$mapping['joinTable']['joinColumns'][0]['name'], // something_else_id
$tableName,
$mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id
$from->getId()
);
);
$deleteSql = sprintf(
"DELETE FROM %s WHERE %s = %d",
$tableName,
$mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id
$from->getId()
$deleteSql = sprintf(
"DELETE FROM %s WHERE %s = %d",
$tableName,
$mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id
$from->getId()
);
);
return [
$sqlInsert, $deleteSql
];
return [
$sqlInsert, $deleteSql
];
foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) {
$sets[] = sprintf('%s = %d', $columns['name'], $to->getId());
@ -183,6 +182,7 @@ class PersonMove
foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) {
$conditions[] = sprintf('%s = %d', $columns['name'], $from->getId());
}
} elseif (array_key_exists('joinColumns', $mapping)) {
$tableName = $this->getTableName($metadata);
foreach ($mapping['joinColumns'] as $columns) {
@ -210,7 +210,6 @@ class PersonMove
{
return [
Person\PersonCenterHistory::class,
AccompanyingPeriodParticipation::class,
AccompanyingPeriod\AccompanyingPeriodWork::class,
Relationship::class
];

View File

@ -59,12 +59,13 @@ class PersonMoveTest extends KernelTestCase
{
$move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher);
$sqls = $move->getSQL($personA, $personB);
//$conn = $this->em->getConnection();
$this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
$conn = $this->em->getConnection();
// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
foreach ($sqls as $sql) {
// var_dump($sql);
$conn->executeStatement($sql);
}
});
// });
$personA = $this->em->find(Person::class, $personA->getId());
$personB = $this->em->find(Person::class, $personB->getId());
@ -118,8 +119,6 @@ class PersonMoveTest extends KernelTestCase
->setStartDate(new \DateTimeImmutable('2023-01-01'))
);
$this->em->persist($personA);
$this->em->persist($personB);
$this->em->persist($household);