Skip to content

Commit

Permalink
Fix the entitymanager state while replaying
Browse files Browse the repository at this point in the history
  • Loading branch information
pablothedude committed Nov 19, 2024
1 parent 67e7463 commit 13336a7
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ private function clearOldAuthorizations(
->setParameter('role', $role)
->setParameter('institution', $institution->getInstitution())
->execute();

$this->getEntityManager()->clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ public function removeAll(): void
$this
->getEntityManager()
->createQuery(
'DELETE FROM Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Sraa',
'DELETE FROM '.Sraa::class,
)
->execute();

$this->getEntityManager()->clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function removeAll(): void
->where('1 = 1')
->getQuery()
->execute();

$this->getEntityManager()->clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,9 @@ private function replaceAllOfType(string $type, array $newSamlEntities): void
$counter = 0;

$this->removeAllOfType($type);
$entityManager->flush();

foreach ($newSamlEntities as $samlEntity) {
$entityManager->persist($samlEntity);

if (++$counter % 25 === 0) {
$entityManager->flush();
}
}

$entityManager->flush();
Expand All @@ -73,8 +68,10 @@ private function removeAllOfType(string $type): void
$this
->getEntityManager()
->createQuery(
'DELETE FROM Surfnet\StepupMiddleware\GatewayBundle\Entity\SamlEntity se WHERE se.type = :type',
'DELETE FROM '.SamlEntity::class.' se WHERE se.type = :type',
)
->execute(['type' => $type]);

$this->getEntityManager()->clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

namespace Surfnet\StepupMiddleware\GatewayBundle\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Surfnet\Stepup\Identity\Value\Institution;
use Surfnet\StepupMiddleware\GatewayBundle\Entity\WhitelistEntry;

Expand All @@ -27,6 +29,12 @@
*/
class WhitelistEntryRepository extends EntityRepository
{
public function __construct(EntityManagerInterface $em, ClassMetadata $class)
{
parent::__construct($em, $class);
}


/**
* @param Institution[] $institutions
* @return WhitelistEntry[]
Expand All @@ -49,9 +57,6 @@ public function saveEntries(array $whitelistEntries): void
$entityManager = $this->getEntityManager();

foreach ($whitelistEntries as $whitelistEntry) {
if ($this->find($whitelistEntry)) {
continue;
}
$entityManager->persist($whitelistEntry);
}

Expand All @@ -68,6 +73,8 @@ public function removeAll(): void
->where('1 = 1')
->getQuery()
->execute();

$this->getEntityManager()->clear();
}

/**
Expand All @@ -81,6 +88,6 @@ public function remove(array $whitelistEntries): void
$entityManager->remove($whitelistEntry);
}

$entityManager->flush();
$entityManager->clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ services:
arguments:
- middleware: "@doctrine.dbal.middleware_connection"
gateway: "@doctrine.dbal.gateway_connection"
deploy: "@doctrine.dbal.deploy_connection"
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
arguments:
- middleware: "@doctrine.dbal.middleware_connection"
gateway: "@doctrine.dbal.gateway_connection"
deploy: "@doctrine.dbal.deploy_connection"

surfnet_stepup_middleware_middleware.institution_configuration_provider:
class: Surfnet\StepupMiddleware\MiddlewareBundle\Migrations\InstitutionConfiguration\InstitutionConfigurationProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DBALConnectionHelper
*/
public function __construct(array $connections)
{
foreach ($connections as $connection) {
foreach ($connections as $name => $connection) {
if (!$connection instanceof Connection) {
throw InvalidArgumentException::invalidType(Connection::class, 'connection', $connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,28 @@ public function replayEvents(OutputInterface $output, int $increments): void

$preparationProgress->setMessage('Starting Transaction');
$this->connectionHelper->beginTransaction();
$preparationProgress->clear();
$preparationProgress->advance();

try {
$preparationProgress->setMessage('Removing data from Read Tables');
$preparationProgress->clear();
$this->wipeReadTables($output);

$preparationProgress->setMessage('Done wiping');
$preparationProgress->clear();
$preparationProgress->advance();

$preparationProgress->setMessage('Determining amount of events to replay...');
$preparationProgress->clear();
$totalEvents = $this->eventHydrator->getCount();

$preparationProgress->advance();

if ($totalEvents == 0) {
// Spaces are needed to overwrite the previous message.
$preparationProgress->setMessage('There are no events to replay. Done. ');
$preparationProgress->clear();
$preparationProgress->finish();
return;
} else {
Expand All @@ -109,6 +116,7 @@ public function replayEvents(OutputInterface $output, int $increments): void
$increments,
);
$preparationProgress->setMessage($defaultMessage);
$preparationProgress->clear();
$preparationProgress->finish();
}

Expand Down Expand Up @@ -139,6 +147,7 @@ public function replayEvents(OutputInterface $output, int $increments): void

unset($eventStream);
$steps = (($count + $increments < $totalEvents) ? $increments : ($totalEvents - $count));
$preparationProgress->clear();
$replayProgress->advance($steps);
}

Expand Down

0 comments on commit 13336a7

Please sign in to comment.