From 97d3765db7eefe18cedbe34f8a5f0f7816a07311 Mon Sep 17 00:00:00 2001 From: Alessandro Magistroni <65421435+AMagistroni@users.noreply.github.com> Date: Wed, 1 Sep 2021 21:22:46 +0200 Subject: [PATCH] Refactor updateSchemaManager --- .../TSql/TSqlResultProcessDbObject.cs | 10 +++ SqlSchemaCompare.Core/UpdateSchemaManager.cs | 70 +++++++++++++------ SqlSchemaCompare.Test/TSql/TSqlTableTest.cs | 3 + SqlSchemaCompare.Test/UtilityTest.cs | 2 +- SqlSchemaCompare.WindowsForm/MainForm.cs | 3 +- .../SqlSchemaCompare.WindowsForm.csproj | 2 +- 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs b/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs index c7dff72..b239c56 100644 --- a/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs +++ b/SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs @@ -11,6 +11,8 @@ public class TSqlResultProcessDbObject 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 { @@ -37,5 +39,13 @@ public void AddToDrop(T dbObjects) 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 + { + ToDisable.Add(dbObjects); + } } } \ No newline at end of file diff --git a/SqlSchemaCompare.Core/UpdateSchemaManager.cs b/SqlSchemaCompare.Core/UpdateSchemaManager.cs index c826ba7..d2f5432 100644 --- a/SqlSchemaCompare.Core/UpdateSchemaManager.cs +++ b/SqlSchemaCompare.Core/UpdateSchemaManager.cs @@ -10,14 +10,10 @@ namespace SqlSchemaCompare.Core public class UpdateSchemaManager { private readonly ISchemaBuilder _schemaBuilder; - private readonly IDbObjectFactory _dbObjectFactory; - private readonly IErrorWriter _errorWriter; private IEnumerable _selectedObjectType; - public UpdateSchemaManager(ISchemaBuilder schemaBuilder, IDbObjectFactory dbObjectFactory, IErrorWriter errorWriter) + public UpdateSchemaManager(ISchemaBuilder schemaBuilder) { _schemaBuilder = schemaBuilder; - _dbObjectFactory = dbObjectFactory; - _errorWriter = errorWriter; } public string UpdateSchema(IEnumerable sourceObjects, IEnumerable destinationObjects, IEnumerable selectedObjectType) { @@ -70,6 +66,14 @@ private void ProcessTrigger(IEnumerable sourceObjects, IEnumerable(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Trigger); + foreach (var trigger in toCreate.Union(toAlter)) + { + if (trigger.EnableObject.Enabled) + resultProcessDbObject.AddToEnable(trigger); + else + resultProcessDbObject.AddToDisable(trigger); + } + if (_selectedObjectType.Contains(DbObjectType.Trigger)) { (toCreate.Union(toAlter)).ToList() @@ -89,15 +93,18 @@ private void ProcessUser(IEnumerable sourceObjects, IEnumerable(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); DropDbObjectByName(originDb, destinationDb, resultProcessDbObject, DbObjectType.User); - if (_selectedObjectType.Contains(DbObjectType.User)) - { - originDb + var toAlter = 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())); + .ToList(); + resultProcessDbObject.AddToCreate(toAlter); + + if (_selectedObjectType.Contains(DbObjectType.User)) + { + toAlter + .ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild + .AppendLine(_schemaBuilder.Build(x, Operation.Alter)) + .AppendLine(_schemaBuilder.BuildSeparator())); } } @@ -132,6 +139,10 @@ private void ProcessTable(IEnumerable sourceObjects, IEnumerable sourceObjects, IEnumerable + + + toCreate.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild .AppendLine(_schemaBuilder.Build(x, Operation.Create)) .AppendLine(_schemaBuilder.BuildSeparator())); @@ -162,6 +173,9 @@ private void ProcessTable(IEnumerable sourceObjects, IEnumerable !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 @@ -187,27 +201,31 @@ private void ProcessTable(IEnumerable sourceObjects, IEnumerable columnsToAdd; - IEnumerable columnsToDrop; - IEnumerable columnsToAlter; + IList columnsToAdd; + IList columnsToDrop; + IList columnsToAlter; columnsToAdd = table.Columns .Where(x => table.Columns .Select(x => x.Name) .Except(destinationTable.Columns.Select(x => x.Name)) - .Contains(x.Name)); + .Contains(x.Name)).ToList(); columnsToDrop = destinationTable.Columns .Where(x => destinationTable.Columns .Select(x => x.Name) .Except(table.Columns.Select(x => x.Name)) - .Contains(x.Name)); + .Contains(x.Name)).ToList(); columnsToAlter = table.Columns .Except(columnsToAdd) - .Where(x => !destinationTable.Columns.Contains(x)); //discard object present in origin, present in destination and equals + .Where(x => !destinationTable.Columns.Contains(x)).ToList(); //discard object present in origin, present in destination and equals + + resultProcessDbObject.AddToCreate(columnsToAdd); + resultProcessDbObject.AddToAlter(columnsToAlter); + resultProcessDbObject.AddToDrop(columnsToDrop); if (_selectedObjectType.Contains(DbObjectType.Column)) { @@ -245,6 +263,7 @@ private void ProcdessIndex(IEnumerable sourceObjects, IEnumerable !destinationDb.Contains(x)).ToList(); //discard object present in origin, present in destination and equals + resultProcessDbObject.AddToAlter(toAlter); if (_selectedObjectType.Contains(DbObjectType.Index)) { @@ -268,6 +287,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 @@ -297,6 +318,9 @@ private void ProcessDbObjectWithoutAlter(IEnumerable sourceObjects, .AppendLine(_schemaBuilder.Build(x, Operation.Alter)) .AppendLine(_schemaBuilder.BuildSeparator())); } + + resultProcessDbObject.AddToAlter(toAlter); + return (toCreate, toAlter, toDrop); } private List CreateDbObject(IEnumerable sourceObjects, IEnumerable destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject @@ -329,8 +353,8 @@ private List CreateDbObjectByName(IEnumerable sourceObjects, IEnumerabl if (_selectedObjectType.Contains(dbObjectType)) { toCreate.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild - .AppendLine(_schemaBuilder.Build(x, Operation.Create)) - .AppendLine(_schemaBuilder.BuildSeparator())); + .AppendLine(_schemaBuilder.Build(x, Operation.Create)) + .AppendLine(_schemaBuilder.BuildSeparator())); } return toCreate; } diff --git a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs index ebbb72d..84f5d9b 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs @@ -357,6 +357,9 @@ ALTER TABLE [dbo].[TBL] DROP COLUMN [column1] errors.ShouldBeEmpty(); } + + + [Theory] [MemberData(nameof(TestDbObjectGenerator.ListDbObjectTypeExceptOne), DbObjectType.Table, MemberType = typeof(TestDbObjectGenerator))] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) diff --git a/SqlSchemaCompare.Test/UtilityTest.cs b/SqlSchemaCompare.Test/UtilityTest.cs index 774b662..3d57e8c 100644 --- a/SqlSchemaCompare.Test/UtilityTest.cs +++ b/SqlSchemaCompare.Test/UtilityTest.cs @@ -35,7 +35,7 @@ public static (string updateFile, string errors) UpdateSchema(string originSchem var loadSchemaManager = new LoadSchemaManager(dbObjectFactory, errorWriter); var (originDbObjects, destinationDbObjects, errors) = loadSchemaManager.LoadSchema(originSchema, destinationSchema); - UpdateSchemaManager updateSchemaManager = new(schemaBuilder, dbObjectFactory, errorWriter); + UpdateSchemaManager updateSchemaManager = new(schemaBuilder); string updateSchema= updateSchemaManager.UpdateSchema(originDbObjects, destinationDbObjects, dbObjectTypes); return (updateSchema, errors); diff --git a/SqlSchemaCompare.WindowsForm/MainForm.cs b/SqlSchemaCompare.WindowsForm/MainForm.cs index a8b3eca..c0097c8 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.cs @@ -233,9 +233,8 @@ private void BtnCreateUpdateFile_Click(object sender, EventArgs e) var (origin, destination) = GetSchema(); - IDbObjectFactory dbObjectFactory = new TSqlObjectFactory(); ISchemaBuilder schemaBuilder = new TSqlSchemaBuilder(); - UpdateSchemaManager updateSchemaManager = new(schemaBuilder, dbObjectFactory, errorWriter); + UpdateSchemaManager updateSchemaManager = new(schemaBuilder); var updateSchema = updateSchemaManager.UpdateSchema(currentOriginDbObjects, currentDestinationDbObjects, SelectedObjectType()); StringBuilder stringResult = new(); diff --git a/SqlSchemaCompare.WindowsForm/SqlSchemaCompare.WindowsForm.csproj b/SqlSchemaCompare.WindowsForm/SqlSchemaCompare.WindowsForm.csproj index ebc46bf..3750abe 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.10 + 1.0.11