Skip to content

Commit

Permalink
CLI-1210: Drop tables instead of database
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Nov 22, 2023
1 parent 436891f commit 62901d6
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [
Expand All @@ -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()]);
}
}

Expand Down

0 comments on commit 62901d6

Please sign in to comment.