diff --git a/src/bundle/Command/DumpSqlCommand.php b/src/bundle/Command/DumpSqlCommand.php index cbe9e65..e104b9e 100644 --- a/src/bundle/Command/DumpSqlCommand.php +++ b/src/bundle/Command/DumpSqlCommand.php @@ -9,6 +9,12 @@ 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; +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 +34,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 +66,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 +87,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 +127,25 @@ private function introspectSchema(AbstractSchemaManager $schemaManager): Schema { return $schemaManager->createSchema(); } + + private function getPlatformForInput(InputInterface $input): AbstractPlatform + { + $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(); + } }