Skip to content

Commit

Permalink
allow pull:db
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Oct 17, 2024
1 parent 407f282 commit 4bdb647
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 39 deletions.
6 changes: 0 additions & 6 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,17 +525,11 @@ protected function getLocalFilesDir(string $site): string
/**
* @param string|null $site
* @return DatabaseResponse[]
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function determineCloudDatabases(Client $acquiaCloudClient, EnvironmentResponse $chosenEnvironment, string $site = null, bool $multipleDbs = false): array
{
$databasesRequest = new Databases($acquiaCloudClient);
$databases = $databasesRequest->getAll($chosenEnvironment->uuid);
foreach ($databases as $database) {
if ($database->user_name === null) {
throw new AcquiaCliException('Database connection details missing');
}
}

if (count($databases) === 1) {
$this->logger->debug('Only a single database detected on Cloud');
Expand Down
1 change: 1 addition & 0 deletions src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ protected function pullCode(InputInterface $input, OutputInterface $output, bool
/**
* @param bool $onDemand Force on-demand backup.
* @param bool $noImport Skip import.
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function pullDatabase(InputInterface $input, OutputInterface $output, EnvironmentResponse $sourceEnvironment, bool $onDemand = false, bool $noImport = false, bool $multipleDbs = false): void
{
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Pull/PullDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#[RequireAuth]
#[RequireLocalDb]
#[RequireRemoteDb]
#[AsCommand(name: 'pull:database', description: 'Import database backup from a Cloud Platform environment', aliases: ['pull:db'])]
final class PullDatabaseCommand extends PullCommandBase
{
Expand Down Expand Up @@ -51,6 +50,9 @@ protected function configure(): void
);
}

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$noScripts = $input->hasOption('no-scripts') && $input->getOption('no-scripts');
Expand Down
6 changes: 6 additions & 0 deletions src/Command/Push/PushDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ protected function configure(): void
->acceptSite();
}

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$destinationEnvironment = $this->determineEnvironment($input, $output);
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$databases = $this->determineCloudDatabases($acquiaCloudClient, $destinationEnvironment, $input->getArgument('site'));
// We only support pushing a single database.
$database = $databases[0];
if ($database->user_name === null) {
throw new AcquiaCliException('Database connection details missing');
}
$answer = $this->io->confirm("Overwrite the <bg=cyan;options=bold>$database->name</> database on <bg=cyan;options=bold>$destinationEnvironment->name</> with a copy of the database from the current machine?");
if (!$answer) {
return Command::SUCCESS;
Expand Down
9 changes: 1 addition & 8 deletions tests/phpunit/src/Commands/Pull/PullCommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,8 @@ protected function mockListSites(SshHelper|ObjectProphecy $sshHelper): void
->willReturn($process->reveal())->shouldBeCalled();
}

public function mockGetBackup(mixed $environment, bool $perms = true): void
public function mockGetBackup(mixed $environment): void
{
if (!$perms) {
$tamper = static function ($databases): void {
$databases[0]->user_name = null;
};
$this->mockRequest('getEnvironmentsDatabases', $environment->id, null, null, $tamper);
return;
}
$databases = $this->mockRequest('getEnvironmentsDatabases', $environment->id);
$tamper = static function ($backups): void {
$backups[0]->completedAt = $backups[0]->completed_at;
Expand Down
24 changes: 0 additions & 24 deletions tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ public function testPullDatabases(): void
$this->assertStringContainsString('Downloading backup 1', $output);
}

public function testPullDbHelp(): void
{
$help = $this->command->getHelp();
$this->assertStringContainsString('This command requires authentication via the Cloud Platform API.', $help);
$this->assertStringContainsString('This command requires an active database connection. Set the following environment variables prior to running this command: ACLI_DB_HOST, ACLI_DB_NAME, ACLI_DB_USER, ACLI_DB_PASSWORD', $help);
$this->assertStringContainsString('This command requires the \'View database connection details\' permission.', $help);
}

public function testPullProdDatabase(): void
{
$localMachineHelper = $this->mockLocalMachineHelper();
Expand Down Expand Up @@ -310,22 +302,6 @@ public function testPullDatabaseWithMySqlImportError(): void
], self::inputChooseEnvironment());
}

public function testPullDatabaseWithMissingPermission(): void
{
$localMachineHelper = $this->mockLocalMachineHelper();
$this->mockExecuteMySqlConnect($localMachineHelper, true);
$environment = $this->mockGetEnvironment();
$sshHelper = $this->mockSshHelper();
$this->mockListSites($sshHelper);
$this->mockGetBackup($environment, false);

$this->expectException(AcquiaCliException::class);
$this->expectExceptionMessage('Database connection details missing');
$this->executeCommand([
'--no-scripts' => true,
], self::inputChooseEnvironment());
}

/**
* @dataProvider providerTestPullDatabaseWithInvalidSslCertificate
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/src/Commands/Push/PushDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Push\PushDatabaseCommand;
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Helpers\LocalMachineHelper;
use Acquia\Cli\Helpers\SshHelper;
use Acquia\Cli\Tests\CommandTestBase;
Expand Down Expand Up @@ -108,6 +109,30 @@ public function testPushDatabase(int $verbosity, bool $printOutput, bool $pv): v
$this->assertStringContainsString('Overwrite the jxr136 database on dev with a copy of the database from the current machine?', $output);
}

public function testPushDbHelp(): void
{
$help = $this->command->getHelp();
$this->assertStringContainsString('This command requires authentication via the Cloud Platform API.', $help);
$this->assertStringContainsString('This command requires an active database connection. Set the following environment variables prior to running this command: ACLI_DB_HOST, ACLI_DB_NAME, ACLI_DB_USER, ACLI_DB_PASSWORD', $help);
$this->assertStringContainsString('This command requires the \'View database connection details\' permission.', $help);
}

/**
* @throws \Exception
*/
public function testPushDatabaseWithMissingPermission(): void
{
$environment = $this->mockGetEnvironment();
$tamper = static function ($databases): void {
$databases[0]->user_name = null;
};
$this->mockRequest('getEnvironmentsDatabases', $environment->id, null, null, $tamper);

$this->expectException(AcquiaCliException::class);
$this->expectExceptionMessage('Database connection details missing');
$this->executeCommand([], self::inputChooseEnvironment());
}

protected function mockUploadDatabaseDump(
ObjectProphecy $localMachineHelper,
ObjectProphecy $process,
Expand Down

0 comments on commit 4bdb647

Please sign in to comment.