From: Tim Düsterhus Date: Thu, 2 Jul 2020 13:40:19 +0000 (+0200) Subject: Ignore errors if a to-be-dropped key / column does not exist in DatabaseEditor X-Git-Tag: 5.3.0_Alpha_1~166^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=bf64fe54fe0dd11ca30872f0782e93f5bf53cfd6;p=GitHub%2FWoltLab%2FWCF.git Ignore errors if a to-be-dropped key / column does not exist in DatabaseEditor Resolves #3166 --- diff --git a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php index 80b7e0667b..bcdff00f29 100644 --- a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php @@ -1,5 +1,6 @@ dbObj->prepareStatement($sql); - $statement->execute(); + try { + $sql = "ALTER TABLE `".$tableName."` DROP COLUMN `".$columnName."`"; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute(); + } + catch (DatabaseQueryExecutionException $e) { + if ($e->getCode() != '42000') { + throw $e; + } + if (in_array($columnName, array_column($this->getColumns($tableName), 'name'))) { + throw $e; + } + } } /** @@ -331,27 +342,57 @@ class MySQLDatabaseEditor extends DatabaseEditor { * @inheritDoc */ public function dropIndex($tableName, $indexName) { - $sql = "ALTER TABLE `".$tableName."` DROP INDEX `".$indexName."`"; - $statement = $this->dbObj->prepareStatement($sql); - $statement->execute(); + try { + $sql = "ALTER TABLE `".$tableName."` DROP INDEX `".$indexName."`"; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute(); + } + catch (DatabaseQueryExecutionException $e) { + if ($e->getCode() != '42000') { + throw $e; + } + if (in_array($indexName, $this->getIndices($tableName))) { + throw $e; + } + } } /** * @inheritDoc */ public function dropPrimaryKey($tableName) { - $sql = "ALTER TABLE ".$tableName." DROP PRIMARY KEY"; - $statement = $this->dbObj->prepareStatement($sql); - $statement->execute(); + try { + $sql = "ALTER TABLE ".$tableName." DROP PRIMARY KEY"; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute(); + } + catch (DatabaseQueryExecutionException $e) { + if ($e->getCode() != '42000') { + throw $e; + } + if (in_array("PRIMARY", $this->getIndices($tableName))) { + throw $e; + } + } } /** * @inheritDoc */ public function dropForeignKey($tableName, $indexName) { - $sql = "ALTER TABLE `".$tableName."` DROP FOREIGN KEY `".$indexName."`"; - $statement = $this->dbObj->prepareStatement($sql); - $statement->execute(); + try { + $sql = "ALTER TABLE `".$tableName."` DROP FOREIGN KEY `".$indexName."`"; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute(); + } + catch (DatabaseQueryExecutionException $e) { + if ($e->getCode() != '42000') { + throw $e; + } + if (in_array($indexName, array_keys($this->getForeignKeys($tableName)))) { + throw $e; + } + } } /**