Skip to content

Commit

Permalink
Fix error index disable.
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 19, 2021
1 parent c72e511 commit a28bf56
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 39 deletions.
12 changes: 0 additions & 12 deletions SqlSchemaCompare.Core/Common/EnumExtension.cs

This file was deleted.

6 changes: 2 additions & 4 deletions SqlSchemaCompare.Core/Common/ErrorWriter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlSchemaCompare.Core.Common
{
public class ErrorWriter: IErrorWriter
public class ErrorWriter : IErrorWriter
{
public string GetErrors(IEnumerable<ErrorParser> errorsOrigin, IEnumerable<ErrorParser> errorsDestination)
{
Expand Down
3 changes: 2 additions & 1 deletion SqlSchemaCompare.Core/TSql/Factory/FactoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SqlSchemaCompare.Core.DbStructures;
using System;

namespace SqlSchemaCompare.Core.TSql.Factory
{
Expand All @@ -11,7 +12,7 @@ protected Operation GetOperation(string operation)
return Operation.Create;
else if (operation == "ALTER")
return Operation.Alter;
else return Operation.Drop;
else throw new NotImplementedException();
}
}
}
3 changes: 2 additions & 1 deletion SqlSchemaCompare.Core/TSql/Factory/TSqlIndexFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public DbObject CreateAlter(ParserRuleContext context, ICharStream stream)
Name = indexContext.id_().GetText(),
Schema = string.Empty,
Operation = GetOperation(indexContext.GetChild(0).GetText()),
ParentName = indexContext.table_name().GetText()
ParentName = indexContext.table_name().GetText(),
ColumnNames = Enumerable.Empty<string>()
};
}
}
Expand Down
8 changes: 1 addition & 7 deletions SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ public string GetStartCommentInLine()
return "--";
}

public string BuildUse(string databaseName)
{
if (databaseName.Contains("["))
return $"USE {databaseName}";
else
return $"USE [{databaseName}]";
}
public string BuildUse(string databaseName) => $"USE {databaseName}";

