From 96bd1e093bfa8587205f4413193545c6685d7cd6 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 22 Jun 2023 09:45:19 +0200 Subject: [PATCH] [Messenger] Preserve existing Doctrine schema --- Tests/Transport/DoctrineIntegrationTest.php | 3 +- .../DoctrinePostgreSqlIntegrationTest.php | 3 +- Transport/Connection.php | 38 ++++++++++++++++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Tests/Transport/DoctrineIntegrationTest.php b/Tests/Transport/DoctrineIntegrationTest.php index 2375459..e4e2256 100644 --- a/Tests/Transport/DoctrineIntegrationTest.php +++ b/Tests/Transport/DoctrineIntegrationTest.php @@ -18,7 +18,6 @@ use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; use Doctrine\DBAL\Tools\DsnParser; -use Doctrine\ORM\ORMSetup; use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Bridge\Doctrine\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Bridge\Doctrine\Transport\Connection; @@ -37,7 +36,7 @@ protected function setUp(): void { $dsn = getenv('MESSENGER_DOCTRINE_DSN') ?: 'pdo-sqlite://:memory:'; $params = class_exists(DsnParser::class) ? (new DsnParser())->parse($dsn) : ['url' => $dsn]; - $config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration() : new Configuration(); + $config = new Configuration(); if (class_exists(DefaultSchemaManagerFactory::class)) { $config->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } diff --git a/Tests/Transport/DoctrinePostgreSqlIntegrationTest.php b/Tests/Transport/DoctrinePostgreSqlIntegrationTest.php index 2a0df10..63fe3eb 100644 --- a/Tests/Transport/DoctrinePostgreSqlIntegrationTest.php +++ b/Tests/Transport/DoctrinePostgreSqlIntegrationTest.php @@ -17,7 +17,6 @@ use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; use Doctrine\DBAL\Tools\DsnParser; -use Doctrine\ORM\ORMSetup; use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Bridge\Doctrine\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Bridge\Doctrine\Transport\PostgreSqlConnection; @@ -42,7 +41,7 @@ protected function setUp(): void $url = "pdo-pgsql://postgres:password@$host"; $params = class_exists(DsnParser::class) ? (new DsnParser())->parse($url) : ['url' => $url]; - $config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration() : new Configuration(); + $config = new Configuration(); if (class_exists(DefaultSchemaManagerFactory::class)) { $config->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } diff --git a/Transport/Connection.php b/Transport/Connection.php index 6ce2503..6bb601c 100644 --- a/Transport/Connection.php +++ b/Transport/Connection.php @@ -482,13 +482,39 @@ private function updateSchema(): void $comparator = $this->createComparator($schemaManager); $schemaDiff = $this->compareSchemas($comparator, method_exists($schemaManager, 'introspectSchema') ? $schemaManager->introspectSchema() : $schemaManager->createSchema(), $this->getSchema()); $platform = $this->driverConnection->getDatabasePlatform(); - $queries = method_exists($platform, 'getAlterSchemaSQL') ? $platform->getAlterSchemaSQL($schemaDiff) : $schemaDiff->toSaveSql($platform); + $exec = method_exists($this->driverConnection, 'executeStatement') ? 'executeStatement' : 'exec'; - foreach ($queries as $sql) { - if (method_exists($this->driverConnection, 'executeStatement')) { - $this->driverConnection->executeStatement($sql); - } else { - $this->driverConnection->exec($sql); + if (!method_exists(SchemaDiff::class, 'getCreatedSchemas')) { + foreach ($schemaDiff->toSaveSql($platform) as $sql) { + $this->driverConnection->$exec($sql); + } + + return; + } + + if ($platform->supportsSchemas()) { + foreach ($schemaDiff->getCreatedSchemas() as $schema) { + $this->driverConnection->$exec($platform->getCreateSchemaSQL($schema)); + } + } + + if ($platform->supportsSequences()) { + foreach ($schemaDiff->getAlteredSequences() as $sequence) { + $this->driverConnection->$exec($platform->getAlterSequenceSQL($sequence)); + } + + foreach ($schemaDiff->getCreatedSequences() as $sequence) { + $this->driverConnection->$exec($platform->getCreateSequenceSQL($sequence)); + } + } + + foreach ($platform->getCreateTablesSQL($schemaDiff->getCreatedTables()) as $sql) { + $this->driverConnection->$exec($sql); + } + + foreach ($schemaDiff->getAlteredTables() as $tableDiff) { + foreach ($platform->getAlterTableSQL($tableDiff) as $sql) { + $this->driverConnection->$exec($sql); } } }