Skip to content

Commit c5f4f61

Browse files
committed
MAGETWO-88814: [Performance] Some indexes become recreated on db with prefixes
1 parent 71395ad commit c5f4f61

File tree

14 files changed

+168
-30
lines changed

14 files changed

+168
-30
lines changed

app/code/Magento/Catalog/etc/db_schema_whitelist.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,8 @@
10961096
"index": {
10971097
"CAT_CTGR_PRD_IDX_REPLICA_PRD_ID_STORE_ID_CTGR_ID_VISIBILITY": true,
10981098
"IDX_87EB2E3059853CF89A75B4C55074810B": true,
1099-
"CAT_CTGR_PRD_IDX_PRD_ID_STORE_ID_CTGR_ID_VISIBILITY": true
1099+
"CAT_CTGR_PRD_IDX_PRD_ID_STORE_ID_CTGR_ID_VISIBILITY": true,
1100+
"CAT_CTGR_PRD_IDX_STORE_ID_CTGR_ID_VISIBILITY_IS_PARENT_POSITION": true
11001101
},
11011102
"constraint": {
11021103
"PRIMARY": true

app/code/Magento/MysqlMq/etc/db_schema.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
<constraint xsi:type="primary" name="PRIMARY">
4545
<column name="id"/>
4646
</constraint>
47-
<constraint xsi:type="foreign" name="QUEUE_MESSAGE_ID_QUEUE_MESSAGE_STATUS_MESSAGE_ID"
47+
<constraint xsi:type="foreign" name="QUEUE_MESSAGE_STATUS_MESSAGE_ID_QUEUE_MESSAGE_ID"
4848
table="queue_message_status" column="message_id" referenceTable="queue_message" referenceColumn="id"
4949
onDelete="CASCADE"/>
50-
<constraint xsi:type="foreign" name="QUEUE_ID_QUEUE_MESSAGE_STATUS_QUEUE_ID" table="queue_message_status"
50+
<constraint xsi:type="foreign" name="QUEUE_MESSAGE_STATUS_QUEUE_ID_QUEUE_ID" table="queue_message_status"
5151
column="queue_id" referenceTable="queue" referenceColumn="id" onDelete="CASCADE"/>
5252
<constraint xsi:type="unique" name="QUEUE_MESSAGE_STATUS_QUEUE_ID_MESSAGE_ID">
5353
<column name="queue_id"/>

app/code/Magento/MysqlMq/etc/db_schema_whitelist.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
},
3434
"constraint": {
3535
"PRIMARY": true,
36+
"QUEUE_MESSAGE_STATUS_MESSAGE_ID_QUEUE_MESSAGE_ID": true,
3637
"QUEUE_MESSAGE_ID_QUEUE_MESSAGE_STATUS_MESSAGE_ID": true,
38+
"QUEUE_MESSAGE_STATUS_QUEUE_ID_QUEUE_ID": true,
3739
"QUEUE_ID_QUEUE_MESSAGE_STATUS_QUEUE_ID": true,
3840
"QUEUE_MESSAGE_STATUS_QUEUE_ID_MESSAGE_ID": true
3941
}
4042
}
41-
}
43+
}

app/code/Magento/Newsletter/etc/db_schema_whitelist.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"index": {
1313
"NEWSLETTER_SUBSCRIBER_CUSTOMER_ID": true,
14-
"NEWSLETTER_SUBSCRIBER_STORE_ID": true
14+
"NEWSLETTER_SUBSCRIBER_STORE_ID": true,
15+
"NEWSLETTER_SUBSCRIBER_SUBSCRIBER_EMAIL": true
1516
},
1617
"constraint": {
1718
"PRIMARY": true,
@@ -112,4 +113,4 @@
112113
"NLTTR_PROBLEM_SUBSCRIBER_ID_NLTTR_SUBSCRIBER_SUBSCRIBER_ID": true
113114
}
114115
}
115-
}
116+
}

