Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for refactor log script files #47

Merged
merged 7 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DacpacTool/BuildOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
4 changes: 2 additions & 2 deletions src/DacpacTool/PackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/DacpacTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,11 +17,12 @@ static async Task<int> Main(string[] args)
new Option<string>(new string[] { "--name", "-n" }, "Name of the package"),
new Option<string>(new string[] { "--version", "-v" }, "Version of the package"),
new Option<FileInfo>(new string[] { "--output", "-o" }, "Filename of the output package"),
new Option<SqlServerVersion>(new string[] { "--sqlServerVersion", "-sv" }, () => SqlServerVersion.Sql150, description: "Target version of the model"),
new Option<SqlServerVersion>(new string[] { "--sqlServerVersion", "-sv" }, () => SqlServerVersion.Sql150, description: "Target version of the model"),
new Option<FileInfo[]>(new string[] { "--input", "-i" }, "Input file name(s)"),
new Option<FileInfo[]>(new string[] { "--reference", "-r" }, "Reference(s) to include"),
new Option<FileInfo>(new string[] { "--predeploy" }, "Filename of optional pre-deployment script"),
new Option<FileInfo>(new string[] { "--postdeploy" }, "Filename of optional post-deployment script"),
new Option<FileInfo>(new string[] { "--refactorlog" }, "Filename of optional refactor log script"),
new Option<string[]>(new string[] { "--property", "-p" }, "Properties to be set on the model"),
new Option<string[]>(new string[] { "--sqlcmdvar", "-sc" }, "SqlCmdVariable(s) to include"),
};
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
<SqlCmdVariableArguments>@(SqlCmdVariable->'-sc %(Identity)', ' ')</SqlCmdVariableArguments>
<PreDeploymentScriptArgument>@(PreDeploy->'--predeploy %(Identity)', ' ')</PreDeploymentScriptArgument>
<PostDeploymentScriptArgument>@(PostDeploy->'--postdeploy %(Identity)', ' ')</PostDeploymentScriptArgument>
<DacpacToolCommand>dotnet $(DacpacToolExe) build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(PropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument)</DacpacToolCommand>
<RefactorLogScriptArgument>@(RefactorLog->'--refactorlog %(Identity)', ' ')</RefactorLogScriptArgument>
<DacpacToolCommand>dotnet $(DacpacToolExe) build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(PropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument)</DacpacToolCommand>
</PropertyGroup>
<!-- Run it, except during design-time builds -->
<Message Importance="Low" Text="Running command: $(DacpacToolCommand)" />
Expand Down
54 changes: 52 additions & 2 deletions test/DacpacTool.Tests/PackageBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<DacServicesException>(() => packageBuilder.SaveToDisk(tempFile, new PackageOptions() { RefactorLogPath = "NonExistingProject.refactorlog" }));

// Cleanup
tempFile.Delete();
}

[TestMethod]
[ValidPropertiesTestData]
public void SetProperty_Valid(PropertyInfo property, string value, object expected)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
jmezach marked this conversation as resolved.
Show resolved Hide resolved
<Operations Version="1.0" xmlns="http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/02">
<Operation Name="Rename Refactor" Key="28824ed4-6ce7-42ae-9814-c7409c6e1a8a" ChangeDateTime="09/01/2020 18:56:35">
<Property Name="ElementName" Value="[dbo].[Product].[price]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[Product]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="Price" />
</Operation>
<Operation Name="Rename Refactor" Key="04c343fa-e6d3-4923-ab01-28a60ddd553f" ChangeDateTime="09/01/2020 18:56:38">
<Property Name="ElementName" Value="[dbo].[Product].[description]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[Product]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="Description" />
</Operation>
<Operation Name="Rename Refactor" Key="119b3933-c2e4-476c-877b-40c376bf2bde" ChangeDateTime="09/01/2020 18:56:42">
<Property Name="ElementName" Value="[dbo].[Product].[name]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[Product]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="Name" />
</Operation>
</Operations>
1 change: 1 addition & 0 deletions test/TestProjectWithPrePost/TestProjectWithPrePost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PostDeploy Include="Post-Deployment\Script.PostDeployment.sql" />
<PreDeploy Include="Pre-Deployment\Script.PreDeployment.sql" />
<RefactorLog Include="RefactorLog\TestProjectWithPrePost.refactorlog" />
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.targets" />
Expand Down