1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] DDC-169 - Replaced SchemaTool::getUpdateSql() with the Schema Comparator Code.

This commit is contained in:
beberlei 2009-12-06 12:23:13 +00:00
parent 8a46eb0439
commit 58e99d39d2
11 changed files with 7 additions and 1521 deletions

View File

@ -549,270 +549,15 @@ class SchemaTool
*/
public function getUpdateSchemaSql(array $classes)
{
$sql = array();
$conn = $this->_em->getConnection();
$sm = $conn->getSchemaManager();
$tables = $sm->listTables();
$newClasses = array();
foreach ($classes as $class) {
$tableName = $class->getTableName();
$tableExists = false;
foreach ($tables as $index => $table) {
if ($tableName == $table) {
$tableExists = true;
unset($tables[$index]);
break;
}
}
if ( ! $tableExists) {
$newClasses[] = $class;
} else {
$newFields = array();
$updateFields = array();
$dropIndexes = array();
$newJoinColumns = array();
$currentColumns = $sm->listTableColumns($tableName);
foreach ($class->fieldMappings as $fieldName => $fieldMapping) {
$exists = false;
foreach ($currentColumns as $index => $column) {
if ($column['name'] == $fieldMapping['columnName']) {
// Column exists, check for changes
$columnInfo = $column;
$columnChanged = false;
// 1. check for nullability change
$columnInfo['notnull'] = ( ! isset($columnInfo['notnull']))
? false : $columnInfo['notnull'];
$notnull = ! $class->isNullable($fieldName);
if ($columnInfo['notnull'] != $notnull) {
$columnInfo['notnull'] = $notnull;
$columnChanged = true;
}
unset($notnull);
// 2. check for uniqueness change
$columnInfo['unique'] = ( ! isset($columnInfo['unique']))
? false : $columnInfo['unique'];
$unique = $class->isUniqueField($fieldName);
if ($columnInfo['unique'] != $unique) {
// We need to call a special DROP INDEX if it was defined
if ($columnInfo['unique']) {
$dropIndexes[] = $column['name'];
}
$columnInfo['unique'] = $unique;
$columnChanged = true;
}
unset($unique);
// 3. check for type change
$type = Type::getType($fieldMapping['type']);
if ($columnInfo['type'] != $type) {
$columnInfo['type'] = $type;
$columnChanged = true;
}
unset($type);
// 4. check for scale and precision change
if (strtolower($columnInfo['type']) == 'decimal') {
/*// Doesn't work yet, see DDC-89
if($columnInfo['length'] != $fieldMapping['precision'] ||
$columnInfo['scale'] != $fieldMapping['scale']) {
$sm = $this->_em->getConnection()->getSchemaManager();
$columnInfo['length'] = $fieldMapping['precision'];
$columnInfo['scale'] = $fieldMapping['scale'];
$columnChanged = true;
}*/
}
// 5. check for length change of strings
elseif(strtolower($fieldMapping['type']) == 'string') {
if(!isset($fieldMapping['length']) || $fieldMapping['length'] === null) {
$fieldMapping['length'] = 255;
}
$fromSchema = $sm->createSchema();
$toSchema = $this->getSchemaFromMetadata($classes);
if($columnInfo['length'] != $fieldMapping['length']) {
$columnInfo['length'] = $fieldMapping['length'];
$columnChanged = true;
}
}
// 6. check for flexible and fixed length
$fieldMapping['fixed'] = ( ! isset($fieldMapping['fixed']))
? false : $fieldMapping['fixed'];
if ($columnInfo['fixed'] != $fieldMapping['fixed']) {
$columnInfo['fixed'] = $fieldMapping['fixed'];
$columnChanged = true;
}
// Only add to column changed list if it was actually changed
if ($columnChanged) {
$updateFields[] = $columnInfo;
}
unset($currentColumns[$index]);
$exists = true;
break;
}
}
if ( ! $exists) {
$newFields[] = $fieldMapping;
}
}
foreach ($class->associationMappings as $assoc) {
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
foreach ($assoc->targetToSourceKeyColumns as $targetColumn => $sourceColumn) {
$exists = false;
foreach ($currentColumns as $index => $column) {
if ($column['name'] == $sourceColumn) {
// Column exists, check for changes
// 1. check for nullability change
unset($currentColumns[$index]);
$exists = true;
break;
}
}
if ( ! $exists) {
$newJoinColumns[$sourceColumn] = array(
'name' => $sourceColumn,
'type' => 'integer' //FIXME!!!
);
}
}
}
}
// Drop indexes
if ($dropIndexes) {
foreach ($dropIndexes as $dropIndex) {
$sql[] = $this->_platform->getDropIndexSql($tableName, $dropIndex);
}
}
// Create new columns
if ($newFields || $newJoinColumns) {
$changes = array();
foreach ($newFields as $newField) {
$options = array();
$table = new \Doctrine\DBAL\Schema\Table("foo");
$changes['add'][$newField['columnName']] = $this->_gatherUpdateColumn($class, $newField, $options, $table);
}
foreach ($newJoinColumns as $name => $joinColumn) {
$joinColumn['type'] = Type::getType($joinColumn['type']);
$changes['add'][$name] = $joinColumn;
}
$sql = array_merge($sql, $this->_platform->getAlterTableSql($tableName, $changes));
}
// Update existent columns
if ($updateFields) {
$changes = array();
foreach ($updateFields as $updateField) {
// Now we pick the Type instance
$changes['change'][$updateField['name']] = array(
'definition' => $updateField
);
}
$sql = array_merge($sql, $this->_platform->getAlterTableSql($tableName, $changes));
}
// Drop any remaining columns
if ($currentColumns) {
$changes = array();
foreach ($currentColumns as $column) {
$options = array();
$changes['remove'][$column['name']] = $column;
}
$sql = array_merge($sql, $this->_platform->getAlterTableSql($tableName, $changes));
}
}
}
if ($newClasses) {
$sql = array_merge($this->getCreateSchemaSql($newClasses), $sql);
}
// Drop any remaining tables (Probably not a good idea, because the given class list
// may not be complete!)
/*if ($tables) {
foreach ($tables as $table) {
$sql[] = $this->_platform->getDropTableSql($table);
}
}*/
return $sql;
}
$comparator = new \Doctrine\DBAL\Schema\Comparator();
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
/**
* Temporary method, required because schema update is not on par with schema create refactorings.
*
* @param <type> $class
* @param array $mapping
* @param array $options
* @param <type> $table
* @return <type>
*/
private function _gatherUpdateColumn($class, array $mapping, array &$options, $table)
{
$column = array();
$column['name'] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
$column['type'] = Type::getType($mapping['type']);
$column['length'] = isset($mapping['length']) ? $mapping['length'] : null;
$column['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
$column['unique'] = isset($mapping['unique']) ? $mapping['unique'] : false;
$column['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
if(strtolower($column['type']) == 'string' && $column['length'] === null) {
$column['length'] = 255;
}
if (isset($mapping['precision'])) {
$column['precision'] = $mapping['precision'];
}
if (isset($mapping['scale'])) {
$column['scale'] = $mapping['scale'];
}
if (isset($mapping['default'])) {
$column['default'] = $mapping['default'];
}
if ($class->isIdentifier($mapping['fieldName'])) {
$column['primary'] = true;
$options['primary'][] = $mapping['columnName'];
if ($class->isIdGeneratorIdentity()) {
$column['autoincrement'] = true;
}
}
return $column;
return $schemaDiff->toSql($this->_platform);
}
private function _getCommitOrder(array $classes)

View File

@ -62,81 +62,4 @@ class MySqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(1, count($sql));
$this->assertEquals("CREATE TABLE boolean_model (id INT AUTO_INCREMENT NOT NULL, booleanField TINYINT(1) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]);
}
public function testGetUpdateSchemaSql()
{
$this->markTestSkipped('Update Schema Tool stuff wont be needed anymore soon!');
$classes = array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\SchemaToolEntityA')
);
$tool = new SchemaTool($this->_em);
$tool->createSchema($classes);
// Add field to SchemaToolEntityA
$classA = $classes[0];
$classA->mapField(array(
'fieldName' => 'newField',
'columnName' => 'new_field',
'type' => 'string',
'length' => 50,
'nullable' => false
));
// Test create column with no length and nullable defaults to 255, NOT NULL
$classA->mapField(array(
'fieldName' => 'newField2',
'columnName' => 'new_field2',
'type' => 'string',
));
// Introduce SchemaToolEntityB
$classB = new ClassMetadata(__NAMESPACE__ . '\SchemaToolEntityB');
$classB->setTableName('schematool_entity_b');
$classB->mapField(array(
'fieldName' => 'id',
'columnName' => 'id',
'type' => 'integer',
'nullable' => false,
'id' => true
));
$classB->mapField(array(
'fieldName' => 'field',
'columnName' => 'field',
'type' => 'string',
'nullable' => false
));
$classes[] = $classB;
$sql = $tool->getUpdateSchemaSql($classes);
$this->assertEquals(2, count($sql));
$this->assertEquals("CREATE TABLE schematool_entity_b (id INT NOT NULL, field VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]);
$this->assertEquals("ALTER TABLE schematool_entity_a ADD new_field VARCHAR(50) NOT NULL, ADD new_field2 VARCHAR(255) NOT NULL", $sql[1]);
$tool->updateSchema($classes);
// Change from 50 to default value (by setting null)
$classA->fieldMappings['newField']['length'] = null;
$sql = $tool->getUpdateSchemaSql($classes);
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE schematool_entity_a CHANGE new_field new_field VARCHAR(255) NOT NULL", $sql[0]);
}
}
/** @Entity @Table(name="schematool_entity_a") */
class SchemaToolEntityA {
/** @Id @Column(type="integer") */
private $id;
private $newField;
private $newField2;
}
class SchemaToolEntityB {
private $id;
private $field;
}
}

