Skip to content

Commit fef5d3e

Browse files
authored
feat: Invoke-AzSqlDatabaseMigration supports AccessToken login (#451)
* support access-token login * code cleanup * remove dbg statement * some docs
1 parent be399cd commit fef5d3e

4 files changed

Lines changed: 46 additions & 14 deletions

File tree

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ The current version is stored in a table "DatabaseVersion", which will be create
2828
| ------------------------| --------------------------------------- | ----------------------------------------------------------------------------------- |
2929
| `ServerName` | yes | The full name of the SQL Server that hosts the SQL Database. |
3030
| `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 |
31+
| `UserName` | no | The UserName of the user that must be used to login to the SQL Database. Prefer AccessToken instead |
32+
| `Password` | no | The Password of the user that must be used to login to the SQL Database. Prefer AccessToken instead |
33+
| `AccessToken` | no | The access token used to authenticate to SQL Server, as an alternative to user/password or Windows Authentication. Do not specify UserName/Password when using this parameter. |
3334
| `TrustServerCertificate`| no (default: `$false`) | Indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust. |
3435
| `ScriptsFolder` | no (default: `$PSScriptRoot/sqlScripts` | The directory folder where the SQL migration scripts are located on the file system |
3536
| `ScriptsFileFilter` | no (default: `*.sql`) | The file filter to limit the SQL script files to use during the migrations |
@@ -65,6 +66,20 @@ PS> Invoke-AzSqlDatabaseMigration `
6566
# Done migrating database. Current Database version is 1.0.0
6667
```
6768

69+
**Login using AccessToken**
70+
71+
```powershell
72+
PS> Connect-AzAccount
73+
PS> $access_token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
74+
75+
PS> Invoke-AzSqlDatabaseMigration `
76+
-ServerName "my-server-name.database.windows.net" `
77+
-DatabaseName "my-database-name" `
78+
-AccessToken $access_token
79+
# DB migration 1.0.0 applied!
80+
# Done migrating database. Current Database version is 1.0.0
81+
```
82+
6883
### Adding SQL scripts so they can be picked up by the script
6984

7085
1. In the location where you want to run the script add a folder where the migration scripts will be placed. By default, we're looking in a folder called `SqlScripts`, but this can be any folder as it is configurable via the `ScriptsFolder` argument.
2 Bytes
Binary file not shown.

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ class DatabaseVersion : System.IComparable {
7171
The name of the user to be used to connect to the Azure SQL Database.
7272
7373
.Parameter Password
74-
The password to be used to connect to the Azure SQL Database.
74+
The password to be used to connect to the Azure SQL Database for the specified UserName. Prefer connecting via AccessToken instead.
75+
76+
.Parameter AccessToken
77+
The access token used to authenticate to SQL Server. Do not specify UserName/Password when using this parameter.
7578
7679
.Parameter TrustServerCertificate
7780
Indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust.
@@ -92,16 +95,17 @@ function Invoke-AzSqlDatabaseMigration {
9295
param(
9396
[Parameter(Mandatory = $true)][string] $ServerName = $(throw "Please provide the name of the SQL Server that hosts the SQL Database. (Do not include 'database.windows.net'"),
9497
[Parameter(Mandatory = $true)][string] $DatabaseName = $(throw "Please provide the name of the SQL Database"),
95-
[Parameter(Mandatory = $true)][string] $UserName = $(throw "Please provide the UserName of the SQL Database"),
96-
[Parameter(Mandatory = $true)][string] $Password = $(throw "Please provide the Password of the SQL Database"),
98+
[Parameter(Mandatory = $false)][string] $UserName,
99+
[Parameter(Mandatory = $false)][string] $Password,
100+
[Parameter(Mandatory = $false)][string] $AccessToken,
97101
[Parameter(Mandatory = $false)][switch] $TrustServerCertificate,
98102
[Parameter(Mandatory = $false)][string] $ScriptsFolder = "$PSScriptRoot/sqlScripts",
99103
[Parameter(Mandatory = $false)][string] $ScriptsFileFilter = "*.sql",
100104
[Parameter(Mandatory = $false)][string] $DatabaseSchema = "dbo",
101105
[Parameter(Mandatory = $false)][string] $DatabaseVersionTable = "DatabaseVersion"
102106
)
103107

104-
. $PSScriptRoot\Scripts\Invoke-AzSqlDatabaseMigration.ps1 -ServerName $ServerName -DatabaseName $DatabaseName -UserName $UserName -Password $Password -TrustServerCertificate $TrustServerCertificate -ScriptsFolder $ScriptsFolder -ScriptsFileFilter $ScriptsFileFilter -DatabaseSchema $DatabaseSchema -DatabaseVersionTable $DatabaseVersionTable
108+
. $PSScriptRoot\Scripts\Invoke-AzSqlDatabaseMigration.ps1 -ServerName $ServerName -DatabaseName $DatabaseName -UserName $UserName -Password $Password -AccessToken $AccessToken -TrustServerCertificate $TrustServerCertificate -ScriptsFolder $ScriptsFolder -ScriptsFileFilter $ScriptsFileFilter -DatabaseSchema $DatabaseSchema -DatabaseVersionTable $DatabaseVersionTable
105109
}
106110

107111
Export-ModuleMember -Function Invoke-AzSqlDatabaseMigration

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
param(
22
[Parameter(Mandatory = $true)][string] $ServerName = $(throw "Please provide the name of the SQL Server that hosts the SQL Database. (Do not include 'database.windows.net'"),
33
[Parameter(Mandatory = $true)][string] $DatabaseName = $(throw "Please provide the name of the SQL Database"),
4-
[Parameter(Mandatory = $true)][string] $UserName = $(throw "Please provide the user name of the user that must be used to perform the update"),
5-
[Parameter(Mandatory = $true)][string] $Password = $(throw "Please provide the password of the user that must be used to perform the update"),
4+
[Parameter(Mandatory = $false)][string] $UserName,
5+
[Parameter(Mandatory = $false)][string] $Password,
6+
[Parameter(Mandatory = $false)][string] $AccessToken,
67
[Parameter(Mandatory = $false)][bool] $TrustServerCertificate = $false,
78
[Parameter(Mandatory = $false)][string] $ScriptsFolder = "$PSScriptRoot/sqlScripts",
89
[Parameter(Mandatory = $false)][string] $ScriptsFileFilter = "*.sql",
@@ -29,28 +30,40 @@ function Execute-DbCommandWithResult($params, [string] $query) {
2930
return $result
3031
}
3132

32-
function Create-DbParams([string] $DatabaseName, [string] $serverInstance, [string] $UserName, [string] $Password, [bool] $TrustServerCertificate) {
33+
function Create-DbParams([string] $DatabaseName, [string] $serverInstance, [string] $UserName = $null, [string] $Password = $null, [string] $AccessToken = $null, [bool] $TrustServerCertificate) {
3334
Write-Debug "databasename = $DatabaseName"
3435
Write-Debug "serverinstance = $serverInstance"
3536
Write-Debug "username = $UserName"
3637

37-
return $params = @{
38+
$params = @{
3839
'Database' = $DatabaseName
39-
'ServerInstance' = $serverInstance
40-
'Username' = $UserName
41-
'Password' = $Password
40+
'ServerInstance' = $serverInstance
4241
'TrustServerCertificate' = $TrustServerCertificate
4342
'OutputSqlErrors' = $true
4443
'AbortOnError' = $true
4544
}
45+
46+
if ($UserName) {
47+
$params['UserName'] = $UserName
48+
}
49+
50+
if ($Password) {
51+
$params['Password'] = $Password
52+
}
53+
54+
if ($AccessToken) {
55+
$params['AccessToken'] = $AccessToken
56+
}
57+
58+
return $params
4659
}
4760

4861
function Get-SqlScriptFileText([string] $scriptPath, [string] $fileName) {
4962
$currentfilepath = "$scriptPath/$fileName.sql"
5063
return $query = Get-Content $currentfilepath
5164
}
5265

53-
$params = Create-DbParams $DatabaseName $ServerName $UserName $Password $TrustServerCertificate
66+
$params = Create-DbParams $DatabaseName $ServerName $UserName $Password $AccessToken $TrustServerCertificate
5467

5568
$createDatabaseVersionTable = "IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$DatabaseVersionTable' AND TABLE_SCHEMA = '$DatabaseSchema' ) " +
5669
"BEGIN " +

0 commit comments

Comments
 (0)