diff --git a/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs b/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs index 7e62988..ec202af 100644 --- a/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs +++ b/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs @@ -16,6 +16,7 @@ public enum DbObjectType TableContraint, Column, EnableTrigger, + UseDatabase, Other } } diff --git a/SqlSchemaCompare.Core/DbStructures/UseDbObject.cs b/SqlSchemaCompare.Core/DbStructures/UseDbObject.cs new file mode 100644 index 0000000..8f20912 --- /dev/null +++ b/SqlSchemaCompare.Core/DbStructures/UseDbObject.cs @@ -0,0 +1,7 @@ +namespace SqlSchemaCompare.Core.DbStructures +{ + public class UseDbObject : DbObject + { + public override DbObjectType DbObjectType => DbObjectType.UseDatabase; + } +} diff --git a/SqlSchemaCompare.Core/TSql/Factory/TSqlSimpleDbObjectFactory.cs b/SqlSchemaCompare.Core/TSql/Factory/TSqlSimpleDbObjectFactory.cs new file mode 100644 index 0000000..4729058 --- /dev/null +++ b/SqlSchemaCompare.Core/TSql/Factory/TSqlSimpleDbObjectFactory.cs @@ -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))}; + } + } +} diff --git a/SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs b/SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs new file mode 100644 index 0000000..791471b --- /dev/null +++ b/SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs @@ -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)) }; + } + } +} diff --git a/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs b/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs index 84aa84c..01c75c8 100644 --- a/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs +++ b/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs @@ -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 DDLParserRule = new List() { typeof(Cfl_statementContext) }; @@ -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 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) { @@ -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) { diff --git a/SqlSchemaCompare.Core/UpdateSchemaManager.cs b/SqlSchemaCompare.Core/UpdateSchemaManager.cs index 0dd7c55..97d28ed 100644 --- a/SqlSchemaCompare.Core/UpdateSchemaManager.cs +++ b/SqlSchemaCompare.Core/UpdateSchemaManager.cs @@ -19,7 +19,7 @@ public UpdateSchemaManager(ISchemaBuilder schemaBuilder, IDbObjectFactory dbObje _dbObjectFactory = dbObjectFactory; _errorWriter = errorWriter; } - public string UpdateSchema(IEnumerable sourceObjects, IEnumerable destinationObjects, string databaseName, IEnumerable selectedObjectType) + public string UpdateSchema(IEnumerable sourceObjects, IEnumerable destinationObjects, IEnumerable selectedObjectType) { _selectedObjectType = selectedObjectType; TSqlResultProcessDbObject resultProcessDbObject = new(); @@ -43,10 +43,13 @@ public string UpdateSchema(IEnumerable sourceObjects, IEnumerable(); 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}"; } } diff --git a/SqlSchemaCompare.Test/TSql/IndexTest.cs b/SqlSchemaCompare.Test/TSql/IndexTest.cs index 0728908..32a2aed 100644 --- a/SqlSchemaCompare.Test/TSql/IndexTest.cs +++ b/SqlSchemaCompare.Test/TSql/IndexTest.cs @@ -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] @@ -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(); @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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(); } diff --git a/SqlSchemaCompare.Test/TSql/MemberTest.cs b/SqlSchemaCompare.Test/TSql/MemberTest.cs index b3483ec..25b7373 100644 --- a/SqlSchemaCompare.Test/TSql/MemberTest.cs +++ b/SqlSchemaCompare.Test/TSql/MemberTest.cs @@ -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(); @@ -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 "); @@ -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 "); @@ -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(); } diff --git a/SqlSchemaCompare.Test/TSql/RoleTest.cs b/SqlSchemaCompare.Test/TSql/RoleTest.cs index 886ec01..2a31060 100644 --- a/SqlSchemaCompare.Test/TSql/RoleTest.cs +++ b/SqlSchemaCompare.Test/TSql/RoleTest.cs @@ -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(); @@ -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 "); @@ -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 = @@ -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 "); @@ -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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs b/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs index 858c246..07c67ba 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs @@ -35,8 +35,7 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; - + const string origin = @"CREATE FUNCTION [dbo].[func1] (@par int) RETURNS INT @@ -54,7 +53,7 @@ RETURN 3 END GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Function }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -65,7 +64,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 FUNCTION [dbo].[func1] (@par int) @@ -77,13 +75,10 @@ RETURN 3 GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Function }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -CREATE FUNCTION [dbo].[func1] (@par int) +@"CREATE FUNCTION [dbo].[func1] (@par int) RETURNS INT AS BEGIN @@ -100,7 +95,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 = @@ -112,13 +106,10 @@ RETURN 3 END GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Function }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP FUNCTION [dbo].[func1] +@"DROP FUNCTION [dbo].[func1] GO "); @@ -130,7 +121,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 FUNCTION [dbo].[func1] (@par int) @@ -149,13 +139,10 @@ RETURN 10 END GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Function }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER FUNCTION [dbo].[func1] (@par int) +@"ALTER FUNCTION [dbo].[func1] (@par int) RETURNS INT AS BEGIN @@ -172,8 +159,6 @@ RETURN 3 public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select Function db object, update schema is created without Function - const string databaseName = "dbName"; - const string origin = @"CREATE FUNCTION [dbo].[func1] (@par int) RETURNS INT @@ -184,7 +169,7 @@ RETURN 3 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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs b/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs index cd5ca8b..4b92c20 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs @@ -29,12 +29,11 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; const string origin = "CREATE SCHEMA [sch1]"; const string destination = "CREATE SCHEMA [sch1]"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Schema }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Schema }); updateSchema.ShouldBeEmpty(); } @@ -44,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 SCHEMA [sch1]"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Schema }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Schema }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -CREATE SCHEMA [sch1] +@"CREATE SCHEMA [sch1] GO "); @@ -67,18 +62,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 = "CREATE SCHEMA [sch1]"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Schema }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Schema }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP SCHEMA [sch1] +@"DROP SCHEMA [sch1] GO "); @@ -90,12 +81,11 @@ DROP SCHEMA [sch1] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select schema db object, update schema is created without schema - const string databaseName = "dbName"; const string origin = "CREATE SCHEMA [sch1]"; 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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs b/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs index fef6e6b..ee852cd 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs @@ -52,7 +52,7 @@ public void UpdateSchemaEqualsDbObject() END GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.StoreProcedure }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -63,7 +63,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 PROCEDURE [dbo].[proc] @@ -75,13 +74,10 @@ public void UpdateSchemaCreateDbObject() GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.StoreProcedure }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -CREATE PROCEDURE [dbo].[proc] +@"CREATE PROCEDURE [dbo].[proc] @par as bit = 0 AS BEGIN @@ -98,7 +94,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 = @@ -110,13 +105,10 @@ public void UpdateSchemaDropDbObject() END GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.StoreProcedure }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP PROCEDURE [dbo].[proc] +@"DROP PROCEDURE [dbo].[proc] GO "); @@ -128,7 +120,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 PROCEDURE [dbo].[proc] @@ -147,13 +138,10 @@ public void UpdateSchemaAlterDbObject() END GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.StoreProcedure }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER PROCEDURE [dbo].[proc] +@"ALTER PROCEDURE [dbo].[proc] @par as bit = 0 AS BEGIN @@ -194,12 +182,9 @@ public void CreateStoreProcedureWithTableInside() public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select StoreProcedure db object, update schema is created without StoreProcedure - const string databaseName = "dbName"; + string origin = -$@"USE [{databaseName}] -GO - -CREATE PROCEDURE [dbo].[proc] +@"CREATE PROCEDURE [dbo].[proc] @par as bit = 0 AS BEGIN @@ -209,7 +194,7 @@ CREATE PROCEDURE [dbo].[proc] 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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs index 6e2eccd..da85d07 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTableTest.cs @@ -8,7 +8,6 @@ namespace SqlSchemaCompare.Test.TSql { public class TSqlTableTest { - private const string databaseName = "dbName"; [Fact] public void CreateTable() { @@ -100,7 +99,7 @@ ALTER TABLE [dbo].[TBL] CHECK CONSTRAINT [FK_Name2] GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -128,13 +127,10 @@ ALTER TABLE [dbo].[TBL] CHECK CONSTRAINT [FK_Name1] "; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -CREATE TABLE [dbo].[TBL] ( +@"CREATE TABLE [dbo].[TBL] ( [ID] [int] IDENTITY(1,1) NOT NULL, [column1] [Date] NOT NULL) GO @@ -183,13 +179,10 @@ ALTER TABLE [schema].[table] CHECK CONSTRAINT [FK_Name1] GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Table, DbObjectType.Schema }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Schema }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP TABLE [schema].[table] +@"DROP TABLE [schema].[table] GO DROP SCHEMA [schema] @@ -218,13 +211,10 @@ [columnToDrop] [nvarchar](20) NOT NULL, [columnToAlter] [nvarchar](20) NULL) GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table, DbObjectType.Column }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER TABLE [dbo].[TBL] ADD COLUMN [columnToAdd] [nvarchar](20) NOT NULL +@"ALTER TABLE [dbo].[TBL] ADD COLUMN [columnToAdd] [nvarchar](20) NOT NULL GO ALTER TABLE [dbo].[TBL] DROP COLUMN [columnToDrop] @@ -262,13 +252,10 @@ ON DELETE CASCADE ALTER TABLE [dbo].[TBL] CHECK CONSTRAINT [FK_Name1] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [FK_Name1] +@"ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [FK_Name1] GO "); @@ -324,13 +311,10 @@ ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT ((1)) FOR [col GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Table }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Table }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [constraintName] +@"ALTER TABLE [dbo].[TBL] DROP CONSTRAINT [constraintName] GO ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT ((0)) FOR [column1] @@ -357,7 +341,7 @@ ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT ((0)) FOR [col 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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs index c66f817..cf7c3a9 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs @@ -52,10 +52,9 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; const string origin = - @"CREATE TRIGGER [trg1] +@"CREATE TRIGGER [trg1] ON DATABASE for create_procedure, alter_procedure, drop_procedure, create_table, alter_table, drop_table, @@ -72,7 +71,7 @@ declare @variable int ENABLE TRIGGER [trg1] ON DATABASE GO"; const string destination = - @"CREATE TRIGGER [trg1] +@"CREATE TRIGGER [trg1] ON DATABASE for create_procedure, alter_procedure, drop_procedure, create_table, alter_table, drop_table, @@ -89,7 +88,7 @@ declare @variable int ENABLE TRIGGER [trg1] ON DATABASE GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -100,7 +99,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 TRIGGER [trg1] @@ -121,13 +119,10 @@ DISABLE TRIGGER [trg1] ON DATABASE GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); updateSchema.ShouldBe( - $@"USE [{databaseName}] -GO - -CREATE TRIGGER [trg1] +@"CREATE TRIGGER [trg1] ON DATABASE for create_procedure, alter_procedure, drop_procedure, create_table, alter_table, drop_table, @@ -153,11 +148,9 @@ 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 TRIGGER [trg1] +@"CREATE TRIGGER [trg1] ON DATABASE for create_procedure, alter_procedure, drop_procedure, create_table, alter_table, drop_table, @@ -175,13 +168,10 @@ ENABLE TRIGGER [trg1] ON DATABASE GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); updateSchema.ShouldBe( - $@"USE [{databaseName}] -GO - -DROP TRIGGER [trg1] +@"DROP TRIGGER [trg1] GO "); @@ -193,8 +183,7 @@ 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 TRIGGER [trg1] ON DATABASE @@ -231,13 +220,10 @@ DISABLE TRIGGER [trg1] ON DATABASE GO "; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Trigger }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Trigger }); updateSchema.ShouldBe( - $@"USE [{databaseName}] -GO - -ALTER TRIGGER [trg1] +@"ALTER TRIGGER [trg1] ON DATABASE for create_procedure, alter_procedure, drop_procedure, create_table, alter_table, drop_table, @@ -263,10 +249,9 @@ ENABLE TRIGGER [trg1] ON DATABASE public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select trigger db object, update schema is created without trigger - const string databaseName = "dbName"; - + const string origin = - @"CREATE TRIGGER [trg1] +@"CREATE TRIGGER [trg1] ON DATABASE for create_procedure, alter_procedure, drop_procedure, create_table, alter_table, drop_table, @@ -285,7 +270,7 @@ ENABLE TRIGGER [trg1] ON DATABASE "; 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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs b/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs index e540766..a251f5a 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlTypeTest.cs @@ -32,7 +32,6 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; const string origin = @"CREATE TYPE [schema].[type1] AS TABLE ( @@ -45,7 +44,7 @@ [ID] [int] IDENTITY(1,1) NOT NULL, [column1] [nvarchar](20) NOT NULL) GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Type }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Type }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -65,13 +64,10 @@ [column1] [nvarchar](20) NOT NULL) GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Type }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Type }); updateSchema.ShouldBe( - $@"USE [{databaseName}] -GO - -CREATE TYPE [schema].[type1] AS TABLE ( +@"CREATE TYPE [schema].[type1] AS TABLE ( [ID] [int] IDENTITY(1,1) NOT NULL, [column1] [nvarchar](20) NOT NULL) GO @@ -85,7 +81,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 = @@ -94,13 +89,10 @@ [ID] [int] IDENTITY(1,1) NOT NULL, [column1] [nvarchar](20) NOT NULL) GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Type }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Type }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP TYPE [schema].[type1] +@"DROP TYPE [schema].[type1] GO "); @@ -125,13 +117,10 @@ [columnDifferent] [nvarchar](20) NOT NULL) [columnDifferent] [nvarchar](20) NULL) GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.Type }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Type }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP TYPE [dbo].[TBL] +@"DROP TYPE [dbo].[TBL] GO CREATE TYPE [dbo].[TBL] AS TABLE ( @@ -148,7 +137,6 @@ [columnDifferent] [nvarchar](20) NOT NULL) public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select type db object, update schema is created without type - const string databaseName = "dbName"; const string origin = @"CREATE TYPE [dbo].[TBL] AS TABLE ( @@ -157,7 +145,7 @@ [columnDifferent] [nvarchar](20) NOT NULL) 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(); } diff --git a/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs b/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs index 9d73c08..801e5e7 100644 --- a/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs +++ b/SqlSchemaCompare.Test/TSql/TSqlViewTest.cs @@ -34,7 +34,6 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; const string origin = @"CREATE VIEW [dbo].[vw1] @@ -47,7 +46,7 @@ public void UpdateSchemaEqualsDbObject() SELECT * FROM [dbo].[tbl1] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.View }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.View }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -58,7 +57,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 VIEW [dbo].[vw1] @@ -67,13 +65,10 @@ public void UpdateSchemaCreateDbObject() GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.View }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.View }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -CREATE VIEW [dbo].[vw1] +@"CREATE VIEW [dbo].[vw1] AS SELECT * FROM [dbo].[tbl1] GO @@ -87,8 +82,7 @@ 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 VIEW [dbo].[vw1] @@ -96,13 +90,10 @@ public void UpdateSchemaDropDbObject() SELECT * FROM [dbo].[tbl1] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.View }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.View }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP VIEW [dbo].[vw1] +@"DROP VIEW [dbo].[vw1] GO "); @@ -114,8 +105,7 @@ 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 VIEW [dbo].[vw1] AS @@ -127,13 +117,10 @@ public void UpdateSchemaAlterDbObject() SELECT * FROM [dbo].[tbl2] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.View }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.View }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER VIEW [dbo].[vw1] +@"ALTER VIEW [dbo].[vw1] AS SELECT * FROM [dbo].[tbl1] GO @@ -147,7 +134,6 @@ ALTER VIEW [dbo].[vw1] public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select view db object, update schema is created without view - const string databaseName = "dbName"; const string origin = @"CREATE VIEW [dbo].[vw1] @@ -156,7 +142,7 @@ public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) 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(); } diff --git a/SqlSchemaCompare.Test/TSql/UserTest.cs b/SqlSchemaCompare.Test/TSql/UserTest.cs index 12250ee..a38cf89 100644 --- a/SqlSchemaCompare.Test/TSql/UserTest.cs +++ b/SqlSchemaCompare.Test/TSql/UserTest.cs @@ -3,9 +3,6 @@ using SqlSchemaCompare.Core.TSql; using System.Linq; using Xunit; -using SqlSchemaCompare.Core.Common; -using System; -using System.Collections.Generic; namespace SqlSchemaCompare.Test.TSql { @@ -48,7 +45,6 @@ public void UpdateSchemaEqualsDbObject() { // When origin equals destination // Expect updateSchema should be empty - const string databaseName = "dbName"; const string origin = @"CREATE USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] @@ -57,7 +53,7 @@ public void UpdateSchemaEqualsDbObject() @"CREATE USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.User }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.User }); updateSchema.ShouldBeEmpty(); errors.ShouldBeEmpty(); @@ -68,20 +64,16 @@ 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 USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] GO"; const string destination = ""; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.User }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.User }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -CREATE USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] +@"CREATE USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] GO "); @@ -93,20 +85,16 @@ 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 USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.User }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.User }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -DROP USER [user] +@"DROP USER [user] GO "); @@ -118,8 +106,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 USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] GO"; @@ -127,13 +113,10 @@ public void UpdateSchemaAlterDbObject() @"CREATE USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[sch1] GO"; - (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, databaseName, new DbObjectType[] { DbObjectType.User }); + (string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.User }); updateSchema.ShouldBe( -$@"USE [{databaseName}] -GO - -ALTER USER [user] WITH DEFAULT_SCHEMA=[dbo] +@"ALTER USER [user] WITH DEFAULT_SCHEMA=[dbo] GO "); @@ -145,14 +128,13 @@ public void UpdateSchemaAlterDbObject() public void UpdateSchemaNotSelectedDbObject(DbObjectType dbObjectTypes) { // When user not select user db object, update schema is created without user - const string databaseName = "dbName"; const string origin = @"CREATE USER [user] FOR LOGIN [user_login] WITH DEFAULT_SCHEMA=[dbo] 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(); } diff --git a/SqlSchemaCompare.Test/UtilityTest.cs b/SqlSchemaCompare.Test/UtilityTest.cs index be1e512..774b662 100644 --- a/SqlSchemaCompare.Test/UtilityTest.cs +++ b/SqlSchemaCompare.Test/UtilityTest.cs @@ -26,7 +26,7 @@ public static (string file1, string file2, string errors) Compare(string originS return (file1, file2, errors); } - public static (string updateFile, string errors) UpdateSchema(string originSchema, string destinationSchema, string databaseName, IEnumerable dbObjectTypes) + public static (string updateFile, string errors) UpdateSchema(string originSchema, string destinationSchema, IEnumerable dbObjectTypes) { IDbObjectFactory dbObjectFactory = new TSqlObjectFactory(); ISchemaBuilder schemaBuilder = new TSqlSchemaBuilder(); @@ -36,7 +36,7 @@ public static (string updateFile, string errors) UpdateSchema(string originSchem var (originDbObjects, destinationDbObjects, errors) = loadSchemaManager.LoadSchema(originSchema, destinationSchema); UpdateSchemaManager updateSchemaManager = new(schemaBuilder, dbObjectFactory, errorWriter); - string updateSchema= updateSchemaManager.UpdateSchema(originDbObjects, destinationDbObjects, databaseName, dbObjectTypes); + string updateSchema= updateSchemaManager.UpdateSchema(originDbObjects, destinationDbObjects, dbObjectTypes); return (updateSchema, errors); } diff --git a/SqlSchemaCompare.WindowsForm/FormSettings.cs b/SqlSchemaCompare.WindowsForm/FormSettings.cs index 99eb50c..451ef32 100644 --- a/SqlSchemaCompare.WindowsForm/FormSettings.cs +++ b/SqlSchemaCompare.WindowsForm/FormSettings.cs @@ -36,13 +36,6 @@ public String UpdateSchemaFile set { this["UpdateSchemaFile"] = value; } } - [UserScopedSettingAttribute()] - public String DatabaseName - { - get { return (String)this["DatabaseName"]; } - set { this["DatabaseName"] = value; } - } - [UserScopedSettingAttribute()] [DefaultSettingValueAttribute("_diff")] public String Suffix diff --git a/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs b/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs index abeeabf..7184e8d 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs @@ -54,8 +54,6 @@ private void InitializeComponent() this.btnUpdateSchema = new System.Windows.Forms.Button(); this.txtUpdateSchemaFile = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); - this.txtDatabaseName = new System.Windows.Forms.TextBox(); - this.label1 = new System.Windows.Forms.Label(); this.GrpCompare = new System.Windows.Forms.GroupBox(); this.ofdUpdateSchemaFile = new System.Windows.Forms.OpenFileDialog(); this.GrpDbObjects = new System.Windows.Forms.GroupBox(); @@ -100,11 +98,12 @@ private void InitializeComponent() // // BtnClear // + this.BtnClear.Enabled = false; this.BtnClear.Image = global::SqlSchemaCompare.WindowsForm.Properties.Resources.gear; this.BtnClear.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; - this.BtnClear.Location = new System.Drawing.Point(392, 169); + this.BtnClear.Location = new System.Drawing.Point(407, 169); this.BtnClear.Name = "BtnClear"; - this.BtnClear.Size = new System.Drawing.Size(85, 36); + this.BtnClear.Size = new System.Drawing.Size(70, 36); this.BtnClear.TabIndex = 8; this.BtnClear.Text = "Clear"; this.BtnClear.TextAlign = System.Drawing.ContentAlignment.MiddleRight; @@ -202,7 +201,7 @@ private void InitializeComponent() // this.btnCreateUpdateFile.Image = global::SqlSchemaCompare.WindowsForm.Properties.Resources.gear; this.btnCreateUpdateFile.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; - this.btnCreateUpdateFile.Location = new System.Drawing.Point(14, 134); + this.btnCreateUpdateFile.Location = new System.Drawing.Point(16, 96); this.btnCreateUpdateFile.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.btnCreateUpdateFile.Name = "btnCreateUpdateFile"; this.btnCreateUpdateFile.Size = new System.Drawing.Size(150, 36); @@ -248,7 +247,7 @@ private void InitializeComponent() // lblInfo // this.lblInfo.AutoSize = true; - this.lblInfo.Location = new System.Drawing.Point(17, 626); + this.lblInfo.Location = new System.Drawing.Point(17, 597); this.lblInfo.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); this.lblInfo.Name = "lblInfo"; this.lblInfo.Size = new System.Drawing.Size(52, 20); @@ -289,14 +288,13 @@ private void InitializeComponent() this.GrpUpdateSchema.Controls.Add(this.btnUpdateSchema); this.GrpUpdateSchema.Controls.Add(this.txtUpdateSchemaFile); this.GrpUpdateSchema.Controls.Add(this.label2); - this.GrpUpdateSchema.Controls.Add(this.txtDatabaseName); - this.GrpUpdateSchema.Controls.Add(this.label1); this.GrpUpdateSchema.Controls.Add(this.btnCreateUpdateFile); + this.GrpUpdateSchema.Enabled = false; this.GrpUpdateSchema.Location = new System.Drawing.Point(17, 423); this.GrpUpdateSchema.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.GrpUpdateSchema.Name = "GrpUpdateSchema"; this.GrpUpdateSchema.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.GrpUpdateSchema.Size = new System.Drawing.Size(494, 174); + this.GrpUpdateSchema.Size = new System.Drawing.Size(494, 154); this.GrpUpdateSchema.TabIndex = 4; this.GrpUpdateSchema.TabStop = false; this.GrpUpdateSchema.Text = "Update schema"; @@ -304,7 +302,7 @@ private void InitializeComponent() // btnUpdateSchema // this.btnUpdateSchema.Image = global::SqlSchemaCompare.WindowsForm.Properties.Resources.folder; - this.btnUpdateSchema.Location = new System.Drawing.Point(437, 74); + this.btnUpdateSchema.Location = new System.Drawing.Point(437, 41); this.btnUpdateSchema.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.btnUpdateSchema.Name = "btnUpdateSchema"; this.btnUpdateSchema.Size = new System.Drawing.Size(40, 36); @@ -314,7 +312,7 @@ private void InitializeComponent() // // txtUpdateSchemaFile // - this.txtUpdateSchemaFile.Location = new System.Drawing.Point(162, 78); + this.txtUpdateSchemaFile.Location = new System.Drawing.Point(164, 46); this.txtUpdateSchemaFile.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.txtUpdateSchemaFile.Name = "txtUpdateSchemaFile"; this.txtUpdateSchemaFile.Size = new System.Drawing.Size(263, 27); @@ -323,30 +321,12 @@ private void InitializeComponent() // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(14, 90); + this.label2.Location = new System.Drawing.Point(16, 46); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(137, 20); this.label2.TabIndex = 12; this.label2.Text = "Update schema file"; // - // txtDatabaseName - // - this.txtDatabaseName.Location = new System.Drawing.Point(162, 30); - this.txtDatabaseName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.txtDatabaseName.Name = "txtDatabaseName"; - this.txtDatabaseName.Size = new System.Drawing.Size(114, 27); - this.txtDatabaseName.TabIndex = 11; - this.txtDatabaseName.TextChanged += new System.EventHandler(this.TxtDatabaseName_TextChanged); - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(14, 40); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(113, 20); - this.label1.TabIndex = 10; - this.label1.Text = "Database name"; - // // GrpCompare // this.GrpCompare.Controls.Add(this.btnOutputDirectory); @@ -355,6 +335,7 @@ private void InitializeComponent() this.GrpCompare.Controls.Add(this.btnCompare); this.GrpCompare.Controls.Add(this.lblOutputDirectory); this.GrpCompare.Controls.Add(this.lblSuffix); + this.GrpCompare.Enabled = false; this.GrpCompare.Location = new System.Drawing.Point(17, 241); this.GrpCompare.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.GrpCompare.Name = "GrpCompare"; @@ -382,9 +363,10 @@ private void InitializeComponent() this.GrpDbObjects.Controls.Add(this.ChkView); this.GrpDbObjects.Controls.Add(this.ChkTable); this.GrpDbObjects.Controls.Add(this.ChkAll); - this.GrpDbObjects.Location = new System.Drawing.Point(527, 35); + this.GrpDbObjects.Enabled = false; + this.GrpDbObjects.Location = new System.Drawing.Point(527, 31); this.GrpDbObjects.Name = "GrpDbObjects"; - this.GrpDbObjects.Size = new System.Drawing.Size(176, 562); + this.GrpDbObjects.Size = new System.Drawing.Size(176, 546); this.GrpDbObjects.TabIndex = 6; this.GrpDbObjects.TabStop = false; this.GrpDbObjects.Text = "Db objects"; @@ -530,7 +512,7 @@ private void InitializeComponent() // // ProgressBar // - this.ProgressBar.Location = new System.Drawing.Point(527, 628); + this.ProgressBar.Location = new System.Drawing.Point(527, 588); this.ProgressBar.Name = "ProgressBar"; this.ProgressBar.Size = new System.Drawing.Size(176, 29); this.ProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee; @@ -541,7 +523,7 @@ private void InitializeComponent() // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(724, 669); + this.ClientSize = new System.Drawing.Size(724, 634); this.Controls.Add(this.ProgressBar); this.Controls.Add(this.GrpDbObjects); this.Controls.Add(this.GrpCompare); @@ -587,15 +569,12 @@ private void InitializeComponent() private System.Windows.Forms.Button btnCreateUpdateFile; private System.Windows.Forms.GroupBox GrpUpdateSchema; private System.Windows.Forms.GroupBox GrpCompare; - private System.Windows.Forms.TextBox txtDatabaseName; - private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button btnUpdateSchema; private System.Windows.Forms.TextBox txtUpdateSchemaFile; private System.Windows.Forms.OpenFileDialog ofdUpdateSchemaFile; private System.Windows.Forms.Button BtnSwapOriginDestination; private System.Windows.Forms.Button BtnLoadSchema; - private System.Windows.Forms.Button BtnClear; private System.Windows.Forms.GroupBox GrpDbObjects; private System.Windows.Forms.CheckBox ChkUser; private System.Windows.Forms.CheckBox ChkFunction; @@ -610,6 +589,7 @@ private void InitializeComponent() private System.Windows.Forms.CheckBox ChkSchema; private System.ComponentModel.BackgroundWorker BackgroundWorker; private System.Windows.Forms.ProgressBar ProgressBar; + private System.Windows.Forms.Button BtnClear; } } diff --git a/SqlSchemaCompare.WindowsForm/MainForm.cs b/SqlSchemaCompare.WindowsForm/MainForm.cs index 8987969..3ca36a7 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.cs @@ -29,12 +29,6 @@ public MainForm() { InitializeComponent(); lblInfo.Text = ""; - - GrpCompare.Enabled = false; - GrpUpdateSchema.Enabled = false; - GrpDbObjects.Enabled = false; - - BtnClear.Enabled = false; } private void MainForm_Load(object sender, EventArgs e) @@ -46,7 +40,6 @@ private void MainForm_Load(object sender, EventArgs e) txtSuffix.Text = formSettings.Suffix; txtOutputDirectory.Text = formSettings.OutputDirectory; txtUpdateSchemaFile.Text = formSettings.UpdateSchemaFile; - txtDatabaseName.Text = formSettings.DatabaseName; errorWriter = new ErrorWriter(); } @@ -171,7 +164,7 @@ private string GetFileNameDiff(string fileName) private void EnableDisableMainForm(string text, bool enable) { lblInfo.Text = text; - groupBoxMain.Enabled = enable; + GrpCompare.Enabled = enable; GrpDbObjects.Enabled = enable; GrpUpdateSchema.Enabled = enable; @@ -185,16 +178,16 @@ private void EnableDisableMainForm(string text, bool enable) private void LoadClearSchemaCompleted(string text, bool isAfterLoad) { lblInfo.Text = text; - this.Enabled = true; btnOriginSchema.Enabled = !isAfterLoad; btnDestinationSchema.Enabled = !isAfterLoad; BtnSwapOriginDestination.Enabled = !isAfterLoad; BtnLoadSchema.Enabled = !isAfterLoad; - BtnClear.Enabled = isAfterLoad; + GrpCompare.Enabled = isAfterLoad; GrpDbObjects.Enabled = isAfterLoad; GrpUpdateSchema.Enabled = isAfterLoad; + BtnClear.Enabled = isAfterLoad; } private void BtnUpdateSchema_Click(object sender, EventArgs e) @@ -208,12 +201,6 @@ private void BtnUpdateSchema_Click(object sender, EventArgs e) } } - private void TxtDatabaseName_TextChanged(object sender, EventArgs e) - { - formSettings.DatabaseName = txtDatabaseName.Text; - formSettings.Save(); - } - private void TxtSuffix_TextChanged(object sender, EventArgs e) { formSettings.Suffix = txtSuffix.Text; @@ -234,12 +221,6 @@ private void BtnCreateUpdateFile_Click(object sender, EventArgs e) return; } - txtDatabaseName.Text = txtDatabaseName.Text.Trim(); - if (txtDatabaseName.Text == string.Empty) - { - MessageBox.Show("Select a database name", ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); - return; - } if (File.Exists(txtUpdateSchemaFile.Text)) File.Delete(txtUpdateSchemaFile.Text); @@ -255,7 +236,7 @@ private void BtnCreateUpdateFile_Click(object sender, EventArgs e) IDbObjectFactory dbObjectFactory = new TSqlObjectFactory(); ISchemaBuilder schemaBuilder = new TSqlSchemaBuilder(); UpdateSchemaManager updateSchemaManager = new(schemaBuilder, dbObjectFactory, errorWriter); - var updateSchema = updateSchemaManager.UpdateSchema(currentOriginDbObjects, currentDestinationDbObjects, txtDatabaseName.Text, SelectedObjectType()); + var updateSchema = updateSchemaManager.UpdateSchema(currentOriginDbObjects, currentDestinationDbObjects, SelectedObjectType()); StringBuilder stringResult = new(); stringResult.AppendLine($"{schemaBuilder.GetStartCommentInLine()} Update Schema {txtOriginSchema.Text} --> {txtDestinationSchema.Text}"); @@ -321,12 +302,6 @@ private void LoadSchemaCompleted(object sender, System.ComponentModel.RunWorkerC ProgressBar.Hide(); LoadClearSchemaCompleted(string.IsNullOrEmpty(e.Result as string) ? SchemaLoaded : SchemaLoadedWithErrors, true); } - - private void BtnClear_Click(object sender, EventArgs e) - { - LoadClearSchemaCompleted(ChooseCompareUpdate, false); - } - private void ChkAll_CheckedChanged(object sender, EventArgs e) { ChkFunction.Checked = ChkAll.Checked; @@ -378,7 +353,12 @@ private IEnumerable SelectedObjectType() return selectedObjectType; } - + + private void BtnClear_Click(object sender, EventArgs e) + { + LoadClearSchemaCompleted(ChooseCompareUpdate, false); + } + public record ParametersLoad(string ErrorFile, string Origin, string Destination); } }