Commit 03be671e authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-255] Fix dropping columns with default contraints in SQLSrv

parent 0e6bb9b0
...@@ -21,6 +21,7 @@ namespace Doctrine\DBAL\Schema; ...@@ -21,6 +21,7 @@ namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Events; use Doctrine\DBAL\Events;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs; use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvException;
/** /**
* SQL Server Schema Manager * SQL Server Schema Manager
...@@ -218,8 +219,45 @@ class SQLServerSchemaManager extends AbstractSchemaManager ...@@ -218,8 +219,45 @@ class SQLServerSchemaManager extends AbstractSchemaManager
} else { } else {
throw $e; throw $e;
} }
} catch(SQLSrvException $e) {
if (strpos($e->getMessage(), 'SQLSTATE [01000, 15472]') === 0) {
return array();
} else {
throw $e;
}
} }
return $this->_getPortableTableIndexesList($tableIndexes, $table); return $this->_getPortableTableIndexesList($tableIndexes, $table);
} }
/**
* @override
*/
public function alterTable(TableDiff $tableDiff)
{
if(count($tableDiff->removedColumns) > 0) {
foreach($tableDiff->removedColumns as $col){
$columnConstraintSql = $this->getColumnConstraintSQL($tableDiff->name, $col->getName());
foreach ($this->_conn->fetchAll($columnConstraintSql) as $constraint) {
$this->_conn->exec("ALTER TABLE $tableDiff->name DROP CONSTRAINT " . $constraint['Name']);
}
}
}
return parent::alterTable($tableDiff);
}
/**
* This function retrieves the constraints for a given column.
*/
private function getColumnConstraintSQL($table, $column)
{
return "SELECT SysObjects.[Name]
FROM SysObjects INNER JOIN (SELECT [Name],[ID] FROM SysObjects WHERE XType = 'U') AS Tab
ON Tab.[ID] = Sysobjects.[Parent_Obj]
INNER JOIN sys.default_constraints DefCons ON DefCons.[object_id] = Sysobjects.[ID]
INNER JOIN SysColumns Col ON Col.[ColID] = DefCons.[parent_column_id] AND Col.[ID] = Tab.[ID]
WHERE Col.[Name] = " . $this->_conn->quote($column) ." AND Tab.[Name] = " . $this->_conn->quote($table) . "
ORDER BY Col.[Name]";
}
} }
Subproject commit 17e774007b98beb2e253e645260e0f9c32f4c936 Subproject commit 1c9d2ab329746637d1c6c058523ec1ee769da31c
...@@ -2,7 +2,11 @@ ...@@ -2,7 +2,11 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;
class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
...@@ -10,4 +14,24 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -10,4 +14,24 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
return "mssql"; return "mssql";
} }
/**
* @group DBAL-255
*/
public function testDropColumnConstraints()
{
$table = new Table('sqlsrv_drop_column');
$table->addColumn('id', 'integer');
$table->addColumn('todrop', 'decimal', array('default' => 10.2));
$this->_sm->createTable($table);
$diff = new TableDiff('sqlsrv_drop_column', array(), array(), array(
new Column('todrop', Type::getType('decimal'))
));
$this->_sm->alterTable($diff);
$columns = $this->_sm->listTableColumns('sqlsrv_drop_column');
$this->assertEquals(1, count($columns));
}
} }
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