Skip to content

Commit

Permalink
Refactor SchemaBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 19, 2021
1 parent a28bf56 commit 0a5aeb6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 31 deletions.
29 changes: 4 additions & 25 deletions SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(" ")..];
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ private List<T> CreateDbObject<T>(IEnumerable<T> sourceObjects, IEnumerable<T> 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<T>(toCreate, Operation.Create);
return toCreate;
Expand All @@ -345,8 +344,7 @@ private List<T> CreateDbObjectByName<T>(IEnumerable<T> 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<T>(toCreate, Operation.Create);
return toCreate;
Expand All @@ -363,7 +361,6 @@ private List<T> DropDbObject<T>(IEnumerable<T> sourceObjects, IEnumerable<T> 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();
}
Expand All @@ -380,7 +377,6 @@ private List<T> DropDbObjectByName<T>(IEnumerable<T> 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;
Expand Down

0 comments on commit 0a5aeb6

Please sign in to comment.