diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index d56645d8f7..cf817a268e 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -396,7 +396,7 @@ class Stream extends \Espo\Core\Services\Base 'orderBy' => 'number', 'order' => 'DESC', 'limit' => $sqLimit, - 'useIndex' => 'createdByNumber', + 'useIndex' => 'number', ]; if ($user->isPortal()) { @@ -510,7 +510,7 @@ class Stream extends \Espo\Core\Services\Base 'orderBy' => 'number', 'order' => 'DESC', 'limit' => $sqLimit, - 'useIndex' => 'createdByNumber', + 'useIndex' => 'number', ]; if ($user->isPortal()) { diff --git a/upgrades/5.8/scripts/AfterUpgrade.php b/upgrades/5.8/scripts/AfterUpgrade.php index 210ccc22cc..1e0ce8f066 100644 --- a/upgrades/5.8/scripts/AfterUpgrade.php +++ b/upgrades/5.8/scripts/AfterUpgrade.php @@ -24,9 +24,14 @@ class AfterUpgrade { public function run($container) { + $this->container = $container; + $entityManager = $container->get('entityManager'); - $this->populateOpportunityContactId($entityManager); + ///$this->populateOpportunityContactId($entityManager); + + $this->manageIndexes(); + } protected function populateOpportunityContactId($entityManager) @@ -63,4 +68,55 @@ class AfterUpgrade $pdo->query($q); } } + + protected function manageIndexes() + { + $pdo = $this->container->get('entityManager')->getPdo(); + + $sth = $pdo->prepare("SHOW INDEX FROM `note`"); + $sth->execute(); + $rows = []; + while ($row = $sth->fetch()) { + $rows[] = $row; + } + + $indexes = []; + + foreach ($rows as $item) { + $k = 0; + foreach ($rows as $item2) { + if ($item['Key_name'] === $item2['Key_name']) { + $k++; + } + } + if ($k === 1 && $item['Column_name'] === 'number') { + $indexes[] = $item['Key_name']; + } + } + + $isFound = false; + + $oldIndexes = []; + + foreach ($indexes as $key) { + if ($key === 'UNIQ_NUMBER') { + $isFound = true; + } + } + + if (!$isFound) { + try { + $sql = "CREATE UNIQUE INDEX UNIQ_NUMBER ON `note` (`number`)"; + $pdo->query($sql); + } catch (\Exception $e) {} + } + + foreach ($indexes as $item) { + if ($item === 'UNIQ_NUMBER') continue; + try { + $sql = "DROP INDEX {$item} ON `note`"; + $pdo->query($sql); + } catch (\Exception $e) {} + } + } }