- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
109 lines
4.1 KiB
PHP
109 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Framework\Database\Schema\Commands;
|
|
|
|
use App\Framework\Console\ConsoleCommand;
|
|
use App\Framework\Console\ExitCode;
|
|
use App\Framework\Database\DatabaseManager;
|
|
use App\Framework\Database\Schema\Comparison\SchemaComparator;
|
|
|
|
/**
|
|
* Command to display schema differences between two databases
|
|
*/
|
|
final readonly class SchemaDiffCommand
|
|
{
|
|
public function __construct(
|
|
private DatabaseManager $databaseManager
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Display schema differences
|
|
*
|
|
* @param string $sourceConnection The name of the source connection (default: default)
|
|
* @param string $targetConnection The name of the target connection
|
|
* @param string|null $sourceSchema The name of the source schema (default: public for PostgreSQL, database name for MySQL)
|
|
* @param string|null $targetSchema The name of the target schema (default: public for PostgreSQL, database name for MySQL)
|
|
* @param bool $detailed Whether to show detailed differences
|
|
* @return ExitCode
|
|
*/
|
|
#[ConsoleCommand('db:schema:diff', 'Display schema differences between two databases')]
|
|
public function __invoke(
|
|
string $sourceConnection = 'default',
|
|
string $targetConnection = 'default',
|
|
?string $sourceSchema = null,
|
|
?string $targetSchema = null,
|
|
bool $detailed = false
|
|
): ExitCode {
|
|
try {
|
|
// Get the connections
|
|
$sourceConn = $this->databaseManager->getConnection($sourceConnection);
|
|
$targetConn = $this->databaseManager->getConnection($targetConnection);
|
|
|
|
// Compare the schemas
|
|
$comparator = new SchemaComparator($sourceConn, $targetConn);
|
|
$difference = $comparator->compare($sourceSchema, $targetSchema);
|
|
|
|
if (! $difference->hasDifferences()) {
|
|
echo "No schema differences found.\n";
|
|
|
|
return ExitCode::SUCCESS;
|
|
}
|
|
|
|
// Display a summary of the differences
|
|
echo "Schema Difference Summary:\n";
|
|
echo str_repeat('-', 80) . "\n";
|
|
|
|
$summary = $difference->getSummary();
|
|
echo "Source schema: " . ($difference->sourceSchema ?? 'default') . "\n";
|
|
echo "Target schema: " . ($difference->targetSchema ?? 'default') . "\n";
|
|
echo "\n";
|
|
echo "Missing tables: {$summary['missing_tables']}\n";
|
|
echo "Extra tables: {$summary['extra_tables']}\n";
|
|
echo "Modified tables: " . count($difference->tableDifferences) . "\n";
|
|
|
|
if ($detailed) {
|
|
echo "\nDetailed Differences:\n";
|
|
echo str_repeat('-', 80) . "\n";
|
|
|
|
// Display missing tables
|
|
if (! empty($difference->missingTables)) {
|
|
echo "\nMissing Tables (in target):\n";
|
|
foreach (array_keys($difference->missingTables) as $tableName) {
|
|
echo " - {$tableName}\n";
|
|
}
|
|
}
|
|
|
|
// Display extra tables
|
|
if (! empty($difference->extraTables)) {
|
|
echo "\nExtra Tables (in target):\n";
|
|
foreach (array_keys($difference->extraTables) as $tableName) {
|
|
echo " - {$tableName}\n";
|
|
}
|
|
}
|
|
|
|
// Display table differences
|
|
if (! empty($difference->tableDifferences)) {
|
|
echo "\nTable Differences:\n";
|
|
foreach ($difference->tableDifferences as $tableDiff) {
|
|
echo "\n" . $tableDiff->getDescription();
|
|
}
|
|
}
|
|
} else {
|
|
echo "\nFor detailed differences, use the --detailed flag.\n";
|
|
}
|
|
|
|
return ExitCode::SUCCESS;
|
|
} catch (\Throwable $e) {
|
|
echo "Error comparing schemas: {$e->getMessage()}\n";
|
|
if (isset($_ENV['APP_DEBUG']) && $_ENV['APP_DEBUG']) {
|
|
echo $e->getTraceAsString() . "\n";
|
|
}
|
|
|
|
return ExitCode::SOFTWARE_ERROR;
|
|
}
|
|
}
|
|
}
|