Skip to content

Commit a6f9225

Browse files
authored
feat: migration delete tracks streaming tables (#614)
1 parent 3b0b15f commit a6f9225

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

packages/PdoEventSourcing/src/Database/EventStreamTableManager.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,26 @@ public function createTable(Connection $connection): void
8484

8585
public function dropTable(Connection $connection): void
8686
{
87+
if ($this->isInitialized($connection)) {
88+
$this->dropStreamTables($connection);
89+
}
8790
$connection->executeStatement($this->getDropTableSql($connection));
8891
}
8992

93+
private function dropStreamTables(Connection $connection): void
94+
{
95+
$streamTableNames = $connection->fetchFirstColumn(
96+
"SELECT stream_name FROM {$this->tableName}"
97+
);
98+
99+
foreach ($streamTableNames as $streamTableName) {
100+
$dropSql = $this->isPostgres($connection)
101+
? "DROP TABLE IF EXISTS {$streamTableName}"
102+
: "DROP TABLE IF EXISTS `{$streamTableName}`";
103+
$connection->executeStatement($dropSql);
104+
}
105+
}
106+
90107
public function isInitialized(Connection $connection): bool
91108
{
92109
return SchemaManagerCompatibility::tableExists($connection, $this->tableName);

packages/PdoEventSourcing/tests/Integration/EventStreamTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Integration;
66

7+
use Ecotone\EventSourcing\Database\EventStreamTableManager;
78
use Ecotone\EventSourcing\EventStore;
89
use Ecotone\EventSourcing\Prooph\LazyProophEventStore;
910
use Ecotone\Lite\EcotoneLite;
@@ -14,8 +15,10 @@
1415
use Enqueue\Dbal\DbalConnectionFactory;
1516
use Ramsey\Uuid\Uuid;
1617
use Test\Ecotone\EventSourcing\EventSourcingMessagingTestCase;
18+
use Test\Ecotone\EventSourcing\Fixture\Ticket\Command\RegisterTicket;
1719
use Test\Ecotone\EventSourcing\Fixture\Ticket\Event\TicketWasClosed;
1820
use Test\Ecotone\EventSourcing\Fixture\Ticket\Event\TicketWasRegistered;
21+
use Test\Ecotone\EventSourcing\Fixture\Ticket\Ticket;
1922
use Test\Ecotone\EventSourcing\Fixture\Ticket\TicketEventConverter;
2023
use Test\Ecotone\EventSourcing\Fixture\TicketWithSynchronousEventDrivenProjection\InProgressTicketList;
2124

@@ -303,4 +306,38 @@ public function test_fetching_with_pagination()
303306
$events[0]->getPayload()
304307
);
305308
}
309+
310+
public function test_deleting_event_stream_table_also_deletes_stream_tables(): void
311+
{
312+
$ecotone = EcotoneLite::bootstrapFlowTestingWithEventStore(
313+
containerOrAvailableServices: [
314+
new InProgressTicketList($this->getConnection()),
315+
new TicketEventConverter(),
316+
DbalConnectionFactory::class => $this->getConnectionFactory(),
317+
],
318+
configuration: ServiceConfiguration::createWithDefaults()
319+
->withEnvironment('prod')
320+
->withSkippedModulePackageNames(ModulePackageList::allPackagesExcept([ModulePackageList::EVENT_SOURCING_PACKAGE]))
321+
->withNamespaces([
322+
'Test\Ecotone\EventSourcing\Fixture\Ticket',
323+
]),
324+
pathToRootCatalog: __DIR__ . '/../../',
325+
runForProductionEventStore: true
326+
);
327+
328+
$ecotone->sendCommand(new RegisterTicket('1', 'johny', 'alert'));
329+
330+
$connection = $this->getConnection();
331+
$eventStreamsTable = LazyProophEventStore::DEFAULT_STREAM_TABLE;
332+
$streamTableName = '_' . sha1(Ticket::class);
333+
334+
$this->assertTrue(self::tableExists($connection, $eventStreamsTable));
335+
$this->assertTrue(self::tableExists($connection, $streamTableName));
336+
337+
$tableManager = new EventStreamTableManager($eventStreamsTable, true, true);
338+
$tableManager->dropTable($connection);
339+
340+
$this->assertFalse(self::tableExists($connection, $streamTableName));
341+
$this->assertFalse(self::tableExists($connection, $eventStreamsTable));
342+
}
306343
}

0 commit comments

Comments
 (0)