Fixes for 'indexes' attribute for manyMany relationships

This commit is contained in:
Taras Machyshyn
2019-10-01 17:17:39 +03:00
parent 87036f83e1
commit 6acba3efb8
4 changed files with 44 additions and 18 deletions
@@ -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;
}
@@ -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)) {
@@ -45,7 +45,8 @@ class Base extends \Espo\Core\Utils\Database\Orm\Base
'conditions',
'additionalColumns',
'midKeys',
'noJoin'
'noJoin',
'indexes'
);
protected function getParams()
@@ -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;
}