Skip to content

Commit

Permalink
Load Default SQLCmd Parameters value into dacpac (#368)
Browse files Browse the repository at this point in the history
* sqlcmd variables default value written in model.xml

* sqlcmd variables default value test

* removed double loop on variable string

* Revert "removed double loop on variable string"

This reverts commit f6dceac.

* moved variable loop inside AddSqlCmdVariables
  • Loading branch information
gicastel authored Dec 2, 2022
1 parent 1539df4 commit 246d09b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
18 changes: 15 additions & 3 deletions src/DacpacTool/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,28 @@ public static IEnumerable<string> GetReferencedDacPackages(this TSqlModel model)
return result;
}

public static void AddSqlCmdVariables(this TSqlModel model, string[] variableNames)
public static void AddSqlCmdVariables(this TSqlModel model, string[] variables)
{
var dataSchemaModel = GetDataSchemaModel(model);

var customData = Activator.CreateInstance(Type.GetType("Microsoft.Data.Tools.Schema.SchemaModel.CustomSchemaData, Microsoft.Data.Tools.Schema.Sql"), "SqlCmdVariables", "SqlCmdVariable");

foreach (var variableName in variableNames)
foreach (var variable in variables)
{
var varWithValue = variable.Split('=', 2);
var variableName = varWithValue[0];
string variableDefaultValue = string.Empty;

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

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

AddCustomData(dataSchemaModel, customData);
Expand Down
9 changes: 2 additions & 7 deletions src/DacpacTool/PackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,12 @@ private void ValidateReference(string referenceFile)
}
}

public void AddSqlCmdVariables(string[] variableNames)
public void AddSqlCmdVariables(string[] variables)
{
// Ensure that the model has been created
EnsureModelCreated();

foreach (var variableName in variableNames)
{
Console.WriteLine($"Adding SqlCmd variable {variableName}");
}

Model.AddSqlCmdVariables(variableNames);
Model.AddSqlCmdVariables(variables);
}

public void AddInputFile(FileInfo inputFile)
Expand Down
2 changes: 1 addition & 1 deletion src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
<InputFileArguments>-i &quot;$(IntermediateOutputPath)$(MSBuildProjectName).InputFiles.txt&quot;</InputFileArguments>
<BuildPropertyArguments>@(BuildPropertyNames->'-bp %(Identity)=%(PropertyValue)', ' ')</BuildPropertyArguments>
<DeployPropertyArguments>@(DeployPropertyNames->'-dp %(Identity)=%(PropertyValue)', ' ')</DeployPropertyArguments>
<SqlCmdVariableArguments>@(SqlCmdVariable->'-sc %(Identity)', ' ')</SqlCmdVariableArguments>
<SqlCmdVariableArguments>@(SqlCmdVariable->'-sc %(Identity)=%(DefaultValue)', ' ')</SqlCmdVariableArguments>
<PreDeploymentScriptArgument>@(PreDeploy->'--predeploy %(Identity)', ' ')</PreDeploymentScriptArgument>
<PostDeploymentScriptArgument>@(PostDeploy->'--postdeploy %(Identity)', ' ')</PostDeploymentScriptArgument>
<RefactorLogScriptArgument>@(RefactorLog->'--refactorlog %(Identity)', ' ')</RefactorLogScriptArgument>
Expand Down
39 changes: 39 additions & 0 deletions test/DacpacTool.Tests/PackageBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,45 @@ public void AddSqlCmdvariable()
tempFile.Delete();
}

[TestMethod]
public void AddSqlCmdvariableWithDefaultValue()
{
// Arrange
var first = "DbReader=dbReaderValue";
var second = "DbWriter=dbWriterValue";
var tempFile = new FileInfo(Path.GetTempFileName());
var packageBuilder = new PackageBuilder();
packageBuilder.SetMetadata("MyPackage", "1.0.0.0");
packageBuilder.UsingVersion(SqlServerVersion.Sql150);

// Act
packageBuilder.AddSqlCmdVariables(new string[] { first, second });

// Assert
packageBuilder.ValidateModel();
packageBuilder.SaveToDisk(tempFile);
var headerParser = new DacpacHeaderParser.HeaderParser(tempFile.FullName);

headerParser.GetCustomData()
.Where(d => d.Category == "SqlCmdVariables"
&& d.Type == "SqlCmdVariable")
.SelectMany(d => d.Items)
.Where(i => i.Name == "DbReader"
&& i.Value == "dbReaderValue")
.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")
.ToList().Count.ShouldBe(1);

// Cleanup
tempFile.Delete();
}

[TestMethod]
public void AddPreDeployment_FilesExist()
{
Expand Down

0 comments on commit 246d09b

Please sign in to comment.