Skip to content

Commit e2d0470

Browse files
committed
MC-36472: Can't upgrade from 2.3.x CE (with MySQL, PHP 7.2, Mysql 5.7 as db engine) to 2.4 B2B (ES7, PHP 7.4, Mysql 8 as db engine) [Module:Magento_CatalogStaging]
1 parent d4600b9 commit e2d0470

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

lib/internal/Magento/Framework/Setup/Declaration/Schema/Db/DbSchemaWriterInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ interface DbSchemaWriterInterface
3030
/**
3131
* Create table from SQL fragments, like columns, constraints, foreign keys, indexes, etc.
3232
*
33-
* @param $tableName
34-
* @param $resource
35-
* @param array $definition
33+
* @param string $tableName
34+
* @param string $resource
35+
* @param array $definition
3636
* @param array $options
3737
* @return Statement
3838
*/
@@ -62,7 +62,7 @@ public function dropTable($tableName, $resource);
6262
public function addElement($elementName, $resource, $tableName, $elementDefinition, $elementType);
6363

6464
/**
65-
* Return statements which reset auto_increment to 1.
65+
* Return statements which reset auto_increment to the current maximum AUTO_INCREMENT column value plus one.
6666
*
6767
* @param string $tableName
6868
* @param string $resource
@@ -78,7 +78,7 @@ public function resetAutoIncrement($tableName, $resource);
7878
* @param string $columnName
7979
* @param string $resource
8080
* @param string $tableName
81-
* @param string $columnDefinition
81+
* @param string $columnDefinition
8282
* @return Statement
8383
*/
8484
public function modifyColumn($columnName, $resource, $tableName, $columnDefinition);

lib/internal/Magento/Framework/Setup/Declaration/Schema/Db/MySQL/DbSchemaWriter.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ public function dropElement($resource, $elementName, $tableName, $type)
250250
*/
251251
public function resetAutoIncrement($tableName, $resource)
252252
{
253-
$sql = 'AUTO_INCREMENT = 1';
253+
$autoIncrementValue = $this->getNextAutoIncrementValue($tableName, $resource);
254+
$sql = "AUTO_INCREMENT = {$autoIncrementValue}";
255+
254256
return $this->statementFactory->create(
255257
sprintf('RESET_AUTOINCREMENT_%s', $tableName),
256258
$tableName,
@@ -300,4 +302,21 @@ public function compile(StatementAggregator $statementAggregator, $dryRun)
300302
}
301303
}
302304
}
305+
306+
/**
307+
* Retrieve next value for AUTO_INCREMENT column.
308+
*
309+
* @param string $tableName
310+
* @param string $resource
311+
* @return int
312+
*/
313+
private function getNextAutoIncrementValue(string $tableName, string $resource): int
314+
{
315+
$adapter = $this->resourceConnection->getConnection($resource);
316+
$sql = sprintf('SELECT MAX(`%s`) + 1 FROM `%s`', $adapter->getAutoIncrementField($tableName), $tableName);
317+
$adapter->resetDdlCache($tableName);
318+
$stmt = $adapter->query($sql);
319+
320+
return (int)$stmt->fetchColumn();
321+
}
303322
}

lib/internal/Magento/Framework/Setup/Declaration/Schema/Operations/AddColumn.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class AddColumn implements OperationInterface
2626
{
2727
/**
28-
* Operation name.
28+
* The name of the operation.
2929
*/
3030
const OPERATION_NAME = 'add_column';
3131

@@ -196,7 +196,7 @@ public function doOperation(ElementHistory $elementHistory)
196196
*/
197197
$element = $elementHistory->getNew();
198198
$definition = $this->definitionAggregator->toDefinition($element);
199-
199+
200200
$statement = $this->dbSchemaWriter->addElement(
201201
$element->getName(),
202202
$element->getTable()->getResource(),
@@ -207,7 +207,7 @@ public function doOperation(ElementHistory $elementHistory)
207207
$statements = $this->setupTriggersIfExists($statement, $elementHistory);
208208

209209
if ($this->columnIsAutoIncrement($element)) {
210-
/** We need to reset auto_increment as new field should goes from 1 */
210+
/** We need to reset auto_increment to the current maximum value + one */
211211
$statements[] = $this->dbSchemaWriter->resetAutoIncrement(
212212
$element->getTable()->getName(),
213213
$element->getTable()->getResource()

0 commit comments

Comments
 (0)