Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/PdoEventSourcing/src/Database/EventStreamTableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,26 @@ public function createTable(Connection $connection): void

public function dropTable(Connection $connection): void
{
if ($this->isInitialized($connection)) {
$this->dropStreamTables($connection);
}
$connection->executeStatement($this->getDropTableSql($connection));
}

private function dropStreamTables(Connection $connection): void
{
$streamTableNames = $connection->fetchFirstColumn(
"SELECT stream_name FROM {$this->tableName}"
);

foreach ($streamTableNames as $streamTableName) {
$dropSql = $this->isPostgres($connection)
? "DROP TABLE IF EXISTS {$streamTableName}"
: "DROP TABLE IF EXISTS `{$streamTableName}`";
$connection->executeStatement($dropSql);
}
}

public function isInitialized(Connection $connection): bool
{
return SchemaManagerCompatibility::tableExists($connection, $this->tableName);
Expand Down
37 changes: 37 additions & 0 deletions packages/PdoEventSourcing/tests/Integration/EventStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Integration;

use Ecotone\EventSourcing\Database\EventStreamTableManager;
use Ecotone\EventSourcing\EventStore;
use Ecotone\EventSourcing\Prooph\LazyProophEventStore;
use Ecotone\Lite\EcotoneLite;
Expand All @@ -14,8 +15,10 @@
use Enqueue\Dbal\DbalConnectionFactory;
use Ramsey\Uuid\Uuid;
use Test\Ecotone\EventSourcing\EventSourcingMessagingTestCase;
use Test\Ecotone\EventSourcing\Fixture\Ticket\Command\RegisterTicket;
use Test\Ecotone\EventSourcing\Fixture\Ticket\Event\TicketWasClosed;
use Test\Ecotone\EventSourcing\Fixture\Ticket\Event\TicketWasRegistered;
use Test\Ecotone\EventSourcing\Fixture\Ticket\Ticket;
use Test\Ecotone\EventSourcing\Fixture\Ticket\TicketEventConverter;
use Test\Ecotone\EventSourcing\Fixture\TicketWithSynchronousEventDrivenProjection\InProgressTicketList;

Expand Down Expand Up @@ -303,4 +306,38 @@ public function test_fetching_with_pagination()
$events[0]->getPayload()
);
}

public function test_deleting_event_stream_table_also_deletes_stream_tables(): void
{
$ecotone = EcotoneLite::bootstrapFlowTestingWithEventStore(
containerOrAvailableServices: [
new InProgressTicketList($this->getConnection()),
new TicketEventConverter(),
DbalConnectionFactory::class => $this->getConnectionFactory(),
],
configuration: ServiceConfiguration::createWithDefaults()
->withEnvironment('prod')
->withSkippedModulePackageNames(ModulePackageList::allPackagesExcept([ModulePackageList::EVENT_SOURCING_PACKAGE]))
->withNamespaces([
'Test\Ecotone\EventSourcing\Fixture\Ticket',
]),
pathToRootCatalog: __DIR__ . '/../../',
runForProductionEventStore: true
);

$ecotone->sendCommand(new RegisterTicket('1', 'johny', 'alert'));

$connection = $this->getConnection();
$eventStreamsTable = LazyProophEventStore::DEFAULT_STREAM_TABLE;
$streamTableName = '_' . sha1(Ticket::class);

$this->assertTrue(self::tableExists($connection, $eventStreamsTable));
$this->assertTrue(self::tableExists($connection, $streamTableName));

$tableManager = new EventStreamTableManager($eventStreamsTable, true, true);
$tableManager->dropTable($connection);

$this->assertFalse(self::tableExists($connection, $streamTableName));
$this->assertFalse(self::tableExists($connection, $eventStreamsTable));
}
}