Skip to content

Commit

Permalink
Removed textbox database name
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Aug 30, 2021
1 parent 7ecc3bc commit 843f481
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 335 deletions.
1 change: 1 addition & 0 deletions SqlSchemaCompare.Core/DbStructures/DbObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum DbObjectType
TableContraint,
Column,
EnableTrigger,
UseDatabase,
Other
}
}
7 changes: 7 additions & 0 deletions SqlSchemaCompare.Core/DbStructures/UseDbObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SqlSchemaCompare.Core.DbStructures
{
public class UseDbObject : DbObject
{
public override DbObjectType DbObjectType => DbObjectType.UseDatabase;
}
}
15 changes: 15 additions & 0 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlSimpleDbObjectFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;

namespace SqlSchemaCompare.Core.TSql.Factory
{
public class TSqlSimpleDbObjectFactory : FactoryBase, IFactory
{
public DbObject Create(ParserRuleContext context, ICharStream stream)
{
return new SimpleDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex))};
}
}
}
15 changes: 15 additions & 0 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;

namespace SqlSchemaCompare.Core.TSql.Factory
{
public class TSqlUseDatabaseFactory : FactoryBase, IFactory
{
public DbObject Create(ParserRuleContext context, ICharStream stream)
{
return new UseDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)) };
}
}
}
14 changes: 9 additions & 5 deletions SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public sealed class TSqlParserUpdateListener : TSqlParserBaseListener
private readonly TSqlTypeCreator _typeFactory;
private readonly TSqlIndexFactory _indexFactory;
private readonly TSqlMemberFactory _memberFactory;
private readonly TSqlSimpleDbObjectFactory _simpleDbObjectFactory;
private readonly TSqlUseDatabaseFactory _useDatabaseFactory;

private readonly IList<Type> DDLParserRule = new List<Type>()
{ typeof(Cfl_statementContext) };
Expand All @@ -40,16 +42,18 @@ public TSqlParserUpdateListener(ICharStream stream)
_typeFactory = new TSqlTypeCreator();
_indexFactory = new TSqlIndexFactory();
_memberFactory = new TSqlMemberFactory();
_simpleDbObjectFactory = new TSqlSimpleDbObjectFactory();
_useDatabaseFactory = new TSqlUseDatabaseFactory();
}
public List<DbObject> DbObjects { get; } = new();
public override void ExitAlter_database([NotNull] Alter_databaseContext context)
{
DbObjects.Add(new SimpleDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)) });
DbObjects.Add(_simpleDbObjectFactory.Create(context, _stream));
}

public override void ExitCreate_columnstore_index([NotNull] Create_columnstore_indexContext context)
{
DbObjects.Add(new SimpleDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)) });
DbObjects.Add(_simpleDbObjectFactory.Create(context, _stream));
}
public override void ExitCreate_table([NotNull] TSqlParser.Create_tableContext context)
{
Expand Down Expand Up @@ -129,17 +133,17 @@ public override void ExitAlter_table([NotNull] TSqlParser.Alter_tableContext con

public override void ExitCreate_nonclustered_columnstore_index([NotNull] Create_nonclustered_columnstore_indexContext context)
{
DbObjects.Add(new SimpleDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)) });
DbObjects.Add(_simpleDbObjectFactory.Create(context, _stream));
}

public override void ExitCreate_sequence([NotNull] Create_sequenceContext context)
{
DbObjects.Add(new SimpleDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)) });
DbObjects.Add(_simpleDbObjectFactory.Create(context, _stream));
}

public override void ExitUse_statement([NotNull] TSqlParser.Use_statementContext context)
{

DbObjects.Add(_useDatabaseFactory.Create(context, _stream));
}
private bool ObjectInsideDDL(RuleContext context)
{
Expand Down
11 changes: 7 additions & 4 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public UpdateSchemaManager(ISchemaBuilder schemaBuilder, IDbObjectFactory dbObje
_dbObjectFactory = dbObjectFactory;
_errorWriter = errorWriter;
}
public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObject> destinationObjects, string databaseName, IEnumerable<DbObjectType> selectedObjectType)
public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObject> destinationObjects, IEnumerable<DbObjectType> selectedObjectType)
{
_selectedObjectType = selectedObjectType;
TSqlResultProcessDbObject resultProcessDbObject = new();
Expand All @@ -43,10 +43,13 @@ public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbOb
}
else
{
var destinationDb = destinationObjects.OfType<UseDbObject>();
StringBuilder useDb = new();
useDb.AppendLine(_schemaBuilder.BuildUse(databaseName));
useDb.AppendLine(_schemaBuilder.BuildSeparator());

if (destinationDb.Any())
{
useDb.AppendLine(destinationDb.First().Sql);
useDb.AppendLine(_schemaBuilder.BuildSeparator());
}
return $"{useDb}{resultProcessDbObject.UpdateSchemaStringBuild}";
}
}
Expand Down
32 changes: 12 additions & 20 deletions SqlSchemaCompare.Test/TSql/IndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void UpdateSchemaEqualsDbObject()
{
// When origin equals destination
// Expect updateSchema should be empty
const string databaseName = "dbName";

const string origin =
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
Expand All @@ -61,7 +60,7 @@ [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";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -72,7 +71,6 @@ public void UpdateSchemaCreateDbObject()
{
// When present db object in origin absent from destination
// Expect updateSchema contains create statement
const string databaseName = "dbName";

const string origin =
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
Expand All @@ -82,13 +80,10 @@ [ID] ASC
GO";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
GO
CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
@"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]
Expand All @@ -103,20 +98,22 @@ public void UpdateSchemaDropDbObject()
{
// When present db object in destination absent from origin
// Expect updateSchema contains drop statement
const string databaseName = "dbName";

const string origin = "";
const string destination =
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
@"USE [dbName]
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";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
@"USE [dbName]
GO
DROP INDEX [indexName] ON [dbo].[table]
Expand All @@ -131,7 +128,6 @@ public void UpdateSchemaAlterDbObject()
{
// When present db object in destination and in origin and are different
// Expect updateSchema contains alter statement
const string databaseName = "dbName";

const string origin =
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
Expand All @@ -147,13 +143,10 @@ [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";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
GO
DROP INDEX [indexName] ON [dbo].[table]
@"DROP INDEX [indexName] ON [dbo].[table]
GO
CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
Expand All @@ -171,7 +164,6 @@ [ID] ASC
public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes)
{
// When user not select Index db object, update schema is created without Index
const string databaseName = "dbName";

const string origin =
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
Expand All @@ -181,7 +173,7 @@ [ID] ASC
GO";
string destination = string.Empty;

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { dbObjectTypes });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { dbObjectTypes });
updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
}
Expand Down
23 changes: 6 additions & 17 deletions SqlSchemaCompare.Test/TSql/MemberTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ public void UpdateSchemaEqualsDbObject()
{
// When origin equals destination
// Expect updateSchema should be empty
const string databaseName = "dbName";

const string origin = "ALTER ROLE [role] ADD MEMBER [member1]";
const string destination = "ALTER ROLE [role] ADD MEMBER [member1]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Member });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Member });

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -46,18 +44,14 @@ public void UpdateSchemaCreateDbObject()
{
// When present db object in origin absent from destination
// Expect updateSchema contains create statement
const string databaseName = "dbName";

const string origin = "ALTER ROLE [role] ADD MEMBER [member1]";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Member });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Member });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
GO
ALTER ROLE [role] ADD MEMBER [member1]
@"ALTER ROLE [role] ADD MEMBER [member1]
GO
");
Expand All @@ -69,18 +63,14 @@ public void UpdateSchemaDropDbObject()
{
// When present db object in destination absent from origin
// Expect updateSchema contains drop statement
const string databaseName = "dbName";

const string origin = "";
const string destination = "ALTER ROLE [role] ADD MEMBER [member1]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Member });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Member });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
GO
ALTER ROLE [role] DROP MEMBER [member1]
@"ALTER ROLE [role] DROP MEMBER [member1]
GO
");
Expand All @@ -92,12 +82,11 @@ ALTER ROLE [role] DROP MEMBER [member1]
public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes)
{
// When user not select Member db object, update schema is created without Member
const string databaseName = "dbName";

const string origin = "ALTER ROLE [role] ADD MEMBER [member1]";
string destination = string.Empty;

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { dbObjectTypes });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { dbObjectTypes });
updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
}
Expand Down
24 changes: 6 additions & 18 deletions SqlSchemaCompare.Test/TSql/RoleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public void UpdateSchemaEqualsDbObject()
{
// When origin equals destination
// Expect updateSchema should be empty
const string databaseName = "dbName";

const string origin = "CREATE ROLE [role]";
const string destination = "CREATE ROLE [role]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Role });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Role });

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -45,18 +43,14 @@ public void UpdateSchemaCreateDbObject()
{
// When present db object in origin absent from destination
// Expect updateSchema contains create statement
const string databaseName = "dbName";

const string origin = "CREATE ROLE [role]";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Role });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Role });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
GO
CREATE ROLE [role]
@"CREATE ROLE [role]
GO
");
Expand All @@ -68,7 +62,6 @@ public void UpdateSchemaDropDbObject()
{
// When present db object in destination absent from origin
// Expect updateSchema contains drop statement
const string databaseName = "dbName";

const string origin = "";
const string destination =
Expand All @@ -79,13 +72,10 @@ ALTER ROLE [role] ADD MEMBER [user]
GO
";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Role });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Role });

updateSchema.ShouldBe(
$@"USE [{databaseName}]
GO
DROP ROLE [role]
@"DROP ROLE [role]
GO
");
Expand All @@ -97,12 +87,10 @@ DROP ROLE [role]
public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes)
{
// When user not select role db object, update schema is created without role
const string databaseName = "dbName";

const string origin = "CREATE ROLE [role]";
string destination = string.Empty;

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { dbObjectTypes });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { dbObjectTypes });
updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
}
Expand Down
Loading

0 comments on commit 843f481

Please sign in to comment.