-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jonathan Mezach <[email protected]>
- Loading branch information
Showing
9 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SqlServer.Dac; | ||
|
||
namespace MSBuild.Sdk.SqlProj.Aspire; | ||
|
||
/// <summary> | ||
/// Provides the actual implementation of the <see cref="IDacpacDeployer"/> interface. | ||
/// </summary> | ||
internal class DacpacDeployer : IDacpacDeployer | ||
{ | ||
/// <inheritdoc cref="IDacpacDeployer.Deploy(string, string, string, ILogger, CancellationToken)"> | ||
public void Deploy(string dacpacPath, string targetConnectionString, string targetDatabaseName, ILogger deploymentLogger, CancellationToken cancellationToken) | ||
{ | ||
var dacPackage = DacPackage.Load(dacpacPath, DacSchemaModelStorageType.Memory); | ||
var dacServices = new DacServices(targetConnectionString); | ||
dacServices.Message += (sender, args) => deploymentLogger.LogInformation(args.Message.ToString()); | ||
dacServices.Deploy(dacPackage, targetDatabaseName, true, new DacDeployOptions(), cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MSBuild.Sdk.SqlProj.Aspire; | ||
|
||
/// <summary> | ||
/// Abstracts the actual deployment of the .dacpac file to a SQL Server database. | ||
/// </summary> | ||
public interface IDacpacDeployer | ||
{ | ||
/// <summary> | ||
/// Deployes the provided <paramref name="dacpacPath">.dacpac</paramref> file to the specified <paramref name="targetConnectionString">SQL Server</paramref> | ||
/// using the provided <paramref name="targetDatabaseName">database name</paramref>. | ||
/// </summary> | ||
/// <param name="dacpacPath">Path to the .dacpac file to deploy.</param> | ||
/// <param name="targetConnectionString">Connection string to the SQL Server.</param> | ||
/// <param name="targetDatabaseName">Name of the target database to deploy to.</param> | ||
/// <param name="deploymentLogger">An <see cref="ILogger"> to write the deployment log to.</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the deployment operation.</param> | ||
void Deploy(string dacpacPath, string targetConnectionString, string targetDatabaseName, ILogger deploymentLogger, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/MSBuild.Sdk.SqlProj.Aspire.Tests/PublishSqlProjectLifecycleHookTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Aspire.Hosting; | ||
using Aspire.Hosting.Lifecycle; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
|
||
namespace MSBuild.Sdk.SqlProj.Aspire.Tests; | ||
|
||
public class PublishSqlProjectLifecycleHookTests | ||
{ | ||
[Fact] | ||
public async Task AfterResourcesCreatedAsync_PublishesDacpacToTargetDatabase() | ||
{ | ||
// Arrange | ||
var dacDeployerMock = Substitute.For<IDacpacDeployer>(); | ||
var appBuilder = DistributedApplication.CreateBuilder(); | ||
appBuilder.Services.AddSingleton(dacDeployerMock); | ||
|
||
var targetDatabase = appBuilder.AddSqlServer("sql") | ||
.WithEndpoint("tcp", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 1433)) // Simulated endpoint | ||
.AddDatabase("test"); | ||
appBuilder.AddSqlProject<TestProject>("MySqlProject") | ||
.PublishTo(targetDatabase); | ||
|
||
// Act | ||
using var app = appBuilder.Build(); | ||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
var lifecycleHook = Assert.Single(app.Services.GetServices<IDistributedApplicationLifecycleHook>().OfType<PublishSqlProjectLifecycleHook>()); | ||
await lifecycleHook.AfterResourcesCreatedAsync(appModel, CancellationToken.None); | ||
|
||
// Assert | ||
var expectedPath = Path.GetFullPath(Path.Combine(appBuilder.AppHostDirectory, "../../../../TestProject/bin/Debug/netstandard2.0/TestProject.dacpac")); | ||
dacDeployerMock.Received().Deploy(Arg.Is(expectedPath), Arg.Any<string>(), Arg.Is("test"), Arg.Any<ILogger>(), Arg.Any<CancellationToken>()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Aspire.Hosting; | ||
|
||
namespace MSBuild.Sdk.SqlProj.Aspire.Tests; | ||
|
||
internal class TestProject : IProjectMetadata | ||
{ | ||
public string ProjectPath { get; } = "../../../../TestProject/TestProject.csproj"; | ||
} |