Skip to content

Commit

Permalink
Drop constraint before drop column
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Aug 31, 2021
1 parent 6c432ae commit 6b8870d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
35 changes: 17 additions & 18 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ private void ProcessTrigger(IEnumerable<DbObject> sourceObjects, IEnumerable<DbO
if (_selectedObjectType.Contains(DbObjectType.Trigger))
{
(toCreate.Union(toAlter)).ToList()
.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild
.AppendLine(_schemaBuilder.Build(x.EnableObject, x.EnableObject.Enabled
? Operation.Enabled
: Operation.Disabled))
.AppendLine(_schemaBuilder.BuildSeparator()));
.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild
.AppendLine(_schemaBuilder.Build(x.EnableObject, x.EnableObject.Enabled
? Operation.Enabled
: Operation.Disabled))
.AppendLine(_schemaBuilder.BuildSeparator()));
}
}

Expand All @@ -89,16 +89,15 @@ private void ProcessUser(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObje
var toCreate = CreateDbObjectByName<User>(originDb, destinationDb, resultProcessDbObject, DbObjectType.User);
DropDbObjectByName<User>(originDb, destinationDb, resultProcessDbObject, DbObjectType.User);


if (_selectedObjectType.Contains(DbObjectType.User))
{
originDb
.Except(toCreate)
.Where(x => !destinationDb.Contains(x)) //discard object present in origin, present in destination and equals
.ToList()
.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild
.AppendLine(_schemaBuilder.Build(x, Operation.Alter))
.AppendLine(_schemaBuilder.BuildSeparator()));
.Except(toCreate)
.Where(x => !destinationDb.Contains(x)) //discard object present in origin, present in destination and equals
.ToList()
.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild
.AppendLine(_schemaBuilder.Build(x, Operation.Alter))
.AppendLine(_schemaBuilder.BuildSeparator()));
}

}
Expand Down Expand Up @@ -153,12 +152,6 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
else
{
var destinationTable = destinationDb.Single(x => x.Identifier == tableOrigin.Identifier);
if (tableOrigin != destinationTable)
{
//columns different
ProcessTableColumn(tableOrigin, destinationTable, resultProcessDbObject);
}

var constraintToCreate = CreateDbObject<Table.TableConstraint>(tableOrigin.Constraints, destinationTable.Constraints, resultProcessDbObject, DbObjectType.Table);

var constraintToDrop = DropDbObject<Table.TableConstraint>(tableOrigin.Constraints, destinationTable.Constraints)
Expand All @@ -181,6 +174,12 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
.AppendLine(_schemaBuilder.Build(x, Operation.Create))
.AppendLine(_schemaBuilder.BuildSeparator()));
}

if (tableOrigin != destinationTable)
{
//columns different
ProcessTableColumn(tableOrigin, destinationTable, resultProcessDbObject);
}
}
}
DropDbObject<Table>(originDb, destinationDb, resultProcessDbObject, DbObjectType.Table);
Expand Down
33 changes: 33 additions & 0 deletions SqlSchemaCompare.Test/TSql/TSqlTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,39 @@ ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT ((1)) FOR [col
ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT ((0)) FOR [column1]
GO
");
errors.ShouldBeEmpty();
}

[Fact]
public void DropConstraintBeforeDropColumn()
{
const string origin =
@"CREATE TABLE [dbo].[TBL] ([ID] [int] NOT NULL)
GO
";

const string destination =
@"CREATE TABLE [dbo].[TBL] (
[ID] [int] NOT NULL,
[column1] [int] NOT NULL)
GO
ALTER TABLE [dbo].[TBL] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([column1])
REFERENCES [dbo].[tbl2] ([ID])
GO
";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column });

updateSchema.ShouldBe(
@"ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [FK_constraint]
GO
ALTER TABLE [dbo].[TBL] DROP COLUMN [column1]
GO
");
errors.ShouldBeEmpty();
}
Expand Down

0 comments on commit 6b8870d

Please sign in to comment.