diff --git a/src/Controllers/Database/Structure/CopyTableController.php b/src/Controllers/Database/Structure/CopyTableController.php index 99b969e554..dcf14e77c9 100644 --- a/src/Controllers/Database/Structure/CopyTableController.php +++ b/src/Controllers/Database/Structure/CopyTableController.php @@ -41,7 +41,6 @@ final class CopyTableController implements InvocableController $targetDb, $selectedValue, MoveScope::from($request->getParsedBodyParam('what')), - false, MoveMode::SingleTable, $request->getParsedBodyParam('drop_if_exists') === 'true', ); diff --git a/src/Controllers/Database/Structure/CopyTableWithPrefixController.php b/src/Controllers/Database/Structure/CopyTableWithPrefixController.php index 1206d8802e..4ed0cefefd 100644 --- a/src/Controllers/Database/Structure/CopyTableWithPrefixController.php +++ b/src/Controllers/Database/Structure/CopyTableWithPrefixController.php @@ -41,7 +41,6 @@ final class CopyTableWithPrefixController implements InvocableController Current::$database, $newTableName, MoveScope::StructureAndData, - false, MoveMode::SingleTable, $dropIfExists, ); diff --git a/src/Operations.php b/src/Operations.php index 04ba44ba42..47b36ebcf7 100644 --- a/src/Operations.php +++ b/src/Operations.php @@ -195,14 +195,14 @@ class Operations // for importing via the mysql client or our Import feature) $triggers = Triggers::getDetails($this->dbi, $db, $table); + $moveScope = MoveScope::tryFrom($copyMode) ?? MoveScope::StructureAndData; if ( ! TableMover::moveCopy( $db, $table, $newDatabaseName->getName(), $table, - MoveScope::tryFrom($copyMode) ?? MoveScope::StructureAndData, - $move, + $move ? MoveScope::Move : $moveScope, MoveMode::WholeDatabase, isset($_POST['drop_if_exists']) && $_POST['drop_if_exists'] === 'true', ) @@ -276,8 +276,7 @@ class Operations $view, $newDatabaseName->getName(), $view, - MoveScope::StructureOnly, - $move, + $move ? MoveScope::Move : MoveScope::StructureOnly, MoveMode::WholeDatabase, true, ); @@ -890,13 +889,13 @@ class Operations $message = Message::error(__('Can\'t copy table to same one!')); } } else { + $move = isset($_POST['submit_move']); TableMover::moveCopy( $db, $table, $targetDb, (string) $_POST['new_name'], - MoveScope::from($_POST['what']), - isset($_POST['submit_move']), + $move ? MoveScope::Move : MoveScope::from($_POST['what']), MoveMode::SingleTable, isset($_POST['drop_if_exists']) && $_POST['drop_if_exists'] === 'true', ); diff --git a/src/Table/MoveScope.php b/src/Table/MoveScope.php index 97103d9fcf..4dc7600667 100644 --- a/src/Table/MoveScope.php +++ b/src/Table/MoveScope.php @@ -9,4 +9,5 @@ enum MoveScope: string case StructureOnly = 'structure'; case StructureAndData = 'data'; case DataOnly = 'dataonly'; + case Move = 'move'; } diff --git a/src/Table/TableMover.php b/src/Table/TableMover.php index 733221a7a2..fd7ddbb4e5 100644 --- a/src/Table/TableMover.php +++ b/src/Table/TableMover.php @@ -34,7 +34,6 @@ class TableMover * @param string $sourceTable source table * @param string $targetDb target database * @param string $targetTable target table - * @param bool $move whether to move */ public static function moveCopy( string $sourceDb, @@ -42,7 +41,6 @@ class TableMover string $targetDb, string $targetTable, MoveScope $what, - bool $move, MoveMode $mode, bool $addDropIfExists, ): bool { @@ -51,7 +49,7 @@ class TableMover $relation = new Relation($dbi); // Try moving the tables directly, using native `RENAME` statement. - if ($move && $what === MoveScope::StructureAndData) { + if ($what === MoveScope::Move) { $tbl = new Table($sourceTable, $sourceDb, $dbi); if ($tbl->rename($targetTable, $targetDb)) { $GLOBALS['message'] = $tbl->getLastMessage(); @@ -180,7 +178,7 @@ class TableMover // This is to avoid some issues when renaming databases with views // See: https://github.com/phpmyadmin/phpmyadmin/issues/16422 - if ($move) { + if ($what === MoveScope::Move) { $dbi->selectDb($targetDb); } @@ -193,7 +191,7 @@ class TableMover // Phase 3: Adding constraints. // All constraint names are removed because they must be unique. - if ($move && ! empty($GLOBALS['sql_constraints_query'])) { + if ($what === MoveScope::Move && ! empty($GLOBALS['sql_constraints_query'])) { $GLOBALS['sql_constraints_query'] = self::getConstraintsSqlWithoutNames( $GLOBALS['sql_constraints_query'], $destination, @@ -204,6 +202,7 @@ class TableMover // We can only execute it if both tables have been created. // When performing the whole database move, // the constraints can only be created after all tables have been created. + // Thus, we must keep the global so that the caller can execute these queries. if ($mode === MoveMode::SingleTable) { $dbi->query($GLOBALS['sql_constraints_query']); unset($GLOBALS['sql_constraints_query']); @@ -280,7 +279,7 @@ class TableMover $table = new Table($targetTable, $targetDb, $dbi); // Copy the data unless this is a VIEW - if (($what === MoveScope::StructureAndData || $what === MoveScope::DataOnly) && ! $table->isView()) { + if ($what !== MoveScope::StructureOnly && ! $table->isView()) { $sqlSetMode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'"; $dbi->query($sqlSetMode); $GLOBALS['sql_query'] .= "\n\n" . $sqlSetMode . ';'; @@ -301,7 +300,7 @@ class TableMover $relationParameters = $relation->getRelationParameters(); // Drops old table if the user has requested to move it - if ($move) { + if ($what === MoveScope::Move) { // This could avoid some problems with replicated databases, when // moving table from replicated one to not replicated one $dbi->selectDb($sourceDb); diff --git a/tests/unit/Table/TableTest.php b/tests/unit/Table/TableTest.php index 6c48cf8edf..476da8918b 100644 --- a/tests/unit/Table/TableTest.php +++ b/tests/unit/Table/TableTest.php @@ -1311,7 +1311,6 @@ class TableTest extends AbstractTestCase $sourceDb = 'PMA'; $targetTable = 'PMA_BookMark_new'; $targetDb = 'PMA_new'; - $move = true; unset($GLOBALS['sql_drop_table']); @@ -1328,8 +1327,7 @@ class TableTest extends AbstractTestCase $sourceTable, $targetDb, $targetTable, - MoveScope::DataOnly, - $move, + MoveScope::Move, MoveMode::SingleTable, true, ); @@ -1349,7 +1347,6 @@ class TableTest extends AbstractTestCase $targetDb, $targetTable, MoveScope::DataOnly, - false, MoveMode::SingleTable, true, ); @@ -1399,8 +1396,7 @@ class TableTest extends AbstractTestCase 'ad', 'bb', 'ad', - MoveScope::StructureOnly, - true, + MoveScope::Move, MoveMode::WholeDatabase, true, );