app/code/Magento/Vault/etc/db_schema.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
<constraint xsi:type="foreign" name="VAULT_PAYMENT_TOKEN_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID"
3131
table="vault_payment_token" column="customer_id" referenceTable="customer_entity"
3232
referenceColumn="entity_id" onDelete="CASCADE"/>
33-
<constraint xsi:type="unique" name="UNQ_54DCE14AEAEA03B587F9EF723EB10A10">
33+
<constraint xsi:type="unique" name="VAULT_PAYMENT_TOKEN_PAYMENT_METHOD_CODE_CSTR_ID_GATEWAY_TOKEN">
3434
<column name="payment_method_code"/>
3535
<column name="customer_id"/>
3636
<column name="gateway_token"/>
3737
</constraint>
38-
<constraint xsi:type="unique" name="VAULT_PAYMENT_TOKEN_HASH_UNIQUE_INDEX_PUBLIC_HASH">
38+
<constraint xsi:type="unique" name="VAULT_PAYMENT_TOKEN_PUBLIC_HASH">
3939
<column name="public_hash"/>
4040
</constraint>
4141
</table>

app/code/Magento/Vault/etc/db_schema_whitelist.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"constraint": {
1717
"PRIMARY": true,
1818
"VAULT_PAYMENT_TOKEN_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID": true,
19+
"VAULT_PAYMENT_TOKEN_PAYMENT_METHOD_CODE_CSTR_ID_GATEWAY_TOKEN": true,
1920
"UNQ_54DCE14AEAEA03B587F9EF723EB10A10": true,
21+
"VAULT_PAYMENT_TOKEN_PUBLIC_HASH": true,
2022
"VAULT_PAYMENT_TOKEN_HASH_UNIQUE_INDEX_PUBLIC_HASH": true
2123
}
2224
},
@@ -31,4 +33,4 @@
3133
"FK_4ED894655446D385894580BECA993862": true
3234
}
3335
}
34-
}
36+
}

lib/internal/Magento/Framework/Setup/Declaration/Schema/Declaration/SchemaBuilder.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\DB\Adapter\AdapterInterface;
99
use Magento\Framework\Phrase;
10+
use Magento\Framework\Setup\Declaration\Schema\TableNameResolver;
1011
use Magento\Framework\Stdlib\BooleanUtils;
1112
use Magento\Framework\Setup\Exception;
1213
use Magento\Framework\Setup\Declaration\Schema\Dto\Column;
@@ -67,6 +68,11 @@ class SchemaBuilder
6768
*/
6869
private $resourceConnection;
6970

71+
/**
72+
* @var TableNameResolver
73+
*/
74+
private $tableNameResolver;
75+
7076
/**
7177
* SchemaBuilder constructor.
7278
*
@@ -75,20 +81,23 @@ class SchemaBuilder
7581
* @param Sharding $sharding
7682
* @param ValidationComposite $validationComposite
7783
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
84+
* @param TableNameResolver $tableNameResolver
7885
* @internal param array $tablesData
7986
*/
8087
public function __construct(
8188
ElementFactory $elementFactory,
8289
BooleanUtils $booleanUtils,
8390
Sharding $sharding,
8491
ValidationComposite $validationComposite,
85-
\Magento\Framework\App\ResourceConnection $resourceConnection
92+
\Magento\Framework\App\ResourceConnection $resourceConnection,
93+
TableNameResolver $tableNameResolver
8694
) {
8795
$this->sharding = $sharding;
8896
$this->elementFactory = $elementFactory;
8997
$this->booleanUtils = $booleanUtils;
9098
$this->validationComposite = $validationComposite;
9199
$this->resourceConnection = $resourceConnection;
100+
$this->tableNameResolver = $tableNameResolver;
92101
}
93102

94103
/**
@@ -295,16 +304,7 @@ private function getFullIndexName(
295304
return $name;
296305
}
297306

298-
/**
299-
* Replica tables should be identical to the original -
300-
* indexes and constraints must use the original table name to calculate their own names.
301-
*/
302-
$tableName = $table->getName();
303-
$tableIsReplica = preg_match('#(?<table_name>\S+)_replica$#i', $table->getName(), $matches);
304-
305-
if ($tableIsReplica) {
306-
$tableName = $matches['table_name'];
307-
}
307+
$tableName = $this->tableNameResolver->getNameOfOriginTable($table->getName());
308308

