diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 918245ced5..e1aa7485b1 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -14247,7 +14247,7 @@ parameters: - message: "#^Parameter \\#1 \\$list of class PhpMyAdmin\\\\SqlParser\\\\Parser constructor expects PhpMyAdmin\\\\SqlParser\\\\TokensList\\|PhpMyAdmin\\\\SqlParser\\\\UtfString\\|string\\|null, mixed given\\.$#" - count: 3 + count: 2 path: src/Table/TableMover.php - @@ -14255,6 +14255,11 @@ parameters: count: 6 path: src/Table/TableMover.php + - + message: "#^Parameter \\#2 \\$constraintsSql of static method PhpMyAdmin\\\\Table\\\\TableMover\\:\\:getConstraintsSqlWithoutNames\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Table/TableMover.php + - message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#" count: 1 diff --git a/src/Table/TableMover.php b/src/Table/TableMover.php index aa89f44cd8..538dd27ee2 100644 --- a/src/Table/TableMover.php +++ b/src/Table/TableMover.php @@ -218,38 +218,18 @@ class TableMover // All constraint names are removed because they must be unique. if ($move && ! empty($GLOBALS['sql_constraints_query'])) { - $parser = new Parser($GLOBALS['sql_constraints_query']); - - /** - * The ALTER statement that generates the constraints. - * - * @var AlterStatement $statement - */ - $statement = $parser->statements[0]; - - // Changing the altered table to the destination. - $statement->table = $destination; - - // Removing the name of the constraints. - foreach ($statement->altered as $altered) { - // All constraint names are removed because they must be unique. - if (! $altered->options->has('CONSTRAINT')) { - continue; - } - - $altered->field = null; - } - - // Building back the query. - $GLOBALS['sql_constraints_query'] = $statement->build() . ';'; - - // Executing it. - if ($mode === MoveMode::SingleTable) { - $dbi->query($GLOBALS['sql_constraints_query']); - } + $GLOBALS['sql_constraints_query'] = self::getConstraintsSqlWithoutNames( + $GLOBALS['sql_constraints_query'], + $destination, + ); $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query']; + + // 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. if ($mode === MoveMode::SingleTable) { + $dbi->query($GLOBALS['sql_constraints_query']); unset($GLOBALS['sql_constraints_query']); } } @@ -518,4 +498,32 @@ class TableMover return $lastId; } + + private static function getConstraintsSqlWithoutNames(string $constraintsSql, Expression $destination): string + { + $parser = new Parser($constraintsSql); + + /** + * The ALTER statement that generates the constraints. + * + * @var AlterStatement $statement + */ + $statement = $parser->statements[0]; + + // Changing the altered table to the destination. + $statement->table = $destination; + + // Removing the name of the constraints. + foreach ($statement->altered as $altered) { + // All constraint names are removed because they must be unique. + if (! $altered->options->has('CONSTRAINT')) { + continue; + } + + $altered->field = null; + } + + // Building back the query. + return $statement->build() . ';'; + } }