composer require doctrine/dbal
Created
January 14, 2020 20:15
-
-
Save butschster/ffd7189201dc9bbc90cff8e746734b5b to your computer and use it in GitHub Desktop.
Database diagram builder for https://dbdiagram.io/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Doctrine\DBAL\Schema\AbstractSchemaManager; | |
use Illuminate\Console\Command; | |
use Illuminate\Database\ConnectionInterface; | |
use Illuminate\Support\Arr; | |
class BuildDatabaseSchemaDiagramCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'db:diagram-build'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Build database schema diagram'; | |
protected $tables = []; | |
/** | |
* Execute the console command. | |
* @param ConnectionInterface $connection | |
*/ | |
public function handle(ConnectionInterface $connection) | |
{ | |
/** @var AbstractSchemaManager $schema */ | |
$schema = $connection->getDoctrineSchemaManager(); | |
foreach ($schema->listTables() as $table) { | |
$data = [ | |
'columns' => [], | |
'indexes' => [], | |
'refs' => [], | |
]; | |
foreach ($table->getColumns() as $column) { | |
$props = []; | |
if ($column->getNotnull()) { | |
$props[] = 'not null'; | |
} | |
if ($column->getDefault()) { | |
$props[] = 'default: `' . $column->getDefault() . '`'; | |
} | |
if ($column->getComment()) { | |
$props[] = 'note: "' . $column->getComment() . '"'; | |
} | |
$data['columns'][$column->getName()] = [ | |
'type' => strtolower(str_replace('Type', '', class_basename($column->getType()))), | |
'props' => $props, | |
]; | |
} | |
foreach ($table->getIndexes() as $index) { | |
if ($index->isPrimary()) { | |
$data['indexes'][] = [ | |
'columns' => $index->getColumns(), | |
'props' => ['pk'], | |
]; | |
} | |
if ($index->isUnique()) { | |
$data['indexes'][] = [ | |
'columns' => $index->getColumns(), | |
'props' => ['unique'], | |
]; | |
} | |
} | |
foreach ($table->getForeignKeys() as $foreignKey) { | |
$data['refs'][] = [ | |
$foreignKey->getLocalTableName() . '.' . Arr::first($foreignKey->getLocalColumns()), | |
$foreignKey->getForeignTableName() . '.' . Arr::first($foreignKey->getForeignColumns()), | |
]; | |
} | |
$this->tables[$table->getName()] = $data; | |
} | |
foreach ($this->tables as $table => $defs) { | |
echo 'Table ' . $table . ' {' . PHP_EOL; | |
foreach ($defs['columns'] as $key => $data) { | |
echo "\t"; | |
echo $key . ' ' . $data['type']; | |
if (!empty($data['props'])) { | |
echo ' [' . implode(',', $data['props']) . ']'; | |
} | |
echo PHP_EOL; | |
} | |
if (!empty($defs['indexes'])) { | |
echo "\t"; | |
echo 'Indexes {' . PHP_EOL; | |
foreach ($defs['indexes'] as $index) { | |
if (empty($index['props'])) { | |
continue; | |
} | |
echo "\t\t"; | |
echo '(' . implode(', ', $index['columns']) . ') [' . implode(', ', $index['props']) . ']'; | |
echo PHP_EOL; | |
} | |
echo "\t}" . PHP_EOL; | |
} | |
echo '}' . PHP_EOL . PHP_EOL; | |
if (!empty($defs['refs'])) { | |
foreach ($defs['refs'] as $ref) { | |
echo 'Ref: ' . implode(' > ', $ref); | |
echo PHP_EOL . PHP_EOL; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment