From fade63a29c981fb6ee938f00d257d565c8c4bd8d Mon Sep 17 00:00:00 2001 From: beberlei Date: Thu, 10 Dec 2009 23:55:47 +0000 Subject: [PATCH] [2.0] DDC-169 - Further refactorings, schema max identifier length is now used for asset generation. Added platform-wide test for unique index generation with create table. --- .../DBAL/Schema/AbstractSchemaManager.php | 16 +++- lib/Doctrine/DBAL/Schema/Schema.php | 16 ++-- lib/Doctrine/DBAL/Schema/SchemaConfig.php | 76 +++++++++++++++++++ lib/Doctrine/DBAL/Schema/Table.php | 33 +++++++- .../ORM/Mapping/Driver/DatabaseDriver.php | 2 +- lib/Doctrine/ORM/Tools/SchemaTool.php | 3 +- .../SchemaManagerFunctionalTestCase.php | 1 + .../Platforms/AbstractPlatformTestCase.php | 13 ++++ .../DBAL/Platforms/MsSqlPlatformTest.php | 7 ++ .../DBAL/Platforms/MySqlPlatformTest.php | 7 ++ .../DBAL/Platforms/OraclePlatformTest.php | 8 ++ .../DBAL/Platforms/PostgreSqlPlatformTest.php | 8 ++ .../DBAL/Platforms/SqlitePlatformTest.php | 8 ++ .../Tests/DBAL/Schema/ComparatorTest.php | 49 ++++-------- .../Doctrine/Tests/DBAL/Schema/SchemaTest.php | 25 ++++++ 15 files changed, 226 insertions(+), 46 deletions(-) create mode 100644 lib/Doctrine/DBAL/Schema/SchemaConfig.php diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 4528d6c1a..b91b23b83 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -936,6 +936,20 @@ abstract class AbstractSchemaManager } $tables = $this->listTables(); - return new Schema($tables, $sequences, $this->_platform->createsExplicitIndexForForeignKeys()); + return new Schema($tables, $sequences, $this->createSchemaConfig()); + } + + /** + * Create the configuration for this schema. + * + * @return SchemaConfig + */ + public function createSchemaConfig() + { + $schemaConfig = new SchemaConfig(); + $schemaConfig->setExplicitForeignKeyIndexes($this->_platform->createsExplicitIndexForForeignKeys()); + $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength()); + + return $schemaConfig; } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Schema/Schema.php b/lib/Doctrine/DBAL/Schema/Schema.php index b058c13e5..dea20ec30 100644 --- a/lib/Doctrine/DBAL/Schema/Schema.php +++ b/lib/Doctrine/DBAL/Schema/Schema.php @@ -47,23 +47,28 @@ class Schema extends AbstractAsset protected $_sequences = array(); /** - * @var bool + * @var SchemaConfig */ - protected $_hasExplicitForeignKeyIndexes = false; + protected $_schemaConfig = false; /** * @param array $tables * @param array $sequences + * @param SchemaConfig $schemaConfig */ - public function __construct(array $tables=array(), array $sequences=array(), $hasExplicitForeignKeyIndexes=false) + public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null) { + if ($schemaConfig == null) { + $schemaConfig = new SchemaConfig(); + } + $this->_schemaConfig = $schemaConfig; + foreach ($tables AS $table) { $this->_addTable($table); } foreach ($sequences AS $sequence) { $this->_addSequence($sequence); } - $this->_hasExplicitForeignKeyIndexes = $hasExplicitForeignKeyIndexes; } /** @@ -71,7 +76,7 @@ class Schema extends AbstractAsset */ public function hasExplicitForeignKeyIndexes() { - return $this->_hasExplicitForeignKeyIndexes; + return $this->_schemaConfig->hasExplicitForeignKeyIndexes(); } /** @@ -85,6 +90,7 @@ class Schema extends AbstractAsset } $this->_tables[$tableName] = $table; + $table->setSchemaConfig($this->_schemaConfig); } /** diff --git a/lib/Doctrine/DBAL/Schema/SchemaConfig.php b/lib/Doctrine/DBAL/Schema/SchemaConfig.php new file mode 100644 index 000000000..291babb4a --- /dev/null +++ b/lib/Doctrine/DBAL/Schema/SchemaConfig.php @@ -0,0 +1,76 @@ +. + */ + +namespace Doctrine\DBAL\Schema; + +/** + * Configuration for a Schema + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Benjamin Eberlei + */ +class SchemaConfig +{ + /** + * @var bool + */ + protected $_hasExplicitForeignKeyIndexes = false; + + /** + * @var int + */ + protected $_maxIdentifierLength = 63; + + /** + * @return bool + */ + public function hasExplicitForeignKeyIndexes() + { + return $this->_hasExplicitForeignKeyIndexes; + } + + /** + * @param bool $flag + */ + public function setExplicitForeignKeyIndexes($flag) + { + $this->_hasExplicitForeignKeyIndexes = (bool)$flag; + } + + /** + * @param int $length + */ + public function setMaxIdentifierLength($length) + { + $this->_maxIdentifierLength = (int)$length; + } + + /** + * @return int + */ + public function getMaxIdentifierLength() + { + return $this->_maxIdentifierLength; + } +} \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php index 1fe81fd20..b71867814 100644 --- a/lib/Doctrine/DBAL/Schema/Table.php +++ b/lib/Doctrine/DBAL/Schema/Table.php @@ -85,6 +85,11 @@ class Table extends AbstractAsset */ protected $_idGeneratorType = self::ID_NONE; + /** + * @var SchemaConfig + */ + protected $_schemaConfig = null; + /** * * @param string $tableName @@ -114,6 +119,26 @@ class Table extends AbstractAsset $this->_options = $options; } + /** + * @param SchemaConfig $schemaConfig + */ + public function setSchemaConfig(SchemaConfig $schemaConfig) + { + $this->_schemaConfig = $schemaConfig; + } + + /** + * @return int + */ + protected function _getMaxIdentifierLength() + { + if ($this->_schemaConfig instanceof SchemaConfig) { + return $this->_schemaConfig->getMaxIdentifierLength(); + } else { + return 63; + } + } + /** * Set Primary Key * @@ -145,7 +170,7 @@ class Table extends AbstractAsset { if($indexName == null) { $indexName = $this->_generateIdentifierName( - array_merge(array($this->getName()), $columnNames), "idx" + array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength() ); } @@ -162,7 +187,7 @@ class Table extends AbstractAsset { if ($indexName == null) { $indexName = $this->_generateIdentifierName( - array_merge(array($this->getName()), $columnNames), "uniq" + array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength() ); } @@ -268,7 +293,7 @@ class Table extends AbstractAsset */ public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array()) { - $name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk"); + $name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength()); return $this->addNamedForeignKeyConstraint($name, $foreignTable, $localColumnNames, $foreignColumnNames, $options); } @@ -394,7 +419,7 @@ class Table extends AbstractAsset $name = $constraint->getName(); } else { $name = $this->_generateIdentifierName( - array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk" + array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength() ); } $name = strtolower($name); diff --git a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php index 0e579df63..1a77e0798 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php @@ -88,7 +88,7 @@ class DatabaseDriver implements Driver } $fieldMapping = array(); - if (in_array($column->getName(), $indexes['primary']->getColumns())) { + if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) { $fieldMapping['id'] = true; } diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 6889659e0..ea9421b5e 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -109,7 +109,8 @@ class SchemaTool { $processedClasses = array(); // Reminder for processed classes, used for hierarchies - $schema = new \Doctrine\DBAL\Schema\Schema(); + $sm = $this->_em->getConnection()->getSchemaManager(); + $schema = new \Doctrine\DBAL\Schema\Schema(array(), array(), $sm->createSchemaConfig()); foreach ($classes as $class) { if (isset($processedClasses[$class->name]) || $class->isMappedSuperclass) { diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php index b0c06ce9b..c3715f2a5 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -390,6 +390,7 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest protected function getTestTable($name, $options=array()) { $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), \Doctrine\DBAL\Schema\Table::ID_NONE, $options); + $table->setSchemaConfig($this->_sm->createSchemaConfig()); $table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY); $table->createColumn('id', 'integer', array('notnull' => true)); $table->setPrimaryKey(array('id')); diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php index 3d2fe593c..9d5ae6079 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php @@ -27,6 +27,19 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase abstract public function getGenerateTableSql(); + public function testGenerateTableWithMultiColumnUniqueIndex() + { + $table = new \Doctrine\DBAL\Schema\Table('test'); + $table->createColumn('foo', 'string', array('notnull' => false, 'length' => 255)); + $table->createColumn('bar', 'string', array('notnull' => false, 'length' => 255)); + $table->addUniqueIndex(array("foo", "bar")); + + $sql = $this->_platform->getCreateTableSql($table); + $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql); + } + + abstract public function getGenerateTableWithMultiColumnUniqueIndexSql(); + public function testGeneratesIndexCreationSql() { $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login')); diff --git a/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php index 04b43f83e..a0f7a2784 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php @@ -19,6 +19,13 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'; } + public function getGenerateTableWithMultiColumnUniqueIndexSql() + { + return array( + 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX test_foo_bar_uniq (foo, bar))' + ); + } + public function getGenerateAlterTableSql() { return array( diff --git a/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php index a36a47907..d7fd1b49d 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php @@ -28,6 +28,13 @@ class MySqlPlatformTest extends AbstractPlatformTestCase return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB'; } + public function getGenerateTableWithMultiColumnUniqueIndexSql() + { + return array( + 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX test_foo_bar_uniq (foo, bar)) ENGINE = InnoDB' + ); + } + public function getGenerateAlterTableSql() { return array( diff --git a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php index f059750b5..c5714f290 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php @@ -19,6 +19,14 @@ class OraclePlatformTest extends AbstractPlatformTestCase return 'CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) DEFAULT NULL, PRIMARY KEY(id))'; } + public function getGenerateTableWithMultiColumnUniqueIndexSql() + { + return array( + 'CREATE TABLE test (foo VARCHAR2(255) DEFAULT NULL, bar VARCHAR2(255) DEFAULT NULL)', + 'CREATE UNIQUE INDEX test_foo_bar_uniq ON test (foo, bar)', + ); + } + public function getGenerateAlterTableSql() { return array( diff --git a/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php index 343bb7c41..fdcc84f63 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php @@ -20,6 +20,14 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'; } + public function getGenerateTableWithMultiColumnUniqueIndexSql() + { + return array( + 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)', + 'CREATE UNIQUE INDEX test_foo_bar_uniq ON test (foo, bar)' + ); + } + public function getGenerateAlterTableSql() { return array( diff --git a/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php index 04f52ea0d..af495ac63 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php @@ -19,6 +19,14 @@ class SqlitePlatformTest extends AbstractPlatformTestCase return 'CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL)'; } + public function getGenerateTableWithMultiColumnUniqueIndexSql() + { + return array( + 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)', + 'CREATE UNIQUE INDEX test_foo_bar_uniq ON test (foo, bar)', + ); + } + public function testGeneratesSqlSnippets() { $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct'); diff --git a/tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php b/tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php index dfde7dd5a..ba64da0cb 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php @@ -87,47 +87,28 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase public function testCompareMissingTable() { - $schema1 = new Schema( array( - 'bugdb' => new Table('bugdb', - array ( - 'integerfield1' => new Column('integerfield1', Type::getType('integer')), - ) - ), - ) ); - $schema2 = new Schema( array( - ) ); + $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig; + $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer')))); + $table->setSchemaConfig($schemaConfig); + + $schema1 = new Schema( array($table), array(), $schemaConfig ); + $schema2 = new Schema( array(), array(), $schemaConfig ); - $expected = new SchemaDiff( array(), array(), - array( - 'bugdb' => new Table('bugdb', - array ( - 'integerfield1' => new Column('integerfield1', Type::getType('integer')), - ) - ), - ) - ); + $expected = new SchemaDiff( array(), array(), array('bugdb' => $table) ); + $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); } public function testCompareNewTable() { - $schema1 = new Schema( array( - ) ); - $schema2 = new Schema( array( - 'bugdb' => new Table('bugdb', - array ( - 'integerfield1' => new Column('integerfield1', Type::getType('integer')), - ) - ), - ) ); + $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig; + $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer')))); + $table->setSchemaConfig($schemaConfig); - $expected = new SchemaDiff( array( - 'bugdb' => new Table('bugdb', - array ( - 'integerfield1' => new Column('integerfield1', Type::getType('integer')), - ) - ), - ) ); + $schema1 = new Schema( array(), array(), $schemaConfig ); + $schema2 = new Schema( array($table), array(), $schemaConfig ); + + $expected = new SchemaDiff( array('bugdb' => $table), array(), array() ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); } diff --git a/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php b/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php index a37c71f0c..f004e487e 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php @@ -187,4 +187,29 @@ class SchemaTest extends \PHPUnit_Framework_TestCase $index = current($indexes); $this->assertTrue($index->hasColumnAtPosition('foo_id', 0)); } + + public function testConfigHasExplicitForeignKeyIndex() + { + $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig(); + $schemaConfig->setExplicitForeignKeyIndexes(false); + + $schema = new Schema(array(), array(), $schemaConfig); + $this->assertFalse($schema->hasExplicitForeignKeyIndexes()); + + $schemaConfig->setExplicitForeignKeyIndexes(true); + $this->assertTrue($schema->hasExplicitForeignKeyIndexes()); + } + + public function testConfigMaxIdentifierLength() + { + $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig(); + $schemaConfig->setMaxIdentifierLength(10); + + $schema = new Schema(array(), array(), $schemaConfig); + $table = $schema->createTable("smalltable"); + $table->createColumn('long_id', 'integer'); + $table->addIndex(array('long_id')); + + $this->assertTrue($table->hasIndex('le_id_idx')); + } } \ No newline at end of file