Skip to content

Commit ec2afe9

Browse files
committed
MC-38806: Wrong table comment after reindex
1 parent d288e53 commit ec2afe9

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Indexer/ActiveTableSwitcher.php

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
3838
$toRename = [];
3939
$tableComment = '';
4040
$replicaComment = '';
41-
$renameBatch = [];
4241
foreach ($tableNames as $tableName) {
4342
$outdatedTableName = $tableName . $this->outdatedTableSuffix;
4443
$replicaTableName = $tableName . $this->additionalTableSuffix;
@@ -47,20 +46,14 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
4746
$tableCreateResult = $connection->fetchRow($tableCreateQuery);
4847

4948
if (is_array($tableCreateResult)) {
50-
$tableCommentPosition = strpos((string )end($tableCreateResult), "COMMENT=");
51-
if ($tableCommentPosition) {
52-
$tableComment = substr((string )end($tableCreateResult), $tableCommentPosition + 8);
53-
}
49+
$tableComment = $this->getTableComment($tableCreateResult);
5450
}
5551

5652
$replicaCreateQuery = 'SHOW CREATE TABLE ' . $replicaTableName;
5753
$replicaCreateResult = $connection->fetchRow($replicaCreateQuery);
5854

5955
if (is_array($replicaCreateResult)) {
60-
$replicaCommentPosition = strpos((string )end($replicaCreateResult), "COMMENT=");
61-
if ($replicaCommentPosition) {
62-
$replicaComment = substr((string )end($replicaCreateResult), $replicaCommentPosition + 8);
63-
}
56+
$replicaComment = $this->getTableComment($replicaCreateResult);
6457
}
6558

6659
$renameBatch = [
@@ -78,11 +71,15 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
7871
]
7972
];
8073

81-
if (!empty($renameBatch)) {
82-
$this->switchTableComments($tableName, $replicaTableName, $tableComment, $replicaComment, $connection);
74+
$toRename = $this->mergeRenameTables($renameBatch, $toRename);
75+
76+
if (!empty($toRename) && $replicaComment !== '' && $tableComment !== $replicaComment) {
77+
$changeTableComment = sprintf("ALTER TABLE %s COMMENT=%s", $tableName, $replicaComment);
78+
$connection->query($changeTableComment);
79+
$changeReplicaComment = sprintf("ALTER TABLE %s COMMENT=%s", $replicaTableName, $tableComment);
80+
$connection->query($changeReplicaComment);
8381
}
8482
}
85-
$toRename = array_merge($toRename, $renameBatch);
8683

8784
if (!empty($toRename)) {
8885
$connection->renameTablesBatch($toRename);
@@ -101,21 +98,35 @@ public function getAdditionalTableName($tableName)
10198
}
10299

103100
/**
104-
* Switch table comments
101+
* Returns table comment
105102
*
106-
* @param string $tableName
107-
* @param string $replicaName
108-
* @param string $tableComment
109-
* @param string $replicaComment
110-
* @param AdapterInterface $connection
103+
* @param array $tableCreateResult
104+
* @return string
111105
*/
112-
private function switchTableComments($tableName, $replicaName, $tableComment, $replicaComment, $connection)
106+
private function getTableComment($tableCreateResult)
113107
{
114-
if ($tableComment !== '' && $tableComment !== $replicaComment) {
115-
$changeTableComment = sprintf("ALTER TABLE %s COMMENT=%s", $tableName, $replicaComment);
116-
$connection->query($changeTableComment);
117-
$changeReplicaComment = sprintf("ALTER TABLE %s COMMENT=%s", $replicaName, $tableComment);
118-
$connection->query($changeReplicaComment);
108+
$tableComment = '';
109+
$replicaCommentPosition = strpos((string )end($tableCreateResult), "COMMENT=");
110+
if ($replicaCommentPosition) {
111+
$tableComment = substr((string )end($tableCreateResult), $replicaCommentPosition + 8);
112+
}
113+
return $tableComment;
114+
}
115+
116+
/**
117+
* Merges two arrays
118+
*
119+
* @param array $renameBatch
120+
* @param array $toRename
121+
* @return array
122+
*/
123+
private function mergeRenameTables($renameBatch, $toRename)
124+
{
125+
foreach ($renameBatch as $batch) {
126+
if (!in_array($batch, $toRename)) {
127+
array_push($toRename, $batch);
128+
}
119129
}
130+
return $toRename;
120131
}
121132
}

0 commit comments

Comments
 (0)