Skip to content

Commit

Permalink
[6.x] Fix for dropping columns when using MSSQL as database (#39905)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyotaOkaniwa committed Dec 6, 2021
1 parent 5e56518 commit 2d72d1f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
Expand Up @@ -212,7 +212,7 @@ public function compileDropDefaultConstraint(Blueprint $blueprint, Fluent $comma

$sql = "DECLARE @sql NVARCHAR(MAX) = '';";
$sql .= "SELECT @sql += 'ALTER TABLE [dbo].[{$tableName}] DROP CONSTRAINT ' + OBJECT_NAME([default_object_id]) + ';' ";
$sql .= 'FROM SYS.COLUMNS ';
$sql .= 'FROM sys.columns ';
$sql .= "WHERE [object_id] = OBJECT_ID('[dbo].[{$tableName}]') AND [name] in ({$columns}) AND [default_object_id] <> 0;";
$sql .= 'EXEC(@sql)';

Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Expand Up @@ -122,7 +122,7 @@ public function testDropColumnDropsCreatesSqlToDropDefaultConstraints()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals("DECLARE @sql NVARCHAR(MAX) = '';SELECT @sql += 'ALTER TABLE [dbo].[foo] DROP CONSTRAINT ' + OBJECT_NAME([default_object_id]) + ';' FROM SYS.COLUMNS WHERE [object_id] = OBJECT_ID('[dbo].[foo]') AND [name] in ('bar') AND [default_object_id] <> 0;EXEC(@sql);alter table \"foo\" drop column \"bar\"", $statements[0]);
$this->assertEquals("DECLARE @sql NVARCHAR(MAX) = '';SELECT @sql += 'ALTER TABLE [dbo].[foo] DROP CONSTRAINT ' + OBJECT_NAME([default_object_id]) + ';' FROM sys.columns WHERE [object_id] = OBJECT_ID('[dbo].[foo]') AND [name] in ('bar') AND [default_object_id] <> 0;EXEC(@sql);alter table \"foo\" drop column \"bar\"", $statements[0]);
}

public function testDropPrimary()
Expand Down

0 comments on commit 2d72d1f

Please sign in to comment.