Skip to content

Commit

Permalink
Updated DumpSqlCommand to compare against existing database
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Jul 5, 2023
1 parent 5f53917 commit 8f5e2a5
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/bundle/Command/DumpSqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
namespace Ibexa\Bundle\DoctrineSchema\Command;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Ibexa\DoctrineSchema\Builder\SchemaBuilder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -44,16 +47,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$file = $input->getArgument('file');
if ($file !== null) {
$schema = $this->schemaBuilder->importSchemaFromFile($file);
$toSchema = $this->schemaBuilder->importSchemaFromFile($file);
} else {
$schema = $this->schemaBuilder->buildSchema();
$toSchema = $this->schemaBuilder->buildSchema();
}

$schemaManager = $this->getSchemaManager();
$fromSchema = $this->introspectSchema($schemaManager);

$comparator = new Comparator();
$diff = $comparator->compare($fromSchema, $toSchema);

$io = new SymfonyStyle($input, $output);
foreach ($schema->toSql($this->db->getDatabasePlatform()) as $sql) {
foreach ($diff->toSql($this->db->getDatabasePlatform()) as $sql) {
$io->writeln($sql . ';');
}

return self::SUCCESS;
}


private function getSchemaManager(): AbstractSchemaManager
{
return $this->db->getSchemaManager();
}

private function introspectSchema(AbstractSchemaManager $schemaManager): Schema
{
return $schemaManager->createSchema();
}
}

0 comments on commit 8f5e2a5

Please sign in to comment.