Skip to content

Commit

Permalink
Fix event clearing and deletion issues, ensuring proper cleanup and r…
Browse files Browse the repository at this point in the history
…emoval of events.
  • Loading branch information
butschster committed Oct 2, 2024
1 parent 1e287da commit e2a2d1b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
8 changes: 7 additions & 1 deletion app/modules/Events/Application/EventsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Application\Broadcasting\EventMapperRegistryInterface;
use App\Application\Persistence\DriverEnum;
use Cycle\Database\DatabaseInterface;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
Expand Down Expand Up @@ -38,7 +39,12 @@ public function defineSingletons(): array
EventRepository::class => static fn(
ORMInterface $orm,
EntityManagerInterface $manager,
): EventRepository => new EventRepository($manager, new Select($orm, Event::class)),
DatabaseInterface $db
): EventRepository => new EventRepository(
em: $manager,
db: $db,
select: new Select($orm, Event::class)
),
];
}

Expand Down
21 changes: 16 additions & 5 deletions app/modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,34 @@
use Modules\Projects\Domain\ValueObject\Key;

#[Entity(
role: Event::ROLE,
repository: EventRepositoryInterface::class,
table: Event::TABLE_NAME,
)]
#[Index(columns: ['type'])]
#[Index(columns: ['project'])]
class Event
{
public const TABLE_NAME = 'events';
public const ROLE = 'event';

public const UUID = 'uuid';
public const TYPE = 'type';
public const PAYLOAD = 'payload';
public const TIMESTAMP = 'timestamp';
public const PROJECT = 'project';

/** @internal */
public function __construct(
#[Column(type: 'string(36)', primary: true, typecast: 'uuid')]
#[Column(type: 'string(36)', name: self::UUID, primary: true, typecast: 'uuid')]
private Uuid $uuid,
#[Column(type: 'string(50)')]
#[Column(type: 'string(50)', name: self::TYPE)]
private string $type,
#[Column(type: 'json', typecast: Json::class)]
#[Column(type: 'json', name: self::PAYLOAD, typecast: Json::class)]
private Json $payload,
#[Column(type: 'string(25)', typecast: Timestamp::class)]
#[Column(type: 'string(25)', name: self::TIMESTAMP, typecast: Timestamp::class)]
private Timestamp $timestamp,
#[Column(type: 'string', nullable: true, typecast: Key::class)]
#[Column(type: 'string', name: self::PROJECT, nullable: true, typecast: Key::class)]
private ?Key $project = null,
) {}

Expand Down
33 changes: 8 additions & 25 deletions app/modules/Events/Integration/CycleOrm/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Modules\Events\Integration\CycleOrm;

use Cycle\Database\DatabaseInterface;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Select\Repository;
Expand All @@ -18,6 +19,7 @@ final class EventRepository extends Repository implements EventRepositoryInterfa
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly DatabaseInterface $db,
Select $select,
) {
parent::__construct($select);
Expand All @@ -39,36 +41,17 @@ public function store(Event $event): bool

public function deleteAll(array $scope = []): void
{
$events = $this->select()
$this->db
->delete(Event::TABLE_NAME)
->where($this->buildScope($scope))
->fetchAll();

$batchSize = 100;

$batch = 0;
foreach ($events as $event) {
$this->em->delete($event);

if (++$batch % $batchSize === 0) {
$this->em->run();
$batch = 0;
}
}

$this->em->run();
->run();
}

public function deleteByPK(string $uuid): bool
{
$event = $this->findByPK($uuid);

if ($event === null) {
return false;
}

$this->em->delete($event)->run();

return true;
return $this->db->delete(Event::TABLE_NAME)
->where(Event::UUID, $uuid)
->run() > 0;
}

public function countAll(array $scope = []): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Application\Commands\ClearEvents;
use Modules\Events\Domain\EventRepositoryInterface;
use Modules\Events\Domain\Events\EventsWasClear;
use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ValueObject\Key;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Cqrs\Attribute\CommandHandler;

Expand All @@ -20,7 +22,7 @@ public function __construct(
#[CommandHandler]
public function __invoke(ClearEvents $command): void
{
$scope = ['project' => $command->project];
$scope = ['project' => $command->project ?? (string) Key::create(Project::DEFAULT_KEY)];

if ($command->type) {
$scope['type'] = $command->type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(DeleteEvent $command): void
$this->dispatcher->dispatch(
new EventWasDeleted(
uuid: $command->uuid,
project: $event->getProject()?->value,
project: $event->getProject()?->__toString(),
),
);
}
Expand Down

0 comments on commit e2a2d1b

Please sign in to comment.