diff --git a/src/DacpacTool/BuildOptions.cs b/src/DacpacTool/BuildOptions.cs index 0bb8718f..0bae5ab1 100644 --- a/src/DacpacTool/BuildOptions.cs +++ b/src/DacpacTool/BuildOptions.cs @@ -15,5 +15,6 @@ public class BuildOptions public string[] SqlCmdVar { get; set; } public FileInfo PreDeploy { get; set; } public FileInfo PostDeploy { get; set; } + public FileInfo RefactorLog { get; set; } } } diff --git a/src/DacpacTool/PackageBuilder.cs b/src/DacpacTool/PackageBuilder.cs index 4fe972b5..109068e0 100644 --- a/src/DacpacTool/PackageBuilder.cs +++ b/src/DacpacTool/PackageBuilder.cs @@ -102,7 +102,7 @@ public bool ValidateModel() return _modelValid.Value; } - public void SaveToDisk(FileInfo outputFile) + public void SaveToDisk(FileInfo outputFile, PackageOptions packageOptions = null) { // Ensure that the model has been created and metadata has been set EnsureModelCreated(); @@ -118,7 +118,7 @@ public void SaveToDisk(FileInfo outputFile) } Console.WriteLine($"Writing model to {outputFile.FullName}"); - DacPackageExtensions.BuildPackage(outputFile.FullName, Model, Metadata, new PackageOptions { }); + DacPackageExtensions.BuildPackage(outputFile.FullName, Model, Metadata, packageOptions ?? new PackageOptions { }); } public void SetMetadata(string name, string version) diff --git a/src/DacpacTool/Program.cs b/src/DacpacTool/Program.cs index 630e7d10..30dca15f 100644 --- a/src/DacpacTool/Program.cs +++ b/src/DacpacTool/Program.cs @@ -3,6 +3,7 @@ using System.CommandLine.Invocation; using System.IO; using System.Threading.Tasks; +using Microsoft.SqlServer.Dac; using Microsoft.SqlServer.Dac.Model; namespace MSBuild.Sdk.SqlProj.DacpacTool @@ -16,11 +17,12 @@ static async Task Main(string[] args) new Option(new string[] { "--name", "-n" }, "Name of the package"), new Option(new string[] { "--version", "-v" }, "Version of the package"), new Option(new string[] { "--output", "-o" }, "Filename of the output package"), - new Option(new string[] { "--sqlServerVersion", "-sv" }, () => SqlServerVersion.Sql150, description: "Target version of the model"), + new Option(new string[] { "--sqlServerVersion", "-sv" }, () => SqlServerVersion.Sql150, description: "Target version of the model"), new Option(new string[] { "--input", "-i" }, "Input file name(s)"), new Option(new string[] { "--reference", "-r" }, "Reference(s) to include"), new Option(new string[] { "--predeploy" }, "Filename of optional pre-deployment script"), new Option(new string[] { "--postdeploy" }, "Filename of optional post-deployment script"), + new Option(new string[] { "--refactorlog" }, "Filename of optional refactor log script"), new Option(new string[] { "--property", "-p" }, "Properties to be set on the model"), new Option(new string[] { "--sqlcmdvar", "-sc" }, "SqlCmdVariable(s) to include"), }; @@ -94,7 +96,7 @@ private static int BuildDacpac(BuildOptions options) } // Save the package to disk - packageBuilder.SaveToDisk(options.Output); + packageBuilder.SaveToDisk(options.Output, new PackageOptions() { RefactorLogPath = options.RefactorLog?.FullName }); // Add predeployment and postdeployment scripts (must happen after SaveToDisk) packageBuilder.AddPreDeploymentScript(options.PreDeploy, options.Output); diff --git a/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets b/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets index ff7ae161..978eb724 100644 --- a/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets +++ b/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets @@ -156,7 +156,8 @@ @(SqlCmdVariable->'-sc %(Identity)', ' ') @(PreDeploy->'--predeploy %(Identity)', ' ') @(PostDeploy->'--postdeploy %(Identity)', ' ') - dotnet $(DacpacToolExe) build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(PropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) + @(RefactorLog->'--refactorlog %(Identity)', ' ') + dotnet $(DacpacToolExe) build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(PropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) diff --git a/test/DacpacTool.Tests/PackageBuilderTests.cs b/test/DacpacTool.Tests/PackageBuilderTests.cs index 250cad6e..2264bc5a 100644 --- a/test/DacpacTool.Tests/PackageBuilderTests.cs +++ b/test/DacpacTool.Tests/PackageBuilderTests.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Reflection; using Microsoft.Data.Tools.Schema.Sql.Packaging; +using Microsoft.SqlServer.Dac; using Microsoft.SqlServer.Dac.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; using Shouldly; @@ -130,10 +131,12 @@ public void AddPreDeployment_FilesExist() packageBuilder.SetMetadata("MyPackage", "1.0.0.0"); packageBuilder.UsingVersion(SqlServerVersion.Sql150); packageBuilder.ValidateModel(); - packageBuilder.SaveToDisk(tempFile); + var packageOptions = new PackageOptions() { RefactorLogPath = "../../../../TestProjectWithPrePost/RefactorLog/TestProjectWithPrePost.refactorlog" }; // Act - packageBuilder.AddPreDeploymentScript( + packageBuilder.SaveToDisk(tempFile, packageOptions); + + packageBuilder.AddPreDeploymentScript( new FileInfo("../../../../TestProjectWithPrePost/Pre-Deployment/Script.PreDeployment.sql"), tempFile); @@ -145,6 +148,7 @@ public void AddPreDeployment_FilesExist() var package = Package.Open(tempFile.FullName); var prePart = package.GetPart(new Uri("/predeploy.sql", UriKind.Relative)); var postPart = package.GetPart(new Uri("/postdeploy.sql", UriKind.Relative)); + var refactorPart = package.GetPart(new Uri("/refactor.xml", UriKind.Relative)); prePart.ShouldNotBeNull(); prePart.ContentType.ShouldBe("text/plain"); @@ -154,6 +158,10 @@ public void AddPreDeployment_FilesExist() postPart.ContentType.ShouldBe("text/plain"); postPart.GetStream().ShouldNotBeNull(); + refactorPart.ShouldNotBeNull(); + refactorPart.ContentType.ShouldBe("text/xml"); + refactorPart.GetStream().ShouldNotBeNull(); + // Cleanup package.Close(); tempFile.Delete(); @@ -222,6 +230,31 @@ public void AddPostDeployment_NoFilePresent() tempFile.Delete(); } + [TestMethod] + public void AddRefactorLog_NoFilePresent() + { + // Arrange + var tempFile = new FileInfo(Path.GetTempFileName()); + var packageBuilder = new PackageBuilder(); + packageBuilder.SetMetadata("MyPackage", "1.0.0.0"); + packageBuilder.UsingVersion(SqlServerVersion.Sql150); + packageBuilder.ValidateModel(); + + // Act + packageBuilder.SaveToDisk(tempFile, new PackageOptions() { RefactorLogPath = null }); + + // Assert + var package = Package.Open(tempFile.FullName); + + package.GetParts() + .Where(p => p.Uri == new Uri("/refactor.log", UriKind.Relative)) + .FirstOrDefault() + .ShouldBeNull(); + + // Cleanup + package.Close(); + tempFile.Delete(); + } [TestMethod] public void AddPreDeployment_WrongOrder() @@ -291,6 +324,23 @@ public void AddPostDeployment_PostNotExists() tempFile.Delete(); } + [TestMethod] + public void AddRefactorLog_RefactorNotExists() + { + // Arrange + var tempFile = new FileInfo(Path.GetTempFileName()); + var packageBuilder = new PackageBuilder(); + packageBuilder.SetMetadata("MyPackage", "1.0.0.0"); + packageBuilder.UsingVersion(SqlServerVersion.Sql150); + packageBuilder.ValidateModel(); + + // Act & Assert + Should.Throw(() => packageBuilder.SaveToDisk(tempFile, new PackageOptions() { RefactorLogPath = "NonExistingProject.refactorlog" })); + + // Cleanup + tempFile.Delete(); + } + [TestMethod] [ValidPropertiesTestData] public void SetProperty_Valid(PropertyInfo property, string value, object expected) diff --git a/test/TestProjectWithPrePost/RefactorLog/TestProjectWithPrePost.refactorlog b/test/TestProjectWithPrePost/RefactorLog/TestProjectWithPrePost.refactorlog new file mode 100644 index 00000000..581ef82c --- /dev/null +++ b/test/TestProjectWithPrePost/RefactorLog/TestProjectWithPrePost.refactorlog @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/TestProjectWithPrePost/TestProjectWithPrePost.csproj b/test/TestProjectWithPrePost/TestProjectWithPrePost.csproj index c7fb40f5..929584bb 100644 --- a/test/TestProjectWithPrePost/TestProjectWithPrePost.csproj +++ b/test/TestProjectWithPrePost/TestProjectWithPrePost.csproj @@ -14,6 +14,7 @@ +