From 62901d616a982fe54878f29e91ac55ecb6a31f04 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Wed, 22 Nov 2023 08:03:12 -0800 Subject: [PATCH] CLI-1210: Drop tables instead of database --- src/Command/Pull/PullCommandBase.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Command/Pull/PullCommandBase.php b/src/Command/Pull/PullCommandBase.php index 7158c475b..ef594ef6e 100644 --- a/src/Command/Pull/PullCommandBase.php +++ b/src/Command/Pull/PullCommandBase.php @@ -189,7 +189,7 @@ private function doImportRemoteDatabase( string $localFilepath, Closure $outputCallback = NULL ): void { - $this->dropLocalDatabase($databaseHost, $databaseUser, $databaseName, $databasePassword, $outputCallback); + $this->dropDbTables($databaseHost, $databaseUser, $databaseName, $databasePassword, $outputCallback); $this->createLocalDatabase($databaseHost, $databaseUser, $databaseName, $databasePassword, $outputCallback); $this->importDatabaseDump($localFilepath, $databaseHost, $databaseUser, $databaseName, $databasePassword, $outputCallback); $this->localMachineHelper->getFilesystem()->remove($localFilepath); @@ -341,9 +341,9 @@ private function connectToLocalDatabase(string $dbHost, string $dbUser, string $ } } - private function dropLocalDatabase(string $dbHost, string $dbUser, string $dbName, string $dbPassword, ?\Closure $outputCallback = NULL): void { + private function dropDbTables(string $dbHost, string $dbUser, string $dbName, string $dbPassword, ?\Closure $outputCallback = NULL): void { if ($outputCallback) { - $outputCallback('out', "Dropping database $dbName"); + $outputCallback('out', "Dropping tables from database $dbName"); } $this->localMachineHelper->checkRequiredBinariesExist(['mysql']); $command = [ @@ -352,12 +352,30 @@ private function dropLocalDatabase(string $dbHost, string $dbUser, string $dbNam $dbHost, '--user', $dbUser, + $dbName, + '-e', + 'SHOW TABLES;', + ]; + $process = $this->localMachineHelper->execute($command, $outputCallback, NULL, FALSE, NULL, ['MYSQL_PWD' => $dbPassword]); + $output = trim($process->getOutput()); + $tables = explode(PHP_EOL, $output); + foreach ($tables as &$table) { + $table = "`$table`"; + } + $sql = 'DROP TABLE ' . implode(', ', $tables); + $command = [ + 'mysql', + '--host', + $dbHost, + '--user', + $dbUser, + $dbName, '-e', - 'DROP DATABASE IF EXISTS ' . $dbName, + $sql, ]; $process = $this->localMachineHelper->execute($command, $outputCallback, NULL, FALSE, NULL, ['MYSQL_PWD' => $dbPassword]); if (!$process->isSuccessful()) { - throw new AcquiaCliException('Unable to drop a local database. {message}', ['message' => $process->getErrorOutput()]); + throw new AcquiaCliException('Unable to drop tables from database. {message}', ['message' => $process->getErrorOutput()]); } }