diff --git a/SqlSchemaCompare.Core/UpdateSchemaManager.cs b/SqlSchemaCompare.Core/UpdateSchemaManager.cs index 40b44a3..c826ba7 100644 --- a/SqlSchemaCompare.Core/UpdateSchemaManager.cs +++ b/SqlSchemaCompare.Core/UpdateSchemaManager.cs @@ -73,11 +73,11 @@ private void ProcessTrigger(IEnumerable sourceObjects, IEnumerable 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())); } } @@ -89,16 +89,15 @@ private void ProcessUser(IEnumerable sourceObjects, IEnumerable(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); DropDbObjectByName(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())); } } @@ -153,12 +152,6 @@ private void ProcessTable(IEnumerable sourceObjects, IEnumerable x.Identifier == tableOrigin.Identifier); - if (tableOrigin != destinationTable) - { - //columns different - ProcessTableColumn(tableOrigin, destinationTable, resultProcessDbObject); - } - var constraintToCreate = CreateDbObject(tableOrigin.Constraints, destinationTable.Constraints, resultProcessDbObject, DbObjectType.Table); var constraintToDrop = DropDbObject(tableOrigin.Constraints, destinationTable.Constraints) @@ -181,6 +174,12 @@ private void ProcessTable(IEnumerable sourceObjects, IEnumerable(originDb, destinationDb, resultProcessDbObject, DbObjectType.Table); diff --git a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs index da85d07..ebbb72d 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs @@ -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(); }