Skip to content

Commit

Permalink
[DoctrineExtra] New GenerateGraphSchemaCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Oct 15, 2024
1 parent aa87229 commit 1fc9f84
Show file tree
Hide file tree
Showing 7 changed files with 1,069 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends default-mysql-c
#MONGODB
RUN pecl install mongodb-1.17.2 && docker-php-ext-enable mongodb

#Graphviz
RUN apt-get update && apt-get install -y --no-install-recommends graphviz

ARG PUID=1000
ARG PGID=1000

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ composer-normalize:
sudo chmod a+x composer-normalize
docker-compose exec php php composer-normalize

generate-artifact:
docker-compose exec php composer generate:artifact
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@
"test:run:coverage": [
"Composer\\Config::disableProcessTimeout",
"vendor/bin/phpunit --coverage-html ./tmp/phpunit/report"
],
"generate:artifact": [
"Composer\\Config::disableProcessTimeout",
"bin/console draw:doctrine:generate-graph-schema | dot -Tsvg -o doc/databse.svg"
]
},
"minimum-stability": "dev",
Expand Down
926 changes: 926 additions & 0 deletions doc/databse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions packages/doctrine-extra/ORM/Command/GenerateGraphSchemaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Draw\DoctrineExtra\ORM\Command;

use Doctrine\DBAL\Schema\Visitor\Graphviz;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class GenerateGraphSchemaCommand extends Command
{
public function __construct(
private EntityManagerInterface $entityManager)
{
parent::__construct();
}

protected function configure(): void
{
$this
->setName('draw:doctrine:generate-graph-schema')
->setDescription('Get dot from database schema.')
->setHelp(\sprintf('Usage: bin/console %s | dot -Tsvg -o /tmp/databse.svg', $this->getName()))
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->writeln($this->getDot());

return Command::SUCCESS;
}

/**
* Get dot from database schema.
*/
protected function getDot(): string
{
/** @var array<int, ClassMetadata<object>> $metadata */
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();

usort($metadata, static fn (ClassMetadata $a, ClassMetadata $b): int => $a->getTableName() <=> $b->getTableName());

$tool = new SchemaTool($this->entityManager);
$schema = $tool->getSchemaFromMetadata($metadata);

$visitor = new Graphviz();

$visitor->acceptSchema($schema);

foreach ($schema->getTables() as $table) {
$visitor->acceptTable($table);
foreach ($table->getColumns() as $column) {
$visitor->acceptColumn($table, $column);
}
foreach ($table->getIndexes() as $index) {
$visitor->acceptIndex($table, $index);
}
foreach ($table->getForeignKeys() as $foreignKey) {
$visitor->acceptForeignKey($table, $foreignKey);
}
}

foreach ($schema->getSequences() as $sequence) {
$visitor->acceptSequence($sequence);
}

return $visitor->getOutput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Draw\Component\DependencyInjection\Integration\Test\IntegrationTestCase;
use Draw\Component\DependencyInjection\Integration\Test\ServiceConfiguration;
use Draw\DoctrineExtra\DependencyInjection\DoctrineExtraIntegration;
use Draw\DoctrineExtra\ORM\Command\GenerateGraphSchemaCommand;
use Draw\DoctrineExtra\ORM\Command\MysqlDumpCommand;
use Draw\DoctrineExtra\ORM\Command\MysqlImportFileCommand;
use Draw\DoctrineExtra\ORM\EntityHandler;
Expand Down Expand Up @@ -76,6 +77,12 @@ public static function provideTestLoad(): iterable
MysqlDumpCommand::class,
]
),
new ServiceConfiguration(
'draw.doctrine_extra.orm.command.generate_graph_schema_command',
[
GenerateGraphSchemaCommand::class,
]
),
],
[
'doctrine' => [
Expand Down
52 changes: 52 additions & 0 deletions tests/DoctrineExtra/ORM/Command/GenerateGraphSchemaCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Tests\DoctrineExtra\ORM\Command;

use App\Tests\FilteredCommandTestTrait;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService;
use Draw\Component\Tester\Application\CommandDataTester;
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;
use Draw\DoctrineExtra\ORM\Command\GenerateGraphSchemaCommand;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;

/**
* @internal
*/
class GenerateGraphSchemaCommandTest extends KernelTestCase implements AutowiredInterface
{
use FilteredCommandTestTrait;

#[AutowireService(GenerateGraphSchemaCommand::class)]
protected ?Command $command = null;

public function getCommandName(): string
{
return 'draw:doctrine:generate-graph-schema';
}

public static function provideTestArgument(): iterable
{
return [];
}

public static function provideTestOption(): iterable
{
return [];
}

/**
* This test is just to make sure the command still does run.
*
* It will fail when we update to doctrine orm 3.
*/
public function testExecute(): void
{
$this->execute([])
->test(
CommandDataTester::create()
->setExpectedDisplay(null)
)
;
}
}

0 comments on commit 1fc9f84

Please sign in to comment.