-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1878 from josiahdj/PortDacPacToFake5
Ported Fake.Sql.DacPac to FAKE 5
- Loading branch information
Showing
10 changed files
with
223 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Packaging and Deploying SQL Databases | ||
|
||
FAKE can be used to create a SQL DACPAC and also deploy it to a SQL Server using the MSDeploy executable. This is installed by default with Visual Studio and with the SQL Server Data Tools (SSDT) package. | ||
|
||
DACPACs automatically diff from the source to the destination and generate the SQL script dynamically. | ||
|
||
You can read up more on DACPac and MSDeploy arguments [here](https://msdn.microsoft.com/en-us/library/hh550081%28v=vs.103%29.aspx). | ||
|
||
## Sample usage | ||
|
||
Ensure that you have already built your database project (you can do this with standard MSBuild). Then, use the ``deployDb`` command to deploy the ``dbProject.dacpac`` to the ``myDatabase``. | ||
|
||
open Fake.Sql | ||
|
||
/// the database for local development + compile | ||
Target.create "DeployLocalDb" (fun _ -> | ||
let connectionString = "Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True;Database=MyDatabase;Pooling=False" | ||
let dacPacPath = "path/to/dbProject.dacpac" | ||
DacPac.deployDb (fun args -> { args with Source = dacPacPath; Destination = localDbConnectionString }) |> ignore) | ||
|
||
## Deployment Options | ||
|
||
You can optionally specify the type of command to use (again, refer to the documentation above for more detail): - | ||
|
||
* Deploy - full deploy to destination | ||
* Script - SQL script | ||
* Report - XML report of diff | ||
|
||
In addition, you can also elect to deploy to Dacpac files rather than SQL databases - simply pass in the pass to the dacpac that you wish to generate. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Auto-Generated by FAKE; do not edit | ||
namespace System | ||
open System.Reflection | ||
|
||
[<assembly: AssemblyTitleAttribute("FAKE - F# Make Sql Server Data Tools DacPac operations")>] | ||
[<assembly: AssemblyProductAttribute("FAKE - F# Make")>] | ||
[<assembly: AssemblyVersionAttribute("5.0.0")>] | ||
[<assembly: AssemblyInformationalVersionAttribute("5.0.0-rc005")>] | ||
[<assembly: AssemblyFileVersionAttribute("5.0.0")>] | ||
do () | ||
|
||
module internal AssemblyVersionInformation = | ||
let [<Literal>] AssemblyTitle = "FAKE - F# Make Sql Server Data Tools DacPac operations" | ||
let [<Literal>] AssemblyProduct = "FAKE - F# Make" | ||
let [<Literal>] AssemblyVersion = "5.0.0" | ||
let [<Literal>] AssemblyInformationalVersion = "5.0.0-rc005" | ||
let [<Literal>] AssemblyFileVersion = "5.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net46;netstandard1.6;netstandard2.0</TargetFrameworks> | ||
<DefineConstants>$(DefineConstants);DOTNETCORE</DefineConstants> | ||
<AssemblyName>Fake.Sql.DacPac</AssemblyName> | ||
<OutputType>Library</OutputType> | ||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="AssemblyInfo.fs" /> | ||
<Compile Include="Sql.DacPac.fs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Fake.Core.Environment\Fake.Core.Environment.fsproj" /> | ||
<ProjectReference Include="..\Fake.Core.Process\Fake.Core.Process.fsproj" /> | ||
<ProjectReference Include="..\Fake.IO.FileSystem\Fake.IO.FileSystem.fsproj" /> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<DefineConstants>$(DefineConstants);NETSTANDARD2_0</DefineConstants> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> | ||
</PropertyGroup> | ||
<Import Project="..\..\..\.paket\Paket.Restore.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
namespace Fake.Sql | ||
|
||
/// Contains helpers around deploying databases. | ||
[<RequireQualifiedAccess>] | ||
module DacPac = | ||
|
||
open Fake.Core | ||
open Fake.IO.FileSystemOperators | ||
open Fake.IO.Globbing.Operators | ||
open System.IO | ||
open System | ||
|
||
/// The type of action to execute. | ||
type DeployAction = | ||
/// Generate and apply a synchronisation script between two databases. | ||
| Deploy | ||
/// Generate a SQL script to sync two databases. | ||
| Script of OutputPath:string | ||
/// Generate an XML report for the differences between two databases. | ||
| Report of OutputPath:string | ||
|
||
/// Configuration arguments for DacPac deploy | ||
type DeployDbArgs = { | ||
/// The path to SqlPackage.exe. | ||
SqlPackageToolPath : string | ||
/// Type of action to execute. Defaults to Deploy. | ||
Action : DeployAction | ||
/// Path to source (path to DACPAC or Connection String). | ||
Source : string | ||
/// Path to destination (path to DACPAC or Connection String). | ||
Destination : string | ||
/// Timeout for deploy (defaults to 120 seconds). | ||
Timeout : int | ||
/// Block deployment if data loss can occur. Defaults to true. | ||
BlockOnPossibleDataLoss : bool | ||
/// Drops objects in the destination that do not exist in the source. Defaults to false. | ||
DropObjectsNotInSource : bool | ||
/// Recreates the database from scratch on publish (rather than an in-place update). Defaults to false. | ||
RecreateDb : bool | ||
/// Additional configuration parameters required by sqlpackage.exe | ||
AdditionalSqlPackageProperties : (string * string) list | ||
/// SQLCMD variables | ||
Variables : (string * string) list } | ||
|
||
let validPaths = | ||
let getSqlVersion (path:string) = path.Split '\\' |> Array.item 3 |> int | ||
let getVsVersion path = (Path.GetDirectoryName path |> DirectoryInfo).Name |> int | ||
let sql = !!(Environment.ProgramFilesX86 </> @"Microsoft SQL Server\**\DAC\bin\SqlPackage.exe") |> Seq.map(fun path -> path, getSqlVersion path) | ||
let vs = !!(Environment.ProgramFilesX86 </> @"Microsoft Visual Studio*\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\*\SqlPackage.exe") |> Seq.map(fun path -> path, getVsVersion path) | ||
|
||
[ sql; vs ] | ||
|> List.collect Seq.toList | ||
|> List.sortByDescending snd | ||
|> List.map fst | ||
|
||
/// The default DacPac deployment arguments. | ||
let DefaultDeploymentArgs = | ||
{ SqlPackageToolPath = | ||
validPaths | ||
|> List.tryHead | ||
|> defaultArg <| "" | ||
Action = Deploy | ||
Source = "" | ||
Destination = "" | ||
Timeout = 120 | ||
BlockOnPossibleDataLoss = true | ||
DropObjectsNotInSource = false | ||
RecreateDb = false | ||
AdditionalSqlPackageProperties = [] | ||
Variables = [] } | ||
|
||
module PropertyKeys = | ||
/// When creating a new SQL Azure database, specifies the database service tier to use e.g. S2, P1 | ||
let sqlAzureDbSize = "DatabaseServiceObjective" | ||
|
||
let private generateCommandLine args = | ||
let action, outputPath = | ||
match args with | ||
| Deploy -> "Publish", None | ||
| Script outputPath -> "Script", Some outputPath | ||
| Report outputPath -> "DeployReport", Some outputPath | ||
let outputPath = defaultArg(outputPath |> Option.map(sprintf """/OutputPath:"%s" """)) "" | ||
action, outputPath | ||
|
||
/// Deploys a SQL DacPac or database to another database or DacPac. | ||
let deployDb setParams = | ||
let args = setParams DefaultDeploymentArgs | ||
let action, outputPath = generateCommandLine args.Action | ||
|
||
let concat parameter = | ||
List.map (fun (key, value) -> sprintf "/%s:%s=%s" parameter key value) | ||
>> String.concat " " | ||
|
||
let additionalParameters = args.AdditionalSqlPackageProperties |> concat "p" | ||
|
||
let variables = args.Variables |> concat "v" | ||
|
||
if System.String.IsNullOrWhiteSpace args.SqlPackageToolPath then | ||
failwith "No SqlPackage.exe filename was given." | ||
|
||
if not (File.Exists args.SqlPackageToolPath) then | ||
let paths = | ||
if validPaths |> List.contains args.SqlPackageToolPath then validPaths | ||
else [ args.SqlPackageToolPath ] | ||
failwithf "Unable to find a valid instance of SqlPackage.exe. Paths checked were: %A." paths | ||
|
||
let result = | ||
Process.execRaw | ||
(fun psi -> { psi with Arguments = sprintf """/Action:%s /SourceFile:"%s" /TargetConnectionString:"%s" %s /p:BlockOnPossibleDataLoss=%b /p:DropObjectsNotInSource=%b /p:CommandTimeout=%d /p:CreateNewDatabase=%b %s %s""" action args.Source args.Destination outputPath args.BlockOnPossibleDataLoss args.DropObjectsNotInSource args.Timeout args.RecreateDb additionalParameters variables; FileName = args.SqlPackageToolPath }) | ||
TimeSpan.MaxValue | ||
true | ||
(printfn "SqlPackage error: %s") | ||
(printfn "%s") | ||
|
||
match result with | ||
| 0 -> () | ||
| _ -> failwith "Error executing DACPAC deployment. Please see output for error details." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
group netcore | ||
|
||
FSharp.Core | ||
NETStandard.Library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters