From 246d09b8184a5128f5b5e4b4698336db81e9d4b8 Mon Sep 17 00:00:00 2001 From: Giacomo Castellani Date: Fri, 2 Dec 2022 12:41:08 +0100 Subject: [PATCH] Load Default SQLCmd Parameters value into dacpac (#368) * 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 f6dceac7b3e4dcac10505170b89420a4b4345d57. * moved variable loop inside AddSqlCmdVariables --- src/DacpacTool/Extensions.cs | 18 +++++++-- src/DacpacTool/PackageBuilder.cs | 9 +---- src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets | 2 +- test/DacpacTool.Tests/PackageBuilderTests.cs | 39 ++++++++++++++++++++ 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/DacpacTool/Extensions.cs b/src/DacpacTool/Extensions.cs index 9ddb4e18..9ed4d7ae 100644 --- a/src/DacpacTool/Extensions.cs +++ b/src/DacpacTool/Extensions.cs @@ -204,16 +204,28 @@ public static IEnumerable 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); diff --git a/src/DacpacTool/PackageBuilder.cs b/src/DacpacTool/PackageBuilder.cs index 92dc7cf8..3ad0b101 100644 --- a/src/DacpacTool/PackageBuilder.cs +++ b/src/DacpacTool/PackageBuilder.cs @@ -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) diff --git a/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets b/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets index de375573..ed75ac80 100644 --- a/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets +++ b/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets @@ -221,7 +221,7 @@ -i "$(IntermediateOutputPath)$(MSBuildProjectName).InputFiles.txt" @(BuildPropertyNames->'-bp %(Identity)=%(PropertyValue)', ' ') @(DeployPropertyNames->'-dp %(Identity)=%(PropertyValue)', ' ') - @(SqlCmdVariable->'-sc %(Identity)', ' ') + @(SqlCmdVariable->'-sc %(Identity)=%(DefaultValue)', ' ') @(PreDeploy->'--predeploy %(Identity)', ' ') @(PostDeploy->'--postdeploy %(Identity)', ' ') @(RefactorLog->'--refactorlog %(Identity)', ' ') diff --git a/test/DacpacTool.Tests/PackageBuilderTests.cs b/test/DacpacTool.Tests/PackageBuilderTests.cs index 6de50b2e..53f8c004 100644 --- a/test/DacpacTool.Tests/PackageBuilderTests.cs +++ b/test/DacpacTool.Tests/PackageBuilderTests.cs @@ -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() {