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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ phpunit-integration: vendor

.PHONY: phpunit-integration-postgres
phpunit-integration-postgres: vendor ## run phpunit integration tests on postgres
DB_URL="pdo-pgsql://postgres:postgres@localhost:5432/eventstore?charset=utf8" vendor/bin/phpunit --testsuite=integration
DB_URL="pdo-pgsql://postgres:postgres@127.0.0.1:5432/eventstore?charset=utf8" vendor/bin/phpunit --testsuite=integration

.PHONY: phpunit-integration-mysql
phpunit-integration-mysql: vendor ## run phpunit integration tests on mysql
DB_URL="pdo-mysql://root@localhost:3306/eventstore?charset=utf8" vendor/bin/phpunit --testsuite=integration
DB_URL="pdo-mysql://root@127.0.0.1:3306/eventstore?charset=utf8" vendor/bin/phpunit --testsuite=integration

.PHONY: phpunit-unit
phpunit-unit: vendor ## run phpunit unit tests
Expand Down
6 changes: 6 additions & 0 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<code><![CDATA[archive]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Schema/DoctrineHelper.php">
<InternalMethod>
<code><![CDATA[getParams]]></code>
<code><![CDATA[getParams]]></code>
</InternalMethod>
</file>
<file src="src/Schema/DoctrineSchemaDirector.php">
<InternalMethod>
<code><![CDATA[getName]]></code>
Expand Down
54 changes: 27 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Cryptography/DoctrineCipherKeyStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Patchlevel\EventSourcing\Schema\DoctrineHelper;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator;
use Patchlevel\Hydrator\Cryptography\Cipher\CipherKey;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyNotExists;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function remove(string $id): void

public function configureSchema(Schema $schema, Connection $connection): void
{
if ($connection !== $this->connection) {
if (!DoctrineHelper::sameDatabase($this->connection, $connection)) {
return;
}

Expand Down
44 changes: 44 additions & 0 deletions src/Schema/DoctrineHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Schema;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Throwable;

use function bin2hex;
use function random_bytes;
use function sprintf;

final class DoctrineHelper
{
public static function sameDatabase(Connection $connectionA, Connection $connectionB): bool
{
if ($connectionA === $connectionB) {
return true;
}

if ($connectionA->getParams() === $connectionB->getParams()) {
return true;
}

$checkTable = 'same_db_check_' . bin2hex(random_bytes(7));
$connectionA->executeStatement(sprintf('CREATE TABLE %s (id INTEGER NOT NULL)', $checkTable));

try {
$connectionB->executeStatement(sprintf('DROP TABLE %s', $checkTable));
} catch (Throwable) {
// ignore
}

try {
$connectionA->executeStatement(sprintf('DROP TABLE %s', $checkTable));

return false;
} catch (TableNotFoundException) {
return true;
}
}
}
3 changes: 2 additions & 1 deletion src/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Message\Serializer\HeadersSerializer;
use Patchlevel\EventSourcing\Schema\DoctrineHelper;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Patchlevel\EventSourcing\Store\Criteria\AggregateIdCriterion;
Expand Down Expand Up @@ -314,7 +315,7 @@ public function transactional(Closure $function): void

public function configureSchema(Schema $schema, Connection $connection): void
{
if ($this->connection !== $connection) {
if (!DoctrineHelper::sameDatabase($this->connection, $connection)) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Store/StreamDoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Message\Serializer\HeadersSerializer;
use Patchlevel\EventSourcing\Schema\DoctrineHelper;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Patchlevel\EventSourcing\Store\Criteria\ArchivedCriterion;
Expand Down Expand Up @@ -396,7 +397,7 @@ public function archive(Criteria|null $criteria = null): void

public function configureSchema(Schema $schema, Connection $connection): void
{
if ($this->connection !== $connection) {
if (!DoctrineHelper::sameDatabase($this->connection, $connection)) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Subscription/Store/DoctrineSubscriptionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\Schema\DoctrineHelper;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator;
use Patchlevel\EventSourcing\Subscription\RunMode;
use Patchlevel\EventSourcing\Subscription\Status;
Expand Down Expand Up @@ -208,6 +209,10 @@ public function inLock(Closure $closure): mixed

public function configureSchema(Schema $schema, Connection $connection): void
{
if (!DoctrineHelper::sameDatabase($this->connection, $connection)) {
return;
}

$table = $schema->createTable($this->tableName);

$table->addColumn('id', Types::STRING)
Expand Down
9 changes: 9 additions & 0 deletions tests/DbalManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Tools\DsnParser;
use Patchlevel\EventSourcing\Console\DoctrineHelper;
use RuntimeException;
Expand Down Expand Up @@ -45,6 +46,14 @@ public static function createConnection(string $dbName = self::DEFAULT_DB_NAME):
$databases = $schemaManager->listDatabases();

if (in_array($dbName, $databases, true)) {
if ($tempConnection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$tempConnection->executeStatement("
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = '{$dbName}';
");
}

$schemaManager->dropDatabase($dbName);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Integration/Store/DoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector;
Expand Down Expand Up @@ -224,4 +225,38 @@ public function testLoad(): void
$stream?->close();
}
}

public function testConfigureSchemaSameDatabase(): void
{
$connection = DbalManager::createConnection();
$otherConnection = DbalManager::createConnection();

$store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
);

$schema = new Schema();

$store->configureSchema($schema, $otherConnection);

self::assertTrue($schema->hasTable('eventstore'));
}

public function testConfigureSchemaNotSameDatabase(): void
{
$connection = DbalManager::createConnection();
$otherConnection = DbalManager::createConnection('other');

$store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
);

$schema = new Schema();

$store->configureSchema($schema, $otherConnection);

self::assertFalse($schema->hasTable('eventstore'));
}
}
Loading
Loading