Commit 48c78a2d authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-112, DBAL-113 - Implement AbstractPlatform::getAlterSequenceSQL() and fix...

DBAL-112, DBAL-113 - Implement AbstractPlatform::getAlterSequenceSQL() and fix comparator code for sequences
parent b0f8dd89
...@@ -1046,6 +1046,17 @@ abstract class AbstractPlatform ...@@ -1046,6 +1046,17 @@ abstract class AbstractPlatform
{ {
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
/**
* Gets the SQL statement to change a sequence on this platform.
*
* @param \Doctrine\DBAL\Schema\Sequence $sequence
* @return string
*/
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
throw DBALException::notSupported(__METHOD__);
}
/** /**
* Gets the SQL to create a constraint on a table on this platform. * Gets the SQL to create a constraint on a table on this platform.
......
...@@ -143,6 +143,12 @@ class OraclePlatform extends AbstractPlatform ...@@ -143,6 +143,12 @@ class OraclePlatform extends AbstractPlatform
' MINVALUE ' . $sequence->getInitialValue() . ' MINVALUE ' . $sequence->getInitialValue() .
' INCREMENT BY ' . $sequence->getAllocationSize(); ' INCREMENT BY ' . $sequence->getAllocationSize();
} }
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -452,6 +452,12 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -452,6 +452,12 @@ class PostgreSqlPlatform extends AbstractPlatform
' START ' . $sequence->getInitialValue(); ' START ' . $sequence->getInitialValue();
} }
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize();
}
/** /**
* Drop existing sequence * Drop existing sequence
* @param \Doctrine\DBAL\Schema\Sequence $sequence * @param \Doctrine\DBAL\Schema\Sequence $sequence
......
...@@ -99,7 +99,7 @@ class Comparator ...@@ -99,7 +99,7 @@ class Comparator
$diff->newSequences[] = $sequence; $diff->newSequences[] = $sequence;
} else { } else {
if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) { if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
$diff->changedSequences[] = $fromSchema->getSequence($sequenceName); $diff->changedSequences[] = $toSchema->getSequence($sequenceName);
} }
} }
} }
......
...@@ -132,8 +132,7 @@ class SchemaDiff ...@@ -132,8 +132,7 @@ class SchemaDiff
if ($platform->supportsSequences() == true) { if ($platform->supportsSequences() == true) {
foreach ($this->changedSequences AS $sequence) { foreach ($this->changedSequences AS $sequence) {
$sql[] = $platform->getDropSequenceSQL($sequence); $sql[] = $platform->getAlterSequenceSQL($sequence);
$sql[] = $platform->getCreateSequenceSQL($sequence);
} }
if ($saveMode === false) { if ($saveMode === false) {
......
...@@ -66,6 +66,16 @@ class Sequence extends AbstractAsset ...@@ -66,6 +66,16 @@ class Sequence extends AbstractAsset
{ {
return $this->_initialValue; return $this->_initialValue;
} }
public function setAllocationSize($allocationSize)
{
$this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1;
}
public function setInitialValue($initialValue)
{
$this->_initialValue = (is_numeric($initialValue))?$initialValue:1;
}
/** /**
* @param Visitor $visitor * @param Visitor $visitor
......
...@@ -652,6 +652,25 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -652,6 +652,25 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns)); $this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
$this->assertEquals(0, count($tableDiff->removedColumns)); $this->assertEquals(0, count($tableDiff->removedColumns));
} }
/**
* @group DBAL-112
*/
public function testChangedSequence()
{
$schema = new Schema();
$sequence = $schema->createSequence('baz');
$schemaNew = clone $schema;
/* @var $schemaNew Schema */
$schemaNew->getSequence('baz')->setAllocationSize(20);
$c = new \Doctrine\DBAL\Schema\Comparator;
$diff = $c->compare($schema, $schemaNew);
$this->assertSame($diff->changedSequences[0] , $schemaNew->getSequence('baz'));
}
/** /**
* @group DBAL-106 * @group DBAL-106
......
...@@ -19,11 +19,11 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase ...@@ -19,11 +19,11 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
public function testSchemaDiffToSql() public function testSchemaDiffToSql()
{ {
$diff = $this->createSchemaDiff(); $diff = $this->createSchemaDiff();
$platform = $this->createPlatform(); $platform = $this->createPlatform(true);
$sql = $diff->toSql($platform); $sql = $diff->toSql($platform);
$expected = array('drop_orphan_fk', 'drop_seq', 'create_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table'); $expected = array('drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table');
$this->assertEquals($expected, $sql); $this->assertEquals($expected, $sql);
} }
...@@ -31,28 +31,34 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase ...@@ -31,28 +31,34 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
public function testSchemaDiffToSaveSql() public function testSchemaDiffToSaveSql()
{ {
$diff = $this->createSchemaDiff(); $diff = $this->createSchemaDiff();
$platform = $this->createPlatform(1, 0, 0); $platform = $this->createPlatform(false);
$sql = $diff->toSaveSql($platform); $sql = $diff->toSaveSql($platform);
$expected = array('drop_seq', 'create_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table'); $expected = array('alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table');
$this->assertEquals($expected, $sql); $this->assertEquals($expected, $sql);
} }
public function createPlatform($dropSequenceCount=2, $dropTableCount=1, $dropOrphanedFkCount=1) public function createPlatform($unsafe = false)
{ {
$platform = $this->getMock('Doctrine\Tests\DBAL\Mocks\MockPlatform'); $platform = $this->getMock('Doctrine\Tests\DBAL\Mocks\MockPlatform');
$platform->expects($this->exactly($dropSequenceCount)) if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropSequenceSql') ->method('getDropSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence')) ->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('drop_seq')); ->will($this->returnValue('drop_seq'));
$platform->expects($this->exactly(2)) }
$platform->expects($this->exactly(1))
->method('getAlterSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('alter_seq'));
$platform->expects($this->exactly(1))
->method('getCreateSequenceSql') ->method('getCreateSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence')) ->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('create_seq')); ->will($this->returnValue('create_seq'));
if ($dropTableCount > 0) { if ($unsafe) {
$platform->expects($this->exactly($dropTableCount)) $platform->expects($this->exactly(1))
->method('getDropTableSql') ->method('getDropTableSql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\Table')) ->with($this->isInstanceof('Doctrine\DBAL\Schema\Table'))
->will($this->returnValue('drop_table')); ->will($this->returnValue('drop_table'));
...@@ -69,8 +75,8 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase ...@@ -69,8 +75,8 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
->method('getAlterTableSql') ->method('getAlterTableSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\TableDiff')) ->with($this->isInstanceOf('Doctrine\DBAL\Schema\TableDiff'))
->will($this->returnValue(array('alter_table'))); ->will($this->returnValue(array('alter_table')));
if ($dropOrphanedFkCount > 0) { if ($unsafe) {
$platform->expects($this->exactly($dropOrphanedFkCount)) $platform->expects($this->exactly(1))
->method('getDropForeignKeySql') ->method('getDropForeignKeySql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\ForeignKeyConstraint'), $this->equalTo('local_table')) ->with($this->isInstanceof('Doctrine\DBAL\Schema\ForeignKeyConstraint'), $this->equalTo('local_table'))
->will($this->returnValue('drop_orphan_fk')); ->will($this->returnValue('drop_orphan_fk'));
......
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