From bf64fe54fe0dd11ca30872f0782e93f5bf53cfd6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 2 Jul 2020 15:40:19 +0200 Subject: [PATCH] Ignore errors if a to-be-dropped key / column does not exist in DatabaseEditor Resolves #3166 --- .../editor/MySQLDatabaseEditor.class.php | 65 +++++++++++++++---- 1 file changed, 53 insertions(+), 12 deletions(-) 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; + } + } } /** -- 2.20.1