From a15179771e0a3484845f4d20ed50e286f00953a2 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 27 Jan 2012 20:53:55 +0100 Subject: [PATCH] Fixed deletion of conflicting tables Previously foreign keys from 3rd party tables were not properly removed, causing MySQL to commit suicide --- .../editor/MySQLDatabaseEditor.class.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 b960268471..e1950f1bdc 100644 --- a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php @@ -262,6 +262,33 @@ class MySQLDatabaseEditor extends DatabaseEditor { $this->tables[$row['TABLE_NAME']][] = $row['CONSTRAINT_NAME']; } + // handle foreign keys from 3rd party tables + $conditions = new PreparedStatementConditionBuilder(); + $conditions->add("TABLE_SCHEMA = ?", array($currentDB)); + $conditions->add("REFERENCED_TABLE_NAME IN (?)", array($conflictedTables)); + $conditions->add("CONSTRAINT_NAME LIKE ?", array('%_fk')); + + $sql = "SELECT CONSTRAINT_NAME, TABLE_NAME + FROM information_schema.KEY_COLUMN_USAGE + ".$conditions; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute($conditions->getParameters()); + $foreignConstraints = array(); + while ($row = $statement->fetchArray()) { + if (!isset($foreignConstraints[$row['TABLE_NAME']])) { + $foreignConstraints[$row['TABLE_NAME']] = array(); + } + + $foreignConstraints[$row['TABLE_NAME']][] = $row['CONSTRAINT_NAME']; + } + + // drop foreign keys from 3rd party tables + foreach ($foreignConstraints as $tableName => $foreignKeys) { + foreach ($foreignKeys as $fk) { + $this->dropForeignKey($tableName, $fk); + } + } + // drop foreign keys foreach ($this->tables as $tableName => $foreignKeys) { foreach ($foreignKeys as $fk) { -- 2.20.1