Skip to content

Commit

Permalink
DiscardObjects and DiscardSchemas during compare
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Dec 21, 2023
1 parent 6192b30 commit e9494e2
Show file tree
Hide file tree
Showing 11 changed files with 693 additions and 407 deletions.
10 changes: 10 additions & 0 deletions SqlSchemaCompare.Core/Common/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace SqlSchemaCompare.Core.Common
{
public class Configuration
{
public List<string> DiscardObjects { get; set; } = new List<string>();
public List<string> DiscardSchemas { get; set; } = new List<string>();
}
}
22 changes: 22 additions & 0 deletions SqlSchemaCompare.Core/Common/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

namespace SqlSchemaCompare.Core.Common
{
public static class StringExtension
{
public static bool StartsWithAny(this string value, List<string> list)
{
if (value is null)
return false;

foreach (var item in list)
{
if (value.StartsWith(item))
{
return true;
}
}
return false;
}
}
}
21 changes: 18 additions & 3 deletions SqlSchemaCompare.Core/CompareSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,40 @@ namespace SqlSchemaCompare.Core
public class CompareSchemaManager
{
private readonly ISchemaBuilder _schemaBuilder;
public CompareSchemaManager(ISchemaBuilder schemaBuilder)
private readonly Configuration _configuration;
public CompareSchemaManager(Configuration configuration, ISchemaBuilder schemaBuilder)
{
_schemaBuilder = schemaBuilder;
_configuration = configuration;
}

public (string file1, string file2) Compare(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObject> destinationObjects, IEnumerable<DbObjectType> selectedObjectType)
{
var sourceObjectsFilteredSelected = sourceObjects.Where(x => selectedObjectType.Contains(x.DbObjectType));
var destinationObjectsFilteredSelected = destinationObjects.Where(x => selectedObjectType.Contains(x.DbObjectType));

var objectsSchemaResult1 = sourceObjectsFilteredSelected.Select(x => x.Sql).ToList();
var objectsSchemaResult2 = destinationObjectsFilteredSelected.Select(x => x.Sql).ToList();
var sourceFilteredByConfiguration = FilterByConfiguration(sourceObjectsFilteredSelected);
var destinationFilteredByConfiguration = FilterByConfiguration(destinationObjectsFilteredSelected);

var objectsSchemaResult1 = sourceFilteredByConfiguration.Select(x => x.Sql).ToList();
var objectsSchemaResult2 = destinationFilteredByConfiguration.Select(x => x.Sql).ToList();

StringBuilder stringBuilder1 = BuildStringBuilder(objectsSchemaResult1, objectsSchemaResult2);
StringBuilder stringBuilder2 = BuildStringBuilder(objectsSchemaResult2, objectsSchemaResult1);

return (stringBuilder1.ToString().Trim(), stringBuilder2.ToString().Trim());
}

private IEnumerable<DbObject> FilterByConfiguration(IEnumerable<DbObject> dbObjects)
{
return dbObjects
.Except(dbObjects.Where(x => _configuration.DiscardSchemas.Contains(x.Schema)))
.Except(dbObjects.Where(x => x.ParentName.StartsWithAny(_configuration.DiscardSchemas)))
.Except(dbObjects.Where(x => _configuration.DiscardObjects.Contains(x.Identifier)))
.Except(dbObjects.Where(x => _configuration.DiscardObjects.Contains(x.ParentName)))
.Except(dbObjects.Where(x => _configuration.DiscardSchemas.Contains(x.Name)));
}

private StringBuilder BuildStringBuilder(List<string> objectsSchema1, List<string> objectsSchema2)
{
StringBuilder stringBuilder = new();
Expand Down
70 changes: 66 additions & 4 deletions SqlSchemaCompare.Test/TSql/CompareTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Shouldly;
using SqlSchemaCompare.Core.DbStructures;
using System.Collections.Generic;
using System.Data;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
Expand All @@ -16,7 +18,7 @@ public void OrderStatement()
CREATE TABLE [dbo].[tbl_A]([ID] [int] IDENTITY(0,1) NOT NULL)
GO
";
var (file1, file2, errors) = UtilityTest.Compare(sql, "", new DbObjectType[] { DbObjectType.Table });
var (file1, file2, errors) = UtilityTest.Compare(sql, "", new Core.Common.Configuration(), new DbObjectType[] { DbObjectType.Table });

file1.ShouldBe(
@"CREATE TABLE [dbo].[tbl_A]([ID] [int] IDENTITY(0,1) NOT NULL)
Expand All @@ -39,7 +41,7 @@ public void CompareEqualFile()
CREATE TABLE [dbo].[tbl_Z]([ID] [int] IDENTITY(0,1) NOT NULL)
GO
";
var (file1, file2, errors) = UtilityTest.Compare(sql, sql, new DbObjectType[] { DbObjectType.Table });
var (file1, file2, errors) = UtilityTest.Compare(sql, sql, new Core.Common.Configuration(), new DbObjectType[] { DbObjectType.Table });

file1.ShouldBeEmpty();
file2.ShouldBeEmpty();
Expand All @@ -57,7 +59,7 @@ SET ANSI_NULLS ON
";

//SET QUOTED_IDENTIFIER ON
var (file1, _, errors) = UtilityTest.Compare(sql, "", new DbObjectType[] { DbObjectType.Table });
var (file1, _, errors) = UtilityTest.Compare(sql, "", new Core.Common.Configuration(), new DbObjectType[] { DbObjectType.Table });

file1.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -81,7 +83,7 @@ CREATE TABLE [dbo].[tbl]([ID] [int] IDENTITY(0,1) NOT NULL)
CREATE TABLE [dbo].[only_file_2]([ID] [int] IDENTITY(0,1) NOT NULL)
GO
";
var (file1, file2, errors) = UtilityTest.Compare(sql1, sql2, new DbObjectType[] { DbObjectType.Table });
var (file1, file2, errors) = UtilityTest.Compare(sql1, sql2, new Core.Common.Configuration(), new DbObjectType[] { DbObjectType.Table });

file1.ShouldBe(
@"CREATE TABLE [dbo].[only_file_1]([ID] [int] IDENTITY(0,1) NOT NULL)
Expand All @@ -97,5 +99,65 @@ CREATE TABLE [dbo].[tbl]([ID] [int] IDENTITY(0,1) NULL)
GO");
errors.ShouldBeEmpty();
}
[Fact]
public void DiscardSchema()
{
const string sql1 =
@"CREATE SCHEMA [test]
GO
";

var configuration = new Core.Common.Configuration()
{
DiscardSchemas = new List<string> { "[test]" }
};

var (file1, file2, errors) = UtilityTest.Compare(sql1, string.Empty, configuration, new DbObjectType[] { DbObjectType.Table });

file1.ShouldBeEmpty();
file2.ShouldBeEmpty();
errors.ShouldBeEmpty();
}
[Fact]
public void DiscardObjects()
{
const string sql1 =
@"CREATE TABLE [dbo].[only_file_1]([ID] [int] IDENTITY(0,1) NOT NULL)
GO
";

var configuration = new Core.Common.Configuration()
{
DiscardObjects = new List<string> { "[dbo].[only_file_1]" }
};

var (file1, file2, errors) = UtilityTest.Compare(sql1, string.Empty, configuration, new DbObjectType[] { DbObjectType.Table });

file1.ShouldBeEmpty();
file2.ShouldBeEmpty();
errors.ShouldBeEmpty();
}
[Fact]
public void DiscardSchema_table()
{
string sql1 =
$@"CREATE TABLE [schema].[TableName](
[Id] [int] IDENTITY(1,1) NOT NULL,
[col1] [char](8) NULL
) ON [db1]
GO
";

var configuration = new Core.Common.Configuration()
{
DiscardSchemas = new List<string> { "[schema]" }
};

var (file1, file2, errors) = UtilityTest.Compare(sql1, string.Empty, configuration, new DbObjectType[] { DbObjectType.Table });

file1.ShouldBeEmpty();
file2.ShouldBeEmpty();
errors.ShouldBeEmpty();
}
}
}
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Test/TSql/ErrorWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void OrderStatement()
const string sql =
@"CREATE TABLE [dbo].[tbl_Z]([ID] [int] IDENTITY(0,1) NOT NU)
GO";
var (_, _, errors) = UtilityTest.Compare(sql, sql, new DbObjectType[] { DbObjectType.Table });
var (_, _, errors) = UtilityTest.Compare(sql, sql, new Core.Common.Configuration(), new DbObjectType[] { DbObjectType.Table });

