Skip to content

Commit

Permalink
Refactor updateSchemaManager
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 1, 2021
1 parent 4313cfd commit 97d3765
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
10 changes: 10 additions & 0 deletions SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class TSqlResultProcessDbObject
public List<DbObject> ToDrop { get; private set; } = new();
public List<DbObject> ToAlter { get; private set; } = new();
public List<DbObject> ToCreate { get; private set; } = new();
public List<DbObject> ToEnable { get; private set; } = new();
public List<DbObject> ToDisable { get; private set; } = new();

public void AddToAlter<T>(IList<T> dbObjects) where T: DbObject
{
Expand All @@ -37,5 +39,13 @@ public void AddToDrop<T>(T dbObjects) where T : DbObject
{
ToDrop.Add(dbObjects);
}
public void AddToEnable<T>(T dbObjects) where T : DbObject
{
ToEnable.Add(dbObjects);
}
public void AddToDisable<T>(T dbObjects) where T : DbObject
{
ToDisable.Add(dbObjects);
}
}
}
70 changes: 47 additions & 23 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ namespace SqlSchemaCompare.Core
public class UpdateSchemaManager
{
private readonly ISchemaBuilder _schemaBuilder;
private readonly IDbObjectFactory _dbObjectFactory;
private readonly IErrorWriter _errorWriter;
private IEnumerable<DbObjectType> _selectedObjectType;
public UpdateSchemaManager(ISchemaBuilder schemaBuilder, IDbObjectFactory dbObjectFactory, IErrorWriter errorWriter)
public UpdateSchemaManager(ISchemaBuilder schemaBuilder)
{
_schemaBuilder = schemaBuilder;
_dbObjectFactory = dbObjectFactory;
_errorWriter = errorWriter;
}
public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObject> destinationObjects, IEnumerable<DbObjectType> selectedObjectType)
{
Expand Down Expand Up @@ -70,6 +66,14 @@ private void ProcessTrigger(IEnumerable<DbObject> sourceObjects, IEnumerable<DbO
{
(var toCreate, var toAlter, var toDrop) = ProcessGenericDbObject<Trigger>(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()
Expand All @@ -89,15 +93,18 @@ private void ProcessUser(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObje
var toCreate = CreateDbObjectByName<User>(originDb, destinationDb, resultProcessDbObject, DbObjectType.User);
DropDbObjectByName<User>(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()));
}

}
Expand Down Expand Up @@ -132,16 +139,20 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
{
if (!destinationIdentifier.Contains(tableOrigin.Identifier))
{
var toCreate = tableOrigin
.Constraints.ToList();
resultProcessDbObject.AddToCreate(toCreate);

//table new
if (_selectedObjectType.Contains(DbObjectType.Table))
{
resultProcessDbObject.UpdateSchemaStringBuild
.AppendLine(_schemaBuilder.Build(tableOrigin, Operation.Create))
.AppendLine(_schemaBuilder.BuildSeparator());

tableOrigin
.Constraints.ToList()
.ForEach(x =>


toCreate.ForEach(x =>
resultProcessDbObject.UpdateSchemaStringBuild
.AppendLine(_schemaBuilder.Build(x, Operation.Create))
.AppendLine(_schemaBuilder.BuildSeparator()));
Expand All @@ -162,6 +173,9 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
.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
Expand All @@ -187,27 +201,31 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj

private void ProcessTableColumn(Table table, Table destinationTable, TSqlResultProcessDbObject resultProcessDbObject)
{
IEnumerable<Table.Column> columnsToAdd;
IEnumerable<Table.Column> columnsToDrop;
IEnumerable<Table.Column> columnsToAlter;
IList<Table.Column> columnsToAdd;
IList<Table.Column> columnsToDrop;
IList<Table.Column> 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))
{
Expand Down Expand Up @@ -245,6 +263,7 @@ private void ProcdessIndex(IEnumerable<DbObject> sourceObjects, IEnumerable<DbOb
var toAlter = originDb
.Except(toCreate)
.Where(x => !destinationDb.Contains(x)).ToList(); //discard object present in origin, present in destination and equals
resultProcessDbObject.AddToAlter<Index>(toAlter);

if (_selectedObjectType.Contains(DbObjectType.Index))
{
Expand All @@ -268,6 +287,8 @@ private void ProcessDbObjectWithoutAlter<T>(IEnumerable<DbObject> sourceObjects,
.Except(toCreate)
.Where(x => !destinationDb.Contains(x)).ToList(); //discard object present in origin, present in destination and equals

resultProcessDbObject.AddToAlter<T>(toAlter);

if (_selectedObjectType.Contains(dbObjectType))
{
toAlter.ForEach(x => resultProcessDbObject.UpdateSchemaStringBuild
Expand Down Expand Up @@ -297,6 +318,9 @@ private void ProcessDbObjectWithoutAlter<T>(IEnumerable<DbObject> sourceObjects,
.AppendLine(_schemaBuilder.Build(x, Operation.Alter))
.AppendLine(_schemaBuilder.BuildSeparator()));
}

resultProcessDbObject.AddToAlter<T>(toAlter);

return (toCreate, toAlter, toDrop);
}
private List<T> CreateDbObject<T>(IEnumerable<T> sourceObjects, IEnumerable<T> destinationObjects, TSqlResultProcessDbObject resultProcessDbObject, DbObjectType dbObjectType) where T : DbObject
Expand Down Expand Up @@ -329,8 +353,8 @@ private List<T> CreateDbObjectByName<T>(IEnumerable<T> 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;
}
Expand Down
3 changes: 3 additions & 0 deletions SqlSchemaCompare.Test/TSql/TSqlTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Test/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions SqlSchemaCompare.WindowsForm/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>true</SignAssembly>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<Version>1.0.10</Version>
<Version>1.0.11</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 97d3765

Please sign in to comment.