From 7770d9f24b732df90da81e19ed34850092085ba7 Mon Sep 17 00:00:00 2001 From: Z Chen <13544267+zijchen@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:48:24 -0800 Subject: [PATCH] Add target to remove included files via :r from build (#497) * Add target to remove included files via :r from build * Update DacFx to 162.5.57 * Fix merge conflict * Clean up extra space --- src/Microsoft.Build.Sql/sdk/Sdk.targets | 7 +++++ test/Microsoft.Build.Sql.Tests/BuildTests.cs | 28 +++++++++++++++++++ .../Script.PostDeployment1.sql | 1 + .../VerifyBuildWithIncludeFiles/Table1.sql | 5 ++++ .../VerifyBuildWithIncludeFiles/Table2.sql | 5 ++++ 5 files changed, 46 insertions(+) create mode 100644 test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Script.PostDeployment1.sql create mode 100644 test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table1.sql create mode 100644 test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table2.sql diff --git a/src/Microsoft.Build.Sql/sdk/Sdk.targets b/src/Microsoft.Build.Sql/sdk/Sdk.targets index bf9344c..abb5a46 100644 --- a/src/Microsoft.Build.Sql/sdk/Sdk.targets +++ b/src/Microsoft.Build.Sql/sdk/Sdk.targets @@ -77,6 +77,13 @@ + + + + + + + diff --git a/test/Microsoft.Build.Sql.Tests/BuildTests.cs b/test/Microsoft.Build.Sql.Tests/BuildTests.cs index ff344e0..ff4c154 100644 --- a/test/Microsoft.Build.Sql.Tests/BuildTests.cs +++ b/test/Microsoft.Build.Sql.Tests/BuildTests.cs @@ -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."); + } + } + } + } } } \ No newline at end of file diff --git a/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Script.PostDeployment1.sql b/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Script.PostDeployment1.sql new file mode 100644 index 0000000..50ae5f7 --- /dev/null +++ b/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Script.PostDeployment1.sql @@ -0,0 +1 @@ +:r Table2.sql \ No newline at end of file diff --git a/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table1.sql b/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table1.sql new file mode 100644 index 0000000..c4cde37 --- /dev/null +++ b/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table1.sql @@ -0,0 +1,5 @@ +CREATE TABLE [dbo].[Table1] +( + c1 int NOT NULL PRIMARY KEY, + c2 int NULL +) \ No newline at end of file diff --git a/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table2.sql b/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table2.sql new file mode 100644 index 0000000..4ea4ae6 --- /dev/null +++ b/test/Microsoft.Build.Sql.Tests/TestData/VerifyBuildWithIncludeFiles/Table2.sql @@ -0,0 +1,5 @@ +CREATE TABLE [dbo].[Table2] +( + c1 int NOT NULL PRIMARY KEY, + c2 int NULL +) \ No newline at end of file