errors.ShouldBe(
@"**************** ORIGIN ****************
Expand Down
5 changes: 3 additions & 2 deletions SqlSchemaCompare.Test/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace SqlSchemaCompare.Test
{
public static class UtilityTest
{
public static (string file1, string file2, string errors) Compare(string originSchema, string destinationSchema, IEnumerable<DbObjectType> dbObjectTypes)
public static (string file1, string file2, string errors)
Compare(string originSchema, string destinationSchema, Configuration configuration, IEnumerable<DbObjectType> dbObjectTypes)
{
var schemaBuilder = new TSqlSchemaBuilder();
var dbObjectFactory = new TSqlObjectFactory();
Expand All @@ -17,7 +18,7 @@ public static (string file1, string file2, string errors) Compare(string originS
var loadSchemaManager = new LoadSchemaManager(dbObjectFactory, errorWriter);
var (originDbObjects, destinationDbObjects, errors) = loadSchemaManager.LoadSchema(originSchema, destinationSchema);

var compareSchemaManager = new CompareSchemaManager(schemaBuilder);
var compareSchemaManager = new CompareSchemaManager(configuration, schemaBuilder);
var (file1, file2) = compareSchemaManager.Compare(originDbObjects, destinationDbObjects, dbObjectTypes);

return (file1, file2, errors);
Expand Down
7 changes: 7 additions & 0 deletions SqlSchemaCompare.WindowsForm/FormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public string DestinationSchema
get => (string)this[nameof(DestinationSchema)];
set => this[nameof(DestinationSchema)] = value;
}
[UserScopedSetting()]
public string Configuration
{
get => (string)this[nameof(Configuration)];
set => this[nameof(Configuration)] = value;
}

[UserScopedSetting()]
public string OutputDirectory
{
Expand Down
Loading

0 comments on commit e9494e2

Please sign in to comment.