[2.0] DDC-84 - Added increment alter table / update schema tests for Postgres Platform - Fixed several bugs in Doctrine\DBAL\Platforms\PostgreSqlPlatform
This commit is contained in:
parent
6a26de11ff
commit
d369d27f8b
@ -532,23 +532,22 @@ class PostgreSqlPlatform extends AbstractPlatform
|
||||
|
||||
if (isset($changes['change']) && is_array($changes['change'])) {
|
||||
foreach ($changes['change'] as $fieldName => $field) {
|
||||
if (isset($field['type'])) {
|
||||
$serverInfo = $this->getServerVersion();
|
||||
|
||||
if (is_array($serverInfo) && $serverInfo['major'] < 8) {
|
||||
throw DoctrineException::changeColumnRequiresPostgreSQL8OrAbove($field['type']);
|
||||
}
|
||||
$query = 'ALTER ' . $fieldName . ' TYPE ' . $this->getTypeDeclarationSql($field['definition']);
|
||||
if (isset($field['definition']['type']) && $field['definition']['type'] instanceof \Doctrine\DBAL\Types\Type) {
|
||||
$type = $field['definition']['type'];
|
||||
|
||||
// here was a server version check before, but DBAL API does not support this anymore.
|
||||
$query = 'ALTER ' . $fieldName . ' TYPE ' . $type->getSqlDeclaration($field['definition'], $this);
|
||||
$sql[] = 'ALTER TABLE ' . $name . ' ' . $query;
|
||||
}
|
||||
if (array_key_exists('default', $field)) {
|
||||
$query = 'ALTER ' . $fieldName . ' SET DEFAULT ' . $this->quote($field['definition']['default'], $field['definition']['type']);
|
||||
if (isset($field['definition']['default'])) {
|
||||
$query = 'ALTER ' . $fieldName . ' SET DEFAULT ' . $field['definition']['default'];
|
||||
$sql[] = 'ALTER TABLE ' . $name . ' ' . $query;
|
||||
}
|
||||
if ( ! empty($field['notnull'])) {
|
||||
if (isset($field['definition']['notnull']) && is_bool($field['definition']['notnull'])) {
|
||||
$query = 'ALTER ' . $fieldName . ' ' . ($field['definition']['notnull'] ? 'SET' : 'DROP') . ' NOT NULL';
|
||||
$sql[] = 'ALTER TABLE ' . $name . ' ' . $query;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ class AllTests
|
||||
$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');
|
||||
|
||||
|
||||
return $suite;
|
||||
|
@ -13,13 +13,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testAddField()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$md->mapField(array('fieldName' => 'street', 'type' => 'string'));
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($md));
|
||||
$sql = $this->_doTestAddField();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals(
|
||||
@ -30,13 +24,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeColumnName()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$classMetadata->fieldMappings['city']['columnName'] = 'the_city';
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeColumnName();
|
||||
|
||||
$this->assertEquals(2, count($sql));
|
||||
$this->assertEquals("ALTER TABLE cms_addresses ADD the_city VARCHAR(50) NOT NULL", $sql[0]);
|
||||
@ -45,13 +33,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeNullability()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$classMetadata->fieldMappings['city']['nullable'] = true;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeNullability();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) DEFAULT NULL", $sql[0]);
|
||||
@ -62,28 +44,14 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
*/
|
||||
public function testChangeNullabilityToNull()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
|
||||
$this->assertFalse($classMetadata->fieldMappings['city']['nullable']);
|
||||
unset($classMetadata->fieldMappings['city']['nullable']);
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeNullabilityToNull();
|
||||
|
||||
$this->assertEquals(0, count($sql));
|
||||
}
|
||||
|
||||
public function testChangeType()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$classMetadata->fieldMappings['city']['type'] = "text";
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeType();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city TINYTEXT NOT NULL", $sql[0]);
|
||||
@ -91,13 +59,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeUniqueness()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$classMetadata->fieldMappings['city']['unique'] = true;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeUniqueness();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) NOT NULL UNIQUE", $sql[0]);
|
||||
@ -105,13 +67,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeLength()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$classMetadata->fieldMappings['city']['length'] = 200;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeLength();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(200) NOT NULL', $sql[0]);
|
||||
@ -122,13 +78,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
*/
|
||||
public function testChangeLengthToNull()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$classMetadata->fieldMappings['city']['length'] = null;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeLengthToNull();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(255) NOT NULL', $sql[0]);
|
||||
@ -136,16 +86,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeDecimalLengthPrecision()
|
||||
{
|
||||
$this->markTestSkipped('Decimal Scale changes not supported yet, because of DDC-89.');
|
||||
|
||||
$decimalModel = new \Doctrine\Tests\Models\Generic\DecimalModel();
|
||||
|
||||
$st = $this->_getSchemaTool('DecimalModel');
|
||||
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\Generic\DecimalModel");
|
||||
$classMetadata->fieldMappings['decimal']['precision'] = 10;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeDecimalLengthPrecision();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
// invalid sql, because not escaped
|
||||
@ -154,16 +95,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeDecimalLengthScale()
|
||||
{
|
||||
$this->markTestSkipped('Decimal Scale changes not supported yet, because of DDC-89.');
|
||||
|
||||
$decimalModel = new \Doctrine\Tests\Models\Generic\DecimalModel();
|
||||
|
||||
$st = $this->_getSchemaTool('DecimalModel');
|
||||
|
||||
$classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\Generic\DecimalModel");
|
||||
$classMetadata->fieldMappings['decimal']['scale'] = 3;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($classMetadata));
|
||||
$sql = $this->_doTestChangeDecimalLengthScale();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
// invalid sql, because not escaped
|
||||
@ -172,13 +104,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testChangeFixed()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$md->fieldMappings['city']['fixed'] = true;
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($md));
|
||||
$sql = $this->_doTestChangeFixed();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals(
|
||||
@ -189,15 +115,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testAddIndex()
|
||||
{
|
||||
$this->markTestSkipped('Not yet supported by SchemaTool, see DDC-90');
|
||||
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
$md->primaryTable['indexes'] = array('searchCity' => array('columns' => array('city')));
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($md));
|
||||
$sql = $this->_doTestAddIndex();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals(
|
||||
@ -208,13 +126,7 @@ class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
|
||||
|
||||
public function testRemoveField()
|
||||
{
|
||||
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
|
||||
|
||||
$st = $this->_getSchemaTool("Cms");
|
||||
$md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress");
|
||||
unset($md->fieldMappings['city']);
|
||||
|
||||
$sql = $st->getUpdateSchemaSql(array($md));
|
||||
$sql = $this->_doTestRemoveField();
|
||||
|
||||
$this->assertEquals(1, count($sql));
|
||||
$this->assertEquals(
|
||||
|
@ -0,0 +1,146 @@
|
||||
<?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(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]
|
||||
);
|
||||
}
|
||||
}
|
@ -8,26 +8,227 @@ require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
abstract class UpdateSchemaTestCase extends \Doctrine\Tests\OrmTestCase
|
||||
{
|
||||
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 _getSchemaTool($fixtureName)
|
||||
protected function _initSchemaTool($fixtureName)
|
||||
{
|
||||
return $this->_createSchemaTool($fixtureName, $this->_createPlatform());
|
||||
if($this->_em == null || $this->_schemaTool == null) {
|
||||
$this->_createSchemaTool($fixtureName, $this->_createPlatform());
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected function _createPlatform();
|
||||
|
||||
private function _createSchemaTool($fixtureName, $platform)
|
||||
{
|
||||
$fixture = include __DIR__."/DbFixture/".$fixtureName.".php";
|
||||
$fixtureFile = __DIR__."/DbFixture/".$fixtureName.".php";;
|
||||
if(!file_exists($fixtureFile)) {
|
||||
throw new \Exception("Cannot find fixture file: ".$fixtureFile);
|
||||
}
|
||||
$fixture = include $fixtureFile;
|
||||
|
||||
$sm = new UpdateSchemaMock($fixture);
|
||||
|
||||
@ -35,7 +236,16 @@ abstract class UpdateSchemaTestCase extends \Doctrine\Tests\OrmTestCase
|
||||
$this->_em->getConnection()->setDatabasePlatform($platform);
|
||||
$this->_em->getConnection()->getDriver()->setSchemaManager($sm);
|
||||
|
||||
return new SchemaTool($this->_em);
|
||||
$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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,6 +254,13 @@ abstract class UpdateSchemaTestCase extends \Doctrine\Tests\OrmTestCase
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user