Skip to content

Commit

Permalink
CLI-1225: The pull:database command fails silently if pv is not insta…
Browse files Browse the repository at this point in the history
…lled. (#1641)

* fix: send gunzip output to stdout when importing db without pv

* add tests

* fix windows

---------

Co-authored-by: Dane Powell <[email protected]>
  • Loading branch information
mdlutz24 and danepowell authored Nov 27, 2023
1 parent 738d610 commit ef27522
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private function importDatabaseDump(string $localDumpFilepath, string $dbHost, s
}
else {
$this->io->warning('Install `pv` to see progress bar');
$command = "gunzip $localDumpFilepath | MYSQL_PWD=$dbPassword mysql --host=$dbHost --user=$dbUser $dbName";
$command = "gunzip -c $localDumpFilepath | MYSQL_PWD=$dbPassword mysql --host=$dbHost --user=$dbUser $dbName";
}

$process = $this->localMachineHelper->executeFromCmd($command, $outputCallback, NULL, ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL));
Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,12 @@ protected function mockCreateMySqlDumpOnLocal(ObjectProphecy $localMachineHelper
}

protected function mockExecutePvExists(
ObjectProphecy $localMachineHelper
ObjectProphecy $localMachineHelper,
bool $pvExists = TRUE
): void {
$localMachineHelper
->commandExists('pv')
->willReturn(TRUE)
->willReturn($pvExists)
->shouldBeCalled();
}

Expand Down
32 changes: 25 additions & 7 deletions tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

/**
* @property \Acquia\Cli\Command\Pull\PullDatabaseCommand $command
Expand Down Expand Up @@ -69,6 +70,15 @@ public function testPullDatabasesLocalConnectionFailure(): void {
], $inputs);
}

public function testPullDatabaseNoPv(): void {
$this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, 0, TRUE, FALSE);
$inputs = $this->getInputs();

$this->executeCommand(['--no-scripts' => TRUE], $inputs);
$output = $this->getDisplay();
$this->assertStringContainsString(' [WARNING] Install `pv` to see progress bar', $output);
}

public function testPullMultipleDatabases(): void {
$this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE);
$inputs = [
Expand Down Expand Up @@ -177,7 +187,7 @@ public function testPullDatabaseWithMySqlImportError(): void {
/**
* @dataProvider providerTestPullDatabaseWithInvalidSslCertificate
*/
public function testPullDatabaseWithInvalidSslCertificate(mixed $errorCode): void {
public function testPullDatabaseWithInvalidSslCertificate(int $errorCode): void {
$this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, $errorCode);
$inputs = $this->getInputs();

Expand All @@ -187,7 +197,7 @@ public function testPullDatabaseWithInvalidSslCertificate(mixed $errorCode): voi
$this->assertStringContainsString('Trying alternative host other.example.com', $output);
}

protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDropSuccessful, bool $mysqlCreateSuccessful, bool $mysqlImportSuccessful, bool $mockIdeFs = FALSE, bool $onDemand = FALSE, bool $mockGetAcsfSites = TRUE, bool $multidb = FALSE, int $curlCode = 0, bool $existingBackups = TRUE): void {
protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDropSuccessful, bool $mysqlCreateSuccessful, bool $mysqlImportSuccessful, bool $mockIdeFs = FALSE, bool $onDemand = FALSE, bool $mockGetAcsfSites = TRUE, bool $multidb = FALSE, int $curlCode = 0, bool $existingBackups = TRUE, bool $pvExists = TRUE): void {
$applicationsResponse = $this->mockApplicationsRequest();
$this->mockApplicationRequest();
$environmentsResponse = $this->mockAcsfEnvironmentsRequest($applicationsResponse);
Expand Down Expand Up @@ -231,8 +241,10 @@ protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDr
// Database.
$this->mockExecuteMySqlDropDb($localMachineHelper, $mysqlDropSuccessful);
$this->mockExecuteMySqlCreateDb($localMachineHelper, $mysqlCreateSuccessful);
$this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful);

$this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful, $pvExists);
if ($multidb) {
$this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful, $pvExists, 'profserv2', 'profserv2dev', 'drupal');
}
$this->command->localMachineHelper = $localMachineHelper->reveal();
$this->command->sshHelper = $sshHelper->reveal();
}
Expand Down Expand Up @@ -291,14 +303,20 @@ protected function mockExecuteMySqlCreateDb(

protected function mockExecuteMySqlImport(
ObjectProphecy $localMachineHelper,
bool $success
bool $success,
bool $pvExists,
string $dbName = 'jxr5000596dev',
string $dbMachineName = 'db554675',
string $localDbName = 'jxr5000596dev'
): void {
$localMachineHelper->checkRequiredBinariesExist(['gunzip', 'mysql'])->shouldBeCalled();
$this->mockExecutePvExists($localMachineHelper);
$this->mockExecutePvExists($localMachineHelper, $pvExists);
$process = $this->mockProcess($success);
$filePath = Path::join(sys_get_temp_dir(), "dev-$dbName-$dbMachineName-2012-05-15T12:00:00Z.sql.gz");
$command = $pvExists ? "pv $filePath --bytes --rate | gunzip | MYSQL_PWD=drupal mysql --host=localhost --user=drupal $localDbName" : "gunzip -c $filePath | MYSQL_PWD=drupal mysql --host=localhost --user=drupal $localDbName";
// MySQL import command.
$localMachineHelper
->executeFromCmd(Argument::type('string'), Argument::type('callable'),
->executeFromCmd($command, Argument::type('callable'),
NULL, TRUE, NULL)
->willReturn($process->reveal())
->shouldBeCalled();
Expand Down

0 comments on commit ef27522

Please sign in to comment.