Skip to content

Commit

Permalink
Add target to remove included files via :r from build (#497)
Browse files Browse the repository at this point in the history
* Add target to remove included files via :r from build

* Update DacFx to 162.5.57

* Fix merge conflict

* Clean up extra space
  • Loading branch information
zijchen authored Nov 22, 2024
1 parent 42e144b commit 7770d9f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
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
)

0 comments on commit 7770d9f

Please sign in to comment.