entityDefs modifier

This commit is contained in:
Yuri Kuznetsov
2023-02-22 10:51:20 +02:00
parent f9294c652d
commit 46f333fced
5 changed files with 117 additions and 22 deletions
@@ -149,6 +149,10 @@ class Converter
$ormMetadata[$entityType]['skipRebuild'] = true;
}
if ($entityMetadata['modifierClassName'] ?? null) {
$ormMetadata[$entityType]['modifierClassName'] = $entityMetadata['modifierClassName'];
}
/** @var array<string, array<string, mixed>> $ormMetadata */
$ormMetadata = Util::merge(
$ormMetadata,
@@ -29,6 +29,7 @@
namespace Espo\Core\Utils\Database\Schema;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Database\ConfigDataProvider;
use Espo\Core\Utils\Database\MetadataProvider as MetadataProvider;
use Espo\Core\Utils\File\Manager as FileManager;
@@ -50,6 +51,8 @@ use Doctrine\DBAL\Schema\Schema as DbalSchema;
use Doctrine\DBAL\Types\Type as DbalType;
use Espo\ORM\Type\AttributeType;
use const E_USER_DEPRECATED;
/**
* Schema representation builder.
*/
@@ -70,6 +73,7 @@ class Builder
private Metadata $metadata,
private FileManager $fileManager,
private Log $log,
private InjectableFactory $injectableFactory,
private PathProvider $pathProvider,
ConfigDataProvider $configDataProvider,
ColumnPreparatorFactory $columnPreparatorFactory,
@@ -136,6 +140,14 @@ class Builder
$entityType = $entityDefs->getName();
$modifier = $this->getEntityDefsModifier($entityDefs);
if ($modifier) {
$modifiedEntityDefs = $modifier->modify($entityDefs);
$entityDefs = EntityDefs::fromRaw($modifiedEntityDefs->toAssoc(), $entityType);
}
$this->log->debug("Schema\Builder: Entity {$entityType}");
$tableName = Util::toUnderScore($entityType);
@@ -196,6 +208,18 @@ class Builder
$this->addIndexes($table, $entityDefs->getIndexList());
}
private function getEntityDefsModifier(EntityDefs $entityDefs): ?EntityDefsModifier
{
/** @var ?class-string<EntityDefsModifier> $modifierClassName */
$modifierClassName = $entityDefs->getParam('modifierClassName');
if (!$modifierClassName) {
return null;
}
return $this->injectableFactory->create($modifierClassName);
}
/**
* @param array<string, mixed> $ormMeta
* @param ?string[] $entityTypeList
@@ -464,6 +488,13 @@ class Builder
$this->loadData($this->pathProvider->getCustom() . $this->tablesPath)
);
if ($customTables !== []) {
trigger_error(
'Definitions in Database\\Schema\\tables are deprecated and will be remove in v8.0.',
E_USER_DEPRECATED
);
}
return $customTables;
}
@@ -27,24 +27,15 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
return [
'unset' => [
'__APPEND__',
'Preferences',
],
'unsetIgnore' => [
'__APPEND__',
['Preferences', 'fields', 'id'],
['Preferences', 'fields', 'data'],
],
'Preferences' => [
'fields' => [
'id' => [
'type' => 'id',
],
'data' => [
'type' => 'text',
]
]
],
];
namespace Espo\Core\Utils\Database\Schema;
use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\ORM\Defs\EntityDefs as OrmEntityDefs;
/**
* Modifies definitions before building a schema.
*/
interface EntityDefsModifier
{
public function modify(OrmEntityDefs $entityDefs): EntityDefs;
}
@@ -0,0 +1,68 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\Database\Schema\EntityDefsModifiers;
use Espo\Core\Utils\Database\Orm\Defs\AttributeDefs;
use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\Core\Utils\Database\Schema\EntityDefsModifier;
use Espo\ORM\Defs\EntityDefs as OrmEntityDefs;
use Espo\ORM\Type\AttributeType;
/**
* A single JSON column instead of multiple field columns.
*/
class JsonData implements EntityDefsModifier
{
public function modify(OrmEntityDefs $entityDefs): EntityDefs
{
$sourceIdAttribute = $entityDefs->getAttribute('id');
$idAttribute = AttributeDefs::create('id')
->withType(AttributeType::ID);
$length = $sourceIdAttribute->getLength();
$dbType = $sourceIdAttribute->getParam('dbType');
if ($length) {
$idAttribute = $idAttribute->withLength($length);
}
if ($dbType) {
$idAttribute = $idAttribute->withDbType($dbType);
}
return EntityDefs::create()
->withAttribute($idAttribute)
->withAttribute(
AttributeDefs::create('data')
->withType(AttributeType::JSON_OBJECT)
);
}
}
@@ -186,5 +186,6 @@
"default": false
}
},
"noDeletedAttribute": true
"noDeletedAttribute": true,
"modifierClassName": "Espo\\Core\\Utils\\Database\\Schema\\EntityDefsModifiers\\JsonData"
}