Support for database references in SqlProjects service (#1832)

* initial checkin

* Initial

* Adding test

* Adding comments

* PR feedback
This commit is contained in:
Benjin Dubishar
2023-01-31 16:52:07 -08:00
committed by GitHub
parent 036ce9b527
commit 9fe3aeddc3
16 changed files with 289 additions and 10 deletions

View File

@@ -0,0 +1,32 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
/// <summary>
/// Parameters for adding a Dacpac reference to a SQL project
/// </summary>
public class AddDacpacReferenceParams : AddDatabaseReferenceParams
{
/// <summary>
/// Path to the .dacpac file
/// </summary>
public string DacpacPath { get; set; }
/// <summary>
/// SQLCMD variable name for specifying the other server this reference is to, if different from that of the current project.
/// If this is set, DatabaseVariable must also be set.
/// </summary>
public string? ServerVariable { get; set; }
}
public class AddDacpacReferenceRequest
{
public static readonly RequestType<AddDacpacReferenceParams, ResultStatus> Type = RequestType<AddDacpacReferenceParams, ResultStatus>.Create("sqlprojects/addDacpacReference");
}
}

View File

@@ -0,0 +1,23 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
/// <summary>
/// Base class for add database reference request paramaters
/// </summary>
public abstract class AddDatabaseReferenceParams : SqlProjectParams
{
/// <summary>
/// Whether to suppress missing dependencies
/// </summary>
public bool SuppressMissingDependencies { get; set; }
/// <summary>
/// SQLCMD variable name for specifying the other database this reference is to, if different from that of the current project
/// </summary>
public string? DatabaseVariable { get; set; }
}
}

View File

@@ -0,0 +1,38 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
/// <summary>
/// Parameters for adding a reference to another SQL project
/// </summary>
public class AddSqlProjectReferenceParams : AddDatabaseReferenceParams
{
/// <summary>
/// Path to the referenced .sqlproj file
/// </summary>
public string ProjectPath { get; set; }
/// <summary>
/// GUID for the referenced SQL project
/// </summary>
public string? ProjectGuid { get; set; }
/// <summary>
/// SQLCMD variable name for specifying the other server this reference is to, if different from that of the current project.
/// If this is set, DatabaseVariable must also be set.
/// </summary>
public string? ServerVariable { get; set; }
}
public class AddSqlProjectReferenceRequest
{
public static readonly RequestType<AddSqlProjectReferenceParams, ResultStatus> Type = RequestType<AddSqlProjectReferenceParams, ResultStatus>.Create("sqlprojects/addSqlProjectReference");
}
}

View File

@@ -0,0 +1,27 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlServer.Dac.Projects;
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
/// <summary>
/// Parameters for adding a reference to a system database
/// </summary>
public class AddSystemDatabaseReferenceParams : AddDatabaseReferenceParams
{
/// <summary>
/// Type of system database
/// </summary>
public SystemDatabase SystemDatabase { get; set; }
}
public class AddSystemDatabaseReferenceRequest
{
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlprojects/addSystemDatabaseReference");
}
}

View File

@@ -0,0 +1,26 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
/// <summary>
/// Parameters for deleting a database reference
/// </summary>
public class DeleteDatabaseReferenceParams : SqlProjectParams
{
/// <summary>
/// Name of the reference to be deleted. Name of the System DB, path of the sqlproj, or path of the dacpac
/// </summary>
public string Name { get; set; }
}
public class DeleteDatabaseReferenceRequest
{
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlprojects/deleteDatabaseReference");
}
}

View File

@@ -10,6 +10,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class CloseSqlProjectRequest
{
public static readonly RequestType<SqlProjectParams, ResultStatus> Type = RequestType<SqlProjectParams, ResultStatus>.Create("sqlprojects/closeProject");
public static readonly RequestType<SqlProjectParams, ResultStatus> Type = RequestType<SqlProjectParams, ResultStatus>.Create("sqlProjects/closeProject");
}
}

View File

@@ -34,6 +34,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
public class NewSqlProjectRequest
{
public static readonly RequestType<NewSqlProjectParams, ResultStatus> Type = RequestType<NewSqlProjectParams, ResultStatus>.Create("sqlprojects/newProject");
public static readonly RequestType<NewSqlProjectParams, ResultStatus> Type = RequestType<NewSqlProjectParams, ResultStatus>.Create("sqlProjects/newProject");
}
}

View File

@@ -10,6 +10,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class OpenSqlProjectRequest
{
public static readonly RequestType<SqlProjectParams, ResultStatus> Type = RequestType<SqlProjectParams, ResultStatus>.Create("sqlprojects/openProject");
public static readonly RequestType<SqlProjectParams, ResultStatus> Type = RequestType<SqlProjectParams, ResultStatus>.Create("sqlProjects/openProject");
}
}

View File

