Skip to content

Commit

Permalink
Adding VS support
Browse files Browse the repository at this point in the history
* Importing ssdt targets if in VS
* Updating Sdk.targets to bring support for BuildExtensionConfiguration and DeployExtnsionConfiguration and adding tests for the same
  • Loading branch information
Ri7Sh authored Jul 9, 2024
1 parent 38bfb55 commit cf5928e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Microsoft.Build.Sql/sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<Import Condition="'$(NetCoreBuild)' == 'true'" Project="$(MSBuildSdksPath)/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets" />
<Import Condition="'$(NetCoreBuild)' == 'true'" Project="$(MSBuildSdksPath)/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets" />

<Import Condition="'$(NetCoreBuild)' != 'true' AND Exists('$(MSBuildExtensionsPath)\Sdks\Microsoft.SqlProject.Sdk\ssdtprojectsystem.targets')"
Project="$(MSBuildExtensionsPath)\Sdks\Microsoft.SqlProject.Sdk\ssdtprojectsystem.targets" />

<!-- Add dacpac file BuiltProjectOutputGroupOutput, so that it will get included in the NuGet package by the Pack target -->
<Target Name="AddDacpacToBuiltProjectOutputGroupOutput" BeforeTargets="BuiltProjectOutputGroup">
<ItemGroup>
Expand All @@ -70,6 +73,8 @@
<Build Remove="@(PreDeploy)" />
<Build Remove="@(PostDeploy)" />
<Build Remove="@(None)" />
<Build Remove="@(BuildExtensionConfiguration)" />
<Build Remove="@(DeploymentExtensionConfiguration)" />
</ItemGroup>

<!-- Target to resolve package references into database references to the underlying DACPACs inside the packages -->
Expand Down
60 changes: 60 additions & 0 deletions test/Microsoft.Build.Sql.Tests/BuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,66 @@ public void SuccessfulBuildWithPostDeployScript()
this.VerifyDacPackage(expectPostDeployScript: true);
}

[Test]
[Description("Verifies build with deployment extension configuration script.")]
public void SuccessfulBuildWithDeploymentExtensionConfigurationScript()
{
this.RemoveBuildFiles("Table2.sql");
this.AddDeploymentExtensionConfigurationScripts("Table2.sql");

string stdOutput, stdError;
int exitCode = this.RunDotnetCommandOnProject("build", out stdOutput, out stdError);

// Verify success
Assert.AreEqual(0, exitCode, "Build failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();

// Verify the Table2 is not part of the model
using (TSqlModel model = new TSqlModel(this.GetDacpacPath()))
{
var tables = model.GetObjects(DacQueryScopes.UserDefined, ModelSchema.Table);
Assert.IsTrue(tables.Any(), "Expected at least 1 table in the model.");
foreach (var table in tables)
{
if (table.Name.ToString().IndexOf("Table2", StringComparison.OrdinalIgnoreCase) >= 0)
{
Assert.Fail("Table2 should have been excluded from the model.");
}
}
}
}

[Test]
[Description("Verifies build with build extension configuration script.")]
public void SuccessfulBuildWithBuildExtensionConfigurationScript()
{
this.RemoveBuildFiles("Table2.sql");
this.AddBuildExtensionConfigurationScripts("Table2.sql");

string stdOutput, stdError;
int exitCode = this.RunDotnetCommandOnProject("build", out stdOutput, out stdError);

// Verify success
Assert.AreEqual(0, exitCode, "Build failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();

// Verify the Table2 is not part of the model
using (TSqlModel model = new TSqlModel(this.GetDacpacPath()))
{
var tables = model.GetObjects(DacQueryScopes.UserDefined, ModelSchema.Table);
Assert.IsTrue(tables.Any(), "Expected at least 1 table in the model.");
foreach (var table in tables)
{
if (table.Name.ToString().IndexOf("Table2", StringComparison.OrdinalIgnoreCase) >= 0)
{
Assert.Fail("Table2 should have been excluded from the model.");
}
}
}
}

[Test]
[Description("Verifies build with excluding file from project.")]
public void BuildWithExclude()
Expand Down
16 changes: 16 additions & 0 deletions test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ protected void AddPostDeployScripts(params string[] files)
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "PostDeploy", files);
}

/// <summary>
/// Add deploymentextensionconfiguration scripts to the project. <paramref name="files"/> paths are relative.
/// </summary>
protected void AddDeploymentExtensionConfigurationScripts(params string[] files)
{
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "DeploymentExtensionConfiguration", files);
}

/// <summary>
/// Add buildextensionconfiguration scripts to the project. <paramref name="files"/> paths are relative.
/// </summary>
protected void AddBuildExtensionConfigurationScripts(params string[] files)
{
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "BuildExtensionConfiguration", files);
}

/// <summary>
/// Add scripts to the project that are not part of build. <paramref name="files"/> paths are relative.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE [dbo].[Table1]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file to be excluded from project by the test
CREATE TABLE [dbo].[Table2]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE [dbo].[Table1]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file to be excluded from project by the test
CREATE TABLE [dbo].[Table2]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)

0 comments on commit cf5928e

Please sign in to comment.