From 42c5382a037c95960744341030313c7472f6c15b Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 16 Jun 2011 22:34:04 +0200 Subject: [PATCH] DDC-1172 - Handle sequence dropping in SchemaTool. --- lib/Doctrine/ORM/Tools/SchemaTool.php | 18 ++++++++++++++++ .../SchemaTool/PostgreSqlSchemaToolTest.php | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 321076996..d38f5d159 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -619,6 +619,24 @@ class SchemaTool } } } + + if ($this->_platform->supportsSequences()) { + foreach ($schema->getSequences() AS $sequence) { + $visitor->acceptSequence($sequence); + } + foreach ($schema->getTables() AS $table) { + /* @var $sequence Table */ + if ($table->hasPrimaryKey()) { + $columns = $table->getPrimaryKey()->getColumns(); + if (count($columns) == 1) { + $checkSequence = $table->getName() . "_" . $columns[0] . "_seq"; + if ($fullSchema->hasSequence($checkSequence)) { + $visitor->acceptSequence($fullSchema->getSequence($checkSequence)); + } + } + } + } + } return $visitor->getQueries(); } diff --git a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php index 681485251..b93753ce0 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php @@ -80,4 +80,25 @@ class PostgreSqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals("CREATE TABLE boolean_model (id INT NOT NULL, booleanField BOOLEAN NOT NULL, PRIMARY KEY(id))", $sql[0]); $this->assertEquals("CREATE SEQUENCE boolean_model_id_seq INCREMENT BY 1 MINVALUE 1 START 1", $sql[1]); } + + public function testGetDropSchemaSql() + { + $classes = array( + $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'), + ); + + $tool = new SchemaTool($this->_em); + $sql = $tool->getDropSchemaSQL($classes); + + $this->assertEquals(13, count($sql)); + $dropSequenceSQLs = 0; + foreach ($sql AS $stmt) { + if (strpos($stmt, "DROP SEQUENCE") === 0) { + $dropSequenceSQLs++; + } + } + $this->assertEquals(4, $dropSequenceSQLs, "Expect 4 sequences to be dropped."); + } }