Last active
October 21, 2021 06:05
-
-
Save DominikStyp/2765e0d461810bfca29f6c416d4cc100 to your computer and use it in GitHub Desktop.
Dump Database Table to SQL - Laravel Ready Command (cross platform using Symfony Process)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Process\Exception\ProcessFailedException; | |
use Symfony\Component\Process\Process; | |
class DumpTableDataToSQL extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'dump-table-to-sql {table : table to dump}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Dumps table structure and data to SQL'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$table = $this->argument('table'); | |
$targetFile = "database/schema/{$table}.sql"; | |
if(!is_dir($targetDir = dirname($targetFile))){ | |
mkdir($targetDir, 0777, true); | |
} | |
$this->line("Dumping table {$table}"); | |
$passwordPart = empty(config('database.connections.mysql.password')) ? '' : '-p"${:PASSWORD}"'; | |
$command = 'mysqldump -u "${:USERNAME}" -h "${:DB_HOST}" '.$passwordPart.' "${:DB_NAME}" "${:TABLE}" > "${:TARGET_FILE}"'; | |
// we use cross-platform process factory | |
// @see: https://symfony.com/doc/current/components/process.html | |
$process = Process::fromShellCommandline($command, null, [ | |
'USERNAME' => config('database.connections.mysql.username'), | |
'PASSWORD' => config('database.connections.mysql.password'), | |
'DB_HOST' => config('database.connections.mysql.host'), | |
'DB_NAME' => config('database.connections.mysql.database'), | |
'TABLE' => $table, | |
'TARGET_FILE' => $targetFile | |
]); | |
$this->line( json_encode($process->getEnv(), JSON_PRETTY_PRINT) ); | |
$this->line( $process->getCommandLine() ); | |
$process->run(); | |
// if something is wrong we throw an exception | |
if (!$process->isSuccessful()) { | |
throw new ProcessFailedException($process); | |
} | |
$this->line($process->getOutput()); | |
$this->line("table dumped to: $targetFile"); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment