type fixes
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
namespace Espo\Core\Utils\Database;
|
||||
|
||||
use Espo\Core\{
|
||||
Exceptions\Error,
|
||||
Utils\Config,
|
||||
};
|
||||
|
||||
@@ -51,11 +50,11 @@ use RuntimeException;
|
||||
|
||||
class Helper
|
||||
{
|
||||
private $config;
|
||||
private ?Config $config;
|
||||
|
||||
private $dbalConnection;
|
||||
private ?DbalConnection $dbalConnection = null;
|
||||
|
||||
private $pdoConnection;
|
||||
private ?PDO $pdoConnection = null;
|
||||
|
||||
private $driverPlatformMap = [
|
||||
'pdo_mysql' => 'Mysql',
|
||||
@@ -79,7 +78,7 @@ class Helper
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function getDbalConnection()
|
||||
public function getDbalConnection(): DbalConnection
|
||||
{
|
||||
if (!isset($this->dbalConnection)) {
|
||||
$this->dbalConnection = $this->createDbalConnection();
|
||||
@@ -88,7 +87,7 @@ class Helper
|
||||
return $this->dbalConnection;
|
||||
}
|
||||
|
||||
public function getPdoConnection()
|
||||
public function getPdoConnection(): PDO
|
||||
{
|
||||
if (!isset($this->pdoConnection)) {
|
||||
$this->pdoConnection = $this->createPdoConnection();
|
||||
@@ -97,17 +96,17 @@ class Helper
|
||||
return $this->pdoConnection;
|
||||
}
|
||||
|
||||
public function setDbalConnection(DbalConnection $dbalConnection)
|
||||
public function setDbalConnection(DbalConnection $dbalConnection): void
|
||||
{
|
||||
$this->dbalConnection = $dbalConnection;
|
||||
}
|
||||
|
||||
public function setPdoConnection(PDO $pdoConnection)
|
||||
public function setPdoConnection(PDO $pdoConnection): void
|
||||
{
|
||||
$this->pdoConnection = $pdoConnection;
|
||||
}
|
||||
|
||||
public function createDbalConnection(array $params = [])
|
||||
public function createDbalConnection(array $params = []): DbalConnection
|
||||
{
|
||||
if (empty($params) && isset($this->config)) {
|
||||
$params = $this->config->get('database');
|
||||
|
||||
@@ -33,6 +33,8 @@ use Doctrine\DBAL\{
|
||||
Types\Type,
|
||||
Schema\SchemaDiff as DBALSchemaDiff,
|
||||
Schema\Schema as DBALSchema,
|
||||
Connection,
|
||||
Platforms\AbstractPlatform,
|
||||
};
|
||||
|
||||
use Espo\Core\{
|
||||
@@ -54,38 +56,37 @@ use Throwable;
|
||||
|
||||
class Schema
|
||||
{
|
||||
private $config;
|
||||
private Config $config;
|
||||
|
||||
private $metadata;
|
||||
private Metadata $metadata;
|
||||
|
||||
private $fileManager;
|
||||
private FileManager $fileManager;
|
||||
|
||||
private $entityManager;
|
||||
private EntityManager $entityManager;
|
||||
|
||||
private $classMap;
|
||||
private ClassMap $classMap;
|
||||
|
||||
private $comparator;
|
||||
private Comparator $comparator;
|
||||
|
||||
private $converter;
|
||||
private DatabaseConverter $converter;
|
||||
|
||||
private $databaseHelper;
|
||||
private Helper $databaseHelper;
|
||||
|
||||
protected $ormMetadataData;
|
||||
protected OrmMetadataData $ormMetadataData;
|
||||
|
||||
private $log;
|
||||
private Log $log;
|
||||
|
||||
private $fieldTypePath = 'application/Espo/Core/Utils/Database/DBAL/FieldTypes';
|
||||
private string $fieldTypePath = 'application/Espo/Core/Utils/Database/DBAL/FieldTypes';
|
||||
|
||||
private $rebuildActionsPath = 'Core/Utils/Database/Schema/rebuildActions';
|
||||
private string $rebuildActionsPath = 'Core/Utils/Database/Schema/rebuildActions';
|
||||
|
||||
private $schemaConverter;
|
||||
private Converter $schemaConverter;
|
||||
|
||||
/**
|
||||
* Array of rebuildActions classes in format:
|
||||
* [
|
||||
* 'beforeRebuild' => [...],
|
||||
* 'afterRebuild' => [...],
|
||||
* ]
|
||||
* @var ?array{
|
||||
* beforeRebuild: \Espo\Core\Utils\Database\Schema\BaseRebuildActions[],
|
||||
* afterRebuild: \Espo\Core\Utils\Database\Schema\BaseRebuildActions[],
|
||||
* }
|
||||
*/
|
||||
protected $rebuildActionClasses = null;
|
||||
|
||||
@@ -107,7 +108,6 @@ class Schema
|
||||
$this->log = $log;
|
||||
|
||||
$this->databaseHelper = new Helper($this->config);
|
||||
|
||||
$this->comparator = new Comparator();
|
||||
|
||||
$this->initFieldTypes();
|
||||
@@ -126,54 +126,24 @@ class Schema
|
||||
$this->ormMetadataData = $ormMetadataData;
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
protected function getMetadata()
|
||||
{
|
||||
return $this->metadata;
|
||||
}
|
||||
|
||||
protected function getFileManager()
|
||||
{
|
||||
return $this->fileManager;
|
||||
}
|
||||
|
||||
protected function getEntityManager()
|
||||
{
|
||||
return $this->entityManager;
|
||||
}
|
||||
|
||||
protected function getComparator()
|
||||
{
|
||||
return $this->comparator;
|
||||
}
|
||||
|
||||
protected function getConverter()
|
||||
{
|
||||
return $this->converter;
|
||||
}
|
||||
|
||||
public function getPlatform()
|
||||
{
|
||||
return $this->getConnection()->getDatabasePlatform();
|
||||
}
|
||||
|
||||
public function getDatabaseHelper()
|
||||
public function getDatabaseHelper(): Helper
|
||||
{
|
||||
return $this->databaseHelper;
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
public function getPlatform(): AbstractPlatform
|
||||
{
|
||||
return $this->getConnection()->getDatabasePlatform();
|
||||
}
|
||||
|
||||
public function getConnection(): Connection
|
||||
{
|
||||
return $this->getDatabaseHelper()->getDbalConnection();
|
||||
}
|
||||
|
||||
protected function initFieldTypes()
|
||||
protected function initFieldTypes(): void
|
||||
{
|
||||
$typeList = $this->getFileManager()->getFileList($this->fieldTypePath, false, '\.php$');
|
||||
$typeList = $this->fileManager->getFileList($this->fieldTypePath, false, '\.php$');
|
||||
|
||||
foreach ($typeList as $name) {
|
||||
$typeName = preg_replace('/Type\.php$/i', '', $name);
|
||||
@@ -197,12 +167,14 @@ class Schema
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Rebuild database schema.
|
||||
*
|
||||
* @param ?string[] $entityList
|
||||
*/
|
||||
public function rebuild(?array $entityList = null) : bool
|
||||
public function rebuild(?array $entityList = null): bool
|
||||
{
|
||||
if (!$this->getConverter()->process()) {
|
||||
if (!$this->converter->process()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -251,34 +223,34 @@ class Schema
|
||||
return (bool) $result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get current database schema.
|
||||
*
|
||||
* @return \Doctrine\DBAL\Schema\Schema
|
||||
*/
|
||||
protected function getCurrentSchema()
|
||||
/**
|
||||
* Get current database schema.
|
||||
*/
|
||||
protected function getCurrentSchema(): DBALSchema
|
||||
{
|
||||
return $this->getConnection()->getSchemaManager()->createSchema();
|
||||
return $this->getConnection()
|
||||
->getSchemaManager()
|
||||
->createSchema();
|
||||
}
|
||||
|
||||
/*
|
||||
* Get SQL queries of database schema.
|
||||
*
|
||||
* @return array - array of SQL queries
|
||||
*/
|
||||
/**
|
||||
* Get SQL queries of database schema.
|
||||
*
|
||||
* @return string[] Array of SQL queries.
|
||||
*/
|
||||
public function toSql(DBALSchemaDiff $schema)
|
||||
{
|
||||
return $schema->toSaveSql($this->getPlatform());
|
||||
}
|
||||
|
||||
/*
|
||||
* Get SQL queries to get from one to another schema.
|
||||
*
|
||||
* @return array - array of SQL queries.
|
||||
*/
|
||||
/**
|
||||
* Get SQL queries to get from one to another schema.
|
||||
*
|
||||
* @return string[] Array of SQL queries.
|
||||
*/
|
||||
public function getDiffSql(DBALSchema $fromSchema, DBALSchema $toSchema)
|
||||
{
|
||||
$schemaDiff = $this->getComparator()->compare($fromSchema, $toSchema);
|
||||
$schemaDiff = $this->comparator->compare($fromSchema, $toSchema);
|
||||
|
||||
return $this->toSql($schemaDiff);
|
||||
}
|
||||
@@ -286,13 +258,14 @@ class Schema
|
||||
/**
|
||||
* Init Rebuild Actions, get all classes and create them.
|
||||
*/
|
||||
protected function initRebuildActions($currentSchema = null, $metadataSchema = null)
|
||||
protected function initRebuildActions(?DBALSchema $currentSchema = null, ?DBALSchema $metadataSchema = null): void
|
||||
{
|
||||
$methods = [
|
||||
'beforeRebuild',
|
||||
'afterRebuild',
|
||||
];
|
||||
|
||||
/** @var array<string,class-string<\Espo\Core\Utils\Database\Schema\BaseRebuildActions>> */
|
||||
$rebuildActions = $this->classMap->getData($this->rebuildActionsPath, null, $methods);
|
||||
|
||||
$classes = [];
|
||||
@@ -326,18 +299,16 @@ class Schema
|
||||
/**
|
||||
* Execute actions for RebuildAction classes.
|
||||
*
|
||||
* @param string $action action name, possible values 'beforeRebuild' | 'afterRebuild'.
|
||||
* @param string $action An action name, 'beforeRebuild' or 'afterRebuild'.
|
||||
*/
|
||||
protected function executeRebuildActions($action = 'beforeRebuild')
|
||||
protected function executeRebuildActions(string $action = 'beforeRebuild'): void
|
||||
{
|
||||
if (!isset($this->rebuildActionClasses)) {
|
||||
$this->initRebuildActions();
|
||||
}
|
||||
|
||||
if (isset($this->rebuildActionClasses[$action])) {
|
||||
foreach ($this->rebuildActionClasses[$action] as $rebuildActionClass) {
|
||||
$rebuildActionClass->$action();
|
||||
}
|
||||
foreach ($this->rebuildActionClasses[$action] as $rebuildActionClass) {
|
||||
$rebuildActionClass->$action();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ use Espo\Core\{
|
||||
|
||||
class SchemaProxy
|
||||
{
|
||||
private $container;
|
||||
private Container $container;
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
@@ -48,12 +48,15 @@ class SchemaProxy
|
||||
return $this->container->get('schema');
|
||||
}
|
||||
|
||||
public function rebuild(?array $entityList = null) : bool
|
||||
/**
|
||||
* @param ?string[] $entityList
|
||||
*/
|
||||
public function rebuild(?array $entityList = null): bool
|
||||
{
|
||||
return $this->getSchema()->rebuild();
|
||||
}
|
||||
|
||||
public function getDatabaseHelper() : Helper
|
||||
public function getDatabaseHelper(): Helper
|
||||
{
|
||||
return $this->getSchema()->getDatabaseHelper();
|
||||
}
|
||||
|
||||
@@ -33,6 +33,11 @@ use Espo\Core\Utils\Util;
|
||||
|
||||
class Utils
|
||||
{
|
||||
/**
|
||||
* @param array<string,mixed> $ormMeta
|
||||
* @param string[] $ignoreFlags
|
||||
* @return array<string,array<string,mixed>>
|
||||
*/
|
||||
public static function getIndexes(array $ormMeta, array $ignoreFlags = [])
|
||||
{
|
||||
$indexList = [];
|
||||
@@ -87,6 +92,10 @@ class Utils
|
||||
return $indexList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $fieldDefs
|
||||
* @return ?string
|
||||
*/
|
||||
public static function getIndexTypeByFieldDefs(array $fieldDefs)
|
||||
{
|
||||
if ($fieldDefs['type'] != 'id' && isset($fieldDefs['unique']) && $fieldDefs['unique']) {
|
||||
@@ -96,8 +105,16 @@ class Utils
|
||||
if (isset($fieldDefs['index']) && $fieldDefs['index']) {
|
||||
return 'index';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fieldName
|
||||
* @param array<string,mixed> $fieldDefs
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getIndexNameByFieldDefs($fieldName, array $fieldDefs, $default = null)
|
||||
{
|
||||
$indexType = static::getIndexTypeByFieldDefs($fieldDefs);
|
||||
@@ -117,6 +134,11 @@ class Utils
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $fieldsDefs
|
||||
* @param bool $isTableColumnNames
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public static function getEntityIndexListByFieldsDefs(array $fieldsDefs, $isTableColumnNames = false)
|
||||
{
|
||||
$indexList = [];
|
||||
@@ -150,6 +172,10 @@ class Utils
|
||||
return $indexList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $indexDefs
|
||||
* @return string
|
||||
*/
|
||||
public static function getIndexTypeByIndexDefs(array $indexDefs)
|
||||
{
|
||||
if (
|
||||
@@ -166,6 +192,12 @@ class Utils
|
||||
return 'index';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $type
|
||||
* @param int $maxLength
|
||||
* @return string
|
||||
*/
|
||||
public static function generateIndexName($name, $type = 'index', $maxLength = 60)
|
||||
{
|
||||
switch ($type) {
|
||||
@@ -185,6 +217,14 @@ class Utils
|
||||
return substr(implode('_', $nameList), 0, $maxLength);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array<string,mixed> $ormMeta
|
||||
* @param int $indexMaxLength
|
||||
* @param ?array<string,mixed> $indexList
|
||||
* @param int $characterLength
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public static function getFieldListExceededIndexMaxLength(
|
||||
array $ormMeta,
|
||||
$indexMaxLength = 1000,
|
||||
@@ -244,6 +284,11 @@ class Utils
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $ormFieldDefs
|
||||
* @param int $characterLength
|
||||
* @return int
|
||||
*/
|
||||
protected static function getFieldLength(array $ormFieldDefs, $characterLength = 4)
|
||||
{
|
||||
$length = 0;
|
||||
@@ -264,7 +309,7 @@ class Utils
|
||||
$type = static::getDbFieldType($ormFieldDefs);
|
||||
|
||||
$length = isset($defaultLength[$type]) ? $defaultLength[$type] : $length;
|
||||
$length = isset($ormFieldDefs['len']) ? $ormFieldDefs['len'] : $length;
|
||||
//$length = isset($ormFieldDefs['len']) ? $ormFieldDefs['len'] : $length;
|
||||
|
||||
switch ($type) {
|
||||
case 'varchar':
|
||||
@@ -275,11 +320,19 @@ class Utils
|
||||
return $length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $ormFieldDefs
|
||||
* @return string
|
||||
*/
|
||||
protected static function getDbFieldType(array $ormFieldDefs)
|
||||
{
|
||||
return isset($ormFieldDefs['dbType']) ? $ormFieldDefs['dbType'] : $ormFieldDefs['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $ormFieldDefs
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFieldType(array $ormFieldDefs)
|
||||
{
|
||||
return isset($ormFieldDefs['type']) ? $ormFieldDefs['type'] : static::getDbFieldType($ormFieldDefs);
|
||||
|
||||
@@ -33,6 +33,9 @@ use Espo\Core\Utils\Database\Schema\BaseRebuildActions as Base;
|
||||
|
||||
class AddSystemUser extends Base
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function afterRebuild()
|
||||
{
|
||||
$userId = $this->getConfig()->get('systemUserAttributes.id');
|
||||
|
||||
@@ -33,6 +33,9 @@ use Espo\Core\Utils\Currency\DatabasePopulator;
|
||||
|
||||
class Currency extends \Espo\Core\Utils\Database\Schema\BaseRebuildActions
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function afterRebuild()
|
||||
{
|
||||
$populator = new DatabasePopulator($this->getConfig(), $this->getEntityManager());
|
||||
|
||||
@@ -36,6 +36,9 @@ use Exception;
|
||||
|
||||
class FulltextIndex extends BaseRebuildActions
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function beforeRebuild()
|
||||
{
|
||||
$currentSchema = $this->getCurrentSchema();
|
||||
|
||||
@@ -75,7 +75,7 @@ class ClassMap
|
||||
* Return paths to class files.
|
||||
*
|
||||
* @param ?string[] $allowedMethods If specified, classes w/o specified method will be ignored.
|
||||
* @return array<string,string>
|
||||
* @return array<string,class-string>
|
||||
*/
|
||||
public function getData(
|
||||
string $path,
|
||||
@@ -138,7 +138,7 @@ class ClassMap
|
||||
/**
|
||||
* @param string[]|string $dirs
|
||||
* @param ?string[] $allowedMethods
|
||||
* @return array<string,string>
|
||||
* @return array<string,class-string>
|
||||
*/
|
||||
private function getClassNameHash($dirs, ?array $allowedMethods = [], bool $subDirs = false): array
|
||||
{
|
||||
@@ -162,7 +162,7 @@ class ClassMap
|
||||
/**
|
||||
* @param string[] $fileList
|
||||
* @param ?string[] $allowedMethods
|
||||
* @param array<string,string> $data
|
||||
* @param array<string,class-string> $data
|
||||
*/
|
||||
private function fillHashFromFileList(
|
||||
array $fileList,
|
||||
|
||||
Reference in New Issue
Block a user