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

[DoctrineExtra] New GenerateGraphSchemaCommand #300

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
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
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)
)
;
}
}
Loading