Commit a26464da authored by beberlei's avatar beberlei

[2.0] DDC-169 - Fix Bug in SchemaTool Unique Handling

parent 3e823f06
...@@ -288,7 +288,6 @@ class SchemaTool ...@@ -288,7 +288,6 @@ class SchemaTool
$options['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true; $options['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
$options['platformOptions'] = array(); $options['platformOptions'] = array();
$options['platformOptions']['unique'] = isset($mapping['unique']) ? $mapping['unique'] : false;
$options['platformOptions']['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false; $options['platformOptions']['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
if(strtolower($columnType) == 'string' && $options['length'] === null) { if(strtolower($columnType) == 'string' && $options['length'] === null) {
...@@ -313,6 +312,11 @@ class SchemaTool ...@@ -313,6 +312,11 @@ class SchemaTool
} else { } else {
$table->createColumn($columnName, $columnType, $options); $table->createColumn($columnName, $columnType, $options);
} }
$isUnique = isset($mapping['unique']) ? $mapping['unique'] : false;
if ($isUnique) {
$table->addUniqueIndex(array($columnName));
}
} }
/** /**
......
...@@ -28,7 +28,7 @@ class MySqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -28,7 +28,7 @@ class MySqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase
$sql = $tool->getCreateSchemaSql($classes); $sql = $tool->getCreateSchemaSql($classes);
$this->assertEquals(8, count($sql)); $this->assertEquals(8, count($sql));
$this->assertEquals("CREATE TABLE cms_addresses (id INT AUTO_INCREMENT NOT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]); $this->assertEquals("CREATE TABLE cms_addresses (id INT AUTO_INCREMENT NOT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]);
$this->assertEquals("CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, status VARCHAR(50) NOT NULL, username VARCHAR(255) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[1]); $this->assertEquals("CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, status VARCHAR(50) NOT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX cms_users_username_uniq (username), PRIMARY KEY(id)) ENGINE = InnoDB", $sql[1]);
$this->assertEquals("CREATE TABLE cms_users_groups (user_id INT DEFAULT NULL, group_id INT DEFAULT NULL, PRIMARY KEY(user_id, group_id)) ENGINE = InnoDB", $sql[2]); $this->assertEquals("CREATE TABLE cms_users_groups (user_id INT DEFAULT NULL, group_id INT DEFAULT NULL, PRIMARY KEY(user_id, group_id)) ENGINE = InnoDB", $sql[2]);
$this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(phonenumber)) ENGINE = InnoDB", $sql[3]); $this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(phonenumber)) ENGINE = InnoDB", $sql[3]);
$this->assertEquals("ALTER TABLE cms_addresses ADD FOREIGN KEY (user_id) REFERENCES cms_users(id)", $sql[4]); $this->assertEquals("ALTER TABLE cms_addresses ADD FOREIGN KEY (user_id) REFERENCES cms_users(id)", $sql[4]);
......
<?php
namespace Doctrine\Tests\ORM\Tools;
use Doctrine\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../TestInit.php';
class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
{
public function testAddUniqueIndexForUniqueFieldAnnocation()
{
$em = $this->_getTestEntityManager();
$schemaTool = new SchemaTool($em);
$classes = array(
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
);
$schema = $schemaTool->getSchemaFromMetadata($classes);
$this->assertTrue($schema->hasTable('cms_users'));
$this->assertTrue($schema->getTable('cms_users')->hasIndex('cms_users_username_uniq'));
$this->assertEquals(
array('username'),
array_map('strtolower', $schema->getTable('cms_users')->getIndex('cms_users_username_uniq')->getColumns())
);
}
}
\ No newline at end of file
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