Merge branch 'DBAL-115'
This commit is contained in:
commit
0b1077a3b3
@ -592,68 +592,35 @@ class SchemaTool
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Get SQL to drop the tables defined by the passed classes.
|
||||||
|
*
|
||||||
* @param array $classes
|
* @param array $classes
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getDropSchemaSQL(array $classes)
|
public function getDropSchemaSQL(array $classes)
|
||||||
{
|
{
|
||||||
/* @var $conn \Doctrine\DBAL\Connection */
|
$visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->_platform);
|
||||||
$conn = $this->_em->getConnection();
|
$schema = $this->getSchemaFromMetadata($classes);
|
||||||
|
|
||||||
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
|
|
||||||
$sm = $conn->getSchemaManager();
|
|
||||||
|
|
||||||
$sql = array();
|
|
||||||
$orderedTables = array();
|
|
||||||
|
|
||||||
foreach ($classes AS $class) {
|
$sm = $this->_em->getConnection()->getSchemaManager();
|
||||||
if ($class->isIdGeneratorSequence() && !$class->isMappedSuperclass && $class->name == $class->rootEntityName && $this->_platform->supportsSequences()) {
|
$fullSchema = $sm->createSchema();
|
||||||
$sql[] = $this->_platform->getDropSequenceSQL($class->sequenceGeneratorDefinition['sequenceName']);
|
foreach ($fullSchema->getTables() AS $table) {
|
||||||
}
|
if (!$schema->hasTable($table->getName())) {
|
||||||
}
|
foreach ($table->getForeignKeys() AS $foreignKey) {
|
||||||
|
/* @var $foreignKey \Doctrine\DBAL\Schema\ForeignKeyConstraint */
|
||||||
$commitOrder = $this->_getCommitOrder($classes);
|
if ($schema->hasTable($foreignKey->getForeignTableName())) {
|
||||||
$associationTables = $this->_getAssociationTables($commitOrder);
|
$visitor->acceptForeignKey($table, $foreignKey);
|
||||||
|
}
|
||||||
// Drop association tables first
|
}
|
||||||
foreach ($associationTables as $quotedAssociationTableName) {
|
} else {
|
||||||
// avoid adding duplicates
|
$visitor->acceptTable($table);
|
||||||
if (!in_array($quotedAssociationTableName, $orderedTables)) {
|
foreach ($table->getForeignKeys() AS $foreignKey) {
|
||||||
$orderedTables[] = $quotedAssociationTableName;
|
$visitor->acceptForeignKey($table, $foreignKey);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop tables in reverse commit order
|
|
||||||
for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
|
|
||||||
$class = $commitOrder[$i];
|
|
||||||
|
|
||||||
if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
|
|
||||||
|| $class->isMappedSuperclass) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!in_array($class->getTableName(), $orderedTables)) {
|
|
||||||
$orderedTables[] = $class->getQuotedTableName($this->_platform);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$supportsForeignKeyConstraints = $conn->getDatabasePlatform()->supportsForeignKeyConstraints();
|
|
||||||
$dropTablesSql = array();
|
|
||||||
|
|
||||||
foreach ($orderedTables AS $tableName) {
|
|
||||||
if ($supportsForeignKeyConstraints) {
|
|
||||||
$foreignKeys = $sm->listTableForeignKeys($tableName);
|
|
||||||
|
|
||||||
foreach ($foreignKeys AS $foreignKey) {
|
|
||||||
$sql[] = $this->_platform->getDropForeignKeySQL($foreignKey, $tableName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$dropTablesSql[] = $this->_platform->getDropTableSQL($tableName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_merge($sql, $dropTablesSql);
|
return $visitor->getQueries();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -701,45 +668,4 @@ class SchemaTool
|
|||||||
return $schemaDiff->toSql($this->_platform);
|
return $schemaDiff->toSql($this->_platform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _getCommitOrder(array $classes)
|
|
||||||
{
|
|
||||||
$calc = new CommitOrderCalculator;
|
|
||||||
|
|
||||||
// Calculate dependencies
|
|
||||||
foreach ($classes as $class) {
|
|
||||||
$calc->addClass($class);
|
|
||||||
|
|
||||||
foreach ($class->associationMappings as $assoc) {
|
|
||||||
if ($assoc['isOwningSide']) {
|
|
||||||
$targetClass = $this->_em->getClassMetadata($assoc['targetEntity']);
|
|
||||||
|
|
||||||
if ( ! $calc->hasClass($targetClass->name)) {
|
|
||||||
$calc->addClass($targetClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add dependency ($targetClass before $class)
|
|
||||||
$calc->addDependency($targetClass, $class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $calc->getCommitOrder();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function _getAssociationTables(array $classes)
|
|
||||||
{
|
|
||||||
$associationTables = array();
|
|
||||||
|
|
||||||
foreach ($classes as $class) {
|
|
||||||
/* @var $class \Doctrine\ORM\Mapping\ClassMetadata */
|
|
||||||
foreach ($class->associationMappings as $assoc) {
|
|
||||||
if ($assoc['isOwningSide'] && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
|
|
||||||
$associationTables[] = $class->getQuotedJoinTableName($assoc, $this->_platform);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $associationTables;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -50,4 +50,19 @@ class CompanySchemaTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$this->assertFalse($table->getColumn('pricePerHour')->getNotnull());
|
$this->assertFalse($table->getColumn('pricePerHour')->getNotnull());
|
||||||
$this->assertFalse($table->getColumn('maxPrice')->getNotnull());
|
$this->assertFalse($table->getColumn('maxPrice')->getNotnull());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DBAL-115
|
||||||
|
*/
|
||||||
|
public function testDropPartSchemaWithForeignKeys()
|
||||||
|
{
|
||||||
|
if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
||||||
|
$this->markTestSkipped("Foreign Key test");
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = $this->_schemaTool->getDropSchemaSQL(array(
|
||||||
|
$this->_em->getClassMetadata('Doctrine\Tests\Models\Company\CompanyManager'),
|
||||||
|
));
|
||||||
|
$this->assertEquals(3, count($sql));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user