From 0a5aeb6161aba40a2b787a500f12240bb53fe6f8 Mon Sep 17 00:00:00 2001 From: Alessandro Magistroni <65421435+AMagistroni@users.noreply.github.com> Date: Mon, 20 Sep 2021 00:41:57 +0200 Subject: [PATCH] Refactor SchemaBuilder --- .../TSql/TSqlSchemaBuilder.cs | 29 +++---------------- SqlSchemaCompare.Core/UpdateSchemaManager.cs | 8 ++--- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs b/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs index 7a0957d..cc20fb9 100644 --- a/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs +++ b/SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs @@ -13,16 +13,16 @@ public string Build(DbObject dbObject, Operation operation, ResultProcessDbObjec { return dbObject.DbObjectType switch { - DbObjectType.Table => BuildCreateDropTable(dbObject as Table, operation), + DbObjectType.Table => BuildGenericDbObjects("TABLE", dbObject as Table, operation), DbObjectType.TableDefaultContraint => BuildTableConstraint(dbObject as TableConstraint, operation, resultProcessDbObject), DbObjectType.TablePrimaryKeyContraint => BuildTableConstraint(dbObject as TableConstraint, operation, resultProcessDbObject), DbObjectType.TableForeignKeyContraint => BuildTableConstraint(dbObject as TableConstraint, operation, resultProcessDbObject), 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.StoreProcedure => BuildGenericDbObjects("PROCEDURE", dbObject as StoreProcedure, operation), + DbObjectType.Function => BuildGenericDbObjects("FUNCTION", dbObject as Function, operation), DbObjectType.Schema => BuildCreateDrop(dbObject as Schema, "SCHEMA", operation), - DbObjectType.Trigger => BuildTrigger(dbObject as Trigger, operation), + DbObjectType.Trigger => BuildGenericDbObjects("TRIGGER", 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), @@ -145,11 +145,6 @@ private string BuildUser(User user, Operation operation) throw new NotSupportedException($"Operation not supported on store {user}"); } } - - private string BuildTrigger(Trigger trigger, Operation operation) - { - return BuildGenericDbObjects("TRIGGER", trigger, operation); - } private string BuildEnableTrigger(Trigger.EnabledDbObject enabledDbObject, Operation operation) { var partialSql = enabledDbObject.Sql[enabledDbObject.Sql.IndexOf(" ")..]; @@ -158,22 +153,6 @@ private string BuildEnableTrigger(Trigger.EnabledDbObject enabledDbObject, Opera else return $"DISABLE{partialSql}"; } - - private string BuildFunction(Function function, Operation operation) - { - return BuildGenericDbObjects("FUNCTION", function, operation); - } - - private string BuildStoreProcedure(StoreProcedure storeProcedure, Operation operation) - { - return BuildGenericDbObjects("PROCEDURE", storeProcedure, operation); - } - - private string BuildCreateDropTable(Table table, Operation operation) - { - return BuildGenericDbObjects("TABLE", table, operation); - } - private string BuildCreateDrop(DbObject dbObject, string objectName, Operation operation) { return operation switch diff --git a/SqlSchemaCompare.Core/UpdateSchemaManager.cs b/SqlSchemaCompare.Core/UpdateSchemaManager.cs index 3035c8a..80c4733 100644 --- a/SqlSchemaCompare.Core/UpdateSchemaManager.cs +++ b/SqlSchemaCompare.Core/UpdateSchemaManager.cs @@ -334,8 +334,7 @@ private List CreateDbObject(IEnumerable sourceObjects, IEnumerable d 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 + .Except(destinationObjects.Select(x => x.Identifier)) //looking for complete name present in origin and absent from destination .Contains(dbObject.Identifier)).ToList(); // object with completeName resultProcessDbObject.AddOperation(toCreate, Operation.Create); return toCreate; @@ -345,8 +344,7 @@ private List CreateDbObjectByName(IEnumerable sourceObjects, IEnumerabl 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 + .Except(destinationObjects.Select(x => x.Name)) //looking for name present in origin and absent from destination .Contains(dbObject.Name)).ToList(); // object with completeName resultProcessDbObject.AddOperation(toCreate, Operation.Create); return toCreate; @@ -363,7 +361,6 @@ private List DropDbObject(IEnumerable sourceObjects, IEnumerable des .Where(dbObject => destinationObjects .Select(x => x.Identifier) .Except(sourceObjects.Select(x => x.Identifier)) //looking for complete name present in destination and absent from origin - .OrderBy(x => x).ToList() // list of completeName to be dropped .Contains(dbObject.Identifier)) // object with completeName to be dropped .ToList(); } @@ -380,7 +377,6 @@ private List DropDbObjectByName(IEnumerable sourceObjects, IEnumerable< .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(); return toDrop;