Files
rspade_system/app/RSpade/Commands/Migrate/Migrate_Commit_Command.php
root de4e0438f0 Improve migration and command output verbosity
Add migrate:commit reminder after successful migrations in dev mode
Add --silent flag to rsx:clean and use in framework updates
Add minimal README.md with clone instructions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-22 01:13:35 +00:00

186 lines
6.3 KiB
PHP
Executable File

<?php
namespace App\RSpade\Commands\Migrate;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use App\RSpade\SchemaQuality\SchemaQualityChecker;
use Illuminate\Support\Facades\DB;
use App\RSpade\Core\Database\MigrationPaths;
class Migrate_Commit_Command extends Command
{
protected $signature = 'migrate:commit';
protected $description = 'Commit the migration changes and end the migration session';
protected $backup_dir = '/var/lib/mysql_backup';
protected $flag_file = '/var/www/html/.migrating';
public function handle()
{
// Check if in migration mode
if (!file_exists($this->flag_file)) {
$this->error('[WARNING] No migration session in progress!');
$this->info('Nothing to commit.');
return 1;
}
$session_info = json_decode(file_get_contents($this->flag_file), true);
$started_at = $session_info['started_at'] ?? 'unknown';
$this->info(' Migration session started at: ' . $started_at);
// Check for unran migrations
$this->info("\n Checking for unran migrations...");
$unran_migrations = $this->check_unran_migrations();
if (!empty($unran_migrations)) {
$this->error('[ERROR] Found unran migration files:');
foreach ($unran_migrations as $migration) {
$this->error(' - ' . $migration);
}
$this->info('');
$this->warn('Please either:');
$this->warn(' 1. Run them with: php artisan migrate');
$this->warn(' 2. Remove the migration files');
return 1;
}
$this->info('[OK] All migrations have been executed.');
// Run schema inspection in development mode
if (app()->environment() !== 'production') {
$this->info("\n Running database schema standards check...");
$checker = new SchemaQualityChecker();
$violations = $checker->check();
if ($checker->has_violations()) {
$this->error('[ERROR] Schema standards check failed with ' . $checker->get_violation_count() . ' violation(s):');
$this->info('');
// Display violations
$grouped = $checker->get_violations_by_severity();
foreach (['critical', 'high', 'medium', 'low'] as $severity) {
if (!empty($grouped[$severity])) {
$this->line(strtoupper($severity) . ' VIOLATIONS:');
foreach ($grouped[$severity] as $violation) {
$this->line($violation->format_output());
}
}
}
$this->info('');
$this->error('[ERROR] Rolling back migration session due to schema violations...');
// Execute rollback
$this->call('migrate:rollback');
$this->info('');
$this->warn('[WARNING] Migration has been rolled back. Fix the schema violations and try again.');
return 1;
}
$this->info('[OK] Schema standards check passed.');
}
$this->info("\n Committing migration changes...");
try {
// Step 1: Remove backup directory
if (is_dir($this->backup_dir)) {
$this->info('[1] Removing backup snapshot...');
$this->run_command(['rm', '-rf', $this->backup_dir]);
}
// Step 2: Remove migration flag
$this->info('[2] Removing migration flag...');
unlink($this->flag_file);
// Success
$this->info('');
$this->info('[OK] Migration changes committed successfully!');
$this->info('');
$this->line('• The backup has been deleted');
$this->line('• Migration mode has been disabled');
$this->line('• The web UI is now accessible');
$this->info('');
// Run post-migration tasks in development mode
if (app()->environment() !== 'production') {
// Regenerate model constants from enum definitions
$this->info('Regenerating model constants...');
$this->call('rsx:constants:regenerate');
// Recompile bundles
$this->newLine();
$this->info('Recompiling bundles...');
$this->call('rsx:bundle:compile');
}
return 0;
} catch (\Exception $e) {
$this->error('[ERROR] Failed to commit: ' . $e->getMessage());
return 1;
}
}
/**
* Run a maintenance artisan command
*/
protected function runMaintenanceCommand(string $command): void
{
try {
$this->call($command);
} catch (\Exception $e) {
$this->warn("Warning: Could not run maintenance command {$command}: " . $e->getMessage());
}
}
/**
* Check for unran migrations
*/
protected function check_unran_migrations(): array
{
$unran = [];
// Get list of migration files from all paths
$files = [];
foreach (MigrationPaths::get_all_paths() as $path) {
if (is_dir($path)) {
$path_files = glob($path . '/*.php');
if ($path_files) {
$files = array_merge($files, $path_files);
}
}
}
// Get list of already run migrations from database
$ran_migrations = DB::table('migrations')->pluck('migration')->toArray();
// Check each file
foreach ($files as $file) {
$filename = basename($file, '.php');
if (!in_array($filename, $ran_migrations)) {
$unran[] = $filename;
}
}
return $unran;
}
/**
* Run a shell command
*/
protected function run_command(array $command): string
{
$process = new Process($command);
$process->setTimeout(60);
$process->run();
if (!$process->isSuccessful()) {
throw new \Exception($process->getErrorOutput());
}
return $process->getOutput();
}
}