Add target to remove included files via :r from build

This commit is contained in:
Zi Chen 2024-09-24 13:09:30 -07:00
Родитель 510d2e3020
Коммит 8b17b67d6c
6 изменённых файлов: 47 добавлений и 0 удалений

Просмотреть файл

@ -12,6 +12,7 @@
<RepositoryUrl>https://github.com/microsoft/DacFx</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<!-- TODO: vBump DacFx -->
<DacFxPackageVersion Condition="'$(DacFxPackageVersion)' == ''">162.4.92</DacFxPackageVersion>
<ScriptDomPackageVersion Condition="'$(ScriptDomPackageVersion)' == ''">161.9142.1</ScriptDomPackageVersion>
<SqlClientPackageVersion Condition="'$(SqlClientPackageVersion)' == ''">5.1.6</SqlClientPackageVersion>

Просмотреть файл

@ -63,6 +63,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" />

Просмотреть файл

@ -361,5 +361,33 @@ namespace Microsoft.Build.Sql.Tests
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.");
}
}
}
}
}
}

Просмотреть файл

@ -0,0 +1 @@
:r Table2.sql

Просмотреть файл

@ -0,0 +1,5 @@
CREATE TABLE [dbo].[Table1]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)

Просмотреть файл

@ -0,0 +1,5 @@
CREATE TABLE [dbo].[Table2]
(
c1 int NOT NULL PRIMARY KEY,
c2 int NULL
)