Commit 9ad13c47 authored by guilhermeblanco's avatar guilhermeblanco

[2.0][DDC-236] Enhanced unique constraints to support names. Fixed general...

[2.0][DDC-236] Enhanced unique constraints to support names. Fixed general issues on XML and YAML exporters. Fixed issues on XML, YAML, Doctrine 1.X and Annotation drivers.
parent d8a1c5c5
......@@ -617,8 +617,8 @@ abstract class AbstractPlatform
$columnListSql = $this->getColumnDeclarationListSql($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $uniqueConstraint) {
$columnListSql .= ', UNIQUE(' . implode(', ', array_values($uniqueConstraint)) . ')';
foreach ($options['uniqueConstraints'] as $name => $definition) {
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSql($name, $definition);
}
}
......@@ -1035,32 +1035,50 @@ abstract class AbstractPlatform
return implode(', ', $constraints);
}
/**
* Obtain DBMS specific SQL code portion needed to set a unique
* constraint declaration to be used in statements like CREATE TABLE.
*
* @param string $name name of the unique constraint
* @param Index $index index definition
* @return string DBMS specific SQL code portion needed
* to set a constraint
*/
public function getUniqueConstraintDeclarationSql($name, Index $index)
{
if (count($index->getColumns()) == 0) {
throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
return 'CONSTRAINT' . $name . ' UNIQUE ('
. $this->getIndexFieldDeclarationListSql($index->getColumns())
. ')';
}
/**
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
*
* @param string $name name of the index
* @param Index $index index definition
* @param Index $index index definition
* @return string DBMS specific SQL code portion needed to set an index
*/
public function getIndexDeclarationSql($name, Index $index)
{
$type = '';
$type = '';
if($index->isUnique()) {
$type = "UNIQUE";
$type = 'UNIQUE ';
}
if (count($index->getColumns()) == 0) {
throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
$query = $type . ' INDEX ' . $name;
$query .= ' (' . $this->getIndexFieldDeclarationListSql($index->getColumns()) . ')';
return $query;
return $type . 'INDEX ' . $name . ' ('
. $this->getIndexFieldDeclarationListSql($index->getColumns())
. ')';
}
/**
......
......@@ -446,8 +446,8 @@ class MySqlPlatform extends AbstractPlatform
$queryFields = $this->getColumnDeclarationListSql($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $uniqueConstraint) {
$queryFields .= ', UNIQUE(' . implode(', ', array_values($uniqueConstraint)) . ')';
foreach ($options['uniqueConstraints'] as $index => $definition) {
$queryFields .= ', ' . $this->getUniqueIndexDeclarationSql($index, $definition);
}
}
......
......@@ -146,9 +146,9 @@ class Table extends AbstractAsset
* @param string $indexName
* @return Table
*/
public function setPrimaryKey(array $columns, $indexName=false)
public function setPrimaryKey(array $columns, $indexName = false)
{
return $this->_createIndex($columns, $indexName?:"primary", true, true);
return $this->_createIndex($columns, $indexName ?: "primary", true, true);
}
/**
......@@ -166,7 +166,7 @@ class Table extends AbstractAsset
* @param string $indexName
* @return Table
*/
public function addIndex(array $columnNames, $indexName=null)
public function addIndex(array $columnNames, $indexName = null)
{
if($indexName == null) {
$indexName = $this->_generateIdentifierName(
......@@ -183,7 +183,7 @@ class Table extends AbstractAsset
* @param string $indexName
* @return Table
*/
public function addUniqueIndex(array $columnNames, $indexName=null)
public function addUniqueIndex(array $columnNames, $indexName = null)
{
if ($indexName == null) {
$indexName = $this->_generateIdentifierName(
......
......@@ -92,13 +92,17 @@ class AnnotationDriver implements Driver
if ($tableAnnot->indexes !== null) {
foreach ($tableAnnot->indexes as $indexAnnot) {
$primaryTable['indexes'][$indexAnnot->name] = array('columns' => $indexAnnot->columns);
$primaryTable['indexes'][$indexAnnot->name] = array(
'columns' => $indexAnnot->columns
);
}
}
if ($tableAnnot->uniqueConstraints !== null) {
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) {
$primaryTable['uniqueConstraints'][] = $uniqueConstraint->columns;
$primaryTable['uniqueConstraints'][$uniqueConstraint->name] = array(
'columns' => $uniqueConstraint->columns
);
}
}
......
......@@ -101,6 +101,7 @@ class XmlDriver extends AbstractFileDriver
} else {
$columns = $index['columns'];
}
$metadata->primaryTable['indexes'][$index['name']] = array(
'columns' => $columns
);
......@@ -115,7 +116,10 @@ class XmlDriver extends AbstractFileDriver
} else {
$columns = $unique['columns'];
}
$metadata->primaryTable['uniqueConstraints'][] = $columns;
$metadata->primaryTable['uniqueConstraints'][$unique['name']] = array(
'columns' => $columns
);
}
}
......
......@@ -101,11 +101,13 @@ class YamlDriver extends AbstractFileDriver
if ( ! isset($index['name'])) {
$index['name'] = $name;
}
if (is_string($index['columns'])) {
$columns = explode(',', $index['columns']);
} else {
$columns = $index['columns'];
}
$metadata->primaryTable['indexes'][$index['name']] = array(
'columns' => $columns
);
......@@ -114,13 +116,20 @@ class YamlDriver extends AbstractFileDriver
// Evaluate uniqueConstraints
if (isset($element['uniqueConstraints'])) {
foreach ($element['uniqueConstraints'] as $unique) {
if (is_string($index['columns'])) {
foreach ($element['uniqueConstraints'] as $name => $unique) {
if ( ! isset($unique['name'])) {
$unique['name'] = $name;
}
if (is_string($unique['columns'])) {
$columns = explode(',', $unique['columns']);
} else {
$columns = $unique['columns'];
}
$metadata->primaryTable['uniqueConstraints'][] = $columns;
$metadata->primaryTable['uniqueConstraints'][$unique['name']] = array(
'columns' => $columns
);
}
}
......
......@@ -213,13 +213,12 @@ class ConvertDoctrine1Schema
{
if (isset($model['indexes']) && $model['indexes']) {
foreach ($model['indexes'] as $name => $index) {
$metadata->primaryTable['indexes'][$name] = array(
$type = (isset($index['type']) && $index['type'] == 'unique')
? 'uniqueConstraints' : 'indexes';
$metadata->primaryTable[$type][$name] = array(
'columns' => $index['fields']
);
if (isset($index['type']) && $index['type'] == 'unique') {
$metadata->primaryTable['uniqueConstraints'][] = $index['fields'];
}
}
}
}
......
......@@ -99,8 +99,9 @@ class XmlExporter extends AbstractExporter
if (isset($metadata->primaryTable['indexes'])) {
$indexesXml = $root->addChild('indexes');
foreach ($metadata->primaryTable['indexes'] as $name => $index) {
$indexXml = $root->addChild('index');
$indexXml = $indexesXml->addChild('index');
$indexXml->addAttribute('name', $name);
$indexXml->addAttribute('columns', implode(',', $index['columns']));
}
......@@ -108,9 +109,11 @@ class XmlExporter extends AbstractExporter
if (isset($metadata->primaryTable['uniqueConstraints'])) {
$uniqueConstraintsXml = $root->addChild('unique-constraints');
foreach ($metadata->primaryTable['uniqueConstraints'] as $uniqueConstraint) {
foreach ($metadata->primaryTable['uniqueConstraints'] as $unique) {
$uniqueConstraintXml = $uniqueConstraintsXml->addChild('unique-constraint');
$uniqueConstraintXml->addAttribute('columns', $uniqueConstraint['columns']);
$uniqueConstraintXml->addAttribute('name', $name);
$uniqueConstraintXml->addAttribute('columns', implode(',', $unique['columns']));
}
}
......
......@@ -92,9 +92,7 @@ class YamlExporter extends AbstractExporter
}
if (isset($metadata->primaryTable['uniqueConstraints'])) {
foreach ($metadata->primaryTable['uniqueConstraints'] as $uniqueConstraint) {
$array['uniqueConstraints'][]['columns'] = $uniqueConstraint;
}
$array['uniqueConstraints'] = $metadata->primaryTable['uniqueConstraints'];
}
$fieldMappings = $metadata->fieldMappings;
......
......@@ -201,7 +201,7 @@ class SchemaTool
if (isset($class->primaryTable['uniqueConstraints'])) {
foreach ($class->primaryTable['uniqueConstraints'] AS $indexName => $indexData) {
$table->addUniqueIndex($indexData, $indexName);
$table->addUniqueIndex($indexData['columns'], $indexName);
}
}
......@@ -354,8 +354,7 @@ class SchemaTool
$theJoinTable = $schema->createTable($mapping->getQuotedJoinTableName($this->_platform));
$primaryKeyColumns = array();
$uniqueConstraints = array();
$primaryKeyColumns = $uniqueConstraints = array();
// Build first FK constraint (relation table => source table)
$this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints);
......@@ -363,8 +362,10 @@ class SchemaTool
// Build second FK constraint (relation table => target table)
$this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
foreach($uniqueConstraints AS $unique) {
$theJoinTable->addUniqueIndex($unique);
foreach($uniqueConstraints AS $indexName => $unique) {
$theJoinTable->addUniqueIndex(
$unique['columns'], is_numeric($indexName) ? null : $indexName
);
}
$theJoinTable->setPrimaryKey($primaryKeyColumns);
......@@ -413,7 +414,7 @@ class SchemaTool
}
if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
$uniqueConstraints[] = array($columnName);
$uniqueConstraints[] = array('columns' => $columnName);
}
if (isset($joinColumn['onUpdate'])) {
......
......@@ -65,7 +65,7 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName);
$this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName);
$this->assertEquals('username', $metadatas['User']->primaryTable['indexes']['username']['columns'][0]);
$this->assertEquals('username', $metadatas['User']->primaryTable['uniqueConstraints']['username']['columns'][0]);
unlink(__DIR__ . '/convert/User.dcm.yml');
unlink(__DIR__ . '/convert/Profile.dcm.yml');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment