added script as alter (#564)

* added script as alter
This commit is contained in:
Leila Lali
2017-12-07 12:49:38 -08:00
committed by GitHub
parent 4dd30c5341
commit d20215cb19
12 changed files with 87 additions and 17 deletions

Binary file not shown.

View File

@@ -19,7 +19,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.9" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />

View File

@@ -18,8 +18,8 @@
<Reference Include="System.Data.SqlClient" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.9" />
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" />

View File

@@ -15,6 +15,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting.Contracts
Insert = 2,
Update = 3,
Delete = 4,
Execute = 5
Execute = 5,
Alter = 6
}
}

View File

@@ -87,6 +87,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
switch (this.Parameters.Operation)
{
case ScriptingOperationType.Create:
case ScriptingOperationType.Alter:
case ScriptingOperationType.Delete: // Using Delete here is wrong. delete usually means delete rows from table but sqlopsstudio sending the operation name as delete instead of drop
resultScript = GenerateScriptAs(server, urns, options);
break;
@@ -438,6 +439,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
try
{
scripter = new SqlServer.Management.Smo.Scripter(server);
if(this.Parameters.Operation == ScriptingOperationType.Alter)
{
options.ScriptForAlter = true;
foreach (var urn in urns)
{
SqlSmoObject smoObject = server.GetSmoObject(urn);
// without calling the toch method, no alter script get generated from smo
smoObject.Touch();
}
}
scripter.Options = options;
scripter.ScriptingError += ScripterScriptingError;

View File

@@ -163,7 +163,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
// To script Select, alter and execute use scripting as operation. The other operation doesn't support those types
if( (parameters.ScriptingObjects != null && parameters.ScriptingObjects.Count == 1 && parameters.ScriptOptions != null
&& parameters.ScriptOptions.TypeOfDataToScript == "SchemaOnly" && parameters.ScriptDestination == "ToEditor") ||
parameters.Operation == ScriptingOperationType.Select || parameters.Operation == ScriptingOperationType.Execute)
parameters.Operation == ScriptingOperationType.Select || parameters.Operation == ScriptingOperationType.Execute ||
parameters.Operation == ScriptingOperationType.Alter)
{
return true;
}

View File

@@ -32,7 +32,7 @@
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.9" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />

View File

@@ -12,6 +12,7 @@ using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
@@ -113,10 +114,51 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
Type = "Table"
};
string expectedScript = null;
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
}
[Fact]
public async void VerifyScriptAsAlter()
{
string query = @"CREATE PROCEDURE testSp1 @StartProductID [int] AS BEGIN Select * from sys.all_columns END
GO
CREATE VIEW testView1 AS SELECT * from sys.all_columns
GO
CREATE FUNCTION testFun1() RETURNS [int] AS BEGIN RETURN 1 END
GO";
ScriptingOperationType scriptCreateDrop = ScriptingOperationType.Alter;
List<ScriptingObject> scriptingObjects = new List<ScriptingObject>
{
new ScriptingObject
{
Name = "testSp1",
Schema = "dbo",
Type = "StoredProcedure"
},
new ScriptingObject
{
Name = "testView1",
Schema = "dbo",
Type = "View"
},
new ScriptingObject
{
Name = "testFun1",
Schema = "dbo",
Type = "UserDefinedFunction"
}
};
List<string> expectedScripts = new List<string>
{
"ALTER PROCEDURE [dbo].[testSp1]",
"ALTER VIEW [dbo].[testView1]",
"ALTER FUNCTION [dbo].[testFun1]"
};
await VerifyScriptAsForMultipleObjects(query, scriptingObjects, scriptCreateDrop, expectedScripts);
}
[Fact]
public async void VerifyScriptAsExecuteStoredProcedure()
{
@@ -238,6 +280,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
}
private async Task VerifyScriptAs(string query, ScriptingObject scriptingObject, ScriptingOperationType operation, string expectedScript)
{
await VerifyScriptAsForMultipleObjects(query, new List<ScriptingObject> { scriptingObject }, operation, new List<string> { expectedScript });
}
private async Task VerifyScriptAsForMultipleObjects(string query, List<ScriptingObject> scriptingObjects, ScriptingOperationType operation, List<string> expectedScripts)
{
var testDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, query, "ScriptingTests");
try
@@ -271,10 +318,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
ScriptCreateDrop = scriptCreateOperation,
};
scriptingParams.ScriptingObjects = new List<ScriptingObject>
{
scriptingObject
};
scriptingParams.ScriptingObjects = scriptingObjects;
ScriptingService service = new ScriptingService();
@@ -282,7 +326,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
Thread.Sleep(2000);
await service.ScriptingTask;
requestContext.Verify(x => x.SendResult(It.Is<ScriptingResult>(r => VerifyScriptingResult(r, expectedScript))));
requestContext.Verify(x => x.SendResult(It.Is<ScriptingResult>(r => VerifyScriptingResult(r, expectedScripts))));
connectionService.Disconnect(new ServiceLayer.Connection.Contracts.DisconnectParams
{
OwnerUri = queryTempFile.FilePath
@@ -299,9 +343,21 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
}
}
private static bool VerifyScriptingResult(ScriptingResult result, string expected)
private static bool VerifyScriptingResult(ScriptingResult result, List<string> expectedScripts)
{
return string.IsNullOrEmpty(expected) ? string.IsNullOrEmpty(result.Script) : !string.IsNullOrEmpty(result.Script) && result.Script.Contains(expected);
if (expectedScripts == null || (expectedScripts.Count > 0 && expectedScripts.All(x => x == null)))
{
return string.IsNullOrEmpty(result.Script);
}
foreach (string expectedScript in expectedScripts)
{
if (!result.Script.Contains(expectedScript))
{
return false;
}
}
return true;
}
}
}

View File

@@ -12,7 +12,7 @@
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.9" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Scripts/CreateTestDatabaseObjects.sql" />

View File

@@ -12,7 +12,7 @@
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.9" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Microsoft.SqlTools.Hosting/Microsoft.SqlTools.Hosting.csproj" />

View File

@@ -13,7 +13,7 @@
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.9" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">