From 8ae1f6754771c9cb4f8cc5fb5045812224d59bf3 Mon Sep 17 00:00:00 2001 From: Alessandro Magistroni <65421435+AMagistroni@users.noreply.github.com> Date: Thu, 2 Sep 2021 14:03:26 +0200 Subject: [PATCH] Refactor update schema manager. Add order by db object --- .../DbStructures/Operation.cs | 3 +- SqlSchemaCompare.Core/DbStructures/Table.cs | 9 +- .../TSql/Factory/TSqlTableFactory.cs | 2 +- .../TSql/TSqlResultProcessDbObject.cs | 48 +-- .../TSql/TSqlSchemaBuilder.cs | 57 ++- SqlSchemaCompare.Core/UpdateSchemaManager.cs | 331 +++++++----------- SqlSchemaCompare.Test/TSql/IndexTest.cs | 2 +- SqlSchemaCompare.Test/TSql/MemberTest.cs | 2 +- SqlSchemaCompare.Test/TSql/RoleTest.cs | 2 +- .../TSql/TSqlFunctionTest.cs | 2 +- SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs | 2 +- .../TSql/TSqlStoreProcedureTest.cs | 3 +- SqlSchemaCompare.Test/TSql/TSqlTableTest.cs | 101 ++++-- SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs | 8 +- SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs | 4 +- SqlSchemaCompare.Test/TSql/TSqlViewTest.cs | 2 +- SqlSchemaCompare.Test/TSql/UserTest.cs | 2 +- .../TestDbObjectGenerator.cs | 4 +- SqlSchemaCompare.WindowsForm/MainForm.cs | 1 - .../SqlSchemaCompare.WindowsForm.csproj | 2 +- 20 files changed, 267 insertions(+), 320 deletions(-) diff --git a/SqlSchemaCompare.Core/DbStructures/Operation.cs b/SqlSchemaCompare.Core/DbStructures/Operation.cs index bb67a09..c71c50c 100644 --- a/SqlSchemaCompare.Core/DbStructures/Operation.cs +++ b/SqlSchemaCompare.Core/DbStructures/Operation.cs @@ -6,7 +6,6 @@ public enum Operation Alter, Drop, Enabled, - Disabled, - Add + Disabled } } diff --git a/SqlSchemaCompare.Core/DbStructures/Table.cs b/SqlSchemaCompare.Core/DbStructures/Table.cs index 2e40d0f..14a9cef 100644 --- a/SqlSchemaCompare.Core/DbStructures/Table.cs +++ b/SqlSchemaCompare.Core/DbStructures/Table.cs @@ -9,14 +9,9 @@ public class Column : DbObject { public override DbObjectType DbObjectType => DbObjectType.Column; } public class TableConstraint : DbObject - { - public enum TableConstraintType - { - DefaultWithName, - DefaultWithoutName - } + { public override DbObjectType DbObjectType => DbObjectType.TableContraint; - public TableConstraintType ConstraintType { get; set; } + public string ColumnNameForeighKey { get; set; } } public IList Columns { get; } = new List(); public IList Constraints { get; } = new List(); diff --git a/SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs b/SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs index 60705c2..de2c7d7 100644 --- a/SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs +++ b/SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs @@ -69,7 +69,7 @@ public Table.TableConstraint CreateAlterTable(ParserRuleContext context) Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)), Name = name, ParentName = tableName, - ConstraintType = name == string.Empty ? Table.TableConstraint.TableConstraintType.DefaultWithoutName : Table.TableConstraint.TableConstraintType.DefaultWithName + ColumnNameForeighKey = alterTableContext.fk?.GetText() }; } } diff --git a/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs b/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs index b239c56..2eb7933 100644 --- a/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs +++ b/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs @@ -1,51 +1,29 @@ using SqlSchemaCompare.Core.DbStructures; using System.Collections.Generic; -using System.Text; +using System.Linq; namespace SqlSchemaCompare.Core.TSql { public class TSqlResultProcessDbObject { - - public StringBuilder UpdateSchemaStringBuild { get; set; } = new(); - public List ToDrop { get; private set; } = new(); - public List ToAlter { get; private set; } = new(); - public List ToCreate { get; private set; } = new(); - public List ToEnable { get; private set; } = new(); - public List ToDisable { get; private set; } = new(); - - public void AddToAlter(IList dbObjects) where T: DbObject - { - ToAlter.AddRange(dbObjects); - } - public void AddToCreate(IList dbObjects) where T : DbObject - { - ToCreate.AddRange(dbObjects); - } - public void AddToDrop(IList dbObjects) where T : DbObject + public class OperationOnDbObject { - ToDrop.AddRange(dbObjects); + public DbObject DbObject { get; init; } + public Operation Operation { get; init; } } - - public void AddToAlter(T dbObjects) where T : DbObject - { - ToAlter.Add(dbObjects); - } - public void AddToCreate(T dbObjects) where T : DbObject + public List OperationsOnDbObject { get; private set; } = new(); + public void AddOperation(DbObject dbObjects, Operation operation) where T : DbObject { - ToCreate.Add(dbObjects); + OperationsOnDbObject.Add(new OperationOnDbObject { DbObject = dbObjects, Operation = operation } ); } - public void AddToDrop(T dbObjects) where T : DbObject + public void AddOperation(IList dbObjects, Operation operation) where T : DbObject { - ToDrop.Add(dbObjects); - } - public void AddToEnable(T dbObjects) where T : DbObject - { - ToEnable.Add(dbObjects); - } - public void AddToDisable(T dbObjects) where T : DbObject + dbObjects.ToList().ForEach(x => AddOperation(x, operation)); + } + + public IEnumerable GetDbObject(DbObjectType dbObjectType, Operation operation) { - ToDisable.Add(dbObjects); + return OperationsOnDbObject.Where(x => x.DbObject.DbObjectType == dbObjectType && x.Operation == operation).Select(x => x.DbObject); } } } \ No newline at end of file diff --git a/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs b/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs index ce708f0..fb2c5b7 100644 --- a/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs +++ b/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs @@ -10,39 +10,24 @@ public class TSqlSchemaBuilder : ISchemaBuilder { public string Build(DbObject dbObject, Operation operation) { - switch (dbObject.DbObjectType) + return dbObject.DbObjectType switch { - case DbObjectType.Table: - return BuildCreateDropTable(dbObject as Table, operation); - case DbObjectType.TableContraint: - return BuildTableConstraint(dbObject as Table.TableConstraint, operation); - case DbObjectType.Column: - return BuildAlterTableElement("COLUMN", dbObject as Table.Column, operation); - case DbObjectType.View: - return BuildView(dbObject as View, operation); - case DbObjectType.StoreProcedure: - return BuildStoreProcedure(dbObject as StoreProcedure, operation); - case DbObjectType.Function: - return BuildFunction(dbObject as Function, operation); - case DbObjectType.Schema: - return BuildCreateDrop(dbObject as Schema, "SCHEMA", operation); - case DbObjectType.Trigger: - return BuildTrigger(dbObject as Trigger, operation); - case DbObjectType.User: - return BuildUser(dbObject as User, operation); - case DbObjectType.Role: - return BuildCreateDrop(dbObject as Role, "ROLE", operation); - case DbObjectType.Type: - return BuildCreateDrop(dbObject as TypeDbObject, "TYPE", operation); - case DbObjectType.Index: - return BuildIndex(dbObject as Index, operation); - case DbObjectType.Member: - return BuildMember(dbObject as Member, operation); - case DbObjectType.EnableTrigger: - return BuildEnableTrigger(dbObject as Trigger.EnabledDbObject, operation); - default: - throw new NotImplementedException(); - } + DbObjectType.Table => BuildCreateDropTable(dbObject as Table, operation), + DbObjectType.TableContraint => BuildTableConstraint(dbObject as Table.TableConstraint, operation), + DbObjectType.Column => BuildColumn(dbObject as Table.Column, operation), + DbObjectType.View => BuildView(dbObject as View, operation), + DbObjectType.StoreProcedure => BuildStoreProcedure(dbObject as StoreProcedure, operation), + DbObjectType.Function => BuildFunction(dbObject as Function, operation), + DbObjectType.Schema => BuildCreateDrop(dbObject as Schema, "SCHEMA", operation), + DbObjectType.Trigger => BuildTrigger(dbObject as Trigger, operation), + DbObjectType.User => BuildUser(dbObject as User, operation), + DbObjectType.Role => BuildCreateDrop(dbObject as Role, "ROLE", operation), + DbObjectType.Type => BuildCreateDrop(dbObject as TypeDbObject, "TYPE", operation), + DbObjectType.Index => BuildIndex(dbObject as Index, operation), + DbObjectType.Member => BuildMember(dbObject as Member, operation), + DbObjectType.EnableTrigger => BuildEnableTrigger(dbObject as Trigger.EnabledDbObject, operation), + _ => throw new NotImplementedException(), + }; } public string GetStartCommentInLine() @@ -82,13 +67,13 @@ private string BuildMember(Member member, Operation operation) }; } - private string BuildAlterTableElement(String itemName, DbObject dbObject, Operation operation) + private string BuildColumn(DbObject dbObject, Operation operation) { return operation switch { - Operation.Create => $"ALTER TABLE {dbObject.ParentName} ADD {itemName} {dbObject.Sql}", - Operation.Drop => $"ALTER TABLE {dbObject.ParentName} DROP {itemName} {dbObject.Name}", - Operation.Alter => $"ALTER TABLE {dbObject.ParentName} ALTER {itemName} {dbObject.Sql}", + Operation.Create => $"ALTER TABLE {dbObject.ParentName} ADD {dbObject.Sql}", + Operation.Drop => $"ALTER TABLE {dbObject.ParentName} DROP COLUMN {dbObject.Name}", + Operation.Alter => $"ALTER TABLE {dbObject.ParentName} ALTER COLUMN {dbObject.Sql}", _ => throw new NotSupportedException("Alter not supported on schema"), }; } diff --git a/SqlSchemaCompare.Core/UpdateSchemaManager.cs b/SqlSchemaCompare.Core/UpdateSchemaManager.cs index d2f5432..5613688 100644 --- a/SqlSchemaCompare.Core/UpdateSchemaManager.cs +++ b/SqlSchemaCompare.Core/UpdateSchemaManager.cs @@ -15,6 +15,56 @@ public UpdateSchemaManager(ISchemaBuilder schemaBuilder) { _schemaBuilder = schemaBuilder; } + + private List<(DbObjectType DbObjectType, Operation Operation)> OrderItemSchema => new() + { + ( DbObjectType.User , Operation.Create ), + ( DbObjectType.User, Operation.Drop ), + ( DbObjectType.User, Operation.Alter ), + + ( DbObjectType.Role, Operation.Create ), + ( DbObjectType.Role, Operation.Drop ), + + ( DbObjectType.Member, Operation.Create ), + ( DbObjectType.Member, Operation.Drop ), + + ( DbObjectType.Schema, Operation.Create ), + + ( DbObjectType.Table, Operation.Create ), + ( DbObjectType.TableContraint, Operation.Drop), + ( DbObjectType.Column, Operation.Create), + ( DbObjectType.Column, Operation.Drop ), + ( DbObjectType.Column, Operation.Alter ), + ( DbObjectType.TableContraint, Operation.Create), + ( DbObjectType.Table, Operation.Drop ), + + ( DbObjectType.StoreProcedure, Operation.Create ), + ( DbObjectType.StoreProcedure, Operation.Drop ), + ( DbObjectType.StoreProcedure, Operation.Alter ), + + ( DbObjectType.Function, Operation.Create ), + ( DbObjectType.Function, Operation.Drop ), + ( DbObjectType.Function, Operation.Alter ), + + ( DbObjectType.View, Operation.Create ), + ( DbObjectType.View, Operation.Drop ), + ( DbObjectType.View, Operation.Alter ), + + ( DbObjectType.Trigger, Operation.Drop ), + ( DbObjectType.Trigger, Operation.Alter ), + ( DbObjectType.Trigger, Operation.Create), + ( DbObjectType.EnableTrigger, Operation.Enabled ), + ( DbObjectType.EnableTrigger, Operation.Disabled ), + + ( DbObjectType.Type, Operation.Drop ), + ( DbObjectType.Type, Operation.Create ), + + ( DbObjectType.Index, Operation.Drop ), + ( DbObjectType.Index, Operation.Create ), + + ( DbObjectType.Schema, Operation.Drop ), + }; + public string UpdateSchema(IEnumerable sourceObjects, IEnumerable destinationObjects, IEnumerable selectedObjectType) { _selectedObjectType = selectedObjectType; @@ -23,7 +73,7 @@ public string UpdateSchema(IEnumerable sourceObjects, IEnumerable(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.StoreProcedure); ProcessGenericDbObject(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Function); @@ -31,23 +81,39 @@ public string UpdateSchema(IEnumerable sourceObjects, IEnumerable(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Type); ProcdessIndex(sourceObjects, destinationObjects, resultProcessDbObject); - ProcessSchema(sourceObjects, destinationObjects, Operation.Drop, resultProcessDbObject); - if (resultProcessDbObject.UpdateSchemaStringBuild.Length == 0) - { - return string.Empty; - } - else - { + StringBuilder updateSchemaStringBuild = new(); + if (resultProcessDbObject.OperationsOnDbObject.Any()) + { var destinationDb = destinationObjects.OfType(); - StringBuilder useDb = new(); if (destinationDb.Any()) { - useDb.AppendLine(_schemaBuilder.BuildUse(destinationDb.First().Name)); - useDb.AppendLine(_schemaBuilder.BuildSeparator()); + updateSchemaStringBuild.AppendLine(_schemaBuilder.BuildUse(destinationDb.First().Name)); + updateSchemaStringBuild.AppendLine(_schemaBuilder.BuildSeparator()); } - return $"{useDb}{resultProcessDbObject.UpdateSchemaStringBuild}"; + + foreach (var objectToWrite in OrderItemSchema) + { + if (_selectedObjectType.Contains(objectToWrite.DbObjectType)) + { + var dbObjects = objectToWrite.Operation switch + { + Operation.Alter => resultProcessDbObject.GetDbObject(objectToWrite.DbObjectType, Operation.Alter), + Operation.Create => resultProcessDbObject.GetDbObject(objectToWrite.DbObjectType, Operation.Create), + Operation.Disabled => resultProcessDbObject.GetDbObject(objectToWrite.DbObjectType, Operation.Disabled), + Operation.Enabled => resultProcessDbObject.GetDbObject(objectToWrite.DbObjectType, Operation.Enabled), + Operation.Drop => resultProcessDbObject.GetDbObject(objectToWrite.DbObjectType, Operation.Drop), + _ => throw new System.NotImplementedException(), + }; + + dbObjects.ToList() + .ForEach(x => updateSchemaStringBuild + .AppendLine(_schemaBuilder.Build(x, objectToWrite.Operation)) + .AppendLine(_schemaBuilder.BuildSeparator())); + } + } } + return updateSchemaStringBuild.ToString(); } private void ProcessMember(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) @@ -55,33 +121,23 @@ private void ProcessMember(IEnumerable sourceObjects, IEnumerable(); var destinationDb = destinationObjects.OfType(); - var roleNameDropped = resultProcessDbObject.ToDrop.OfType().Select(x => x.Name); + var roleNameDropped = resultProcessDbObject.GetDbObject(DbObjectType.Role, Operation.Drop).Select(x => x.Name); destinationDb = destinationDb.Except(destinationDb.Where(x => roleNameDropped.Contains(x.RoleName))); - CreateDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Member); - DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Member); + CreateDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Member); + DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Member); } private void ProcessTrigger(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) { - (var toCreate, var toAlter, var toDrop) = ProcessGenericDbObject(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Trigger); + (var toCreate, var toAlter, _) = ProcessGenericDbObject(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Trigger); foreach (var trigger in toCreate.Union(toAlter)) { if (trigger.EnableObject.Enabled) - resultProcessDbObject.AddToEnable(trigger); + resultProcessDbObject.AddOperation(trigger.EnableObject, Operation.Enabled); else - resultProcessDbObject.AddToDisable(trigger); - } - - 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())); + resultProcessDbObject.AddOperation(trigger.EnableObject, Operation.Disabled); } } @@ -90,23 +146,14 @@ private void ProcessUser(IEnumerable sourceObjects, IEnumerable(); var destinationDb = destinationObjects.OfType(); - var toCreate = CreateDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); - DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); + var toCreate = CreateDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); + DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); var toAlter = originDb .Except(toCreate) .Where(x => !destinationDb.Contains(x)) //discard object present in origin, present in destination and equals .ToList(); - resultProcessDbObject.AddToCreate(toAlter); - - if (_selectedObjectType.Contains(DbObjectType.User)) - { - toAlter - .ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Alter)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } - + resultProcessDbObject.AddOperation(toAlter, Operation.Alter); } private void ProcessRole(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) @@ -114,19 +161,17 @@ private void ProcessRole(IEnumerable sourceObjects, IEnumerable(); var destinationDb = destinationObjects.OfType(); - CreateDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Role); - DropDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Role); + CreateDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Role); + DropDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Role); } - private void ProcessSchema(IEnumerable sourceObjects, IEnumerable destinationObjects, Operation operation, TSqlResultProcessDbObject resultProcessDbObject) + private void ProcessSchema(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) { var originDb = sourceObjects.OfType(); var destinationDb = destinationObjects.OfType(); - - if (operation == Operation.Create) - CreateDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Schema); - else - DropDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Schema); + + CreateDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Schema); + DropDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Schema); } private void ProcessTable(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) @@ -139,67 +184,36 @@ private void ProcessTable(IEnumerable sourceObjects, IEnumerable - resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } - - resultProcessDbObject.AddToCreate(tableOrigin); + resultProcessDbObject.AddOperation(tableOrigin.Constraints, Operation.Create); + resultProcessDbObject.AddOperation(tableOrigin, Operation.Create); } else { var destinationTable = destinationDb.Single(x => x.Identifier == tableOrigin.Identifier); - var constraintToCreate = CreateDbObject(tableOrigin.Constraints, destinationTable.Constraints, resultProcessDbObject, DbObjectType.Table); + var constraintToCreate = CreateDbObject(tableOrigin.Constraints, destinationTable.Constraints, resultProcessDbObject, DbObjectType.Table); - var constraintToDrop = DropDbObject(tableOrigin.Constraints, destinationTable.Constraints) + var constraintToDrop = DropDbObject(tableOrigin.Constraints, destinationTable.Constraints) .GroupBy(x => x.Name) .Select(x => x.First()).ToList(); - var toAlter = tableOrigin.Constraints + var constraintsToAlter = tableOrigin.Constraints .Except(constraintToCreate) .Where(x => !destinationTable.Constraints.Contains(x)).ToList(); //discard object present in origin, present in destination and equals - resultProcessDbObject.AddToDrop(constraintToDrop); - resultProcessDbObject.AddToAlter(toAlter); - - if (_selectedObjectType.Contains(DbObjectType.Table)) - { - constraintToDrop.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator())); - - toAlter.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator()) - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } + resultProcessDbObject.AddOperation(constraintToDrop, Operation.Drop); + resultProcessDbObject.AddOperation(constraintsToAlter, Operation.Drop); + resultProcessDbObject.AddOperation(constraintsToAlter, Operation.Create); if (tableOrigin != destinationTable) { //columns different - ProcessTableColumn(tableOrigin, destinationTable, resultProcessDbObject); + ProcessTableColumn(tableOrigin, destinationTable, destinationObjects, resultProcessDbObject); } } } - DropDbObject
(originDb, destinationDb, resultProcessDbObject, DbObjectType.Table); + DropDbObject(originDb, destinationDb, resultProcessDbObject, DbObjectType.Table); } - - private void ProcessTableColumn(Table table, Table destinationTable, TSqlResultProcessDbObject resultProcessDbObject) + private void ProcessTableColumn(Table table, Table destinationTable, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) { IList columnsToAdd; IList columnsToDrop; @@ -221,32 +235,19 @@ private void ProcessTableColumn(Table table, Table destinationTable, TSqlResultP columnsToAlter = table.Columns .Except(columnsToAdd) - .Where(x => !destinationTable.Columns.Contains(x)).ToList(); //discard object present in origin, present in destination and equals + .Where(x => !destinationTable.Columns.Contains(x)).ToList(); - resultProcessDbObject.AddToCreate(columnsToAdd); - resultProcessDbObject.AddToAlter(columnsToAlter); - resultProcessDbObject.AddToDrop(columnsToDrop); + resultProcessDbObject.AddOperation(columnsToAdd, Operation.Create); + resultProcessDbObject.AddOperation(columnsToAlter, Operation.Alter); + resultProcessDbObject.AddOperation(columnsToDrop, Operation.Drop); - if (_selectedObjectType.Contains(DbObjectType.Column)) - { - columnsToAdd - .ToList() - .ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - - columnsToDrop - .ToList() - .ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator())); - - columnsToAlter - .ToList() - .ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Alter)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } + var columnsOfCostraintToDropAndCreate = destinationTable + .Constraints.Select(x => x.ColumnNameForeighKey) + .Intersect(columnsToAlter.Select(x => x.Name)); + + var constrainttoDropAndCreate = destinationTable.Constraints.Where(x => columnsOfCostraintToDropAndCreate.Contains(x.ColumnNameForeighKey)).ToList(); + resultProcessDbObject.AddOperation(constrainttoDropAndCreate, Operation.Drop); + resultProcessDbObject.AddOperation(constrainttoDropAndCreate, Operation.Create); } private void ProcdessIndex(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject) @@ -254,25 +255,18 @@ private void ProcdessIndex(IEnumerable sourceObjects, IEnumerable(); var destinationDb = destinationObjects.OfType(); - var tableNameToDrop = resultProcessDbObject.ToDrop.OfType
().Select(x => x.Identifier); + var tableNameToDrop = resultProcessDbObject.GetDbObject(DbObjectType.Table, Operation.Drop).Select(x => x.Identifier); destinationDb = destinationDb.Except(destinationDb.Where(x => tableNameToDrop.Contains(x.TableName))); - var toCreate = CreateDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Index); - DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Index); + var toCreate = CreateDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Index); + DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.Index); var toAlter = originDb .Except(toCreate) .Where(x => !destinationDb.Contains(x)).ToList(); //discard object present in origin, present in destination and equals - resultProcessDbObject.AddToAlter(toAlter); - - if (_selectedObjectType.Contains(DbObjectType.Index)) - { - toAlter.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator()) - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } + + resultProcessDbObject.AddOperation(toAlter, Operation.Drop); + resultProcessDbObject.AddOperation(toAlter, Operation.Create); } private void ProcessDbObjectWithoutAlter(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T: DbObject @@ -287,16 +281,8 @@ private void ProcessDbObjectWithoutAlter(IEnumerable sourceObjects, .Except(toCreate) .Where(x => !destinationDb.Contains(x)).ToList(); //discard object present in origin, present in destination and equals - resultProcessDbObject.AddToAlter(toAlter); - - if (_selectedObjectType.Contains(dbObjectType)) - { - toAlter.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator()) - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } + resultProcessDbObject.AddOperation(toAlter, Operation.Drop); + resultProcessDbObject.AddOperation(toAlter, Operation.Create); } private (List toCreate, List toAlter, List toDrop) ProcessGenericDbObject(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) @@ -312,72 +298,36 @@ private void ProcessDbObjectWithoutAlter(IEnumerable sourceObjects, .Except(toCreate) .Where(x => !destinationDb.Contains(x)).ToList(); //discard object present in origin, present in destination and equals - if (_selectedObjectType.Contains(dbObjectType)) - { - toAlter.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Alter)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } - - resultProcessDbObject.AddToAlter(toAlter); + resultProcessDbObject.AddOperation(toAlter, Operation.Alter); return (toCreate, toAlter, toDrop); } private List CreateDbObject(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject { - var toCreate = CreateDbObject(sourceObjects, destinationObjects); - resultProcessDbObject.AddToCreate(toCreate); - - if (_selectedObjectType.Contains(dbObjectType)) - { - toCreate.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } - return toCreate; - } - private List CreateDbObject(IEnumerable sourceObjects, IEnumerable destinationObjects) where T : DbObject - { - return sourceObjects + var toCreate = sourceObjects .Where(dbObject => sourceObjects .Select(x => x.Identifier) .Except(destinationObjects.Select(x => x.Identifier)) //looking for complete name present in origin and absent from destination .OrderBy(x => x).ToList() // list of completeName to be created .Contains(dbObject.Identifier)).ToList(); // object with completeName - } - private List CreateDbObjectByName(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject - { - var toCreate = CreateDbObjectByName(sourceObjects, destinationObjects); - resultProcessDbObject.AddToCreate(toCreate); - - if (_selectedObjectType.Contains(dbObjectType)) - { - toCreate.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } + resultProcessDbObject.AddOperation(toCreate, Operation.Create); return toCreate; - } - private List CreateDbObjectByName(IEnumerable sourceObjects, IEnumerable destinationObjects) where T : DbObject + } + private List CreateDbObjectByName(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject { - return sourceObjects + var toCreate = sourceObjects .Where(dbObject => sourceObjects .Select(x => x.Name) .Except(destinationObjects.Select(x => x.Name)) //looking for complete name present in origin and absent from destination .OrderBy(x => x).ToList() // list of completeName to be created .Contains(dbObject.Name)).ToList(); // object with completeName + resultProcessDbObject.AddOperation(toCreate, Operation.Create); + return toCreate; } private List DropDbObject(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject { var toDrop = DropDbObject(sourceObjects, destinationObjects); - resultProcessDbObject.AddToDrop(toDrop); - - if (_selectedObjectType.Contains(dbObjectType)) - { - toDrop.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } + resultProcessDbObject.AddOperation(toDrop, Operation.Drop); return toDrop; } private List DropDbObject(IEnumerable sourceObjects, IEnumerable destinationObjects) where T : DbObject @@ -392,25 +342,14 @@ private List DropDbObject(IEnumerable sourceObjects, IEnumerable des } private void DropDbObjectByName(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject { - var toDrop = DropDbObjectByName(sourceObjects, destinationObjects); - resultProcessDbObject.AddToDrop(toDrop); - - if (_selectedObjectType.Contains(dbObjectType)) - { - toDrop.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Drop)) - .AppendLine(_schemaBuilder.BuildSeparator())); - } - } - private List DropDbObjectByName(IEnumerable sourceObjects, IEnumerable destinationObjects) where T : DbObject - { - return destinationObjects + var toDrop = destinationObjects .Where(dbObject => destinationObjects .Select(x => x.Name) .Except(sourceObjects.Select(x => x.Name)) //looking for complete name present in destination and absent from origin .OrderBy(x => x).ToList() // list of completeName to be dropped .Contains(dbObject.Name)) // object with completeName to be dropped .ToList(); - } + resultProcessDbObject.AddOperation(toDrop, Operation.Drop); + } } } diff --git a/SqlSchemaCompare.Test/TSql/IndexTest.cs b/SqlSchemaCompare.Test/TSql/IndexTest.cs index 01d820a..67136d6 100644 --- a/SqlSchemaCompare.Test/TSql/IndexTest.cs +++ b/SqlSchemaCompare.Test/TSql/IndexTest.cs @@ -160,7 +160,7 @@ [ID] ASC } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Index, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Index }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select Index db object, update schema is created without Index diff --git a/SqlSchemaCompare.Test/TSql/MemberTest.cs b/SqlSchemaCompare.Test/TSql/MemberTest.cs index 25b7373..134fcf0 100644 --- a/SqlSchemaCompare.Test/TSql/MemberTest.cs +++ b/SqlSchemaCompare.Test/TSql/MemberTest.cs @@ -78,7 +78,7 @@ public void UpdateSchemaDropDbObject() } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Member, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Member }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select Member db object, update schema is created without Member diff --git a/SqlSchemaCompare.Test/TSql/RoleTest.cs b/SqlSchemaCompare.Test/TSql/RoleTest.cs index 2a31060..e3ab74b 100644 --- a/SqlSchemaCompare.Test/TSql/RoleTest.cs +++ b/SqlSchemaCompare.Test/TSql/RoleTest.cs @@ -83,7 +83,7 @@ ALTER ROLE [role] ADD MEMBER [user] } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Role, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Role }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select role db object, update schema is created without role diff --git a/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs b/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs index 07c67ba..f7925e6 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs @@ -155,7 +155,7 @@ RETURN 3 } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Function, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Function }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select Function db object, update schema is created without Function diff --git a/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs b/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs index 4b92c20..6abd38c 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs @@ -77,7 +77,7 @@ public void UpdateSchemaDropDbObject() } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Schema, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Schema }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select schema db object, update schema is created without schema diff --git a/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs b/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs index ee852cd..23a9ec0 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs @@ -33,7 +33,6 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; const string origin = @"CREATE PROCEDURE [dbo].[proc] @@ -178,7 +177,7 @@ public void CreateStoreProcedureWithTableInside() } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.StoreProcedure, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.StoreProcedure } , MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select StoreProcedure db object, update schema is created without StoreProcedure diff --git a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs index 84f5d9b..7afee38 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs @@ -16,40 +16,40 @@ public void CreateTable() [Id] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [db1]"; - var sqlTable = $@"CREATE TABLE [schema].[TableName]( + var sqlTable = $@"CREATE TABLE [schema].[TableName]( [Id] [int] IDENTITY(1,1) NOT NULL, [col1] [char](8) NULL, {constraint} ) ON [db1]"; - var sql = $@"{sqlTable} + var sql = $@"{sqlTable} GO"; - var objectFactory = new TSqlObjectFactory(); + var objectFactory = new TSqlObjectFactory(); (var dbObjects, var errors) = objectFactory.CreateObjectsForUpdateOperation(sql); - var table = dbObjects.Single() as Table; + var table = dbObjects.Single() as Table; - table.Name.ShouldBe("[TableName]"); - table.Schema.ShouldBe("[schema]"); - table.Sql.ShouldBe(sqlTable); + table.Name.ShouldBe("[TableName]"); + table.Schema.ShouldBe("[schema]"); + table.Sql.ShouldBe(sqlTable); - var column1 = table.Columns.ElementAt(0); - column1.Name.ShouldBe("[Id]"); - column1.Sql.ShouldBe("[Id] [int] IDENTITY(1,1) NOT NULL"); + var column1 = table.Columns.ElementAt(0); + column1.Name.ShouldBe("[Id]"); + column1.Sql.ShouldBe("[Id] [int] IDENTITY(1,1) NOT NULL"); - var column2 = table.Columns.ElementAt(1); - column2.Name.ShouldBe("[col1]"); - column2.Sql.ShouldBe("[col1] [char](8) NULL"); + var column2 = table.Columns.ElementAt(1); + column2.Name.ShouldBe("[col1]"); + column2.Sql.ShouldBe("[col1] [char](8) NULL"); - table.Constraint.ShouldBe(constraint); + table.Constraint.ShouldBe(constraint); errors.Count().ShouldBe(0); - } + } [Fact] public void EqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - + const string origin = @"CREATE TABLE [dbo].[TBL] ( [ID] [int] IDENTITY(1,1) NOT NULL, @@ -127,7 +127,7 @@ ALTER TABLE [dbo].[TBL] CHECK CONSTRAINT [FK_Name1] "; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.TableContraint }); updateSchema.ShouldBe( @"CREATE TABLE [dbo].[TBL] ( @@ -214,7 +214,7 @@ [columnToAlter] [nvarchar](20) NULL) (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column }); updateSchema.ShouldBe( -@"ALTER TABLE [dbo].[TBL] ADD COLUMN [columnToAdd] [nvarchar](20) NOT NULL +@"ALTER TABLE [dbo].[TBL] ADD [columnToAdd] [nvarchar](20) NOT NULL GO ALTER TABLE [dbo].[TBL] DROP COLUMN [columnToDrop] @@ -252,7 +252,7 @@ ON DELETE CASCADE ALTER TABLE [dbo].[TBL] CHECK CONSTRAINT [FK_Name1] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.TableContraint }); updateSchema.ShouldBe( @"ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [FK_Name1] @@ -311,7 +311,7 @@ ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT ((1)) FOR [col GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.TableContraint }); updateSchema.ShouldBe( @"ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [constraintName] @@ -344,7 +344,7 @@ REFERENCES [dbo].[tbl2] ([ID]) GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column, DbObjectType.TableContraint }); updateSchema.ShouldBe( @"ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [FK_constraint] @@ -357,11 +357,66 @@ ALTER TABLE [dbo].[TBL] DROP COLUMN [column1] errors.ShouldBeEmpty(); } - + [Fact] + public void DropConstraintBeforeAlterColumn() + { + const string origin = +@"CREATE TABLE [dbo].[tbl]( + [ID] [int] IDENTITY(1,1) NOT NULL, + [column1] [int] NOT NULL) +GO + +CREATE TABLE [dbo].[tblLookup]( + [ID] [int] NOT NULL, + [description] [nvarchar](50) NOT NULL) +GO + +ALTER TABLE [dbo].[tbl] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([column1]) +REFERENCES [dbo].[tblLookup] ([ID]) + + +"; + + const string destination = +@"CREATE TABLE [dbo].[tbl]( + [ID] [int] IDENTITY(1,1) NOT NULL, + [column1] [tinyint] NOT NULL) +GO + +CREATE TABLE [dbo].[tblLookup]( + [ID] [tinyint] NOT NULL, + [description] [nvarchar](50) NOT NULL) +GO + +ALTER TABLE [dbo].[tbl] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([column1]) +REFERENCES [dbo].[tblLookup] ([ID]) +GO + +"; + + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column, DbObjectType.TableContraint }); + + updateSchema.ShouldBe( +@"ALTER TABLE [dbo].[tbl] DROP CONSTRAINT [FK_constraint] +GO + +ALTER TABLE [dbo].[tbl] ALTER COLUMN [column1] [int] NOT NULL +GO + +ALTER TABLE [dbo].[tblLookup] ALTER COLUMN [ID] [int] NOT NULL +GO + +ALTER TABLE [dbo].[tbl] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([column1]) +REFERENCES [dbo].[tblLookup] ([ID]) +GO + +"); + errors.ShouldBeEmpty(); + } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Table, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Table, DbObjectType.TableContraint }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select table db object, update schema is created without table diff --git a/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs index cf7c3a9..2a1846c 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs @@ -119,7 +119,7 @@ DISABLE TRIGGER [trg1] ON DATABASE GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger, DbObjectType.EnableTrigger }); updateSchema.ShouldBe( @"CREATE TRIGGER [trg1] @@ -168,7 +168,7 @@ ENABLE TRIGGER [trg1] ON DATABASE GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger, DbObjectType.EnableTrigger }); updateSchema.ShouldBe( @"DROP TRIGGER [trg1] @@ -220,7 +220,7 @@ DISABLE TRIGGER [trg1] ON DATABASE GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger, DbObjectType.EnableTrigger }); updateSchema.ShouldBe( @"ALTER TRIGGER [trg1] @@ -245,7 +245,7 @@ ENABLE TRIGGER [trg1] ON DATABASE } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Trigger, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Trigger, DbObjectType.EnableTrigger } , MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select trigger db object, update schema is created without trigger diff --git a/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs index a251f5a..7272e5a 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs @@ -55,7 +55,6 @@ public void UpdateSchemaCreateDbObject() { // When present db object in origin absent from destination // Expect updateSchema contains create statement - const string databaseName = "dbName"; const string origin = @"CREATE TYPE [schema].[type1] AS TABLE ( @@ -104,7 +103,6 @@ public void UpdateSchemaAlterDbObject() { // When present db object in destination and in origin and are different // Expect updateSchema contains alter statement - const string databaseName = "dbName"; const string origin = @"CREATE TYPE [dbo].[TBL] AS TABLE ( @@ -133,7 +131,7 @@ [columnDifferent] [nvarchar](20) NOT NULL) } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Type, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.Type }, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select type db object, update schema is created without type diff --git a/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs b/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs index 801e5e7..b9841e4 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs @@ -130,7 +130,7 @@ public void UpdateSchemaAlterDbObject() } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.View, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.View } , MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select view db object, update schema is created without view diff --git a/SqlSchemaCompare.Test/TSql/UserTest.cs b/SqlSchemaCompare.Test/TSql/UserTest.cs index fbdfb6a..5551893 100644 --- a/SqlSchemaCompare.Test/TSql/UserTest.cs +++ b/SqlSchemaCompare.Test/TSql/UserTest.cs @@ -124,7 +124,7 @@ public void UpdateSchemaAlterDbObject() } [Theory] - [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.User, MemberType = typeof(TestDbObjectGenerator))] + [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), new DbObjectType[] { DbObjectType.User } , MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select user db object, update schema is created without user diff --git a/SqlSchemaCompare.Test/TestDbObjectGenerator.cs b/SqlSchemaCompare.Test/TestDbObjectGenerator.cs index d6408e4..cd5afdc 100644 --- a/SqlSchemaCompare.Test/TestDbObjectGenerator.cs +++ b/SqlSchemaCompare.Test/TestDbObjectGenerator.cs @@ -6,11 +6,11 @@ namespace SqlSchemaCompare.Test { public class TestDbObjectGenerator { - public static IEnumerable ListDbObjectTypeExceptOne(DbObjectType except) + public static IEnumerable ListDbObjectTypeExceptOne(IList except) { foreach (DbObjectType dbObject in Enum.GetValues(typeof(DbObjectType))) { - if (dbObject != except) + if (!except.Contains(dbObject)) yield return new object[] { dbObject }; } } diff --git a/SqlSchemaCompare.WindowsForm/MainForm.cs b/SqlSchemaCompare.WindowsForm/MainForm.cs index c0097c8..ec66072 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.cs @@ -181,7 +181,6 @@ private void LoadClearSchemaCompleted(string text, bool isAfterLoad) btnOriginSchema.Enabled = !isAfterLoad; btnDestinationSchema.Enabled = !isAfterLoad; - BtnSwapOriginDestination.Enabled = !isAfterLoad; BtnLoadSchema.Enabled = !isAfterLoad; GrpCompare.Enabled = isAfterLoad; diff --git a/SqlSchemaCompare.WindowsForm/SqlSchemaCompare.WindowsForm.csproj b/SqlSchemaCompare.WindowsForm/SqlSchemaCompare.WindowsForm.csproj index 3750abe..92ca0d5 100644 --- a/SqlSchemaCompare.WindowsForm/SqlSchemaCompare.WindowsForm.csproj +++ b/SqlSchemaCompare.WindowsForm/SqlSchemaCompare.WindowsForm.csproj @@ -11,7 +11,7 @@ true true win-x86;win-x64 - 1.0.11 + 1.0.12