View File

@ -23,8 +23,6 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\ClassMetadataExporterTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\ConvertDoctrine1SchemaTest');
$suite->addTest(SchemaTool\AllTests::suite());
return $suite;
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Tools_SchemaTool_AllTests::main');
}
require_once __DIR__ . '/../../../TestInit.php';
class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Schema Tool');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaTool\MysqlUpdateSchemaTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaTool\PostgresUpdateSchemaTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaTool\OracleUpdateSchemaTest');
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Tools_SchemaTool_AllTests::main') {
AllTests::main();
}

View File

@ -1,46 +0,0 @@
<?php
require_once realpath(dirname(__FILE__)."/../../../../../../lib/Doctrine/Common/IsolatedClassLoader.php");
$classLoader = new \Doctrine\Common\IsolatedClassLoader('Doctrine');
$classLoader->setBasePath(realpath(dirname(__FILE__)."/../../../../../../lib/"));
$classLoader->register();
$params = array(
'driver' => 'pdo_mysql',
'dbname' => $argv[3],
'user' => $argv[1],
'password' => $argv[2]
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$sm = $conn->getSchemaManager();
if(isset($argv[4])) {
$filterString = $argv[4];
} else {
$filterString = false;
}
$tables = $sm->listTables();
$fixture = array();
foreach($tables AS $tableName) {
if($filterString !== false && strpos($tableName, $filterString) === false) {
continue;
}
$fixture[$tableName] = $sm->listTableColumns($tableName);
}
ksort($fixture);
$regexp = '(Doctrine\\\\DBAL\\\\Types\\\\([a-zA-Z]+)Type::__set_state\(array\([\s]+\)\))';
$code = var_export($fixture, true);
$code = preg_replace(
$regexp,
'Doctrine\\DBAL\\Types\\Type::getType(strtolower("\1"))',
$code
);
echo "<?php\n\return ";
echo $code;
echo ";\n?>\n";

View File

@ -1,347 +0,0 @@
<?php
$fixtures = array (
'cms_addresses' =>
array (
0 =>
array (
'name' => 'id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => true,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'country',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 50,
'unsigned' => false,
'fixed' => false,
),
2 =>
array (
'name' => 'zip',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 50,
'unsigned' => false,
'fixed' => false,
),
3 =>
array (
'name' => 'city',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 50,
'unsigned' => false,
'fixed' => false,
),
4 =>
array (
'name' => 'user_id',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => false,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
),
'cms_articles' =>
array (
0 =>
array (
'name' => 'id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => true,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'topic',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 255,
'unsigned' => false,
'fixed' => false,
),
2 =>
array (
'name' => 'text',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 255,
'unsigned' => false,
'fixed' => false,
),
3 =>
array (
'name' => 'user_id',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => false,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
),
'cms_groups' =>
array (
0 =>
array (
'name' => 'id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => true,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'name',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 50,
'unsigned' => false,
'fixed' => false,
),
),
'cms_phonenumbers' =>
array (
0 =>
array (
'name' => 'phonenumber',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 50,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'user_id',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => false,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
),
'cms_users' =>
array (
0 =>
array (
'name' => 'id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => true,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'status',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 50,
'unsigned' => false,
'fixed' => false,
),
2 =>
array (
'name' => 'username',
'values' =>
array (
),
'primary' => false,
'unique' => true,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 255,
'unsigned' => false,
'fixed' => false,
),
3 =>
array (
'name' => 'name',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("String")),
'length' => 255,
'unsigned' => false,
'fixed' => false,
),
),
'cms_users_groups' =>
array (
0 =>
array (
'name' => 'user_id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => '0',
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'group_id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => '0',
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
),
);
return $fixtures;
?>

View File

@ -1,41 +0,0 @@
<?php
return array (
'decimal_model' =>
array (
0 =>
array (
'name' => 'id',
'values' =>
array (
),
'primary' => true,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => true,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Integer")),
'length' => NULL,
'unsigned' => false,
'fixed' => false,
),
1 =>
array (
'name' => 'decimal',
'values' =>
array (
),
'primary' => false,
'unique' => false,
'default' => NULL,
'notnull' => true,
'autoincrement' => false,
'type' =>
Doctrine\DBAL\Types\Type::getType(strtolower("Decimal")),
'length' => 5,
'unsigned' => false,
'fixed' => false,
),
),
);
?>

View File

@ -1,137 +0,0 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../../TestInit.php';
class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
{
protected function _createPlatform()
{
return new \Doctrine\DBAL\Platforms\MySqlPlatform();
}
public function testAddField()
{
$sql = $this->_doTestAddField();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses ADD street VARCHAR(255) NOT NULL",
$sql[0]
);
}
public function testChangeColumnName()
{
$sql = $this->_doTestChangeColumnName();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ADD the_city VARCHAR(50) NOT NULL", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses DROP city", $sql[1]);
}
public function testChangeNullability()
{
$sql = $this->_doTestChangeNullability();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) DEFAULT NULL", $sql[0]);
}
/**
* @group DDC-102
*/
public function testChangeNullabilityToNull()
{
$sql = $this->_doTestChangeNullabilityToNull();
$this->assertEquals(0, count($sql));
}
public function testChangeType()
{
$sql = $this->_doTestChangeType();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city TINYTEXT NOT NULL", $sql[0]);
}
public function testChangeUniqueness()
{
$sql = $this->_doTestChangeUniqueness();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) NOT NULL UNIQUE", $sql[0]);
}
public function testChangeLength()
{
$sql = $this->_doTestChangeLength();
$this->assertEquals(1, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(200) NOT NULL', $sql[0]);
}
/**
* @group DDC-101
*/
public function testChangeLengthToNull()
{
$sql = $this->_doTestChangeLengthToNull();
$this->assertEquals(1, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(255) NOT NULL', $sql[0]);
}
public function testChangeDecimalLengthPrecision()
{
$sql = $this->_doTestChangeDecimalLengthPrecision();
$this->assertEquals(1, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[0]);
}
public function testChangeDecimalLengthScale()
{
$sql = $this->_doTestChangeDecimalLengthScale();
$this->assertEquals(1, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[0]);
}
public function testChangeFixed()
{
$sql = $this->_doTestChangeFixed();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses CHANGE city city CHAR(50) NOT NULL",
$sql[0]
);
}
public function testAddIndex()
{
$sql = $this->_doTestAddIndex();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"CREATE INDEX searchCity (city)",
$sql[0]
);
}
public function testRemoveField()
{
$sql = $this->_doTestRemoveField();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses DROP city",
$sql[0]
);
}
}