@@ -31,6 +31,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
public class AddSqlCmdVariableRequest
{
public static readonly RequestType<AddSqlCmdVariableParams, ResultStatus> Type = RequestType<AddSqlCmdVariableParams, ResultStatus>.Create("sqlprojects/addSqlCmdVariable");
public static readonly RequestType<AddSqlCmdVariableParams, ResultStatus> Type = RequestType<AddSqlCmdVariableParams, ResultStatus>.Create("sqlProjects/addSqlCmdVariable");
}
}

View File

@@ -21,6 +21,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
public class DeleteSqlCmdVariableRequest
{
public static readonly RequestType<DeleteSqlCmdVariableParams, ResultStatus> Type = RequestType<DeleteSqlCmdVariableParams, ResultStatus>.Create("sqlprojects/deleteSqlCmdVariable");
public static readonly RequestType<DeleteSqlCmdVariableParams, ResultStatus> Type = RequestType<DeleteSqlCmdVariableParams, ResultStatus>.Create("sqlProjects/deleteSqlCmdVariable");
}
}

View File

@@ -10,6 +10,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class UpdateSqlCmdVariableRequest
{
public static readonly RequestType<AddSqlCmdVariableParams, ResultStatus> Type = RequestType<AddSqlCmdVariableParams, ResultStatus>.Create("sqlprojects/updateSqlCmdVariable");
public static readonly RequestType<AddSqlCmdVariableParams, ResultStatus> Type = RequestType<AddSqlCmdVariableParams, ResultStatus>.Create("sqlProjects/updateSqlCmdVariable");
}
}

View File

@@ -10,6 +10,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class AddSqlObjectScriptRequest
{
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlprojects/addSqlObjectScript");
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlProjects/addSqlObjectScript");
}
}

View File

@@ -10,6 +10,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class DeleteSqlObjectScriptRequest
{
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlprojects/deleteSqlObjectScript");
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlProjects/deleteSqlObjectScript");
}
}

View File

@@ -10,6 +10,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class ExcludeSqlObjectScriptRequest
{
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlprojects/excludeSqlObjectScript");
public static readonly RequestType<SqlProjectScriptParams, ResultStatus> Type = RequestType<SqlProjectScriptParams, ResultStatus>.Create("sqlProjects/excludeSqlObjectScript");
}
}

View File

@@ -99,6 +99,48 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects
#endregion
#region Database Reference calls
internal async Task HandleAddSystemDatabaseReferenceRequest(AddSystemDatabaseReferenceParams requestParams, RequestContext<ResultStatus> requestContext)
{
await RunWithErrorHandling(() => GetProject(requestParams.ProjectUri).DatabaseReferences.Add(
new SystemDatabaseReference(
requestParams.SystemDatabase,
requestParams.SuppressMissingDependencies,
requestParams.DatabaseVariable)),
requestContext);
}
internal async Task HandleAddDacpacReferenceRequest(AddDacpacReferenceParams requestParams, RequestContext<ResultStatus> requestContext)
{
await RunWithErrorHandling(() => GetProject(requestParams.ProjectUri).DatabaseReferences.Add(
new DacpacReference(
requestParams.DacpacPath,
requestParams.SuppressMissingDependencies,
requestParams.DatabaseVariable,
requestParams.ServerVariable)),
requestContext);
}
internal async Task HandleAddSqlProjectReferenceRequest(AddSqlProjectReferenceParams requestParams, RequestContext<ResultStatus> requestContext)
{
await RunWithErrorHandling(() => GetProject(requestParams.ProjectUri).DatabaseReferences.Add(
new SqlProjectReference(
requestParams.ProjectPath,
requestParams.ProjectGuid,
requestParams.SuppressMissingDependencies,
requestParams.DatabaseVariable,
requestParams.ServerVariable)),
requestContext);
}
internal async Task HandleDeleteDatabaseReferenceRequest(DeleteDatabaseReferenceParams requestParams, RequestContext<ResultStatus> requestContext)
{
await RunWithErrorHandling(() => GetProject(requestParams.ProjectUri).DatabaseReferences.Delete(requestParams.Name), requestContext);
}
#endregion
#region SQLCMD variable functions
internal async Task HandleAddSqlCmdVariableRequest(AddSqlCmdVariableParams requestParams, RequestContext<ResultStatus> requestContext)

View File

