Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed enforcing a specific database platform #22

Merged
merged 2 commits into from
Aug 4, 2023
Merged
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
54 changes: 52 additions & 2 deletions src/bundle/Command/DumpSqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +34,17 @@ final class DumpSqlCommand extends Command

private SchemaBuilder $schemaBuilder;

/**
* @phpstan-var array<non-empty-string, class-string<\Doctrine\DBAL\Platforms\AbstractPlatform>>
*/
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;
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
}
Loading