Spaces:
No application file
No application file
File size: 5,458 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
<?php
declare(strict_types=1);
namespace Mautic\LeadBundle\Field;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Schema\SchemaException as DoctrineSchemaException;
use Mautic\CoreBundle\Doctrine\Helper\IndexSchemaHelper;
use Mautic\CoreBundle\Exception\SchemaException;
use Mautic\LeadBundle\Entity\LeadField;
use Psr\Log\LoggerInterface;
class CustomFieldIndex
{
public function __construct(
private IndexSchemaHelper $indexSchemaHelper,
private LoggerInterface $logger,
private FieldsWithUniqueIdentifier $fieldsWithUniqueIdentifier
) {
}
/**
* Update the unique_identifier_search index and add an index for this field.
*
* @throws DriverException
* @throws DoctrineSchemaException
* @throws SchemaException
*/
public function addIndexOnColumn(LeadField $leadField): void
{
try {
/** @var IndexSchemaHelper $modifySchema */
$modifySchema = $this->indexSchemaHelper->setName($leadField->getCustomFieldObject());
$alias = $leadField->getAlias();
$modifySchema->addIndex([$alias], $alias.'_search');
$modifySchema->allowColumn($alias);
$this->updateUniqueIdentifierIndex($leadField);
$modifySchema->executeChanges();
} catch (DriverException $e) {
if (1069 === $e->getCode() /* ER_TOO_MANY_KEYS */) {
$this->logger->warning($e->getMessage());
} else {
throw $e;
}
}
}
/**
* Updates the index for this field.
*
* @throws DriverException
* @throws DoctrineSchemaException
* @throws SchemaException
*/
public function dropIndexOnColumn(LeadField $leadField): void
{
try {
/** @var IndexSchemaHelper $modifySchema */
$modifySchema = $this->indexSchemaHelper->setName($leadField->getCustomFieldObject());
$alias = $leadField->getAlias();
$modifySchema->dropIndex([$alias], $alias.'_search');
$modifySchema->allowColumn($alias);
$modifySchema->executeChanges();
} catch (DriverException $e) {
if (1069 === $e->getCode() /* ER_TOO_MANY_KEYS */) {
$this->logger->warning($e->getMessage());
} else {
throw $e;
}
}
}
/**
* @throws SchemaException
*/
public function isUpdatePending(LeadField $leadField): bool
{
$hasIndex = $this->hasIndex($leadField);
if ($leadField->isIsIndex() !== $hasIndex) {
return true;
}
if (!$this->hasMatchingUniqueIdentifierIndex($leadField)) {
return true;
}
return false;
}
/**
* @throws SchemaException
*/
public function hasIndex(LeadField $leadField): bool
{
return $this->indexSchemaHelper->hasIndex($leadField);
}
public function hasMatchingUniqueIdentifierIndex(LeadField $leadField): bool
{
$uniqueIdentifierColumns = $this->getUniqueIdentifierIndexColumns($leadField->getObject());
try {
return $this->indexSchemaHelper->hasMatchingUniqueIdentifierIndex($leadField, $uniqueIdentifierColumns);
} catch (DoctrineSchemaException) {
// Return true only if there are no unique identifier fields but otherwise assume the index is missing
return 0 === count($uniqueIdentifierColumns);
}
}
/**
* @throws DoctrineSchemaException
* @throws SchemaException
*/
public function updateUniqueIdentifierIndex(LeadField $leadField): void
{
if ($this->hasMatchingUniqueIdentifierIndex($leadField)) {
return;
}
/** @var IndexSchemaHelper $modifySchema */
$modifySchema = $this->indexSchemaHelper->setName($leadField->getCustomFieldObject());
$indexColumns = $this->getUniqueIdentifierIndexColumns($leadField->getObject());
if (!$indexColumns) {
$this->dropIndexForUniqueIdentifiers($leadField);
return;
}
$modifySchema->addIndex($indexColumns, 'unique_identifier_search');
$modifySchema->executeChanges();
}
/**
* @throws DoctrineSchemaException
* @throws SchemaException
*/
private function dropIndexForUniqueIdentifiers(LeadField $leadField): void
{
if (!$this->indexSchemaHelper->hasUniqueIdentifierIndex($leadField)) {
return;
}
/** @var IndexSchemaHelper $modifySchema */
$modifySchema = $this->indexSchemaHelper->setName($leadField->getCustomFieldObject());
$indexColumns = $this->getUniqueIdentifierIndexColumns($leadField->getObject());
$modifySchema->dropIndex($indexColumns, 'unique_identifier_search');
$modifySchema->executeChanges();
}
/**
* @return array<mixed>
*/
private function getUniqueIdentifierIndexColumns(string $object = 'lead'): array
{
// Filters
$filters = ['object' => $object];
// Get list of current uniques
$uniqueIdentifierFields = $this->fieldsWithUniqueIdentifier->getLiveFields($filters);
// Always use email
$indexColumns = array_keys($uniqueIdentifierFields);
// Only use three to prevent max key length errors
return array_slice($indexColumns, 0, 3);
}
}
|