View File

@ -1,136 +0,0 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../../TestInit.php';
class OracleUpdateSchemaTest extends UpdateSchemaTestCase
{
protected function _createPlatform()
{
return new \Doctrine\DBAL\Platforms\OraclePlatform();
}
public function testAddField()
{
$sql = $this->_doTestAddField();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses ADD (street VARCHAR2(255) NOT NULL)",
$sql[0]
);
}
public function testChangeColumnName()
{
$sql = $this->_doTestChangeColumnName();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ADD (the_city VARCHAR2(50) NOT NULL)", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses DROP COLUMN city", $sql[1]);
}
public function testChangeNullability()
{
$sql = $this->_doTestChangeNullability();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses MODIFY (city VARCHAR2(50) DEFAULT NULL)", $sql[0]);
}
/**
* @group DDC-102
*/
public function testChangeNullabilityToNull()
{
$sql = $this->_doTestChangeNullabilityToNull();
$this->assertEquals(0, count($sql));
}
public function testChangeType()
{
$sql = $this->_doTestChangeType();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses MODIFY (city CLOB NOT NULL)", $sql[0]);
}
public function testChangeUniqueness()
{
$this->markTestSkipped('Not supported on Postgres-Sql yet.');
$sql = $this->_doTestChangeUniqueness();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ALTER city TYPE VARCHAR(50)", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses ALTER city SET NOT NULL", $sql[1]);
}
public function testChangeLength()
{
$sql = $this->_doTestChangeLength();
$this->assertEquals(1, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses MODIFY (city VARCHAR2(200) NOT NULL)', $sql[0]);
}
/**
* @group DDC-101
*/
public function testChangeLengthToNull()
{
$sql = $this->_doTestChangeLengthToNull();
$this->assertEquals(1, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses MODIFY (city VARCHAR2(255) NOT NULL)', $sql[0]);
}
public function testChangeDecimalLengthPrecision()
{
$sql = $this->_doTestChangeDecimalLengthPrecision();
$this->assertEquals(2, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[0]);
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[1]);
}
public function testChangeDecimalLengthScale()
{
$sql = $this->_doTestChangeDecimalLengthScale();
$this->assertEquals(2, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[0]);
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[1]);
}
public function testChangeFixed()
{
$sql = $this->_doTestChangeFixed();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses MODIFY (city CHAR(50) NOT NULL)", $sql[0]);
}
public function testAddIndex()
{
$sql = $this->_doTestAddIndex();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"CREATE INDEX searchCity (city)",
$sql[0]
);
}
public function testRemoveField()
{
$sql = $this->_doTestRemoveField();
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses DROP COLUMN city", $sql[0]);
}
}

