Skip to content

Commit

Permalink
Add a progress bar to track the import status
Browse files Browse the repository at this point in the history
  • Loading branch information
toyi committed Sep 19, 2023
1 parent 377bbb6 commit 900555d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
}
},
"require": {
"php": ">=7.4",
"illuminate/console": ">=9",
"phpseclib/phpseclib": "^3.0",
"gabrielelana/byte-units": "^0.5.0"
Expand Down
60 changes: 56 additions & 4 deletions src/SyncDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SFTP;
use phpseclib3\Net\SSH2;
use Symfony\Component\Process\Process;

class SyncDatabaseCommand extends Command
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public function handle()
return 1;
}

$this->delete_local_dump = $this->option('delete-local-dump', true);
$this->delete_local_dump = $this->option('delete-local-dump');

$dump_file = $this->option('dump-file') ? $this->providedDump() : $this->remoteDump();
$this->default_database_config = Config::get('database.connections.' . DB::getDefaultConnection());
Expand All @@ -61,7 +62,23 @@ public function handle()

$this->info("Importing...");

exec('which pv', $output, $code);

$has_pv = $code === 0;

if (!$has_pv) {
$this->warn("The pv binary has not been found. Install it to see the import progress bar.");
}

$import_cmd = [];

if ($has_pv) {
$import_cmd[] = 'pv';
$import_cmd[] = '-n';
$import_cmd[] = $dump_file;
$import_cmd[] = '|';
}

$import_cmd[] = 'mysql';
$import_cmd[] = '-h ' . $this->default_database_config['host'];
if (array_key_exists('port', $this->default_database_config)) {
Expand All @@ -70,9 +87,44 @@ public function handle()
$import_cmd[] = '-u ' . $this->default_database_config['username'];
$import_cmd[] = '-p' . $this->default_database_config['password'];
$import_cmd[] = $this->default_database_config['database'];
$import_cmd[] = ' < ' . $dump_file;
$import_cmd = implode(' ', $import_cmd);
exec($import_cmd);

if (!$has_pv) {
$import_cmd[] = '<';
$import_cmd[] = $dump_file;
}

$import_cmd[] = '2>&1';

$bar = $this->output->createProgressBar(100);

$process = new Process(['bash', '-c', implode(' ', $import_cmd)]);
$process->setTimeout(null);

$process->run(function ($type, $buffer) use ($bar) {
$lines = explode("\n", $buffer);
$lines = array_filter($lines, fn(string $line) => $line !== '');
$buffer = $lines[0];

if(str_starts_with($buffer, 'mysql: [Warning] Using a password')){
return;
}

if (!is_numeric($buffer)) {
$this->warn($buffer);
return;
}

$bar->setProgress((int) $buffer);
});

if (!$process->isSuccessful()) {
$this->error("There was an error during the import.");
return Command::FAILURE;
}

if ($has_pv) {
$bar->finish();
}

if ($this->delete_local_dump) {
exec('rm ' . $dump_file, $output, $code);
Expand Down

0 comments on commit 900555d

Please sign in to comment.