diff --git a/ChangeLog b/ChangeLog index 6ef769093..f699aaaf8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -64,6 +64,8 @@ $HeadURL: https://phpmyadmin.svn.sourceforge.net/svnroot/phpmyadmin/trunk/phpMyA - patch #2918831 [export] Missing backquotes on reserved words, thanks to Virsacer - virsacer - [core] Fix broken cleanup of $_GET +- bug #2924357 [operations] Cannot rename a database that has foreign key +constraints 3.2.4.0 (2009-12-02) - bug [engines] Innodb_buffer_pool_pages_latched no longer returned in status diff --git a/db_operations.php b/db_operations.php index 6aa9d2086..66ee39391 100644 --- a/db_operations.php +++ b/db_operations.php @@ -60,12 +60,30 @@ if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) { $GLOBALS['pma']->databases->build(); } - if (isset($GLOBALS['add_constraints'])) { - $GLOBALS['sql_constraints_query_full_db'] = ''; + if (isset($GLOBALS['add_constraints']) || $move) { + $GLOBALS['sql_constraints_query_full_db'] = array(); } $tables_full = PMA_DBI_get_tables_full($db); $views = array(); + + // remove all foreign key constraints, otherwise we can get errors + require_once './libraries/export/sql.php'; + foreach ($tables_full as $each_table => $tmp) { + $sql_constraints = ''; + $sql_drop_foreign_keys = ''; + $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false); + if (! empty($sql_drop_foreign_keys)) { + PMA_DBI_query($sql_drop_foreign_keys); + } + // keep the constraint we just dropped + if (! empty($sql_constraints)) { + $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints; + } + } + unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure); + + foreach ($tables_full as $each_table => $tmp) { // to be able to rename a db containing views, we // first collect in $views all the views we find and we @@ -119,8 +137,9 @@ if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) { } unset($triggers); + // this does not apply to a rename operation if (isset($GLOBALS['add_constraints'])) { - $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query']; + $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query']; unset($GLOBALS['sql_constraints_query']); } } @@ -142,17 +161,15 @@ if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) { unset($view, $views); // now that all tables exist, create all the accumulated constraints - if (! $_error && isset($GLOBALS['add_constraints'])) { - /** - * @todo this works with mysqli but not with mysql, because - * mysql extension does not accept more than one statement; maybe - * interface with the sql import plugin that handles statement delimiter - */ - PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']); - + if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) { + PMA_DBI_select_db($newname); + foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) { + PMA_DBI_query($one_query); // and prepare to display them - $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db']; - unset($GLOBALS['sql_constraints_query_full_db']); + $GLOBALS['sql_query'] .= "\n" . $one_query; + } + + unset($GLOBALS['sql_constraints_query_full_db'], $one_query); } if (PMA_MYSQL_INT_VERSION >= 50000) { diff --git a/libraries/export/sql.php b/libraries/export/sql.php index f760193a3..9c177b4b3 100644 --- a/libraries/export/sql.php +++ b/libraries/export/sql.php @@ -477,6 +477,7 @@ function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false, $a global $cfgRelation; global $sql_constraints; global $sql_constraints_query; // just the text of the query + global $sql_drop_foreign_keys; $schema_create = ''; $auto_increment = ''; @@ -615,6 +616,7 @@ function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false, $a // let's do the work $sql_constraints_query .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf; $sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf; + $sql_drop_foreign_keys .= 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $crlf; $first = TRUE; for ($j = $i; $j < $sql_count; $j++) { @@ -630,6 +632,11 @@ function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false, $a $str_tmp = preg_replace('/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j]); $sql_constraints_query .= $str_tmp; $sql_constraints .= $str_tmp; + preg_match('/(CONSTRAINT)([\s])([\S]*)([\s])/', $sql_lines[$j], $matches); + if (! $first) { + $sql_drop_foreign_keys .= ', '; + } + $sql_drop_foreign_keys .= 'DROP FOREIGN KEY ' . $matches[3]; } $first = FALSE; } else {