Merge $move into MoveScope

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2024-03-23 12:38:16 +01:00
parent e9a8b439c1
commit a3436e4117
6 changed files with 14 additions and 21 deletions

View File

@ -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',
);

View File

@ -41,7 +41,6 @@ final class CopyTableWithPrefixController implements InvocableController
Current::$database,
$newTableName,
MoveScope::StructureAndData,
false,
MoveMode::SingleTable,
$dropIfExists,
);

View File

@ -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',
);

View File

@ -9,4 +9,5 @@ enum MoveScope: string
case StructureOnly = 'structure';
case StructureAndData = 'data';
case DataOnly = 'dataonly';
case Move = 'move';
}

View File

@ -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);

View File

@ -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,
);