View File

@ -1,144 +0,0 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../../TestInit.php';
class PostgresUpdateSchemaTest extends UpdateSchemaTestCase
{
protected function _createPlatform()
{
return new \Doctrine\DBAL\Platforms\PostgreSqlPlatform();
}
public function testAddField()
{
$sql = $this->_doTestAddField();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses ADD street VARCHAR(255) NOT NULL",
$sql[0]
);
}
public function testChangeColumnName()
{
$sql = $this->_doTestChangeColumnName();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ADD the_city VARCHAR(50) NOT NULL", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses DROP city", $sql[1]);
}
public function testChangeNullability()
{
$sql = $this->_doTestChangeNullability();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ALTER city TYPE VARCHAR(50)", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses ALTER city DROP NOT NULL", $sql[1]);
}
/**
* @group DDC-102
*/
public function testChangeNullabilityToNull()
{
$sql = $this->_doTestChangeNullabilityToNull();
$this->assertEquals(0, count($sql));
}
public function testChangeType()
{
$sql = $this->_doTestChangeType();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ALTER city TYPE TEXT", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses ALTER city SET NOT NULL", $sql[1]);
}
public function testChangeUniqueness()
{
$this->markTestSkipped('Not supported on Postgres-Sql yet.');
$sql = $this->_doTestChangeUniqueness();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ALTER city TYPE VARCHAR(50)", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses ALTER city SET NOT NULL", $sql[1]);
}
public function testChangeLength()
{
$sql = $this->_doTestChangeLength();
$this->assertEquals(2, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses ALTER city TYPE VARCHAR(200)', $sql[0]);
$this->assertEquals('ALTER TABLE cms_addresses ALTER city SET NOT NULL', $sql[1]);
}
/**
* @group DDC-101
*/
public function testChangeLengthToNull()
{
$sql = $this->_doTestChangeLengthToNull();
$this->assertEquals(2, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses ALTER city TYPE VARCHAR(255)', $sql[0]);
$this->assertEquals('ALTER TABLE cms_addresses ALTER city SET NOT NULL', $sql[1]);
}
public function testChangeDecimalLengthPrecision()
{
$sql = $this->_doTestChangeDecimalLengthPrecision();
$this->assertEquals(2, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[0]);
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[1]);
}
public function testChangeDecimalLengthScale()
{
$sql = $this->_doTestChangeDecimalLengthScale();
$this->assertEquals(2, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[0]);
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[1]);
}
public function testChangeFixed()
{
$sql = $this->_doTestChangeFixed();
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ALTER city TYPE CHAR(50)", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses ALTER city SET NOT NULL", $sql[1]);
}
public function testAddIndex()
{
$sql = $this->_doTestAddIndex();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"CREATE INDEX searchCity (city)",
$sql[0]
);
}
public function testRemoveField()
{
$sql = $this->_doTestRemoveField();
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses DROP city",
$sql[0]
);
}
}

