Add sudo support for migrate commands when not running as root
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
|
||||
class Migrate_Rollback_Command extends Command
|
||||
{
|
||||
use PrivilegedCommandTrait;
|
||||
protected $signature = 'migrate:rollback';
|
||||
protected $description = 'Rollback the database to the snapshot taken at migrate:begin';
|
||||
|
||||
@@ -42,29 +43,29 @@ class Migrate_Rollback_Command extends Command
|
||||
try {
|
||||
// Step 1: Stop MySQL using supervisorctl
|
||||
$this->info('[1] Stopping MySQL server...');
|
||||
shell_exec('supervisorctl stop mysql 2>&1');
|
||||
|
||||
$this->shell_exec_privileged('supervisorctl stop mysql 2>&1');
|
||||
|
||||
// Wait a moment for process to die
|
||||
sleep(3);
|
||||
|
||||
|
||||
// Step 2: Clear current MySQL data
|
||||
$this->info('[2] Clearing current database data...');
|
||||
// Use shell_exec to clear directory contents instead of removing directory
|
||||
shell_exec("rm -rf {$this->mysql_data_dir}/* 2>/dev/null");
|
||||
shell_exec("rm -rf {$this->mysql_data_dir}/.* 2>/dev/null");
|
||||
|
||||
$this->shell_exec_privileged("rm -rf {$this->mysql_data_dir}/* 2>/dev/null");
|
||||
$this->shell_exec_privileged("rm -rf {$this->mysql_data_dir}/.* 2>/dev/null");
|
||||
|
||||
// Step 3: Restore backup
|
||||
$this->info('[3] Restoring database snapshot...');
|
||||
shell_exec("cp -r {$this->backup_dir}/* {$this->mysql_data_dir}/");
|
||||
shell_exec("cp -r {$this->backup_dir}/.[^.]* {$this->mysql_data_dir}/ 2>/dev/null");
|
||||
|
||||
$this->shell_exec_privileged("cp -r {$this->backup_dir}/* {$this->mysql_data_dir}/");
|
||||
$this->shell_exec_privileged("cp -r {$this->backup_dir}/.[^.]* {$this->mysql_data_dir}/ 2>/dev/null");
|
||||
|
||||
// Step 4: Fix permissions (MySQL needs to own the files)
|
||||
$this->info('[4] Setting correct permissions...');
|
||||
$this->run_command(['chown', '-R', 'mysql:mysql', $this->mysql_data_dir]);
|
||||
|
||||
$this->run_privileged_command(['chown', '-R', 'mysql:mysql', $this->mysql_data_dir]);
|
||||
|
||||
// Step 5: Start MySQL using supervisorctl
|
||||
$this->info('[5] Starting MySQL server...');
|
||||
shell_exec('supervisorctl start mysql 2>&1');
|
||||
$this->shell_exec_privileged('supervisorctl start mysql 2>&1');
|
||||
|
||||
// Step 6: Wait for MySQL to be ready
|
||||
$this->info('[6] Waiting for MySQL to be ready...');
|
||||
@@ -91,31 +92,15 @@ class Migrate_Rollback_Command extends Command
|
||||
} catch (\Exception $e) {
|
||||
$this->error('[ERROR] Rollback failed: ' . $e->getMessage());
|
||||
$this->error('Manual intervention may be required!');
|
||||
|
||||
|
||||
// Try to restart MySQL
|
||||
$this->info('Attempting to restart MySQL...');
|
||||
shell_exec('supervisorctl start mysql 2>&1');
|
||||
|
||||
$this->shell_exec_privileged('supervisorctl start mysql 2>&1');
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a shell command and check for errors
|
||||
*/
|
||||
protected function run_command(array $command, bool $throw_on_error = true): string
|
||||
{
|
||||
$process = new Process($command);
|
||||
$process->setTimeout(60);
|
||||
$process->run();
|
||||
|
||||
if ($throw_on_error && !$process->isSuccessful()) {
|
||||
throw new ProcessFailedException($process);
|
||||
}
|
||||
|
||||
return $process->getOutput();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for MySQL to be ready for connections
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user