309309
return $this->resourceConnection
310310
->getIdxName(
@@ -340,7 +340,7 @@ private function processIndexes(array $tableData, $resource, Table $table)
340340
* @see MAGETWO-91365
341341
*/
342342
$indexType = AdapterInterface::INDEX_TYPE_INDEX;
343-
if (isset($indexData['index_type']) && $indexData['indexType'] === AdapterInterface::INDEX_TYPE_FULLTEXT) {
343+
if (isset($indexData['indexType']) && $indexData['indexType'] === AdapterInterface::INDEX_TYPE_FULLTEXT) {
344344
$indexType = $indexData['indexType'];
345345
}
346346

@@ -413,7 +413,7 @@ private function processConstraints(array $tableData, $resource, Schema $schema,
413413
*/
414414
$constraintData['name'] = $this->resourceConnection
415415
->getFkName(
416-
$table->getName(),
416+
$this->tableNameResolver->getNameOfOriginTable($table->getName()),
417417
$constraintData['column']->getName(),
418418
$constraintData['referenceTable']->getName(),
419419
$constraintData['referenceColumn']->getName()

lib/internal/Magento/Framework/Setup/Declaration/Schema/Diff/Diff.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
namespace Magento\Framework\Setup\Declaration\Schema\Diff;
88

99
use Magento\Framework\Component\ComponentRegistrar;
10+
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraint;
1011
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Reference;
1112
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface;
13+
use Magento\Framework\Setup\Declaration\Schema\Dto\Index;
1214
use Magento\Framework\Setup\Declaration\Schema\Dto\Table;
1315
use Magento\Framework\Setup\Declaration\Schema\Dto\TableElementInterface;
1416
use Magento\Framework\Setup\Declaration\Schema\ElementHistory;
@@ -158,20 +160,41 @@ private function getWhiteListTables()
158160
* @param string $operation
159161
* @return bool
160162
*/
161-
public function canBeRegistered(ElementInterface $object, $operation)
163+
public function canBeRegistered(ElementInterface $object, $operation): bool
162164
{
163165
if (!isset($this->destructiveOperations[$operation])) {
164166
return true;
165167
}
166168

169+
$checkResult = false;
167170
$whiteList = $this->getWhiteListTables();
168-
$type = $object->getElementType();
169171

170172
if ($object instanceof TableElementInterface) {
171-
return isset($whiteList[$object->getTable()->getNameWithoutPrefix()][$type][$object->getName()]);
173+
$tableNameWithoutPrefix = $object->getTable()->getNameWithoutPrefix();
174+
$type = $object->getElementType();
175+
176+
if ($this->isElementHaveAutoGeneratedName($object)) {
177+
$checkResult =
178+
isset($whiteList[$tableNameWithoutPrefix][$type][$object->getNameWithoutPrefix()]);
179+
} else {
180+
$checkResult = isset($whiteList[$tableNameWithoutPrefix][$type][$object->getName()]);
181+
}
182+
} elseif ($object instanceof Table) {
183+
$checkResult = isset($whiteList[$object->getNameWithoutPrefix()]);
172184
}
173185

174-
return isset($whiteList[$object->getNameWithoutPrefix()]);
186+
return $checkResult;
187+
}
188+
189+
/**
190+
* Check if the element has an auto-generated name.
191+
*
192+
* @param ElementInterface $element
193+
* @return bool
194+
*/
195+
private function isElementHaveAutoGeneratedName(ElementInterface $element): bool
196+
{
197+
return in_array($element->getElementType(), [Index::TYPE, Constraint::TYPE], true);
175198
}
176199

177200
/**

lib/internal/Magento/Framework/Setup/Declaration/Schema/Dto/Factories/Foreign.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\ResourceConnection;
99
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Framework\Setup\Declaration\Schema\TableNameResolver;
1011

1112
/**
1213
* Foreign key constraint factory.
@@ -33,21 +34,29 @@ class Foreign implements FactoryInterface
3334
*/
3435
private $resourceConnection;
3536

37+
/**
38+
* @var TableNameResolver
39+
*/
40+
private $tableNameResolver;
41+
3642
/**
3743
* Constructor.
3844
*
3945
* @param ObjectManagerInterface $objectManager
4046
* @param ResourceConnection $resourceConnection
47+
* @param TableNameResolver $tableNameResolver
4148
* @param string $className
4249
*/
4350
public function __construct(
4451
ObjectManagerInterface $objectManager,
4552
ResourceConnection $resourceConnection,
53+
TableNameResolver $tableNameResolver,
4654
$className = \Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Reference::class
4755
) {
4856
$this->objectManager = $objectManager;
4957
$this->resourceConnection = $resourceConnection;
5058
$this->className = $className;
59+
$this->tableNameResolver = $tableNameResolver;
5160
}
5261

5362
/**
@@ -65,7 +74,9 @@ public function create(array $data)
6574
$nameWithoutPrefix = $this->resourceConnection
6675
->getConnection($data['table']->getResource())
6776
->getForeignKeyName(
68-
$data['table']->getNameWithoutPrefix(),
77+
$this->tableNameResolver->getNameOfOriginTable(
78+
$data['table']->getNameWithoutPrefix()
79+
),
6980
$data['column']->getName(),
7081
$data['referenceTable']->getNameWithoutPrefix(),
7182
$data['referenceColumn']->getName()

lib/internal/Magento/Framework/Setup/Declaration/Schema/Dto/Factories/Index.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
namespace Magento\Framework\Setup\Declaration\Schema\Dto\Factories;
77

88
use Magento\Framework\App\ResourceConnection;
9+
use Magento\Framework\DB\Adapter\AdapterInterface;
910
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Setup\Declaration\Schema\TableNameResolver;
1012

1113
/**
1214
* Index element factory.
@@ -33,21 +35,29 @@ class Index implements FactoryInterface
3335
*/
3436
private $resourceConnection;
3537

38+
/**
39+
* @var TableNameResolver
40+
*/
41+
private $tableNameResolver;
42+
3643
/**
3744
* Constructor.
3845
*
3946
* @param ObjectManagerInterface $objectManager
4047
* @param ResourceConnection $resourceConnection
48+
* @param TableNameResolver $tableNameResolver
4149
* @param string $className
4250
*/
4351
public function __construct(
4452
ObjectManagerInterface $objectManager,
4553
ResourceConnection $resourceConnection,
54+
TableNameResolver $tableNameResolver,
4655
$className = \Magento\Framework\Setup\Declaration\Schema\Dto\Index::class
4756
) {
4857
$this->objectManager = $objectManager;
4958
$this->resourceConnection = $resourceConnection;
5059
$this->className = $className;
60+
$this->tableNameResolver = $tableNameResolver;
5161
}
5262

5363
/**
@@ -62,12 +72,22 @@ public function create(array $data)
6272
$nameWithoutPrefix = $data['name'];
6373

6474
if ($this->resourceConnection->getTablePrefix()) {
75+
/**
76+
* Temporary solution.
77+
* @see MAGETWO-91365
78+
*/
79+
$indexType = AdapterInterface::INDEX_TYPE_INDEX;
80+
if ($data['indexType'] === AdapterInterface::INDEX_TYPE_FULLTEXT) {
81+
$indexType = $data['indexType'];
82+
}
6583
$nameWithoutPrefix = $this->resourceConnection
6684
->getConnection($data['table']->getResource())
6785
->getIndexName(
68-
$data['table']->getNameWithoutPrefix(),
86+
$this->tableNameResolver->getNameOfOriginTable(
87+
$data['table']->getNameWithoutPrefix()
88+
),
6989
$data['column'],
70-
$data['type']
90+
$indexType
7191
);
7292
}
7393

0 commit comments

Comments
 (0)