Files
rspade_system/app/RSpade/Commands/Migrate/Migrate_Status_Command.php
root f6fac6c4bc Fix bin/publish: copy docs.dist from project root
Fix bin/publish: use correct .env path for rspade_system
Fix bin/publish script: prevent grep exit code 1 from terminating script

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 02:08:33 +00:00

131 lines
4.3 KiB
PHP
Executable File

<?php
namespace App\RSpade\Commands\Migrate;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class Migrate_Status_Command extends Command
{
protected $signature = 'migrate:status';
protected $description = 'Show the status of the current migration session';
protected $flag_file = '/var/www/html/.migrating';
protected $backup_dir = '/var/lib/mysql_backup';
public function handle()
{
// Check environment
$environment = app()->environment();
$this->info("Environment: " . $environment);
$this->info("");
// Check if in migration mode
if (!file_exists($this->flag_file)) {
$this->info('✅ No migration session in progress');
$this->info('');
if ($environment !== 'production') {
$this->line('To start a migration session:');
$this->line(' php artisan migrate:begin');
} else {
$this->line('In production, run migrations directly:');
$this->line(' php artisan migrate');
}
return 0;
}
// Migration session is active
$session_info = json_decode(file_get_contents($this->flag_file), true);
$started_at = $session_info['started_at'] ?? 'unknown';
$started_by = $session_info['started_by'] ?? 'unknown';
$this->warn('🔄 Migration session is ACTIVE');
$this->info('');
$this->line('Session Details:');
$this->line(' Started at: ' . $started_at);
$this->line(' Started by: ' . $started_by);
// Check backup status
if (is_dir($this->backup_dir)) {
$backup_size = $this->get_directory_size($this->backup_dir);
$this->line(' Backup size: ' . $this->format_bytes($backup_size));
$this->info(' Backup status: ✅ Available');
} else {
$this->error(' Backup status: ❌ Missing!');
}
$this->info('');
$this->line('Available Commands:');
$this->line(' • php artisan migrate - Run pending migrations');
$this->line(' • php artisan migrate:commit - Keep changes and end session');
$this->line(' • php artisan migrate:rollback - Restore backup and end session');
// Check database connection
$this->info('');
$this->line('Database Status:');
try {
DB::select('SELECT 1');
$this->info(' Connection: ✅ Active');
// Show pending migrations count
$pending = $this->get_pending_migrations_count();
if ($pending > 0) {
$this->warn(" Pending migrations: $pending");
} else {
$this->info(' Pending migrations: 0');
}
} catch (\Exception $e) {
$this->error(' Connection: ❌ Failed');
$this->error(' Error: ' . $e->getMessage());
}
$this->info('');
$this->warn('⚠️ Web UI is disabled during migration mode (development only)');
return 0;
}
/**
* Get directory size in bytes
*/
protected function get_directory_size($path): int
{
$size = 0;
foreach (glob(rtrim($path, '/').'/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : $this->get_directory_size($each);
}
return $size;
}
/**
* Format bytes to human readable
*/
protected function format_bytes($bytes): string
{
$units = ['B', 'KB', 'MB', 'GB'];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
/**
* Get count of pending migrations
*/
protected function get_pending_migrations_count(): int
{
try {
$migrator = app('migrator');
$files = $migrator->getMigrationFiles($migrator->paths());
$ran = $migrator->getRepository()->getRan();
return count(array_diff(array_keys($files), $ran));
} catch (\Exception $e) {
return -1;
}
}
}