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
{
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.
......
......@@ -143,6 +143,12 @@ class OraclePlatform extends AbstractPlatform
' MINVALUE ' . $sequence->getInitialValue() .
' INCREMENT BY ' . $sequence->getAllocationSize();
}
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize();
}
/**
* {@inheritdoc}
......
......@@ -452,6 +452,12 @@ class PostgreSqlPlatform extends AbstractPlatform
' START ' . $sequence->getInitialValue();
}
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize();
}
/**
* Drop existing sequence
* @param \Doctrine\DBAL\Schema\Sequence $sequence
......
......@@ -99,7 +99,7 @@ class Comparator
$diff->newSequences[] = $sequence;
} else {
if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
$diff->changedSequences[] = $fromSchema->getSequence($sequenceName);
$diff->changedSequences[] = $toSchema->getSequence($sequenceName);
}
}
}
......
......@@ -132,8 +132,7 @@ class SchemaDiff
if ($platform->supportsSequences() == true) {
foreach ($this->changedSequences AS $sequence) {
$sql[] = $platform->getDropSequenceSQL($sequence);
$sql[] = $platform->getCreateSequenceSQL($sequence);
$sql[] = $platform->getAlterSequenceSQL($sequence);
}
if ($saveMode === false) {
......
......@@ -66,6 +66,16 @@ class Sequence extends AbstractAsset
{
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
......
......@@ -652,6 +652,25 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
$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
......
......@@ -19,11 +19,11 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
public function testSchemaDiffToSql()
{
$diff = $this->createSchemaDiff();
$platform = $this->createPlatform();
$platform = $this->createPlatform(true);
$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);
}
......@@ -31,28 +31,34 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
public function testSchemaDiffToSaveSql()
{
$diff = $this->createSchemaDiff();
$platform = $this->createPlatform(1, 0, 0);
$platform = $this->createPlatform(false);
$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);
}
public function createPlatform($dropSequenceCount=2, $dropTableCount=1, $dropOrphanedFkCount=1)
public function createPlatform($unsafe = false)
{
$platform = $this->getMock('Doctrine\Tests\DBAL\Mocks\MockPlatform');
$platform->expects($this->exactly($dropSequenceCount))
if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->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')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('create_seq'));
if ($dropTableCount > 0) {
$platform->expects($this->exactly($dropTableCount))
if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropTableSql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\Table'))
->will($this->returnValue('drop_table'));
......@@ -69,8 +75,8 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
->method('getAlterTableSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\TableDiff'))
->will($this->returnValue(array('alter_table')));
if ($dropOrphanedFkCount > 0) {
$platform->expects($this->exactly($dropOrphanedFkCount))
if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropForeignKeySql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\ForeignKeyConstraint'), $this->equalTo('local_table'))
->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