This commit is contained in:
Yuri Kuznetsov
2023-01-24 11:18:05 +02:00
parent 433af312cf
commit 314b5bcd87
3 changed files with 33 additions and 47 deletions
@@ -717,10 +717,8 @@ class Converter
/**
* @param array<string, mixed> $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']);
@@ -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<string,mixed> $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<string, mixed> $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<string,array<string,mixed>> $indexes
* @throws \Doctrine\DBAL\Schema\SchemaException
* @param array<string, array<string, mixed>> $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());
}
}
@@ -37,16 +37,16 @@ class Utils
/**
* Get indexes in specific format.
*
* @param array<string, mixed> $ormMeta
* @param string[] $ignoreFlags
* @param array<string, mixed> $defs
* @param string[] $ignoreFlags @todo Remove parameter?
* @return array<string, array<string, mixed>>
*/
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<string,mixed> $fieldsDefs
* @param array<string, mixed> $fieldsDefs
* @param bool $isTableColumnNames
* @return array<string,mixed>
* @return array<string, mixed>
*/
public static function getEntityIndexListByFieldsDefs(array $fieldsDefs, $isTableColumnNames = false)
{