Skip to content

Commit

Permalink
"Remove usage of sp_MSdropconstraints
Browse files Browse the repository at this point in the history
- Remove usage of sp_MSdropconstraints and replace with logic to just delete FK constraints
- Fixes GH-118
  • Loading branch information
mysticmind committed Oct 7, 2024
1 parent 8634839 commit 0317403
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Weasel.SqlServer/Tables/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ public void WriteCreateStatement(Migrator migrator, TextWriter writer)
{
if (migrator.TableCreation == CreationStyle.DropThenCreate)
{
writer.WriteLine("EXEC sp_MSdropconstraints '{0}', '{1}';", Identifier.Name, Identifier.Schema);
// drop all FK constraints
var sqlVariableName = $"@sql_{Guid.NewGuid().ToString().ToLower().Replace("-", "_")}";
writer.WriteLine("DECLARE {0} NVARCHAR(MAX) = '';", sqlVariableName);
writer.WriteLine("SELECT {0} = {1} + 'ALTER TABLE {2} DROP CONSTRAINT ' + QUOTENAME(name) + ';'",
sqlVariableName, sqlVariableName, Identifier.QualifiedName);
writer.WriteLine("FROM sys.foreign_keys");
writer.WriteLine("WHERE referenced_object_id = OBJECT_ID('{0}');", Identifier.QualifiedName);
writer.WriteLine("EXEC sp_executesql {0}", sqlVariableName);

writer.WriteLine("DROP TABLE IF EXISTS {0};", Identifier);
writer.WriteLine("CREATE TABLE {0} (", Identifier);
}
Expand Down

0 comments on commit 0317403

Please sign in to comment.