From 5392f81d54ba97e2de3c6ffa6ae340933f0c904f Mon Sep 17 00:00:00 2001 From: kisantia <31145923+kisantia@users.noreply.github.com> Date: Mon, 13 May 2019 10:04:02 -0700 Subject: [PATCH] Fix schema compare script formatting (#809) * Add GO after each statement and remove leading and trailing whitespace * Addressing comments * formatting --- .../SchemaCompare/SchemaCompareOperation.cs | 30 +++++-- .../SchemaCompare/SchemaCompareTests.cs | 89 +++++++++++++++++++ 2 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 test/Microsoft.SqlTools.ServiceLayer.UnitTests/SchemaCompare/SchemaCompareTests.cs diff --git a/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs b/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs index 2d05e9d5..1a7858f1 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs @@ -134,7 +134,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent) { - if(difference == null) + if (difference == null) { return null; } @@ -160,16 +160,16 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare { string sourceScript; difference.SourceObject.TryGetScript(out sourceScript); - diffEntry.SourceScript = RemoveExcessWhitespace(sourceScript); + diffEntry.SourceScript = FormatScript(sourceScript); } if (difference.TargetObject != null) { string targetScript; difference.TargetObject.TryGetScript(out targetScript); - diffEntry.TargetScript = RemoveExcessWhitespace(targetScript); + diffEntry.TargetScript = FormatScript(targetScript); } } - + diffEntry.Children = new List(); foreach (SchemaDifference child in difference.Children) @@ -210,10 +210,26 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails); } - private static string RemoveExcessWhitespace(string script) + public static string RemoveExcessWhitespace(string script) { - // replace all multiple spaces with single space - return Regex.Replace(script, " {2,}", " "); + if (script != null) + { + // remove leading and trailing whitespace + script = script.Trim(); + // replace all multiple spaces with single space + script = Regex.Replace(script, " {2,}", " "); + } + return script; + } + + public static string FormatScript(string script) + { + script = RemoveExcessWhitespace(script); + if (!string.IsNullOrWhiteSpace(script) && !script.Equals("null")) + { + script += Environment.NewLine + "GO"; + } + return script; } private static string GetName(string name) diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/SchemaCompare/SchemaCompareTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/SchemaCompare/SchemaCompareTests.cs new file mode 100644 index 00000000..8a315c2a --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/SchemaCompare/SchemaCompareTests.cs @@ -0,0 +1,89 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using Microsoft.SqlTools.ServiceLayer.SchemaCompare; +using Xunit; + +namespace Microsoft.SqlTools.ServiceLayer.UnitTests.SchemaCompare +{ + public class SchemaCompareTests + { + [Fact] + public void FormatScriptAddsGo() + { + string script = "EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'Primary key for AWBuildVersion records.', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'AWBuildVersion', @level2type = N'COLUMN', @level2name = N'SystemInformationID';"; + Assert.DoesNotContain("GO", script); + string result = SchemaCompareOperation.FormatScript(script); + Assert.EndsWith("GO", result); + } + + [Fact] + public void FormatScriptDoesNotAddGoForNullScripts() + { + string script1 = null; + string result1 = SchemaCompareOperation.FormatScript(script1); + Assert.DoesNotContain("GO", result1); + Assert.Equal(null, result1); + + string script2 = "null"; + string result2 = SchemaCompareOperation.FormatScript(script2); + Assert.DoesNotContain("GO", result2); + } + + [Fact] + public void FormatScriptDoesNotAddGoForEmptyStringScripts() + { + string script = string.Empty; + string result = SchemaCompareOperation.FormatScript(script); + Assert.DoesNotContain("GO", result); + Assert.Equal(string.Empty, result); + } + + [Fact] + public void FormatScriptDoesNotAddGoForWhitespaceStringScripts() + { + string script = " \t\n"; + Assert.True(string.IsNullOrWhiteSpace(script)); + string result = SchemaCompareOperation.FormatScript(script); + Assert.DoesNotContain("GO", result); + Assert.Equal(string.Empty, result); + } + + [Fact] + public void RemovesExcessWhitespace() + { + // leading whitespace + string script1 = "\r\n EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'Primary key for AWBuildVersion records.', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'AWBuildVersion', @level2type = N'COLUMN', @level2name = N'SystemInformationID';"; + string result1 = SchemaCompareOperation.RemoveExcessWhitespace(script1); + Assert.False(script1.Equals(result1)); + Assert.False(result1.StartsWith("\r")); + Assert.True(result1.StartsWith("EXECUTE")); + + // trailing whitespace + string script2 = "EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'Primary key for AWBuildVersion records.', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'AWBuildVersion', @level2type = N'COLUMN', @level2name = N'SystemInformationID'; \n"; + string result2 = SchemaCompareOperation.RemoveExcessWhitespace(script2); + Assert.False(script2.Equals(result2)); + Assert.False(result2.EndsWith("\n")); + Assert.True(result2.EndsWith(";")); + + // non-leading/trailing multiple spaces + string script3 = @"CREATE TABLE [dbo].[AWBuildVersion] ( + [SystemInformationID] TINYINT IDENTITY (1, 1) NOT NULL, + [Database Version] NVARCHAR (25) NOT NULL, + [VersionDate] DATETIME NOT NULL, + [ModifiedDate] DATETIME NOT NULL +);"; + string expected3 = @"CREATE TABLE [dbo].[AWBuildVersion] ( + [SystemInformationID] TINYINT IDENTITY (1, 1) NOT NULL, + [Database Version] NVARCHAR (25) NOT NULL, + [VersionDate] DATETIME NOT NULL, + [ModifiedDate] DATETIME NOT NULL +);"; + string result3 = SchemaCompareOperation.RemoveExcessWhitespace(script3); + Assert.True(expected3.Equals(result3)); + } + } +}