Skip to content

Commit 62e5eb8

Browse files
nvarscarkirill-kravtsov-eb
authored andcommitted
Adding module-scope erroraction stop
1 parent 60786f5 commit 62e5eb8

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

dbops.psm1

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

119+
Register-PSFConfigValidation -Name "errorAction" -ScriptBlock {
120+
Param (
121+
$Value
122+
)
123+
$Result = New-Object PSObject -Property @{
124+
Success = $True
125+
Value = $null
126+
Message = ""
127+
}
128+
try {
129+
$Result.Value = [String][System.Management.Automation.ActionPreference]$Value
130+
}
131+
catch {
132+
$Result.Message = $_.Exception.Message
133+
$Result.Success = $False
134+
}
135+
return $Result
136+
}
137+
119138
Register-PSFConfigValidation -Name "tokenRegex" -ScriptBlock {
120139
Param (
121140
$Value
@@ -175,6 +194,10 @@ Set-PSFConfig -FullName dbops.package.slim -Value $false -Validation bool -Initi
175194
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)\}"
176195
$dotNet = try { dotnet --version } catch { $null }
177196
Set-PSFConfig -FullName dbops.runtime.dotnetversion -Value ($dotNet -as [version]) -Description "Current dotnet runtime." -Hidden
197+
Set-PSFConfig -FullName dbops.ErrorActionPreference -Value Stop -Initialize -Description "Module ErrorAction default value" -Validation errorAction
198+
199+
# Module-wide ErrorAction preference
200+
$ErrorActionPreference = Get-PSFConfigValue -FullName dbops.ErrorActionPreference
178201

179202
# extensions for SMO
180203
$typeData = Get-TypeData -TypeName 'Microsoft.SqlServer.Management.Smo.Database'
@@ -213,4 +236,4 @@ $aliases = @(
213236
)
214237
$aliases | ForEach-Object {
215238
if (-not (Test-Path Alias:$($_.AliasName))) { Set-Alias -Scope Global -Name $($_.AliasName) -Value $($_.Definition) }
216-
}
239+
}

functions/Invoke-DBOQuery.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ function Invoke-DBOQuery {
427427
}
428428
catch {
429429
# don't really need anything else here
430-
throw $_
430+
Write-Error $_
431431
}
432432
finally {
433433
# close the connection even when interrupted by Ctrl+C

internal/functions/Invoke-Deployment.ps1

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
if (!$Status.Successful) {
135135
# Throw output error if unsuccessful
136136
if ($Status.Error) {
137-
if ($Status.ErrorScript) {
137+
if ($upgradeResult.errorScript) {
138138
$Status.ErrorScript = $upgradeResult.errorScript.Name
139139
}
140140
throw $Status.Error
@@ -352,17 +352,12 @@
352352
}
353353
}
354354
catch {
355-
throw $_
355+
Write-Error $_
356356
}
357357
finally {
358358
$status.EndTime = [datetime]::Now
359359
}
360-
if ($status.Successful) {
361-
return $status
362-
}
363-
else {
364-
return $status | Select-PSFObject -ShowProperty SourcePath, SqlInstance, Database, Successful, Error, Duration, ErrorScript
365-
}
360+
return $status
366361
}
367362
}
368363
end { }

internal/xml/dbops.format.ps1xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
<ListItem>
109109
<PropertyName>Duration</PropertyName>
110110
</ListItem>
111+
<ListItem>
112+
<PropertyName>ErrorScript</PropertyName>
113+
</ListItem>
111114
</ListItems>
112115
</ListEntry>
113116
</ListEntries>

tests/Install-DBOScript.Tests.ps1

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,8 @@ Describe "Install-DBOScript integration tests" -Tag $commandName, IntegrationTes
457457
}
458458
}
459459
Context "deployments with errors should throw terminating errors" {
460-
BeforeAll {
460+
BeforeEach {
461461
$null = Invoke-DBOQuery @connParams -InputFile $cleanupScript
462-
$null = Install-DBOScript -Absolute -ScriptPath $v1scripts @connParams -SchemaVersionTable $null
463462
}
464463
It "Should return terminating error when object exists" {
465464
#Running package
@@ -480,15 +479,15 @@ Describe "Install-DBOScript integration tests" -Tag $commandName, IntegrationTes
480479
$testResults.Successful | Should -Be $false
481480
$testResults.SqlInstance | Should Be $script:mssqlInstance
482481
$testResults.Database | Should Be $newDbName
483-
$testResults.SourcePath | Should Be $v1scripts
482+
$testResults.SourcePath | Should Be (Get-ChildItem $tranFailScripts).FullName
484483
$testResults.ConnectionType | Should Be 'SQLServer'
485-
$testResults.Configuration.SchemaVersionTable | Should BeNullOrEmpty
484+
$testResults.Configuration.SchemaVersionTable | Should -Be $logTable
486485
$testResults.Error.Message | Should Be "There is already an object named 'a' in the database."
487-
$testResults.ErrorScript | Should -Be (Resolve-Path $v1scripts).Path
488-
$testResults.Scripts.Name | Should Be (Resolve-Path $v1scripts).Path
486+
$testResults.ErrorScript | Should -Be (Get-ChildItem $tranFailScripts)[1].FullName
487+
$testResults.Scripts.Name | Should Be (Get-ChildItem $tranFailScripts)[0].FullName
489488
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
490-
$testResults.StartTime | Should Not BeNullOrEmpty
491-
$testResults.EndTime | Should Not BeNullOrEmpty
489+
$testResults.StartTime | Should -Not -BeNullOrEmpty
490+
$testResults.EndTime | Should -Not -BeNullOrEmpty
492491
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
493492
}
494493
It "should not deploy anything after throwing an error" {
@@ -507,7 +506,7 @@ Describe "Install-DBOScript integration tests" -Tag $commandName, IntegrationTes
507506
#Verifying objects
508507
$testResults = Invoke-DBOQuery @connParams -InputFile $verificationScript
509508
'a' | Should BeIn $testResults.name
510-
'b' | Should BeIn $testResults.name
509+
'b' | Should Not BeIn $testResults.name
511510
'c' | Should Not BeIn $testResults.name
512511
'd' | Should Not BeIn $testResults.name
513512
}

0 commit comments

Comments
 (0)