Commit 888968bb authored by Benjamin Eberlei's avatar Benjamin Eberlei

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

parent c5e7533e
......@@ -213,4 +213,35 @@ class SQLServerSchemaManager extends AbstractSchemaManager
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 14eb4d62b6e0a87cd99200078c38644bbd216e42
Subproject commit 17e774007b98beb2e253e645260e0f9c32f4c936
......@@ -2,7 +2,11 @@
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
{
......@@ -10,4 +14,24 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
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