From 7dc163f2b7f483e4abb46015c0e41b47cddfd757 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 14 Apr 2014 17:21:53 +0200 Subject: [PATCH] [ticket/12012] Drop and recreate indexes when removing columns PHPBB3-12012 --- phpBB/phpbb/db/tools.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/tools.php b/phpBB/phpbb/db/tools.php index 113059900a..bc505010ba 100644 --- a/phpBB/phpbb/db/tools.php +++ b/phpBB/phpbb/db/tools.php @@ -1846,15 +1846,46 @@ class tools case 'mssql': case 'mssqlnative': + // We need the data here + $old_return_statements = $this->return_statements; + $this->return_statements = true; + $indexes = $this->mssql_get_existing_indexes($table_name, $column_name); + // Drop any indexes + $recreate_indexes = array(); if (!empty($indexes)) { - trigger_error("Column '$column_name' on table '$table_name' is still part of an index and can not be removed: " . implode(', ', array_keys($indexes)), E_USER_ERROR); + foreach ($indexes as $index_name => $index_data) + { + $result = $this->sql_index_drop($table_name, $index_name); + $statements = array_merge($statements, $result); + if (sizeof($index_data) > 1) + { + // Remove this column from the index and recreate it + $recreate_indexes[$index_name] = array_diff($index_data, array($column_name)); + } + } } - $statements = $this->mssql_drop_default_constraints($table_name, $column_name); + // Drop default value constraint + $result = $this->mssql_drop_default_constraints($table_name, $column_name); + $statements = array_merge($statements, $result); + + // Remove the column $statements[] = 'ALTER TABLE [' . $table_name . '] DROP COLUMN [' . $column_name . ']'; + + if (!empty($recreate_indexes)) + { + // Recreate indexes after we removed the column + foreach ($recreate_indexes as $index_name => $index_data) + { + $result = $this->sql_create_index($table_name, $index_name, $index_data); + $statements = array_merge($statements, $result); + } + } + + $this->return_statements = $old_return_statements; break; case 'mysql_40':