From 76d746655034d2ab4b2a37ccc4eb3c3a1eef60c9 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 27 Jan 2012 18:42:38 +0100 Subject: [PATCH] Implemented support for table conflicts @Burntime Could you add PostgreSQL support later? See #178 --- .../files/lib/system/WCFSetup.class.php | 4 ++ .../database/editor/DatabaseEditor.class.php | 7 +++ .../editor/MySQLDatabaseEditor.class.php | 46 +++++++++++++++++++ .../editor/PostgreSQLDatabaseEditor.class.php | 7 +++ 4 files changed, 64 insertions(+) diff --git a/wcfsetup/install/files/lib/system/WCFSetup.class.php b/wcfsetup/install/files/lib/system/WCFSetup.class.php index 579e37fed3..b3716bb7e5 100644 --- a/wcfsetup/install/files/lib/system/WCFSetup.class.php +++ b/wcfsetup/install/files/lib/system/WCFSetup.class.php @@ -555,6 +555,10 @@ class WCFSetup extends WCF { // check for table conflicts $conflictedTables = $this->getConflictedTables($db, $dbNumber); + if (!empty($conflictedTables) && ($overwriteTables || self::$developerMode)) { + // remove tables + $db->getEditor()->dropConflictedTables($conflictedTables); + } // write config.inc if (empty($conflictedTables) || $overwriteTables || self::$developerMode) { diff --git a/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php index 7cd88bc5a0..afdc91ccae 100644 --- a/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php @@ -128,4 +128,11 @@ abstract class DatabaseEditor { * @param string $indexName */ public abstract function dropForeignKey($tableName, $indexName); + + /** + * Drops all given databases. + * + * @param array $conflictedTables + */ + public abstract function dropConflictedTables(array $conflictedTables); } 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 53fac0494f..b960268471 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(); + $row = $statement->fetchArray(); + $currentDB = $row['currentDB']; + + // get constraints + $conditions = new PreparedStatementConditionBuilder(); + $conditions->add("TABLE_SCHEMA = ?", array($currentDB)); + $conditions->add("TABLE_NAME IN (?)", array($conflictedTables)); + $conditions->add("REFERENCED_TABLE_NAME IS NOT NULL"); + $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()); + while ($row = $statement->fetchArray()) { + $this->tables[$row['TABLE_NAME']][] = $row['CONSTRAINT_NAME']; + } + + // drop foreign keys + foreach ($this->tables as $tableName => $foreignKeys) { + foreach ($foreignKeys as $fk) { + $this->dropForeignKey($tableName, $fk); + } + } + + // drop tables + foreach (array_keys($this->tables) as $tableName) { + $this->dropTable($tableName); + } + } } diff --git a/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php index aae7fcb636..3e3b67cc71 100644 --- a/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php @@ -387,4 +387,11 @@ class PostgreSQLDatabaseEditor extends DatabaseEditor { throw new DatabaseException("Unknown / unsupported data type '".$mySQLType."'", $this->dbObj); } + + /** + * @see wcf\system\database\editor\DatabaseEditor::dropConflictedTables() + */ + public function dropConflictedTables(array $conflictedTables) { + die('IMPLEMENT ME: PostgreSQLDatabaseEditor::dropConflictedTables'); + } } -- 2.20.1