Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding VS targets path and Excluding BuildExtensionConfiguration and DeployExtensionConfiguration from Build #463

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)" />
Ri7Sh marked this conversation as resolved.
Show resolved Hide resolved
<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
)