Skip to content

Commit

Permalink
Merge pull request #2581 from FoothillSolutions/add-access-token-to-s…
Browse files Browse the repository at this point in the history
…qlpackage

add access token option to sqlpackage
  • Loading branch information
matthid authored Apr 2, 2021
2 parents ff35d64 + b6c3a72 commit a4b56fe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
19 changes: 19 additions & 0 deletions help/markdown/sql-sqlpackage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ Ensure that you have already built your database project (you can do this with s
SqlPackage.deployDb (fun args -> { args with Source = dacPacPath; Destination = connectionString }) |> ignore
)

The following sample shows how to deploy a database project to Azure using an access token:

open Fake.Core
open Fake.Sql

/// the database for local development + compile
Target.create "DeployLocalDb" (fun _ ->
let dacPacPath = "path/to/dbProject.dacpac"
let accessToken = "your-access-token"
let connectionString = "Data Source=your-server-name.database.windows.net; Initial Catalog=your-database-name;"
SqlPackage.deployDb (fun args ->
{ args with
Destination = connectionString
AccessToken = accessToken
Source = dacPacPath
}) |> ignore
)

## Deployment Options

You can optionally specify the type of command to use (again, refer to the documentation above for more detail): -
Expand All @@ -41,6 +59,7 @@ You can provide following arguments (in brackets are given sqlpackage.exe parame

* SqlPackageToolPath - path to sqlpackage.exe
* Action - deployment option (/a)
* AccessToken - An Access token to use in authentication instead of username and password
* Source - specifies a source file to be used as the source of action instead of a database (/SourceFile)
* Destination - specifies a valid SQL Server/Azure connection string to the target database (/TargetConnectionString)
* Timeout - specifies the command timeout in seconds when executing queries against SQL Server (/p:CommandTimeout)
Expand Down
15 changes: 12 additions & 3 deletions src/app/Fake.Sql.SqlPackage/Sql.SqlPackage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module SqlPackage =
SqlPackageToolPath : string
/// Type of action to execute. Defaults to Deploy.
Action : DeployAction
/// Azure AccessToken
AccessToken: string
/// Path to source (path to DACPAC or Connection String).
Source : string
/// Path to destination (path to DACPAC or Connection String).
Expand Down Expand Up @@ -63,6 +65,7 @@ module SqlPackage =
|> List.tryHead
|> defaultArg <| ""
Action = Deploy
AccessToken = ""
Source = ""
Destination = ""
Timeout = None
Expand All @@ -76,6 +79,9 @@ module SqlPackage =
[<Literal>]
let internal Action = "Action"

[<Literal>]
let internal AccessToken = "AccessToken"

[<Literal>]
let internal Source = "Source"

Expand Down Expand Up @@ -106,10 +112,12 @@ module SqlPackage =
[<Literal>]
let internal Profile = "Profile"

let private formatArgument (args:DeployDbArgs) action outputPath additionalParameters variables argumentName =
/// [omit]
let formatArgument (args:DeployDbArgs) action outputPath additionalParameters variables argumentName =

match argumentName with
| Action -> sprintf "/Action:%s" action
| AccessToken when not(String.isNullOrEmpty args.AccessToken) -> sprintf """/AccessToken:"%s" """ args.AccessToken
| Source -> sprintf """/SourceFile:"%s" """ args.Source
| Destination when not(String.isNullOrEmpty(args.Destination)) -> sprintf """/TargetConnectionString:"%s" """ args.Destination
| OutputPath -> sprintf "%s" outputPath
Expand All @@ -118,7 +126,7 @@ module SqlPackage =
| DropObjectsNotInSource when args.DropObjectsNotInSource.IsSome -> sprintf "/p:DropObjectsNotInSource=%b" args.DropObjectsNotInSource.Value
| DropObjectsNotInSource when String.isNullOrEmpty(args.Profile) && args.DropObjectsNotInSource.IsNone -> sprintf "/p:DropObjectsNotInSource=%b" false
| Timeout when args.Timeout.IsSome -> sprintf "/p:CommandTimeout=%d" args.Timeout.Value
| Timeout when String.isNullOrEmpty(args.Profile) && args.Timeout.IsNone -> sprintf "/p:CommandTimeout=%d" args.Timeout.Value
| Timeout when String.isNullOrEmpty(args.Profile) && args.Timeout.IsSome -> sprintf "/p:CommandTimeout=%d" args.Timeout.Value
| RecreateDb when args.RecreateDb.IsSome -> sprintf "/p:CreateNewDatabase=%b" args.RecreateDb.Value
| RecreateDb when String.isNullOrEmpty(args.Profile) && args.RecreateDb.IsNone -> sprintf "/p:CreateNewDatabase=%b" false
| AdditionalSqlPackageProperties when not(String.isNullOrEmpty(additionalParameters)) -> sprintf "%s" additionalParameters
Expand All @@ -144,6 +152,7 @@ module SqlPackage =
let format = formatArgument args action outputPath additionalParameters variables

let actionParameter = format Action
let accessTokenParameter = format AccessToken
let sourceParameter = format Source
let destinationParameter = format Destination
let outputPathParameter = format OutputPath
Expand All @@ -155,7 +164,7 @@ module SqlPackage =
let variablesParameter = format Variables
let profileParameter = format Profile

[ actionParameter; sourceParameter; destinationParameter; outputPathParameter; blockOnPossibleDataLossParameter; dropObjectsNotInSourceParameter; timeoutParameter; recreateDbParameter; additionalSqlPackagePropertiesParameter; variablesParameter; profileParameter ]
[ actionParameter; accessTokenParameter; sourceParameter; destinationParameter; outputPathParameter; blockOnPossibleDataLossParameter; dropObjectsNotInSourceParameter; timeoutParameter; recreateDbParameter; additionalSqlPackagePropertiesParameter; variablesParameter; profileParameter ]
|> List.filter (fun item -> item <> "")
|> String.concat " "

Expand Down
2 changes: 2 additions & 0 deletions src/test/Fake.Core.UnitTests/Fake.Core.UnitTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ProjectReference Include="..\Fake.ExpectoSupport\Fake.ExpectoSupport.fsproj" />
<ProjectReference Include="..\..\app\Fake.Tools.SignTool\Fake.Tools.SignTool.fsproj" />
<ProjectReference Include="..\..\app\Fake.BuildServer.GitLab\Fake.BuildServer.GitLab.fsproj" />
<ProjectReference Include="..\..\app\Fake.Sql.SqlPackage\Fake.Sql.SqlPackage.fsproj" />
</ItemGroup>
<ItemGroup>
<Content Include="runtimeconfig.template.json" />
Expand Down Expand Up @@ -70,6 +71,7 @@
<Compile Include="Fake.Core.FakeVar.fs" />
<Compile Include="Fake.Runtime.fs" />
<Compile Include="Fake.Tools.Octo.fs" />
<Compile Include="Fake.Sql.SqlPackage.fs" />
<Compile Include="Main.fs" />
<None Include="TestFiles/Fake.DotNet.AssemblyInfoFile/AssemblyInfo.cs" CopyToOutputDirectory="PreserveNewest" />
<None Include="TestFiles/Fake.DotNet.AssemblyInfoFile/AssemblyInfo.fs" CopyToOutputDirectory="PreserveNewest" />
Expand Down
34 changes: 34 additions & 0 deletions src/test/Fake.Core.UnitTests/Fake.Sql.SqlPackage.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Fake.Sql.SqlPackageTests

open Fake.Core
open Fake.Sql
open Expecto

[<Tests>]
let tests =
testList "Fake.Sql.SqlPackage.Tests" [
testCase "Test that it produces correct command when using access token" <| fun _ ->
let action = "Publish"
let outputPath = ""
let additionalParameters = ""
let variables = ""
let args: SqlPackage.DeployDbArgs =
{ SqlPackageToolPath = ""
Action = SqlPackage.DeployAction.Deploy
AccessToken = "my-access-token"
Source = ""
Destination = "Data Source=your-server-name.database.windows.net; Initial Catalog=your-database-name;"
Timeout = None
BlockOnPossibleDataLoss = None
DropObjectsNotInSource = None
RecreateDb = None
AdditionalSqlPackageProperties = []
Variables = []
Profile = "" }

let cmd = SqlPackage.formatArgument args action outputPath additionalParameters variables "AccessToken"

Expect.equal cmd
(sprintf "/AccessToken:\"my-access-token\" ") "expected access token to be enclosed in quotes"

]

0 comments on commit a4b56fe

Please sign in to comment.