mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Sql Proj Extract from database to Sql files (#949)
* SqlToolsServices changes for Sql Proj Extract from database to Sql files * Bumped DacFx version. Addressed comments.
This commit is contained in:
@@ -2,10 +2,11 @@
|
|||||||
// Copyright (c) Microsoft. All rights reserved.
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
//
|
//
|
||||||
|
using System;
|
||||||
|
using Microsoft.SqlServer.Dac;
|
||||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.DacFx.Contracts
|
namespace Microsoft.SqlTools.ServiceLayer.DacFx.Contracts
|
||||||
{
|
{
|
||||||
@@ -23,6 +24,11 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx.Contracts
|
|||||||
/// Gets or sets the version of the DAC application
|
/// Gets or sets the version of the DAC application
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ApplicationVersion { get; set; }
|
public string ApplicationVersion { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the target for extraction
|
||||||
|
/// </summary>
|
||||||
|
public DacExtractTarget ExtractTarget { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
// Copyright (c) Microsoft. All rights reserved.
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
//
|
//
|
||||||
|
using Microsoft.SqlServer.Dac;
|
||||||
using Microsoft.SqlTools.Hosting.Protocol;
|
using Microsoft.SqlTools.Hosting.Protocol;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||||
@@ -117,7 +118,8 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
|||||||
// Set connection details database name to ensure the connection string gets created correctly for DW(extract doesn't work if connection is to master)
|
// Set connection details database name to ensure the connection string gets created correctly for DW(extract doesn't work if connection is to master)
|
||||||
connInfo.ConnectionDetails.DatabaseName = parameters.DatabaseName;
|
connInfo.ConnectionDetails.DatabaseName = parameters.DatabaseName;
|
||||||
ExtractOperation operation = new ExtractOperation(parameters, connInfo);
|
ExtractOperation operation = new ExtractOperation(parameters, connInfo);
|
||||||
ExecuteOperation(operation, parameters, SR.ExtractDacpacTaskName, requestContext);
|
string taskName = parameters.ExtractTarget == DacExtractTarget.DacPac ? SR.ExtractDacpacTaskName : SR.ProjectExtractTaskName;
|
||||||
|
ExecuteOperation(operation, parameters, taskName, requestContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
|||||||
public override void Execute()
|
public override void Execute()
|
||||||
{
|
{
|
||||||
Version version = ParseVersion(this.Parameters.ApplicationVersion);
|
Version version = ParseVersion(this.Parameters.ApplicationVersion);
|
||||||
this.DacServices.Extract(this.Parameters.PackageFilePath, this.Parameters.DatabaseName, this.Parameters.ApplicationName, version, null, null, null, this.CancellationToken);
|
DacExtractOptions extractOptions = GetExtractOptions(this.Parameters.ExtractTarget);
|
||||||
|
this.DacServices.Extract(this.Parameters.PackageFilePath, this.Parameters.DatabaseName, this.Parameters.ApplicationName, version, applicationDescription:null, tables:null, extractOptions:extractOptions, cancellationToken:this.CancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Version ParseVersion(string incomingVersion)
|
public static Version ParseVersion(string incomingVersion)
|
||||||
@@ -43,5 +44,11 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
|||||||
|
|
||||||
return parsedVersion;
|
return parsedVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DacExtractOptions GetExtractOptions(DacExtractTarget extractTarget)
|
||||||
|
{
|
||||||
|
DacExtractOptions extractOptions = new DacExtractOptions() { ExtractTarget = extractTarget };
|
||||||
|
return extractOptions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2941,6 +2941,14 @@ namespace Microsoft.SqlTools.ServiceLayer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string ProjectExtractTaskName
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return Keys.GetString(Keys.ProjectExtractTaskName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static string ExtractInvalidVersion
|
public static string ExtractInvalidVersion
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -4390,6 +4398,9 @@ namespace Microsoft.SqlTools.ServiceLayer
|
|||||||
public const string GenerateScriptTaskName = "GenerateScriptTaskName";
|
public const string GenerateScriptTaskName = "GenerateScriptTaskName";
|
||||||
|
|
||||||
|
|
||||||
|
public const string ProjectExtractTaskName = "ProjectExtractTaskName";
|
||||||
|
|
||||||
|
|
||||||
public const string ExtractInvalidVersion = "ExtractInvalidVersion";
|
public const string ExtractInvalidVersion = "ExtractInvalidVersion";
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1748,6 +1748,10 @@
|
|||||||
<value>Generate script</value>
|
<value>Generate script</value>
|
||||||
<comment></comment>
|
<comment></comment>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="ProjectExtractTaskName" xml:space="preserve">
|
||||||
|
<value>Extract project files</value>
|
||||||
|
<comment></comment>
|
||||||
|
</data>
|
||||||
<data name="ExtractInvalidVersion" xml:space="preserve">
|
<data name="ExtractInvalidVersion" xml:space="preserve">
|
||||||
<value>Invalid version '{0}' passed. Version must be in the format x.x.x.x where x is a number.</value>
|
<value>Invalid version '{0}' passed. Version must be in the format x.x.x.x where x is a number.</value>
|
||||||
<comment></comment>
|
<comment></comment>
|
||||||
|
|||||||
@@ -809,6 +809,7 @@ ImportBacpacTaskName = Import bacpac
|
|||||||
ExtractDacpacTaskName = Extract dacpac
|
ExtractDacpacTaskName = Extract dacpac
|
||||||
DeployDacpacTaskName = Deploy dacpac
|
DeployDacpacTaskName = Deploy dacpac
|
||||||
GenerateScriptTaskName = Generate script
|
GenerateScriptTaskName = Generate script
|
||||||
|
ProjectExtractTaskName = Extract project files
|
||||||
ExtractInvalidVersion = Invalid version '{0}' passed. Version must be in the format x.x.x.x where x is a number.
|
ExtractInvalidVersion = Invalid version '{0}' passed. Version must be in the format x.x.x.x where x is a number.
|
||||||
|
|
||||||
############################################################################
|
############################################################################
|
||||||
|
|||||||
@@ -2082,6 +2082,11 @@
|
|||||||
<note>.
|
<note>.
|
||||||
Parameters: 0 - editionCode (int) </note>
|
Parameters: 0 - editionCode (int) </note>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="ProjectExtractTaskName">
|
||||||
|
<source>Extract project files</source>
|
||||||
|
<target state="new">Extract project files</target>
|
||||||
|
<note></note>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" Version="1.0.0" />
|
<PackageReference Include="Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" Version="1.0.0" />
|
||||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4745.1-preview" />
|
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4763.1-preview" />
|
||||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0-preview3-26501-04" />
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0-preview3-26501-04" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||||
<PackageReference Include="xunit" Version="2.2.0" />
|
<PackageReference Include="xunit" Version="2.2.0" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4745.1-preview" />
|
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4763.1-preview" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
// Copyright (c) Microsoft. All rights reserved.
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
//
|
//
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.SqlServer.Dac;
|
||||||
using Microsoft.SqlTools.Hosting.Protocol;
|
using Microsoft.SqlTools.Hosting.Protocol;
|
||||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||||
@@ -9,9 +13,6 @@ using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
|||||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||||
@@ -159,6 +160,82 @@ CREATE TABLE [dbo].[table3]
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify the extract request to create Sql file
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public async void ExtractDBToFileTarget()
|
||||||
|
{
|
||||||
|
var result = GetLiveAutoCompleteTestObjects();
|
||||||
|
SqlTestDb testdb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, doNotCleanupDb:false, databaseName:null, query:SourceScript, dbNamePrefix:"DacFxExtractDBToFileTarget");
|
||||||
|
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DacFxTest");
|
||||||
|
Directory.CreateDirectory(folderPath);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var extractParams = new ExtractParams
|
||||||
|
{
|
||||||
|
DatabaseName = testdb.DatabaseName,
|
||||||
|
PackageFilePath = Path.Combine(folderPath, string.Format("{0}.sql", testdb.DatabaseName)),
|
||||||
|
ApplicationName = "test",
|
||||||
|
ApplicationVersion = "1.0.0.0",
|
||||||
|
ExtractTarget = DacExtractTarget.File
|
||||||
|
};
|
||||||
|
|
||||||
|
DacFxService service = new DacFxService();
|
||||||
|
ExtractOperation operation = new ExtractOperation(extractParams, result.ConnectionInfo);
|
||||||
|
service.PerformOperation(operation, TaskExecutionMode.Execute);
|
||||||
|
|
||||||
|
VerifyAndCleanup(extractParams.PackageFilePath);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
testdb.Cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify the extract request to create a Flat file structure
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public async void ExtractDBToFlatTarget()
|
||||||
|
{
|
||||||
|
var result = GetLiveAutoCompleteTestObjects();
|
||||||
|
SqlTestDb testdb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, doNotCleanupDb: false, databaseName: null, query: SourceScript, dbNamePrefix: "DacFxExtractDBToFlatTarget");
|
||||||
|
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DacFxTest","FlatExtract");
|
||||||
|
Directory.CreateDirectory(folderPath);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var extractParams = new ExtractParams
|
||||||
|
{
|
||||||
|
DatabaseName = testdb.DatabaseName,
|
||||||
|
PackageFilePath = folderPath,
|
||||||
|
ApplicationName = "test",
|
||||||
|
ApplicationVersion = "1.0.0.0",
|
||||||
|
ExtractTarget = DacExtractTarget.Flat
|
||||||
|
};
|
||||||
|
|
||||||
|
DacFxService service = new DacFxService();
|
||||||
|
ExtractOperation operation = new ExtractOperation(extractParams, result.ConnectionInfo);
|
||||||
|
service.PerformOperation(operation, TaskExecutionMode.Execute);
|
||||||
|
|
||||||
|
// Verify two sql files are generated in the target folder path
|
||||||
|
int actualCnt = Directory.GetFiles(folderPath, "*.sql", SearchOption.AllDirectories).Length;
|
||||||
|
Assert.Equal(actualCnt, 2);
|
||||||
|
|
||||||
|
// Remove the directory
|
||||||
|
if (Directory.Exists(folderPath))
|
||||||
|
{
|
||||||
|
Directory.Delete(folderPath, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
testdb.Cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Verify the deploy dacpac request
|
/// Verify the deploy dacpac request
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||||
<PackageReference Include="xunit" Version="2.2.0" />
|
<PackageReference Include="xunit" Version="2.2.0" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4745.1-preview" />
|
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4763.1-preview" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||||
|
|||||||
Reference in New Issue
Block a user