Skip to content

Commit 2ecbe23

Browse files
authored
Merge pull request #89 from sqlcollaborative/development
0.5.8
2 parents 14e5970 + 6f659db commit 2ecbe23

13 files changed

+154
-66
lines changed

dbops.psm1

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ Register-PSFConfigValidation -Name "connectionType" -ScriptBlock {
116116
return $Result
117117
}
118118

119+
Register-PSFConfigValidation -Name "tokenRegex" -ScriptBlock {
120+
Param (
121+
$Value
122+
)
123+
$failMessage = "Should contain capture group (token)"
124+
$Result = New-Object PSObject -Property @{
125+
Success = $True
126+
Value = $null
127+
Message = ""
128+
}
129+
try {
130+
if (([string]$Value) -is [string] -and [string]$Value -like '*(token)*') {
131+
$Result.Value = [string]$Value
132+
}
133+
else {
134+
$Result.Message = $failMessage
135+
$Result.Success = $False
136+
}
137+
}
138+
catch {
139+
$Result.Message = "Failed to convert value to string. $failMessage"
140+
$Result.Success = $False
141+
}
142+
return $Result
143+
}
144+
119145
# defining defaults
120146

121147
Set-PSFConfig -FullName dbops.ApplicationName -Value "dbops" -Initialize -Description "Application name in the connection string"
@@ -145,7 +171,8 @@ Set-PSFConfig -FullName dbops.mail.Subject -Value "DBOps deployment status" -Ini
145171
Set-PSFConfig -FullName dbops.security.encryptionkey -Value "~/.dbops.key" -Initialize -Description "Path to a custom encryption key used to encrypt/decrypt passwords. The key should be a binary file with a length of 128, 192 or 256 bits. Key will be generated automatically if not exists."
146172
Set-PSFConfig -FullName dbops.security.usecustomencryptionkey -Value ($PSVersionTable.Platform -eq 'Unix') -Validation bool -Initialize -Description "Determines whether to use a custom encryption key for storing passwords. Enabled by default only on Unix platforms."
147173
Set-PSFConfig -FullName dbops.rdbms.type -Value 'SqlServer' -Validation connectionType -Initialize -Description "Assumes a certain RDBMS as a default one for each command. SQLServer by default"
148-
Set-PSFConfig -FullName dbops.package.slim -Value $false -Validation bool -Initialize -Description "Decides whether to make the packages 'slim' and omit module files when creating the package. Default: false."
174+
Set-PSFConfig -FullName dbops.package.slim -Value $false -Validation bool -Initialize -Description "Decides whether to make the packages 'slim' and omit module files when creating the package. Default: `$false"
175+
Set-PSFConfig -FullName dbops.config.variabletoken -Value "\#\{(token)\}" -Validation tokenRegex -Initialize -Description "Variable replacement token. Regex string that will be replaced with values from -Variables parameters. Default: \#\{(token)\}"
149176

150177
# extensions for SMO
151178
$typeData = Get-TypeData -TypeName 'Microsoft.SqlServer.Management.Smo.Database'

