mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-17 02:51:45 -05:00
Fix schema compare script formatting (#809)
* Add GO after each statement and remove leading and trailing whitespace * Addressing comments * formatting
This commit is contained in:
@@ -160,13 +160,13 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
|||||||
{
|
{
|
||||||
string sourceScript;
|
string sourceScript;
|
||||||
difference.SourceObject.TryGetScript(out sourceScript);
|
difference.SourceObject.TryGetScript(out sourceScript);
|
||||||
diffEntry.SourceScript = RemoveExcessWhitespace(sourceScript);
|
diffEntry.SourceScript = FormatScript(sourceScript);
|
||||||
}
|
}
|
||||||
if (difference.TargetObject != null)
|
if (difference.TargetObject != null)
|
||||||
{
|
{
|
||||||
string targetScript;
|
string targetScript;
|
||||||
difference.TargetObject.TryGetScript(out targetScript);
|
difference.TargetObject.TryGetScript(out targetScript);
|
||||||
diffEntry.TargetScript = RemoveExcessWhitespace(targetScript);
|
diffEntry.TargetScript = FormatScript(targetScript);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,10 +210,26 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
|||||||
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
|
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string RemoveExcessWhitespace(string script)
|
public static string RemoveExcessWhitespace(string script)
|
||||||
{
|
{
|
||||||
|
if (script != null)
|
||||||
|
{
|
||||||
|
// remove leading and trailing whitespace
|
||||||
|
script = script.Trim();
|
||||||
// replace all multiple spaces with single space
|
// replace all multiple spaces with single space
|
||||||
return Regex.Replace(script, " {2,}", " ");
|
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)
|
private static string GetName(string name)
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user