@@ -38,7 +38,6 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
38
38
$ toRename = [];
39
39
$ tableComment = '' ;
40
40
$ replicaComment = '' ;
41
- $ renameBatch = [];
42
41
foreach ($ tableNames as $ tableName ) {
43
42
$ outdatedTableName = $ tableName . $ this ->outdatedTableSuffix ;
44
43
$ replicaTableName = $ tableName . $ this ->additionalTableSuffix ;
@@ -47,20 +46,14 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
47
46
$ tableCreateResult = $ connection ->fetchRow ($ tableCreateQuery );
48
47
49
48
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 );
54
50
}
55
51
56
52
$ replicaCreateQuery = 'SHOW CREATE TABLE ' . $ replicaTableName ;
57
53
$ replicaCreateResult = $ connection ->fetchRow ($ replicaCreateQuery );
58
54
59
55
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 );
64
57
}
65
58
66
59
$ renameBatch = [
@@ -78,11 +71,15 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
78
71
]
79
72
];
80
73
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 );
83
81
}
84
82
}
85
- $ toRename = array_merge ($ toRename , $ renameBatch );
86
83
87
84
if (!empty ($ toRename )) {
88
85
$ connection ->renameTablesBatch ($ toRename );
@@ -101,21 +98,35 @@ public function getAdditionalTableName($tableName)
101
98
}
102
99
103
100
/**
104
- * Switch table comments
101
+ * Returns table comment
105
102
*
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
111
105
*/
112
- private function switchTableComments ( $ tableName , $ replicaName , $ tableComment , $ replicaComment , $ connection )
106
+ private function getTableComment ( $ tableCreateResult )
113
107
{
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
+ }
119
129
}
130
+ return $ toRename ;
120
131
}
121
132
}
0 commit comments