functions/Set-DBODefaultSetting.ps1

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ function Set-DBODefaultSetting {
22
<#
33
.SYNOPSIS
44
Sets configuration entries.
5-
5+
66
.DESCRIPTION
77
This function creates or changes configuration values.
88
These can be used to provide dynamic configuration information outside the PowerShell variable system.
9-
9+
1010
.PARAMETER Name
1111
Name of the configuration entry.
12-
12+
1313
.PARAMETER Value
1414
The value to assign to the named configuration element.
15-
16-
.PARAMETER Handler
17-
A scriptblock that is executed when a value is being set.
18-
Is only executed if the validation was successful (assuming there was a validation, of course)
19-
15+
2016
.PARAMETER Append
2117
Adds the value to the existing configuration instead of overwriting it
22-
18+
2319
.PARAMETER Temporary
2420
The setting is not persisted outside the current session.
2521
By default, settings will be remembered across all powershell sessions.
@@ -39,22 +35,23 @@ function Set-DBODefaultSetting {
3935
4036
.EXAMPLE
4137
Set-DBODefaultSetting -Name ConnectionTimeout -Value 5 -Temporary
42-
38+
4339
Change connection timeout setting for the current Powershell session to 5 seconds.
44-
40+
4541
.EXAMPLE
4642
Set-DBODefaultSetting -Name SchemaVersionTable -Value $null
47-
43+
4844
Change the default SchemaVersionTable setting to null, disabling the deployment logging by default
4945
#>
5046
[CmdletBinding(DefaultParameterSetName = "FullName", SupportsShouldProcess)]
5147
param (
48+
[parameter(Mandatory)]
5249
[string]$Name,
50+
[parameter(Mandatory)]
5351
[AllowNull()]
5452
[AllowEmptyCollection()]
5553
[AllowEmptyString()]
5654
$Value,
57-
[System.Management.Automation.ScriptBlock]$Handler,
5855
[switch]$Append,
5956
[switch]$Temporary,
6057
[ValidateSet('CurrentUser', 'AllUsers')]
@@ -66,7 +63,7 @@ function Set-DBODefaultSetting {
6663
Stop-PSFFunction -Message "Setting named $Name does not exist." -EnableException $true
6764
return
6865
}
69-
66+
7067
$newValue = $Value
7168
if ($append) {
7269
$newValue += (Get-DBODefaultSetting -Name $Name -Value)

internal/classes/DBOps.class.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class DBOpsPackageBase : DBOps {
358358
return $false
359359
}
360360
[string] ExportToJson() {
361-
$exportObject = @{} | Select-Object -Property $this.PropertiesToExport
361+
$exportObject = @{ } | Select-Object -Property $this.PropertiesToExport
362362
foreach ($type in $exportObject.psobject.Properties.name) {
363363
$property = $this.PsObject.Properties | Where-Object Name -eq $type
364364
if ($this.$type -is [DBOps]) {
@@ -1005,7 +1005,13 @@ class DBOpsFile : DBOps {
10051005
return $dPath.Replace('/', '\')
10061006
}
10071007
[string] ExportToJson() {
1008-
return $this | Select-Object -Property $this.PropertiesToExport | ConvertTo-Json -Depth 1
1008+
$expObject = @{ } | Select-Object -Property $this.PropertiesToExport
1009+
foreach ($prop in $this.PropertiesToExport) {
1010+
$expObject.$prop = $this.$prop
1011+
}
1012+
# replace symbols in PackagePath
1013+
$expObject.PackagePath = $this.PackagePath -replace ':', ''
1014+
return $expObject | ConvertTo-Json -Depth 1
10091015
}
10101016
#Writes current script into the archive file
10111017
[void] Save([ZipArchive]$zipFile) {
@@ -1138,7 +1144,7 @@ class DBOpsConfig : DBOps {
11381144

11391145
#Methods
11401146
[hashtable] AsHashtable () {
1141-
$ht = @{}
1147+
$ht = @{ }
11421148
foreach ($property in $this.psobject.Properties.Name) {
11431149
$ht += @{ $property = $this.$property }
11441150
}
@@ -1175,7 +1181,7 @@ class DBOpsConfig : DBOps {
11751181
}
11761182
# Returns a JSON string representin the object
11771183
[string] ExportToJson() {
1178-
$outObject = @{}
1184+
$outObject = @{ }
11791185
foreach ($prop in [DBOpsConfig]::EnumProperties()) {
11801186
if ($this.$prop -is [securestring]) {
11811187
$outObject += @{ $prop = $this.$prop | ConvertTo-EncryptedString }
@@ -1188,7 +1194,8 @@ class DBOpsConfig : DBOps {
11881194
}
11891195
}
11901196
}
1191-
else { $outObject += @{ $prop = $this.$prop }
1197+
else {
1198+
$outObject += @{ $prop = $this.$prop }
11921199
}
11931200
}
11941201
return $outObject | ConvertTo-Json -Depth 3
@@ -1251,7 +1258,7 @@ class DBOpsConfig : DBOps {
12511258
foreach ($key in $config.Keys) {
12521259
if ($key -eq 'Variables') {
12531260
# create new hashtable with all the existing variables
1254-
$hashVar = @{}
1261+
$hashVar = @{ }
12551262
foreach ($variable in $this.Variables.psobject.Properties.Name) {
12561263
$hashVar += @{
12571264
$variable = $this.Variables.$variable
@@ -1304,6 +1311,6 @@ class DBOpsConfig : DBOps {
13041311

13051312
#Returns deploy file name
13061313
static [object]GetDeployFile() {
1307-
return (Get-DBOModuleFileList | Where-Object { $_.Type -eq 'Misc' -and $_.Name -eq "Deploy.ps1"})
1314+
return (Get-DBOModuleFileList | Where-Object { $_.Type -eq 'Misc' -and $_.Name -eq "Deploy.ps1" })
13081315
}
13091316
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
Function Get-VariableTokens {
1+
Function Get-VariableToken {
22
<#
33
.SYNOPSIS
44
Get a list of #{tokens} from the string
5-
5+
66
.DESCRIPTION
77
Returns an array of tokens that matches token regex #{token}
8-
8+
99
.PARAMETER InputString
1010
String to run regex against
11-
11+
1212
.EXAMPLE
1313
Get-VariableTokens '#{foo} myString #{bar}' # returns @('foo','bar')
1414
#>
1515
Param (
16-
[string]$InputString
16+
[string]$InputString,
17+
[string]$RegexString
1718
)
18-
[regex]::matches($InputString, "\#\{([a-zA-Z0-9.]*)\}") | ForEach-Object { $_.Groups[1].Value }
19+
[regex]::matches($InputString, $RegexString) | ForEach-Object { $_.Groups[1].Value }
1920
}

internal/functions/Resolve-VariableToken.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
[CmdletBinding()]
1919
Param (
2020
[object[]]$InputObject,
21-
[object]$Runtime
21+
[object]$Runtime,
22+
[string]$TokenRegex = (Get-DBODefaultSetting -Name config.variabletoken -Value)
2223
)
2324
foreach ($obj in $InputObject) {
2425
if ($obj -is [string]) {
2526
$output = $obj
26-
foreach ($token in (Get-VariableTokens $obj)) {
27+
foreach ($token in (Get-VariableToken -InputString $obj -RegexString $TokenRegex.Replace('token', '[a-zA-Z0-9\.]+'))) {
2728
#Replace variables found in the config
28-
$tokenRegEx = "\#\{$token\}"
29+
$tokenRegExString = $TokenRegex.Replace('token', [Regex]::Escape($token))
2930
if ($Runtime) {
3031
if ($Runtime -is [hashtable]) { $variableList = $Runtime.Keys }
3132
else { $variableList = $Runtime.psobject.Properties.Name }
3233
if ($variableList -contains $token) {
3334
Write-PSFMessage -Level Debug -Message "Replacing token $token"
34-
$output = $output -replace $tokenRegEx, $Runtime.$token
35+
$output = $output -replace $tokenRegExString, $Runtime.$token
3536
}
3637
}
3738
}

internal/json/dbops.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"Internal": [
5151
"internal\\classes\\DBOps.enums.ps1",
5252
"internal\\functions\\Get-DbopsFile.ps1",
53-
"internal\\functions\\Get-VariableTokens.ps1",
53+
"internal\\functions\\Get-VariableToken.ps1",
5454
"internal\\functions\\Resolve-VariableToken.ps1",
5555
"internal\\functions\\Get-NewBuildNumber.ps1",
5656
"internal\\functions\\Get-ExternalLibrary.ps1",

tests/DBOpsBuild.class.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Describe "DBOpsBuild class tests" -Tag $commandName, UnitTests, DBOpsBuild {
6161
}
6262
BeforeEach {
6363
$pkg = [DBOpsPackage]::new()
64+
$pkg.Slim = $true
6465
$pkg.SaveToFile($packageName, $true)
6566
$build = $pkg.NewBuild('1.0')
6667
}
@@ -89,6 +90,7 @@ Describe "DBOpsBuild class tests" -Tag $commandName, UnitTests, DBOpsBuild {
8990
Context "tests other methods" {
9091
BeforeEach {
9192
$pkg = [DBOpsPackage]::new()
93+
$pkg.Slim = $true
9294
$build = $pkg.NewBuild('1.0')
9395
$f = [DBOpsFile]::new($fileObject1, $scriptPath1, $true)
9496
$build.AddScript($f)
@@ -256,7 +258,7 @@ Describe "DBOpsBuild class tests" -Tag $commandName, UnitTests, DBOpsBuild {
256258
$testResults = Get-ArchiveItem "$packageName.test.zip"
257259
$saveTestsErrors = 0
258260
#should trigger file updates for build files and module files
259-
foreach ($testResult in ($oldResults | Where-Object { $_.Path -like (Join-PSFPath -Normalize 'content\1.0\success\*') -or $_.Path -like (Join-PSFPath -Normalize 'Modules\dbops\*') } )) {
261+
foreach ($testResult in ($oldResults | Where-Object { $_.Path -like (Join-PSFPath -Normalize 'content\1.0\success\*') -or $_.Path -like (Join-PSFPath -Normalize 'Modules\dbops\*') } )) {
260262
if ($testResult.LastWriteTime -ge ($testResults | Where-Object Path -eq $testResult.Path).LastWriteTime) {
261263
It "Should have updated Modified date for file $($testResult.Path)" {
262264
$testResult.LastWriteTime -lt ($testResults | Where-Object Path -eq $testResult.Path).LastWriteTime | Should Be $true

tests/DBOpsFile.class.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Describe "DBOpsFile class tests" -Tag $commandName, UnitTests, DBOpsFile {
152152
Context "tests other DBOpsFile methods" {
153153
BeforeEach {
154154
$pkg = [DBOpsPackage]::new()
155+
$pkg.Slim = $true
155156
$build = $pkg.NewBuild('1.0')
156157
$pkg.SaveToFile($packageName, $true)
157158
$file = [DBOpsFile]::new($fileObject1, (Join-PSFPath -Normalize 'success\1.sql'), $true)

tests/Install-DBOPackage.Tests.ps1

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ Describe "Install-DBOPackage integration tests" -Tag $commandName, IntegrationTe
592592
}
593593
Context "testing deployment using variables in config" {
594594
BeforeAll {
595-
$p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force -Configuration @{SqlInstance = '#{srv}'; Database = '#{db}'}
595+
$p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force -Configuration @{SqlInstance = '#{srv}'; Database = '#{db}' }
596596
$outputFile = "$workFolder\log.txt"
597597
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript
598598
}
@@ -602,7 +602,7 @@ Describe "Install-DBOPackage integration tests" -Tag $commandName, IntegrationTe
602602
It "should deploy version 1.0" {
603603
$before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript
604604
$rowsBefore = ($before | Measure-Object).Count
605-
$testResults = Install-DBOPackage "$workFolder\pv1.zip" -Credential $script:mssqlCredential -Variables @{srv = $script:mssqlInstance; db = $newDbName} -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent
605+
$testResults = Install-DBOPackage "$workFolder\pv1.zip" -Credential $script:mssqlCredential -Variables @{srv = $script:mssqlInstance; db = $newDbName } -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent
606606
$testResults.Successful | Should Be $true
607607
$testResults.Scripts.Name | Should Be $v1Journal
608608
$testResults.SqlInstance | Should Be $script:mssqlInstance
@@ -666,4 +666,42 @@ Describe "Install-DBOPackage integration tests" -Tag $commandName, IntegrationTe
666666
'd' | Should Not BeIn $testResults.name
667667
}
668668
}
669+
Context "testing deployment from a package with an absolute path" {
670+
BeforeAll {
671+
$p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force -Absolute
672+
$outputFile = "$workFolder\log.txt"
673+
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript
674+
}
675+
AfterAll {
676+
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -Query "IF OBJECT_ID('SchemaVersions') IS NOT NULL DROP TABLE SchemaVersions"
677+
}
678+
It "should deploy version 1.0" {
679+
$before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript
680+
$rowsBefore = ($before | Measure-Object).Count
681+
$testResults = Install-DBOPackage "$workFolder\pv1.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -Silent
682+
$testResults.Successful | Should Be $true
683+
$absolutePath = Get-Item $v1scripts | ForEach-Object { Join-PSFPath 1.0 ($_.FullName -replace '^/|^\\|^\\\\|\.\\|\./|:', "") }
684+
$testResults.Scripts.Name | Should BeIn ($absolutePath -replace '/', '\')
685+
$testResults.SqlInstance | Should Be $script:mssqlInstance
686+
$testResults.Database | Should Be $newDbName
687+
$testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip")
688+
$testResults.ConnectionType | Should Be 'SQLServer'
689+
$testResults.Configuration.SchemaVersionTable | Should Be 'SchemaVersions'
690+
$testResults.Error | Should BeNullOrEmpty
691+
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
692+
$testResults.StartTime | Should Not BeNullOrEmpty
693+
$testResults.EndTime | Should Not BeNullOrEmpty
694+
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
695+
'Upgrade successful' | Should BeIn $testResults.DeploymentLog
696+
697+
#Verifying objects
698+
$testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript
699+
'SchemaVersions' | Should BeIn $testResults.name
700+
'a' | Should BeIn $testResults.name
701+
'b' | Should BeIn $testResults.name
702+
'c' | Should Not BeIn $testResults.name
703+
'd' | Should Not BeIn $testResults.name
704+
($testResults | Measure-Object).Count | Should Be ($rowsBefore + 3)
705+
}
706+
}
669707
}

tests/Install-DBOSqlScript.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ Describe "Install-DBOSqlScript integration tests" -Tag $commandName, Integration
123123
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript
124124
}
125125
It "should deploy version 1.0" {
126-
$testResults = Install-DBOSqlScript -Absolute -ScriptPath $v1scripts -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
126+
$testResults = Install-DBOSqlScript -ScriptPath $v1scripts -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
127127
$testResults.Successful | Should Be $true
128-
$testResults.Scripts.Name | Should Be (Resolve-Path $v1scripts).Path
128+
$testResults.Scripts.Name | Should Be (Get-Item $v1scripts).Name
129129
$testResults.SqlInstance | Should Be $script:mssqlInstance
130130
$testResults.Database | Should Be $newDbName
131131
$testResults.SourcePath | Should Be $v1scripts
@@ -147,9 +147,9 @@ Describe "Install-DBOSqlScript integration tests" -Tag $commandName, Integration
147147
'd' | Should Not BeIn $testResults.name
148148
}
149149
It "should deploy version 2.0" {
150-
$testResults = Install-DBOSqlScript -Absolute -ScriptPath $v2scripts -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
150+
$testResults = Install-DBOSqlScript -ScriptPath $v2scripts -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
151151
$testResults.Successful | Should Be $true
152-
$testResults.Scripts.Name | Should Be (Resolve-Path $v2scripts).Path
152+
$testResults.Scripts.Name | Should Be (Get-Item $v2scripts).Name
153153
$testResults.SqlInstance | Should Be $script:mssqlInstance
154154
$testResults.Database | Should Be $newDbName
155155
$testResults.SourcePath | Should Be $v2scripts

0 commit comments

Comments
 (0)