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

Add target to remove included files via :r from build #497

Merged
merged 7 commits into from
Nov 22, 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
7 changes: 7 additions & 0 deletions src/Microsoft.Build.Sql/sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
</ItemGroup>
</Target>

<!-- Remove files included via :r in pre/post-deployment scripts from build. Fix for https://github.com/microsoft/DacFx/issues/103 -->
<Target Name="RemoveSqlCmdIncludeFilesFromBuild" BeforeTargets="SqlBuild" DependsOnTargets="_SetupSqlBuildInputs">
<ItemGroup>
<Build Remove="@(__SqlScriptDependentFiles)" MatchOnMetadata="Identity" MatchOnMetadataOptions="PathLike" />
</ItemGroup>
</Target>

<ItemGroup>
<!-- This is necessary for building on non-Windows platforms. -->
<PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" IsImplicitlyDefined="true" />
Expand Down
28 changes: 28 additions & 0 deletions test/Microsoft.Build.Sql.Tests/BuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,33 @@ public void VerifyBuildWithReleaseConfiguration()
Assert.AreEqual(string.Empty, stdError);
FileAssert.Exists(Path.Combine(WorkingDirectory, "bin", "Release", DatabaseProjectName + ".dacpac"));
}

[Test]
// https://github.com/microsoft/DacFx/issues/103
public void VerifyBuildWithIncludeFiles()
{
// Post-deployment script includes Table2.sql which creates Table2, it should not be part of the model
this.AddPostDeployScripts("Script.PostDeployment1.sql");
int exitCode = this.RunDotnetCommandOnProject("build", out _, out string stdError, "-bl");

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

// 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.");
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:r Table2.sql
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,5 @@
CREATE TABLE [dbo].[Table2]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)