View File

@ -1,296 +0,0 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../../TestInit.php';
abstract class UpdateSchemaTestCase extends \Doctrine\Tests\OrmTestCase
{
public function setUp()
{
$this->markTestSkipped('Update Schema Tool stuff wont be needed anymore soon!');
}
protected function _doTestAddField()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->mapField(array('fieldName' => 'street', 'type' => 'string'));
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses ADD street VARCHAR(255) NOT NULL",
$sql[0]
);
}
protected function _doTestChangeColumnName()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['columnName'] = 'the_city';
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(2, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses ADD the_city VARCHAR(50) NOT NULL", $sql[0]);
$this->assertEquals("ALTER TABLE cms_addresses DROP city", $sql[1]);
}
protected function _doTestChangeNullability()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['nullable'] = true;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) DEFAULT NULL", $sql[0]);
}
/**
* @group DDC-102
*/
protected function _doTestChangeNullabilityToNull()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$this->assertFalse($classMetadata->fieldMappings['city']['nullable']);
unset($classMetadata->fieldMappings['city']['nullable']);
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(0, count($sql));
}
protected function _doTestChangeType()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['type'] = "text";
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city TINYTEXT NOT NULL", $sql[0]);
}
protected function _doTestChangeUniqueness()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['unique'] = true;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) NOT NULL UNIQUE", $sql[0]);
}
protected function _doTestChangeLength()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['length'] = 200;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(200) NOT NULL', $sql[0]);
}
/**
* @group DDC-101
*/
protected function _doTestChangeLengthToNull()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['length'] = null;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(255) NOT NULL', $sql[0]);
}
protected function _doTestChangeDecimalLengthPrecision()
{
$this->markTestSkipped('Decimal Scale changes not supported yet, because of DDC-89.');
$this->_initSchemaTool('DecimalModel');
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\Generic\DecimalModel");
$classMetadata->fieldMappings['decimal']['precision'] = 10;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[0]);
}
protected function _doTestChangeDecimalLengthScale()
{
$this->markTestSkipped('Decimal Scale changes not supported yet, because of DDC-89.');
$this->_initSchemaTool('DecimalModel');
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\Generic\DecimalModel");
$classMetadata->fieldMappings['decimal']['scale'] = 3;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
// invalid sql, because not escaped
$this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[0]);
}
protected function _doTestChangeFixed()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->fieldMappings['city']['fixed'] = true;
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses CHANGE city city CHAR(50) NOT NULL",
$sql[0]
);
}
protected function _doTestAddIndex()
{
$this->markTestSkipped('Not yet supported by SchemaTool, see DDC-90');
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
$classMetadata->primaryTable['indexes'] = array('searchCity' => array('columns' => array('city')));
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals(
"CREATE INDEX searchCity (city)",
$sql[0]
);
}
protected function _doTestRemoveField()
{
$this->_initSchemaTool("Cms");
$classMetadata = $this->_getMetadataFor("Doctrine\Tests\Models\CMS\CmsAddress");
unset($classMetadata->fieldMappings['city']);
return $this->_getUpdateSchemaSql(array($classMetadata));
$this->assertEquals(1, count($sql));
$this->assertEquals(
"ALTER TABLE cms_addresses DROP city",
$sql[0]
);
}
/*
* Utility methods start here
*/
/**
* @var \Doctrine\ORM\EntityManager
*/
private $_em = null;
/**
* @var \Doctrine\ORM\Tools\SchemaTool
*/
private $_schemaTool = null;
/**
*
* @param string $fixtureName
* @return \Doctrine\ORM\Tools\SchemaTool
*/
protected function _initSchemaTool($fixtureName)
{
if($this->_em == null || $this->_schemaTool == null) {
$this->_createSchemaTool($fixtureName, $this->_createPlatform());
}
}
abstract protected function _createPlatform();
private function _createSchemaTool($fixtureName, $platform)
{
$fixtureFile = __DIR__."/DbFixture/".$fixtureName.".php";;
if(!file_exists($fixtureFile)) {
throw new \Exception("Cannot find fixture file: ".$fixtureFile);
}
$fixture = include $fixtureFile;
$sm = new UpdateSchemaMock($fixture);
$this->_em = $this->_getTestEntityManager(null, null, null, false);
$this->_em->getConnection()->setDatabasePlatform($platform);
$this->_em->getConnection()->getDriver()->setSchemaManager($sm);
$this->_schemaTool = new SchemaTool($this->_em);
}
protected function _getUpdateSchemaSql(array $classMetadata)
{
if($this->_schemaTool !== null) {
return $this->_schemaTool->getUpdateSchemaSql($classMetadata);
} else {
throw new \Exception("SchemaTool was not initialized.");
}
}
/**
* @param string $className
* @return \Doctrine\ORM\Mapping\ClassMetadata
*/
protected function _getMetadataFor($className)
{
if(!class_exists($className, true)) {
throw new \Exception("Class ".$className." used for UpdateSchemaTestCase was not found!");
}
if($this->_em == null) {
throw new \Exception("SchemaTool and EntityManager are not initialized.");
}
return $this->_em->getClassMetadata($className);
}
}
class UpdateSchemaMock extends \Doctrine\DBAL\Schema\AbstractSchemaManager
{
private $_fixtureData;
public function __construct($fixtureData)
{
$this->_fixtureData = $fixtureData;
}
public function listTables()
{
return array_keys($this->_fixtureData);
}
public function listTableColumns($tableName)
{
return $this->_fixtureData[$tableName];
}
protected function _getPortableTableColumnDefinition($column)
{
return $column;
}
}