diff --git a/application/Espo/Core/Utils/Database/Orm/Converter.php b/application/Espo/Core/Utils/Database/Orm/Converter.php index 563febf70c..ca0d8962db 100644 --- a/application/Espo/Core/Utils/Database/Orm/Converter.php +++ b/application/Espo/Core/Utils/Database/Orm/Converter.php @@ -30,6 +30,7 @@ namespace Espo\Core\Utils\Database\Orm; use Espo\Core\Utils\Util; +use Espo\ORM\Defs\IndexDefs; use Espo\ORM\Entity; use Espo\Core\Utils\Database\Schema\Utils as SchemaUtils; use Espo\Core\Utils\Metadata; @@ -733,19 +734,23 @@ class Converter if (isset($ormMetadata[$entityType]['indexes'])) { foreach ($ormMetadata[$entityType]['indexes'] as $indexName => &$indexData) { - if (!isset($indexData['key'])) { - $indexType = SchemaUtils::getIndexTypeByIndexDefs($indexData); - $indexData['key'] = SchemaUtils::generateIndexName($indexName, $indexType); + $indexDefs = IndexDefs::fromRaw($indexData, $indexName); + + if (!$indexDefs->getKey()) { + $indexData['key'] = SchemaUtils::generateIndexName($indexDefs, $entityType); } } } if (isset($ormMetadata[$entityType]['relations'])) { - foreach ($ormMetadata[$entityType]['relations'] as $relationName => &$relationData) { + foreach ($ormMetadata[$entityType]['relations'] as &$relationData) { if (isset($relationData['indexes'])) { foreach ($relationData['indexes'] as $indexName => &$indexData) { - $indexType = SchemaUtils::getIndexTypeByIndexDefs($indexData); - $indexData['key'] = SchemaUtils::generateIndexName($indexName, $indexType); + $indexDefs = IndexDefs::fromRaw($indexData, $indexName); + + $relationName = $relationData['relationName'] ?? ''; + + $indexData['key'] = SchemaUtils::generateIndexName($indexDefs, $relationName); } } } diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index 5d0a600105..1469509ace 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -40,6 +40,7 @@ use Espo\Core\Utils\Util; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Schema as DbalSchema; use Doctrine\DBAL\Types\Type as DbalType; +use Espo\ORM\Defs\IndexDefs; class Converter { @@ -305,12 +306,14 @@ class Converter * Prepare a relation table for the manyMany relation. * * @param string $entityName - * @param array $relationParams + * @param array $relationParams * @throws \Doctrine\DBAL\Schema\SchemaException */ protected function prepareManyMany(string $entityName, $relationParams): Table { - $tableName = Util::toUnderScore($relationParams['relationName']); + $relationName = $relationParams['relationName']; + + $tableName = Util::toUnderScore($relationName); $this->log->debug('DBAL: prepareManyMany invoked for ' . $entityName, [ 'tableName' => $tableName, 'parameters' => $relationParams @@ -342,7 +345,7 @@ class Converter ]); } else { - foreach($relationParams['midKeys'] as $midKey) { + foreach ($relationParams['midKeys'] as $midKey) { $columnName = Util::toUnderScore($midKey); $table->addColumn( @@ -354,7 +357,11 @@ class Converter ]) ); - $table->addIndex([$columnName], SchemaUtils::generateIndexName($columnName)); + $indexDefs = IndexDefs::fromRaw([], $columnName); + + $indexName = SchemaUtils::generateIndexName($indexDefs, $relationName); + + $table->addIndex([$columnName], $indexName); $uniqueIndex[] = $columnName; } @@ -411,10 +418,11 @@ class Converter /** @var string[] $uniqueIndex */ $uniqueIndexName = implode('_', $uniqueIndex); - $table->addUniqueIndex( - $uniqueIndex, - SchemaUtils::generateIndexName($uniqueIndexName, 'unique') - ); + $indexDefs = IndexDefs::fromRaw(['type' => 'unique'], $uniqueIndexName); + + $indexName = SchemaUtils::generateIndexName($indexDefs, $relationName); + + $table->addUniqueIndex($uniqueIndex, $indexName); } return $table; @@ -426,10 +434,14 @@ class Converter */ protected function addIndexes(Table $table, array $indexes): void { - foreach($indexes as $indexName => $indexParams) { - $indexType = !empty($indexParams['type']) ? + foreach ($indexes as $indexName => $indexParams) { + $indexDefs = IndexDefs::fromRaw($indexParams, $indexName); + + $indexType = SchemaUtils::getIndexTypeByIndexDefs($indexDefs); + + /*$indexType = !empty($indexParams['type']) ? $indexParams['type'] : - SchemaUtils::getIndexTypeByIndexDefs($indexParams); + SchemaUtils::getIndexTypeByIndexDefs($indexParams);*/ switch ($indexType) { case 'index': diff --git a/application/Espo/Core/Utils/Database/Schema/Utils.php b/application/Espo/Core/Utils/Database/Schema/Utils.php index c073ed7a11..263a107bfd 100644 --- a/application/Espo/Core/Utils/Database/Schema/Utils.php +++ b/application/Espo/Core/Utils/Database/Schema/Utils.php @@ -30,20 +30,22 @@ namespace Espo\Core\Utils\Database\Schema; use Espo\Core\Utils\Util; +use Espo\ORM\Defs\IndexDefs; class Utils { /** - * @param array $ormMeta + * Get indexes in specific format. + * + * @param array $ormMeta * @param string[] $ignoreFlags - * @return array> + * @return array> */ - public static function getIndexes(array $ormMeta, array $ignoreFlags = []) + public static function getIndexes(array $ormMeta, array $ignoreFlags = []): array { $indexList = []; foreach ($ormMeta as $entityName => $entityParams) { - /* add indexes for additionalTables */ $entityIndexList = static::getEntityIndexListByFieldsDefs($entityParams['fields']); foreach ($entityIndexList as $indexName => $indexParams) { @@ -54,11 +56,9 @@ class Utils if (isset($entityParams['indexes']) && is_array($entityParams['indexes'])) { foreach ($entityParams['indexes'] as $indexName => $indexParams) { - $indexType = static::getIndexTypeByIndexDefs($indexParams); + $indexDefs = IndexDefs::fromRaw($indexParams, $indexName); - $tableIndexName = isset($indexParams['key']) ? - $indexParams['key'] : - static::generateIndexName($indexName, $indexType); + $tableIndexName = $indexParams['key'] ?? static::generateIndexName($indexDefs, $entityName); if (isset($indexParams['flags']) && is_array($indexParams['flags'])) { $skipIndex = false; @@ -79,6 +79,9 @@ class Utils } if (is_array($indexParams['columns'])) { + $indexType = static::getIndexTypeByIndexDefs($indexDefs); + + // @todo Revise, may to be removed. $indexList[$entityName][$tableIndexName]['type'] = $indexType; $indexList[$entityName][$tableIndexName]['columns'] = array_map( @@ -178,19 +181,15 @@ class Utils } /** - * @param array $indexDefs - * @return string + * @return 'unique'|'fulltext'|'index' */ - public static function getIndexTypeByIndexDefs(array $indexDefs) + public static function getIndexTypeByIndexDefs(IndexDefs $indexDefs): string { - if ( - (isset($indexDefs['type']) && $indexDefs['type'] == 'unique') || - (isset($indexDefs['unique']) && $indexDefs['unique']) - ) { + if ($indexDefs->isUnique()) { return 'unique'; } - if (isset($indexDefs['flags']) && in_array('fulltext', $indexDefs['flags'])) { + if (in_array('fulltext', $indexDefs->getFlagList())) { return 'fulltext'; } @@ -198,28 +197,20 @@ class Utils } /** - * @param string $name - * @param string $type - * @param int $maxLength - * @return string + * @todo Move to IndexHelper interface. */ - public static function generateIndexName($name, $type = 'index', $maxLength = 60) + public static function generateIndexName(IndexDefs $defs, string $entityType): string { - switch ($type) { - case 'unique': - $prefix = 'UNIQ'; - break; + $maxLength = 60; - default: - $prefix = 'IDX'; - break; - } + $name = $defs->getName(); + $prefix = $defs->isUnique() ? 'UNIQ' : 'IDX'; - $nameList = []; - $nameList[] = strtoupper($prefix); - $nameList[] = strtoupper(Util::toUnderScore($name)); + $parts = [$prefix, strtoupper(Util::toUnderScore($name))]; - return substr(implode('_', $nameList), 0, $maxLength); + $key = implode('_', $parts); + + return substr($key, 0, $maxLength); } /** diff --git a/application/Espo/ORM/Defs/IndexDefs.php b/application/Espo/ORM/Defs/IndexDefs.php index d7862a678f..a94983d9f3 100644 --- a/application/Espo/ORM/Defs/IndexDefs.php +++ b/application/Espo/ORM/Defs/IndexDefs.php @@ -66,7 +66,7 @@ class IndexDefs */ public function getKey(): string { - return $this->data['key']; + return $this->data['key'] ?? ''; } /** @@ -74,6 +74,11 @@ class IndexDefs */ public function isUnique(): bool { + // For bc. + if (($this->data['unique'] ?? false)) { + return true; + } + $type = $this->data['type'] ?? null; return $type === 'unique';