Fixed deletion of conflicting tables
authorAlexander Ebert <ebert@woltlab.com>
Fri, 27 Jan 2012 19:53:55 +0000 (20:53 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 27 Jan 2012 19:53:55 +0000 (20:53 +0100)
Previously foreign keys from 3rd party tables were not properly removed, causing MySQL to commit suicide

wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php

index b960268471ba90c5ee7c5eb81fe1f9e3c726178c..e1950f1bdc7bd92b8effe52cd530937accdb2ad6 100644 (file)
@@ -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) {