Skip to content

Commit

Permalink
SqlCmdVariable DefaultValue should not be written to .dacpac as per d…
Browse files Browse the repository at this point in the history
…ocs (#656)

* SqlCmdVariable DefaultValue should not be written to .dacpac as per the docs here: https://learn.microsoft.com/sql/tools/sql-database-projects/concepts/sqlcmd-variables

fixes #448

* fix build?

* Update readme
  • Loading branch information
ErikEJ authored Nov 13, 2024
1 parent 669a992 commit 4bcafb1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Especially when using pre- and post-deployment scripts, but also in other scenar
</Project>
```

> Note: The value of `Value` is checked first and if it found to be empty, we'll fall back to `DefaultValue`.
> Note: With version 3.0.0 of the SDK, the `DefaultValue` is not applied to the build output, in line with the standard `.sqlproj` behaviour.
## Package references
`MSBuild.Sdk.SqlProj` supports referencing NuGet packages that contain `.dacpac` packages. These can be referenced by using the `PackageReference` format familiar to .NET developers. They can also be installed through the NuGet Package Manager in Visual Studio.
Expand Down
11 changes: 2 additions & 9 deletions src/DacpacTool/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,11 @@ public static void AddSqlCmdVariables(this TSqlModel model, string[] variables)
{
var varWithValue = variable.Split('=', 2);
var variableName = varWithValue[0];
string variableDefaultValue = string.Empty;

if (varWithValue.Length > 1 && !string.IsNullOrEmpty(varWithValue[1]))
{
variableDefaultValue = varWithValue[1];
Console.WriteLine($"Adding SqlCmd variable {variableName} with default value {variableDefaultValue}");
}
else
Console.WriteLine($"Adding SqlCmd variable {variableName}");
Console.WriteLine($"Adding SqlCmd variable {variableName}");

var setMetadataMethod = customData.GetType().GetMethod("SetMetadata", BindingFlags.Public | BindingFlags.Instance);
setMetadataMethod.Invoke(customData, new object[] { variableName, variableDefaultValue });
setMetadataMethod.Invoke(customData, new object[] { variableName, string.Empty });
}

AddCustomData(dataSchemaModel, customData);
Expand Down
1 change: 1 addition & 0 deletions test/DacpacTool.Tests/DacpacTool.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<AssemblyName>MSBuild.Sdk.SqlProj.DacpacTool.Tests</AssemblyName>
<RootNamespace>MSBuild.Sdk.SqlProj.DacpacTool.Tests</RootNamespace>
<LangVersion>9.0</LangVersion>
<NuGetAudit>false</NuGetAudit>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions test/DacpacTool.Tests/PackageBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using Microsoft.SqlServer.Dac;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.SqlServer.Management.HadrModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

Expand Down Expand Up @@ -264,15 +265,15 @@ public void AddSqlCmdvariableWithDefaultValue()
&& d.Type == "SqlCmdVariable")
.SelectMany(d => d.Items)
.Where(i => i.Name == "DbReader"
&& i.Value == "dbReaderValue")
&& i.Value == string.Empty)
.ToList().Count.ShouldBe(1);

headerParser.GetCustomData()
.Where(d => d.Category == "SqlCmdVariables"
&& d.Type == "SqlCmdVariable")
.SelectMany(d => d.Items)
.Where(i => i.Name == "DbWriter"
&& i.Value == "dbWriterValue")
&& i.Value == string.Empty)
.ToList().Count.ShouldBe(1);

// Cleanup
Expand Down

0 comments on commit 4bcafb1

Please sign in to comment.