@@ -18,6 +18,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SqlProjects
{
public class SqlProjectsServiceTests : TestBase
{
internal const string TEST_GUID = "BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575";
[Test]
public async Task TestErrorDuringExecution()
{
@@ -157,6 +159,95 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SqlProjects
Assert.IsFalse(File.Exists(scriptFullPath), $"{scriptFullPath} expected to have been deleted from disk");
}
[Test]
public async Task TestDatabaseReferenceAddDelete()
{
// Setup
SqlProjectsService service = new();
string projectUri = await service.CreateSqlProject();
SqlCmdVariable databaseVar = new SqlCmdVariable("$(OtherDb)", "OtherDbDefaultValue", "OtherDbValue");
SqlCmdVariable serverVar = new SqlCmdVariable("$(OtherServer)", "OtherServerDefaultValue", "OtherServerValue");
service.Projects[projectUri].SqlCmdVariables.Add(databaseVar);
service.Projects[projectUri].SqlCmdVariables.Add(serverVar);
Assert.AreEqual(0, service.Projects[projectUri].DatabaseReferences.Count, "Baseline number of database references");
// Validate adding a system database reference
MockRequest<ResultStatus> requestMock = new();
await service.HandleAddSystemDatabaseReferenceRequest(new AddSystemDatabaseReferenceParams()
{
ProjectUri = projectUri,
SystemDatabase = SystemDatabase.MSDB,
DatabaseVariable = "$(EmEssDeeBee)",
SuppressMissingDependencies = false
}, requestMock.Object);
requestMock.AssertSuccess(nameof(service.HandleAddSystemDatabaseReferenceRequest));
Assert.AreEqual(1, service.Projects[projectUri].DatabaseReferences.Count, "Database references after adding system db reference");
SystemDatabaseReference systemDbRef = (SystemDatabaseReference)service.Projects[projectUri].DatabaseReferences.First(x => x is SystemDatabaseReference);
Assert.AreEqual(SystemDatabase.MSDB, systemDbRef.SystemDb, "Referenced system DB");
Assert.AreEqual("$(EmEssDeeBee)", systemDbRef.DatabaseVariable);
Assert.IsFalse(systemDbRef.SuppressMissingDependencies, nameof(systemDbRef.SuppressMissingDependencies));
// Validate adding a dacpac reference
string mockReferencePath = Path.Join(Path.GetDirectoryName(projectUri), "OtherDatabase.dacpac");
requestMock = new();
await service.HandleAddDacpacReferenceRequest(new AddDacpacReferenceParams()
{
ProjectUri = projectUri,
DacpacPath = mockReferencePath,
SuppressMissingDependencies = false,
DatabaseVariable = databaseVar.Name,
ServerVariable = serverVar.Name
}, requestMock.Object);
requestMock.AssertSuccess(nameof(service.HandleAddDacpacReferenceRequest));
Assert.AreEqual(2, service.Projects[projectUri].DatabaseReferences.Count, "Database references after adding dacpac reference");
DacpacReference dacpacRef = (DacpacReference)service.Projects[projectUri].DatabaseReferences.First(x => x is DacpacReference);
Assert.AreEqual(mockReferencePath, dacpacRef.DacpacPath, "Referenced dacpac");
Assert.AreEqual(databaseVar.Name, dacpacRef.DatabaseVariable);
Assert.AreEqual(serverVar.Name, dacpacRef.ServerVariable);
Assert.IsFalse(dacpacRef.SuppressMissingDependencies, nameof(dacpacRef.SuppressMissingDependencies));
// Validate adding a project reference
mockReferencePath = Path.Join(Path.GetDirectoryName(projectUri), "..", "OtherDatabase", "OtherDatabase.sqlproj");
requestMock = new();
await service.HandleAddSqlProjectReferenceRequest(new AddSqlProjectReferenceParams()
{
ProjectUri = projectUri,
ProjectPath = mockReferencePath,
ProjectGuid = TEST_GUID,
SuppressMissingDependencies = false,
DatabaseVariable = databaseVar.Name,
ServerVariable = serverVar.Name
}, requestMock.Object);
requestMock.AssertSuccess(nameof(service.HandleAddSqlProjectReferenceRequest));
Assert.AreEqual(3, service.Projects[projectUri].DatabaseReferences.Count, "Database references after adding SQL project reference");
SqlProjectReference projectRef = (SqlProjectReference)service.Projects[projectUri].DatabaseReferences.First(x => x is SqlProjectReference);
Assert.AreEqual(mockReferencePath, projectRef.ProjectPath, "Referenced project");
Assert.AreEqual(TEST_GUID, projectRef.ProjectGuid, "Referenced project GUID");
Assert.AreEqual(databaseVar.Name, projectRef.DatabaseVariable);
Assert.AreEqual(serverVar.Name, projectRef.ServerVariable);
Assert.IsFalse(projectRef.SuppressMissingDependencies, nameof(projectRef.SuppressMissingDependencies));
// Validate deleting a reference
requestMock = new();
await service.HandleDeleteDatabaseReferenceRequest(new DeleteDatabaseReferenceParams()
{
ProjectUri = projectUri,
Name = mockReferencePath
}, requestMock.Object);
requestMock.AssertSuccess(nameof(service.HandleDeleteDatabaseReferenceRequest));
Assert.AreEqual(2, service.Projects[projectUri].DatabaseReferences.Count, "Database references after deleting SQL project reference");
Assert.IsFalse(service.Projects[projectUri].DatabaseReferences.Any(x => x is SqlProjectReference), "Database references list expected to not contain the SQL Project reference");
}
[Test]
public async Task TestSqlCmdVariablesAddDelete()
{
@@ -232,7 +323,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SqlProjects
}, requestMock.Object);
Assert.IsTrue(requestMock.Result.Success, $"Failed to create project: {requestMock.Result.ErrorMessage}");
requestMock.AssertSuccess(nameof(CreateSqlProject));
return projectUri;
}