Skip to content

Commit de395da

Browse files
feat: TrustServerCertificate on AzSqlDatabaseMigration (#434)
* TrustServerCertificate on AzSqlDatabaseMigration Added TrustServerCertificate for scenario's where you want to build locally for integration testing. * Correct description for parameter * Version 22+ of SqlServer module is needed TrustServerCertificate was introduced in version 22
1 parent b7446c6 commit de395da

6 files changed

Lines changed: 44 additions & 26 deletions

File tree

docs/preview/03-Features/powershell/azure-sql.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ If the DatabaseVersion table doesn't exist it will automatically create it.
2424
This function allows you to trigger a database migration, which will only execute the newly provided SQL scripts, based on the provided version number in each of the scripts.
2525
The current version is stored in a table "DatabaseVersion", which will be created if it doesn't exist yet.
2626

27-
| Parameter | Mandatory | Description |
28-
| ------------------- | --------------------------------------- | ----------------------------------------------------------------------------------- |
29-
| `ServerName` | yes | The full name of the SQL Server that hosts the SQL Database. |
30-
| `DatabaseName` | yes | The name of the SQL Database |
31-
| `UserName` | yes | The UserName of the SQL Database |
32-
| `Password` | yes | The Password of the SQL Database |
33-
| `ScriptsFolder` | no (default: `$PSScriptRoot/sqlScripts` | The directory folder where the SQL migration scripts are located on the file system |
34-
| `ScriptsFileFilter` | no (default: `*.sql`) | The file filter to limit the SQL script files to use during the migrations |
35-
| `DatabaseSchema` | no (default: `dbo`) | The database schema to use when running SQL commands on the target database |
27+
| Parameter | Mandatory | Description |
28+
| ------------------------| --------------------------------------- | ----------------------------------------------------------------------------------- |
29+
| `ServerName` | yes | The full name of the SQL Server that hosts the SQL Database. |
30+
| `DatabaseName` | yes | The name of the SQL Database |
31+
| `UserName` | yes | The UserName of the SQL Database |
32+
| `Password` | yes | The Password of the SQL Database |
33+
| `TrustServerCertificate`| no (default: `$false`) | Indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust. |
34+
| `ScriptsFolder` | no (default: `$PSScriptRoot/sqlScripts` | The directory folder where the SQL migration scripts are located on the file system |
35+
| `ScriptsFileFilter` | no (default: `*.sql`) | The file filter to limit the SQL script files to use during the migrations |
36+
| `DatabaseSchema` | no (default: `dbo`) | The database schema to use when running SQL commands on the target database |
3637

3738
Make sure that the credentials that you provide can write tables to the database + any action that you specify in the SQL scripts. (If the user is a member of the `db_ddlamin` role, then that user should have the necessary rights)
3839

@@ -56,6 +57,7 @@ PS> Invoke-AzSqlDatabaseMigration `
5657
-DatabaseName "my-database-name" `
5758
-Username "my-sql-username" `
5859
-Password "my-sql-password" `
60+
-TrustServerCertificate `
5961
-ScriptsFolder "$PSScriptRoot/sql-scripts" `
6062
-ScriptsFileFilter "*.MyScript.sql" `
6163
-DatabaseSchema "custom"
-8 Bytes
Binary file not shown.

src/Arcus.Scripting.Sql/Arcus.Scripting.Sql.psm1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class DatabaseVersion : System.IComparable {
7373
.Parameter Password
7474
The password to be used to connect to the Azure SQL Database.
7575
76+
.Parameter TrustServerCertificate
77+
Indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust.
78+
7679
.Parameter ScriptsFolder
7780
The directory folder where the SQL migration scripts are located on the file system.
7881
@@ -88,12 +91,13 @@ function Invoke-AzSqlDatabaseMigration {
8891
[Parameter(Mandatory = $true)][string] $DatabaseName = $(throw "Please provide the name of the SQL Database"),
8992
[Parameter(Mandatory = $true)][string] $UserName = $(throw "Please provide the UserName of the SQL Database"),
9093
[Parameter(Mandatory = $true)][string] $Password = $(throw "Please provide the Password of the SQL Database"),
94+
[Parameter(Mandatory = $false)][switch] $TrustServerCertificate,
9195
[Parameter(Mandatory = $false)][string] $ScriptsFolder = "$PSScriptRoot/sqlScripts",
9296
[Parameter(Mandatory = $false)][string] $ScriptsFileFilter = "*.sql",
9397
[Parameter(Mandatory = $false)][string] $DatabaseSchema = "dbo"
9498
)
9599

96-
. $PSScriptRoot\Scripts\Invoke-AzSqlDatabaseMigration.ps1 -ServerName $ServerName -DatabaseName $DatabaseName -UserName $UserName -Password $Password -ScriptsFolder $ScriptsFolder -ScriptsFileFilter $ScriptsFileFilter -DatabaseSchema $DatabaseSchema
100+
. $PSScriptRoot\Scripts\Invoke-AzSqlDatabaseMigration.ps1 -ServerName $ServerName -DatabaseName $DatabaseName -UserName $UserName -Password $Password -TrustServerCertificate $TrustServerCertificate -ScriptsFolder $ScriptsFolder -ScriptsFileFilter $ScriptsFileFilter -DatabaseSchema $DatabaseSchema
97101
}
98102

99103
Export-ModuleMember -Function Invoke-AzSqlDatabaseMigration

src/Arcus.Scripting.Sql/Scripts/Invoke-AzSqlDatabaseMigration.ps1

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ param(
33
[Parameter(Mandatory = $true)][string] $DatabaseName = $(throw "Please provide the name of the SQL Database"),
44
[Parameter(Mandatory = $true)][string] $UserName = $(throw "Please provide the user name of the user that must be used to perform the update"),
55
[Parameter(Mandatory = $true)][string] $Password = $(throw "Please provide the password of the user that must be used to perform the update"),
6+
[Parameter(Mandatory = $false)][bool] $TrustServerCertificate = $false,
67
[Parameter(Mandatory = $false)][string] $ScriptsFolder = "$PSScriptRoot/sqlScripts",
78
[Parameter(Mandatory = $false)][string] $ScriptsFileFilter = "*.sql",
89
[Parameter(Mandatory = $false)][string] $DatabaseSchema = "dbo"
@@ -27,18 +28,19 @@ function Execute-DbCommandWithResult($params, [string] $query) {
2728
return $result
2829
}
2930

30-
function Create-DbParams([string] $DatabaseName, [string] $serverInstance, [string] $UserName, [string] $Password) {
31+
function Create-DbParams([string] $DatabaseName, [string] $serverInstance, [string] $UserName, [string] $Password, [bool] $TrustServerCertificate) {
3132
Write-Debug "databasename = $DatabaseName"
3233
Write-Debug "serverinstance = $serverInstance"
3334
Write-Debug "username = $UserName"
3435

3536
return $params = @{
36-
'Database' = $DatabaseName
37-
'ServerInstance' = $serverInstance
38-
'Username' = $UserName
39-
'Password' = $Password
40-
'OutputSqlErrors' = $true
41-
'AbortOnError' = $true
37+
'Database' = $DatabaseName
38+
'ServerInstance' = $serverInstance
39+
'Username' = $UserName
40+
'Password' = $Password
41+
'TrustServerCertificate' = $TrustServerCertificate
42+
'OutputSqlErrors' = $true
43+
'AbortOnError' = $true
4244
}
4345
}
4446

@@ -47,7 +49,7 @@ function Get-SqlScriptFileText([string] $scriptPath, [string] $fileName) {
4749
return $query = Get-Content $currentfilepath
4850
}
4951

50-
$params = Create-DbParams $DatabaseName $ServerName $UserName $Password
52+
$params = Create-DbParams $DatabaseName $ServerName $UserName $Password $TrustServerCertificate
5153

5254
$createDatabaseVersionTable = "IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'DatabaseVersion' AND TABLE_SCHEMA = '$DatabaseSchema' ) " +
5355
"BEGIN " +

src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.Sql.tests.ps1

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ InModuleScope Arcus.Scripting.Sql {
101101
Describe "Arcus Azure SQL integration tests" {
102102
BeforeAll {
103103
$config = & $PSScriptRoot\Load-JsonAppsettings.ps1
104-
$serverInstance = $config.Arcus.Sql.ServerName + '.database.windows.net'
104+
$serverInstance = If($config.Arcus.Sql.UseLocalDb) { $config.Arcus.Sql.ServerName } Else { $config.Arcus.Sql.ServerName + '.database.windows.net' }
105105
$params = @{
106-
'ServerInstance' = $serverInstance
107-
'Database' = $config.Arcus.Sql.DatabaseName
108-
'Username' = $config.Arcus.Sql.UserName
109-
'Password' = $config.Arcus.Sql.Password
110-
'OutputSqlErrors' = $true
111-
'AbortOnError' = $true
106+
'ServerInstance' = $serverInstance
107+
'Database' = $config.Arcus.Sql.DatabaseName
108+
'Username' = $config.Arcus.Sql.UserName
109+
'Password' = $config.Arcus.Sql.Password
110+
'TrustServerCertificate' = $config.Arcus.Sql.TrustServerCertificate
111+
'OutputSqlErrors' = $true
112+
'AbortOnError' = $true
112113
}
113114

114115
& $PSScriptRoot\Connect-AzAccountFromConfig.ps1 -config $config
@@ -147,6 +148,7 @@ InModuleScope Arcus.Scripting.Sql {
147148
-DatabaseName $config.Arcus.Sql.DatabaseName `
148149
-Username $config.Arcus.Sql.Username `
149150
-Password $config.Arcus.Sql.Password `
151+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
150152
-ScriptsFolder "$PSScriptRoot\SqlScripts"
151153

152154
# Assert
@@ -169,6 +171,7 @@ InModuleScope Arcus.Scripting.Sql {
169171
-DatabaseName $config.Arcus.Sql.DatabaseName `
170172
-Username $config.Arcus.Sql.Username `
171173
-Password $config.Arcus.Sql.Password `
174+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
172175
-DatabaseSchema $customSchema `
173176
-ScriptsFolder "$PSScriptRoot\SqlScripts"
174177

@@ -201,6 +204,7 @@ InModuleScope Arcus.Scripting.Sql {
201204
-DatabaseName $config.Arcus.Sql.DatabaseName `
202205
-Username $config.Arcus.Sql.Username `
203206
-Password $config.Arcus.Sql.Password `
207+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
204208
-ScriptsFolder "$PSScriptRoot\SqlScripts"
205209

206210
# Assert
@@ -235,6 +239,7 @@ InModuleScope Arcus.Scripting.Sql {
235239
-DatabaseName $config.Arcus.Sql.DatabaseName `
236240
-Username $config.Arcus.Sql.Username `
237241
-Password $config.Arcus.Sql.Password `
242+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
238243
-ScriptsFolder "$PSScriptRoot\SqlScripts\MigrationScriptsAreSuccessfullyExecuted"
239244

240245
# Assert
@@ -282,6 +287,7 @@ InModuleScope Arcus.Scripting.Sql {
282287
-DatabaseName $config.Arcus.Sql.DatabaseName `
283288
-Username $config.Arcus.Sql.Username `
284289
-Password $config.Arcus.Sql.Password `
290+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
285291
-ScriptsFolder "$PSScriptRoot\SqlScripts\MigrationStopsOnError" } | Should -Throw
286292

287293
$version = Get-AzSqlDatabaseVersion $params
@@ -301,6 +307,7 @@ InModuleScope Arcus.Scripting.Sql {
301307
-DatabaseName $config.Arcus.Sql.DatabaseName `
302308
-Username $config.Arcus.Sql.Username `
303309
-Password $config.Arcus.Sql.Password `
310+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
304311
-ScriptsFolder "$PSScriptRoot\SqlScripts\OldMigrationScriptsAreStillSupported"
305312

306313
$version = Get-AzSqlDatabaseVersion $params
@@ -316,6 +323,7 @@ InModuleScope Arcus.Scripting.Sql {
316323
-DatabaseName $config.Arcus.Sql.DatabaseName `
317324
-Username $config.Arcus.Sql.Username `
318325
-Password $config.Arcus.Sql.Password `
326+
-TrustServerCertificate:([bool]::Parse($config.Arcus.Sql.TrustServerCertificate)) `
319327
-ScriptsFolder "$PSScriptRoot\SqlScripts\OldAndNewNamingConventionSupported"
320328

321329
$version = Get-AzSqlDatabaseVersion $params

src/Arcus.Scripting.Tests.Integration/appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
}
3030
},
3131
"Sql": {
32+
"UseLocalDb": false,
3233
"ServerName": "#{Arcus.Scripting.Sql.ServerName}#",
3334
"DatabaseName": "#{Arcus.Scripting.Sql.DatabaseName}#",
3435
"UserName": "#{Arcus.Scripting.Sql.UserName}#",
35-
"Password": "#{Arcus.Scripting.Sql.Password}#"
36+
"Password": "#{Arcus.Scripting.Sql.Password}#",
37+
"TrustServerCertificate": false
3638
},
3739
"ActiveDirectory": {
3840
"TenantId": "#{Arcus.Scripting.ActiveDirectory.TenantId}#",

0 commit comments

Comments
 (0)