From 2102937aa7788fb76b4f4f8074708dc7a06ba790 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Fri, 2 Feb 2024 13:23:39 -0800 Subject: [PATCH] CLI-1257: pull:db fails with too many tables (#1674) * CLI-1257: pull:db fails with too many tables * fix suffix * fix tests --- src/Command/Pull/PullCommandBase.php | 4 ++- .../Commands/Pull/PullDatabaseCommandTest.php | 27 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Command/Pull/PullCommandBase.php b/src/Command/Pull/PullCommandBase.php index bb6c608d5..8c8faf361 100644 --- a/src/Command/Pull/PullCommandBase.php +++ b/src/Command/Pull/PullCommandBase.php @@ -382,6 +382,8 @@ private function dropDbTables(string $dbHost, string $dbUser, string $dbName, st $tables = $this->listTablesQuoted($process->getOutput()); if ($tables) { $sql = 'DROP TABLE ' . implode(', ', $tables); + $tempnam = $this->localMachineHelper->getFilesystem()->tempnam(sys_get_temp_dir(), 'acli_drop_table_', '.sql'); + $this->localMachineHelper->getFilesystem()->dumpFile($tempnam, $sql); $command = [ 'mysql', '--host', @@ -390,7 +392,7 @@ private function dropDbTables(string $dbHost, string $dbUser, string $dbName, st $dbUser, $dbName, '-e', - $sql, + 'source ' . $tempnam, ]; $process = $this->localMachineHelper->execute($command, $outputCallback, NULL, FALSE, NULL, ['MYSQL_PWD' => $dbPassword]); if (!$process->isSuccessful()) { diff --git a/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php b/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php index 669d01991..4e6d7636d 100644 --- a/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php @@ -69,9 +69,10 @@ public function testPullDatabases(): void { $this->mockListSites($sshHelper); $this->mockGetBackup($environment); $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); - $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE); - $this->mockExecuteMySqlImport($localMachineHelper, TRUE, TRUE, 'my_db', 'my_dbdev', 'drupal'); $fs = $this->prophet->prophesize(Filesystem::class); + $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE, $fs); + $this->mockExecuteMySqlImport($localMachineHelper, TRUE, TRUE, 'my_db', 'my_dbdev', 'drupal'); + $fs->remove(Argument::type('string'))->shouldBeCalled(); $localMachineHelper->getFilesystem()->willReturn($fs)->shouldBeCalled(); $this->executeCommand([ @@ -107,10 +108,11 @@ public function testPullDatabaseNoPv(): void { $this->mockListSites($sshHelper); $this->mockGetBackup($environment); $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); - $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE); - $this->mockExecuteMySqlImport($localMachineHelper, TRUE, FALSE, 'my_db', 'my_dbdev', 'drupal'); $fs = $this->prophet->prophesize(Filesystem::class); $localMachineHelper->getFilesystem()->willReturn($fs)->shouldBeCalled(); + $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE, $fs); + $this->mockExecuteMySqlImport($localMachineHelper, TRUE, FALSE, 'my_db', 'my_dbdev', 'drupal'); + $fs->remove(Argument::type('string'))->shouldBeCalled(); $this->executeCommand([ '--no-scripts' => TRUE, @@ -208,7 +210,9 @@ public function testPullDatabaseWithMySqlDropError(): void { $this->mockListSites($sshHelper); $this->mockGetBackup($environment); $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); - $this->mockExecuteMySqlDropDb($localMachineHelper, FALSE); + $fs = $this->prophet->prophesize(Filesystem::class); + $localMachineHelper->getFilesystem()->willReturn($fs)->shouldBeCalled(); + $this->mockExecuteMySqlDropDb($localMachineHelper, FALSE, $fs); $this->expectException(AcquiaCliException::class); $this->expectExceptionMessage('Unable to drop tables from database'); @@ -225,7 +229,9 @@ public function testPullDatabaseWithMySqlImportError(): void { $this->mockListSites($sshHelper); $this->mockGetBackup($environment); $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); - $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE); + $fs = $this->prophet->prophesize(Filesystem::class); + $localMachineHelper->getFilesystem()->willReturn($fs)->shouldBeCalled(); + $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE, $fs); $this->mockExecuteMySqlImport($localMachineHelper, FALSE, TRUE, 'my_db', 'my_dbdev', 'drupal'); $this->expectException(AcquiaCliException::class); @@ -286,12 +292,12 @@ protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mockIde // Mock IDE filesystem. if ($mockIdeFs) { $this->mockDrupalSettingsRefresh($localMachineHelper); - $this->mockSettingsFiles($fs); } + $this->mockSettingsFiles($fs); // Database. $this->mockExecuteMySqlListTables($localMachineHelper); - $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE); + $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE, $fs); $this->mockExecuteMySqlImport($localMachineHelper, TRUE, TRUE); if ($multiDb) { $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); @@ -357,10 +363,13 @@ protected function mockExecuteMySqlListTables( protected function mockExecuteMySqlDropDb( LocalMachineHelper|ObjectProphecy $localMachineHelper, - bool $success + bool $success, + ObjectProphecy $fs ): void { $localMachineHelper->checkRequiredBinariesExist(["mysql"])->shouldBeCalled(); $process = $this->mockProcess($success); + $fs->tempnam(Argument::type('string'), 'acli_drop_table_', '.sql')->willReturn('something')->shouldBeCalled(); + $fs->dumpfile('something', Argument::type('string'))->shouldBeCalled(); $localMachineHelper ->execute(Argument::type('array'), Argument::type('callable'), NULL, FALSE, NULL, ['MYSQL_PWD' => $this->dbPassword]) ->willReturn($process->reveal())