Files
project-replica/app/Console/Commands/CheckSchemaChanges.php
2026-03-23 00:51:38 +09:00

72 lines
2.0 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\SourceDatabase;
use App\Services\SchemaComparisonService;
use Illuminate\Console\Command;
class CheckSchemaChanges extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'schema:check-changes {--database= : Specific database ID to check}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for schema changes in source databases';
/**
* Execute the console command.
*/
public function handle(SchemaComparisonService $comparisonService): int
{
$databaseId = $this->option('database');
if ($databaseId) {
$databases = SourceDatabase::where('id', $databaseId)->get();
if ($databases->isEmpty()) {
$this->error("Database with ID {$databaseId} not found.");
return Command::FAILURE;
}
} else {
$databases = SourceDatabase::where('is_active', true)->get();
}
if ($databases->isEmpty()) {
$this->info('No source databases configured.');
return Command::SUCCESS;
}
$totalChanges = 0;
foreach ($databases as $database) {
$this->info("Checking schema changes for: {$database->name}");
$changes = $comparisonService->checkForChanges($database);
if ($changes->isEmpty()) {
$this->info(' No changes detected.');
} else {
$this->warn(" Found {$changes->count()} change(s):");
foreach ($changes as $change) {
$this->line(" - {$change['description']}");
}
$totalChanges += $changes->count();
}
}
$this->newLine();
$this->info("Total changes detected: {$totalChanges}");
return Command::SUCCESS;
}
}