From 6acba3efb8cbbd1e4f87ab61a723287daf1eeeef Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 1 Oct 2019 17:17:39 +0300 Subject: [PATCH] Fixes for 'indexes' attribute for manyMany relationships --- .../Core/Utils/Database/DBAL/Schema/Index.php | 4 ++- .../Core/Utils/Database/DBAL/Schema/Table.php | 23 +++++++++++++ .../Utils/Database/Orm/Relations/Base.php | 3 +- .../Core/Utils/Database/Schema/Converter.php | 32 +++++++++---------- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/application/Espo/Core/Utils/Database/DBAL/Schema/Index.php b/application/Espo/Core/Utils/Database/DBAL/Schema/Index.php index ba97e470c1..b0356fd521 100644 --- a/application/Espo/Core/Utils/Database/DBAL/Schema/Index.php +++ b/application/Espo/Core/Utils/Database/DBAL/Schema/Index.php @@ -63,7 +63,7 @@ class Index extends \Doctrine\DBAL\Schema\Index $flags = $this->getFlags(); $otherFlags = $other->getFlags(); - if ( ! $this->isUnique() && !$this->isPrimary() && $flags === $otherFlags) { + if ( ! $this->isUnique() && !$this->isPrimary() /*espo*/ && $flags === $otherFlags /*espo*/) { return true; } else if ($other->isPrimary() != $this->isPrimary()) { return false; @@ -71,9 +71,11 @@ class Index extends \Doctrine\DBAL\Schema\Index return false; } + /*espo*/ if (count($flags) != count($otherFlags) || array_diff($flags, $otherFlags) !== array_diff($otherFlags, $flags)) { return false; } + /*espo*/ return true; } diff --git a/application/Espo/Core/Utils/Database/DBAL/Schema/Table.php b/application/Espo/Core/Utils/Database/DBAL/Schema/Table.php index b4f1ebe2af..5dbea078bb 100644 --- a/application/Espo/Core/Utils/Database/DBAL/Schema/Table.php +++ b/application/Espo/Core/Utils/Database/DBAL/Schema/Table.php @@ -52,6 +52,18 @@ class Table extends \Doctrine\DBAL\Schema\Table return $column; } + public function setPrimaryKey(array $columns, $indexName = false) + { + $primaryKey = $this->_createIndex($columns, $indexName ?: "primary", true, true); + + foreach ($columns as $columnName) { + $column = $this->getColumn($columnName); + $column->setNotnull(true); + } + + return $primaryKey; + } + public function addIndex(array $columnNames, $indexName = null, array $flags = array()) { if($indexName == null) { @@ -63,6 +75,17 @@ class Table extends \Doctrine\DBAL\Schema\Table return $this->_createIndex($columnNames, $indexName, false, false, $flags); } + public function addUniqueIndex(array $columnNames, $indexName = null) + { + if ($indexName === null) { + $indexName = $this->_generateIdentifierName( + array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength() + ); + } + + return $this->_createIndex($columnNames, $indexName, true, false); + } + private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array()) { if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) { diff --git a/application/Espo/Core/Utils/Database/Orm/Relations/Base.php b/application/Espo/Core/Utils/Database/Orm/Relations/Base.php index 9b1b123abd..4474684b20 100644 --- a/application/Espo/Core/Utils/Database/Orm/Relations/Base.php +++ b/application/Espo/Core/Utils/Database/Orm/Relations/Base.php @@ -45,7 +45,8 @@ class Base extends \Espo\Core\Utils\Database\Orm\Base 'conditions', 'additionalColumns', 'midKeys', - 'noJoin' + 'noJoin', + 'indexes' ); protected function getParams() diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index d457357939..d304464ef3 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -325,18 +325,12 @@ class Converter } } //END: add additionalColumns - //add defined indexes - if (!empty($relationParams['indexes'])) { - $normalizedIndexes = SchemaUtils::getIndexList([ - $entityName => [ - 'fields' => [], - 'indexes' => $relationParams['indexes'], - ] - ]); + $table->addColumn('deleted', 'bool', $this->getDbFieldParams(array( + 'type' => 'bool', + 'default' => false, + ))); - $this->addIndexes($table, $normalizedIndexes[$entityName]); - } - //END: add defined indexes + $table->setPrimaryKey(array("id")); //add unique indexes if (!empty($relationParams['conditions'])) { @@ -350,12 +344,18 @@ class Converter } //END: add unique indexes - $table->addColumn('deleted', 'bool', $this->getDbFieldParams(array( - 'type' => 'bool', - 'default' => false, - ))); + //add defined indexes + if (!empty($relationParams['indexes'])) { + $normalizedIndexes = SchemaUtils::getIndexList([ + $entityName => [ + 'fields' => [], + 'indexes' => $relationParams['indexes'], + ] + ]); - $table->setPrimaryKey(array("id")); + $this->addIndexes($table, $normalizedIndexes[$entityName]); + } + //END: add defined indexes return $table; }