diff --git a/application/Espo/Core/Utils/Database/Orm/Converter.php b/application/Espo/Core/Utils/Database/Orm/Converter.php index ca0d8962db..6be7fadb40 100644 --- a/application/Espo/Core/Utils/Database/Orm/Converter.php +++ b/application/Espo/Core/Utils/Database/Orm/Converter.php @@ -717,10 +717,8 @@ class Converter /** * @param array $ormMetadata - * @param string $entityType - * @return void */ - protected function applyIndexes(&$ormMetadata, $entityType) + protected function applyIndexes(&$ormMetadata, string $entityType): void { if (isset($ormMetadata[$entityType]['fields'])) { $indexList = SchemaUtils::getEntityIndexListByFieldsDefs($ormMetadata[$entityType]['fields']); diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index 1469509ace..44e41ae520 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -29,6 +29,7 @@ namespace Espo\Core\Utils\Database\Schema; +use Doctrine\DBAL\Schema\SchemaException; use Espo\Core\Utils\Config; use Espo\Core\Utils\Database\Schema\Utils as SchemaUtils; use Espo\Core\Utils\File\Manager as FileManager; @@ -148,7 +149,7 @@ class Converter * * @param array $ormMeta * @param string[]|string|null $entityList - * @throws \Doctrine\DBAL\Schema\SchemaException + * @throws SchemaException */ public function process(array $ormMeta, $entityList = null): DbalSchema { @@ -307,7 +308,7 @@ class Converter * * @param string $entityName * @param array $relationParams - * @throws \Doctrine\DBAL\Schema\SchemaException + * @throws SchemaException */ protected function prepareManyMany(string $entityName, $relationParams): Table { @@ -399,8 +400,7 @@ class Converter if (!empty($relationParams['indexes'])) { $normalizedIndexes = SchemaUtils::getIndexes([ $entityName => [ - 'fields' => [], - 'indexes' => $relationParams['indexes'], + 'indexes' => $relationParams['indexes'] ] ]); @@ -429,34 +429,21 @@ class Converter } /** - * @param array> $indexes - * @throws \Doctrine\DBAL\Schema\SchemaException + * @param array> $indexes + * @throws SchemaException */ protected function addIndexes(Table $table, array $indexes): void { foreach ($indexes as $indexName => $indexParams) { $indexDefs = IndexDefs::fromRaw($indexParams, $indexName); - $indexType = SchemaUtils::getIndexTypeByIndexDefs($indexDefs); + if ($indexDefs->isUnique()) { + $table->addUniqueIndex($indexDefs->getColumnList(), $indexName); - /*$indexType = !empty($indexParams['type']) ? - $indexParams['type'] : - SchemaUtils::getIndexTypeByIndexDefs($indexParams);*/ - - switch ($indexType) { - case 'index': - case 'fulltext': - $indexFlagList = isset($indexParams['flags']) ? $indexParams['flags'] : []; - - $table->addIndex($indexParams['columns'], $indexName, $indexFlagList); - - break; - - case 'unique': - $table->addUniqueIndex($indexParams['columns'], $indexName); - - break; + continue; } + + $table->addIndex($indexDefs->getColumnList(), $indexName, $indexDefs->getFlagList()); } } diff --git a/application/Espo/Core/Utils/Database/Schema/Utils.php b/application/Espo/Core/Utils/Database/Schema/Utils.php index 263a107bfd..33d6c9f10b 100644 --- a/application/Espo/Core/Utils/Database/Schema/Utils.php +++ b/application/Espo/Core/Utils/Database/Schema/Utils.php @@ -37,16 +37,16 @@ class Utils /** * Get indexes in specific format. * - * @param array $ormMeta - * @param string[] $ignoreFlags + * @param array $defs + * @param string[] $ignoreFlags @todo Remove parameter? * @return array> */ - public static function getIndexes(array $ormMeta, array $ignoreFlags = []): array + public static function getIndexes(array $defs, array $ignoreFlags = []): array { $indexList = []; - foreach ($ormMeta as $entityName => $entityParams) { - $entityIndexList = static::getEntityIndexListByFieldsDefs($entityParams['fields']); + foreach ($defs as $entityType => $entityParams) { + $entityIndexList = static::getEntityIndexListByFieldsDefs($entityParams['fields'] ?? []); foreach ($entityIndexList as $indexName => $indexParams) { if (!isset($entityParams['indexes'][$indexName])) { @@ -58,37 +58,38 @@ class Utils foreach ($entityParams['indexes'] as $indexName => $indexParams) { $indexDefs = IndexDefs::fromRaw($indexParams, $indexName); - $tableIndexName = $indexParams['key'] ?? static::generateIndexName($indexDefs, $entityName); + $tableIndexName = $indexParams['key'] ?? static::generateIndexName($indexDefs, $entityType); - if (isset($indexParams['flags']) && is_array($indexParams['flags'])) { + $columns = $indexDefs->getColumnList(); + $flags = $indexDefs->getFlagList(); + + if ($flags !== []) { $skipIndex = false; foreach ($ignoreFlags as $ignoreFlag) { - if (($flagKey = array_search($ignoreFlag, $indexParams['flags'])) !== false) { - unset($indexParams['flags'][$flagKey]); + if (($flagKey = array_search($ignoreFlag, $flags)) !== false) { + unset($flags[$flagKey]); $skipIndex = true; } } - if ($skipIndex && empty($indexParams['flags'])) { + if ($skipIndex && empty($flags)) { continue; } - $indexList[$entityName][$tableIndexName]['flags'] = $indexParams['flags']; + $indexList[$entityType][$tableIndexName]['flags'] = $flags; } - if (is_array($indexParams['columns'])) { + if ($columns !== []) { $indexType = static::getIndexTypeByIndexDefs($indexDefs); // @todo Revise, may to be removed. - $indexList[$entityName][$tableIndexName]['type'] = $indexType; + $indexList[$entityType][$tableIndexName]['type'] = $indexType; - $indexList[$entityName][$tableIndexName]['columns'] = array_map( - function ($item) { - return Util::toUnderScore($item); - }, - $indexParams['columns'] + $indexList[$entityType][$tableIndexName]['columns'] = array_map( + fn ($item) => Util::toUnderScore($item), + $columns ); } } @@ -142,9 +143,9 @@ class Utils } /** - * @param array $fieldsDefs + * @param array $fieldsDefs * @param bool $isTableColumnNames - * @return array + * @return array */ public static function getEntityIndexListByFieldsDefs(array $fieldsDefs, $isTableColumnNames = false) {