From 62a169bb57d2a617bf7fa188768700238c980907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Fri, 7 Jul 2023 10:57:03 +0200 Subject: [PATCH 1/2] Allowed enforcing a specific database platform --- src/bundle/Command/DumpSqlCommand.php | 52 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/bundle/Command/DumpSqlCommand.php b/src/bundle/Command/DumpSqlCommand.php index cbe9e65..50261bc 100644 --- a/src/bundle/Command/DumpSqlCommand.php +++ b/src/bundle/Command/DumpSqlCommand.php @@ -9,6 +9,11 @@ namespace Ibexa\Bundle\DoctrineSchema\Command; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\MariaDb1027Platform; +use Doctrine\DBAL\Platforms\MySQL57Platform; +use Doctrine\DBAL\Platforms\MySQL80Platform; +use Doctrine\DBAL\Platforms\MySqlPlatform; +use Doctrine\DBAL\Platforms\PostgreSQL100Platform; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Schema; @@ -28,6 +33,17 @@ final class DumpSqlCommand extends Command private SchemaBuilder $schemaBuilder; + /** + * @phpstan-var array> + */ + private const PLATFORM_MAP = [ + 'mysql8' => MySQL80Platform::class, + 'mysql57' => MySQL57Platform::class, + 'mysql' => MySqlPlatform::class, + 'mariadb' => MariaDb1027Platform::class, + 'postgres' => PostgreSQL100Platform::class, + ]; + public function __construct(Connection $db, SchemaBuilder $schemaBuilder) { $this->db = $db; @@ -49,6 +65,16 @@ protected function configure(): void InputOption::VALUE_NONE, 'Compare against current database', ); + + $this->addOption( + 'force-platform', + null, + InputOption::VALUE_REQUIRED, + sprintf( + 'Provide a platform name to use. One of: "%s"', + implode('","', array_keys(self::PLATFORM_MAP)), + ), + ); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -60,15 +86,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int $toSchema = $this->schemaBuilder->buildSchema(); } + $platform = $this->getPlatformForInput($input); + if ($input->getOption('compare')) { $schemaManager = $this->getSchemaManager(); $fromSchema = $this->introspectSchema($schemaManager); $comparator = new Comparator(); $diff = $comparator->compare($fromSchema, $toSchema); - $sqls = $diff->toSql($this->db->getDatabasePlatform()); + $sqls = $diff->toSql($platform); } else { - $sqls = $toSchema->toSql($this->db->getDatabasePlatform()); + $sqls = $toSchema->toSql($platform); } $io = new SymfonyStyle($input, $output); @@ -98,4 +126,24 @@ private function introspectSchema(AbstractSchemaManager $schemaManager): Schema { return $schemaManager->createSchema(); } + + private function getPlatformForInput(InputInterface $input) + { + $forcePlatform = $input->getOption('force-platform'); + + if ($forcePlatform === null) { + return $this->db->getDatabasePlatform(); + } + + if (!isset(self::PLATFORM_MAP[$forcePlatform])) { + throw new \InvalidArgumentException(sprintf( + 'Invalid --force-platform option. Received "%s", expected one of: "%s"', + $forcePlatform, + implode('","', array_keys(self::PLATFORM_MAP)), + )); + } + + $platformClass = self::PLATFORM_MAP[$forcePlatform]; + return new $platformClass(); + } } From 96fbe3802fb87c94fd34d39324f33e6d973a4588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Fri, 7 Jul 2023 11:01:16 +0200 Subject: [PATCH 2/2] Allowed enforcing a specific database platform --- src/bundle/Command/DumpSqlCommand.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bundle/Command/DumpSqlCommand.php b/src/bundle/Command/DumpSqlCommand.php index 50261bc..e104b9e 100644 --- a/src/bundle/Command/DumpSqlCommand.php +++ b/src/bundle/Command/DumpSqlCommand.php @@ -9,6 +9,7 @@ namespace Ibexa\Bundle\DoctrineSchema\Command; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL80Platform; @@ -127,7 +128,7 @@ private function introspectSchema(AbstractSchemaManager $schemaManager): Schema return $schemaManager->createSchema(); } - private function getPlatformForInput(InputInterface $input) + private function getPlatformForInput(InputInterface $input): AbstractPlatform { $forcePlatform = $input->getOption('force-platform'); @@ -144,6 +145,7 @@ private function getPlatformForInput(InputInterface $input) } $platformClass = self::PLATFORM_MAP[$forcePlatform]; + return new $platformClass(); } }