public string BuildSeparator()
{
Expand Down
28 changes: 19 additions & 9 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace SqlSchemaCompare.Core
public class UpdateSchemaManager
{
private readonly ISchemaBuilder _schemaBuilder;
private IEnumerable<DbObjectType> _selectedObjectType;
public UpdateSchemaManager(ISchemaBuilder schemaBuilder)
{
_schemaBuilder = schemaBuilder;
Expand Down Expand Up @@ -71,7 +70,6 @@ public UpdateSchemaManager(ISchemaBuilder schemaBuilder)

public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObject> destinationObjects, IEnumerable<DbObjectType> selectedObjectType)
{
_selectedObjectType = selectedObjectType;
ResultProcessDbObject resultProcessDbObject = new();

ProcessUser(sourceObjects, destinationObjects, resultProcessDbObject);
Expand All @@ -97,7 +95,7 @@ public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbOb

foreach (var objectToWrite in OrderItemSchema)
{
if (_selectedObjectType.Contains(objectToWrite.DbObjectType))
if (selectedObjectType.Contains(objectToWrite.DbObjectType))
{
var dbObjects = objectToWrite.Operation switch
{
Expand Down Expand Up @@ -222,10 +220,15 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
resultProcessDbObject.AddOperation(constraintsToAlter, Operation.Create);

var indexToCreate = CreateDbObjectByName(tableOrigin.Indexes, destinationTable.Indexes, resultProcessDbObject);
DropDbObjectByName(tableOrigin.Indexes, destinationTable.Indexes, resultProcessDbObject);
var indexToDrop = DropDbObjectByName(tableOrigin.Indexes, destinationTable.Indexes)
.GroupBy(x => x.Identifier)
.Select(x => x.First()).ToList();
var identifierToDrop = indexToDrop.Select(x => x.Identifier);
resultProcessDbObject.AddOperation(indexToDrop, Operation.Drop);

var indexToAlter = tableOrigin.Indexes
.Except(indexToCreate)
.Where(x => !identifierToDrop.Contains(x.Identifier)) // Discard index dropped from index to alter
.Where(x => !destinationTable.Indexes.Contains(x)).ToList(); //discard object present in origin, present in destination and equals

resultProcessDbObject.AddOperation(indexToAlter, Operation.Drop);
Expand All @@ -242,9 +245,9 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
}
private void ProcessTableColumn(Table table, Table destinationTable, ResultProcessDbObject resultProcessDbObject)
{
IList<Table.Column> columnsToAdd;
IList<Table.Column> columnsToDrop;
IList<Table.Column> columnsToAlter;
IList<Column> columnsToAdd;
IList<Column> columnsToDrop;
IList<Column> columnsToAlter;

columnsToAdd = table.Columns
.Where(x =>
Expand Down Expand Up @@ -364,7 +367,14 @@ private List<T> DropDbObject<T>(IEnumerable<T> sourceObjects, IEnumerable<T> des
.Contains(dbObject.Identifier)) // object with completeName to be dropped
.ToList();
}
private void DropDbObjectByName<T>(IEnumerable<T> sourceObjects, IEnumerable<T> destinationObjects, ResultProcessDbObject resultProcessDbObject) where T : DbObject

private List<T> DropDbObjectByName<T>(IEnumerable<T> sourceObjects, IEnumerable<T> destinationObjects, ResultProcessDbObject resultProcessDbObject) where T : DbObject
{
var toDrop = DropDbObjectByName(sourceObjects, destinationObjects);
resultProcessDbObject.AddOperation<T>(toDrop, Operation.Drop);
return toDrop;
}
private List<T> DropDbObjectByName<T>(IEnumerable<T> sourceObjects, IEnumerable<T> destinationObjects) where T : DbObject
{
var toDrop = destinationObjects
.Where(dbObject => destinationObjects
Expand All @@ -373,7 +383,7 @@ private void DropDbObjectByName<T>(IEnumerable<T> sourceObjects, IEnumerable<T>
.OrderBy(x => x).ToList() // list of completeName to be dropped
.Contains(dbObject.Name)) // object with completeName to be dropped
.ToList();
resultProcessDbObject.AddOperation<T>(toDrop, Operation.Drop);
return toDrop;
}
}
}
36 changes: 36 additions & 0 deletions SqlSchemaCompare.Test/Miscellaneous.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Shouldly;
using SqlSchemaCompare.Core.DbStructures;
using Xunit;

namespace SqlSchemaCompare.Test
{
public class Miscellaneous
{
[Fact]
public void DbObjectEquals_TwoObjectsNull()
{
DbObject dbObject1 = null;
DbObject dbObject2 = null;

(dbObject1 == dbObject2).ShouldBeTrue();
}

[Fact]
public void DbObjectEquals_OneObjectNull()
{
DbObject dbObject1 = new StoreProcedure();
DbObject dbObject2 = null;

(dbObject2 != dbObject1).ShouldBeTrue();
(dbObject1 != dbObject2).ShouldBeTrue();
}

[Fact]
public void DbObjectEquals_TwoDifferentObjectsEquals()
{
DbObject dbObject1 = new StoreProcedure();
DbObject dbObject2 = new StoreProcedure();
(dbObject1 == dbObject2).ShouldBeTrue();
}
}
}
1 change: 1 addition & 0 deletions SqlSchemaCompare.Test/TSql/CompareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE [dbo].[tbl_Z]([ID] [int] IDENTITY(0,1) NOT NULL)
errors.ShouldBeEmpty();
file2.ShouldBeEmpty();
}

[Fact]
public void CompareEqualFile()
{
Expand Down
98 changes: 98 additions & 0 deletions SqlSchemaCompare.Test/TSql/CreateDatabaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using System.Collections.Generic;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class CreateDatabaseTest
{
private IList<DbObjectType> SelectedObjects;
public CreateDatabaseTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.StoreProcedure);
}

[Fact]
public void DatabaseEquals_UpdateSchemaIsEmpty()
{
const string origin =
@"CREATE DATABASE [db]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'db', FILENAME = N'C:\Data\DB.mdf' , SIZE = 1902592KB , MAXSIZE = UNLIMITED, FILEGROWTH = 524288KB )
LOG ON
( NAME = N'db_log', FILENAME = N'C:\Data\db_log.ldf' , SIZE = 2098432KB , MAXSIZE = 2048GB , FILEGROWTH = 524288KB )
GO
CREATE PROCEDURE [dbo].[proc]
@par as bit = 0
AS
BEGIN
SELECT * from [DBO].[TBL1]
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, origin, SelectedObjects);

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
}

[Fact]
public void DatabaseDifferent_UpdateContainsUseStatement()
{
const string origin =
@"CREATE DATABASE [db]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'db', FILENAME = N'C:\Data\DB.mdf' , SIZE = 1902592KB , MAXSIZE = UNLIMITED, FILEGROWTH = 524288KB )
LOG ON
( NAME = N'db_log', FILENAME = N'C:\Data\db_log.ldf' , SIZE = 2098432KB , MAXSIZE = 2048GB , FILEGROWTH = 524288KB )
GO
CREATE PROCEDURE [dbo].[proc]
@par as bit = 0
AS
BEGIN
SELECT * from [DBO].[TBL]
END
GO";
const string destination =
@"CREATE DATABASE [db]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'db', FILENAME = N'C:\Data\DB.mdf' , SIZE = 1902592KB , MAXSIZE = UNLIMITED, FILEGROWTH = 524288KB )
LOG ON
( NAME = N'db_log', FILENAME = N'C:\Data\db_log.ldf' , SIZE = 2098432KB , MAXSIZE = 2048GB , FILEGROWTH = 524288KB )
GO
CREATE PROCEDURE [dbo].[proc]
@par as bit = 0
AS
BEGIN
SELECT * from [DBO].[TBL1]
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"USE [db]
GO
ALTER PROCEDURE [dbo].[proc]
@par as bit = 0
AS
BEGIN
SELECT * from [DBO].[TBL]
END
GO
");
errors.ShouldBeEmpty();
}
}
}
31 changes: 31 additions & 0 deletions SqlSchemaCompare.Test/TSql/ErrorWriterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Shouldly;
using SqlSchemaCompare.Core.DbStructures;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class ErrorWriterTest
{
[Fact]
public void OrderStatement()
{
string sql =
@"CREATE TABLE [dbo].[tbl_Z]([ID] [int] IDENTITY(0,1) NOT NU)
GO";
var (file1, file2, errors) = UtilityTest.Compare(sql, sql, new DbObjectType[] { DbObjectType.Table });

errors.ShouldBe(
@"**************** ORIGIN ****************
----------------------------------------
Offending token: NU
Line: 1, CharPosition: 56
no viable alternative at input 'NOTNU'
**************** DESTINATION ****************
----------------------------------------
Offending token: NU
Line: 1, CharPosition: 56
no viable alternative at input 'NOTNU'
");
}
}
}
19 changes: 17 additions & 2 deletions SqlSchemaCompare.Test/TSql/TSqlIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,30 @@ public void UpdateSchemaDropDbObject()
// When present db object in destination absent from origin
// Expect updateSchema contains drop statement

const string origin = "CREATE TABLE [dbo].[table] ([ID] [INT] NOT NULL)";
const string origin = "CREATE TABLE [dbo].[table] ([ID] [INT] NOT NULL, [ID2] [int] NOT NULL)";
const string destination =
@"CREATE DATABASE [dbName]
GO
CREATE TABLE [dbo].[table] ([ID] [INT] NOT NULL)
CREATE TABLE [dbo].[table] ([ID] [INT] NOT NULL, [ID2] [int] NOT NULL)
GO
CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
GO
ALTER INDEX [indexName] on [dbo].[table] DISABLE
GO
CREATE NONCLUSTERED INDEX [indexName2] ON [dbo].[table]
(
[ID2] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
GO
ALTER INDEX [indexName2] on [dbo].[table] DISABLE
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);
Expand All @@ -153,6 +165,9 @@ [ID] ASC
DROP INDEX [indexName] ON [dbo].[table]
GO
DROP INDEX [indexName2] ON [dbo].[table]
GO
");
errors.ShouldBeEmpty();
}
Expand Down
2 changes: 0 additions & 2 deletions SqlSchemaCompare.Test/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SqlSchemaCompare.Test
{
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.20</Version>
<Version>1.0.21</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit a28bf56

Please sign in to comment.