Commit 58e99d39 authored by beberlei's avatar beberlei

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

parent 8a46eb04
This diff is collapsed.
......@@ -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;
}
}
\ No newline at end of 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;
}
}
......
<?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();
}
\ No newline at end of file
<?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";
\ No newline at end of file
<?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;
?>
<?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,
),
),
);
?>
<?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]
);
}
}
\ No newline at end of file
<?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]);
}
}
\ No newline at end of file
<?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]
);
}
}
\ No newline at end of file
<?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;
}
}
\ 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