Skip to content

Commit 1afbcdc

Browse files
committed
adding AlwaysRollback mode
1 parent 150af60 commit 1afbcdc

5 files changed

Lines changed: 42 additions & 4 deletions

File tree

functions/Install-DBOPackage.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- SingleTransaction: wrap all the deployment scripts into a single transaction and rollback whole deployment on error
2828
- TransactionPerScript: wrap each script into a separate transaction; rollback single script deployment in case of error
2929
- NoTransaction: deploy as is
30+
- AlwaysRollback: deploy as a single transaction, but rollback changes after the deployment is done
3031
3132
Default: NoTransaction
3233
@@ -149,7 +150,7 @@
149150
[string]$SqlInstance,
150151
[Parameter(Position = 3)]
151152
[string]$Database,
152-
[ValidateSet('SingleTransaction', 'TransactionPerScript', 'NoTransaction')]
153+
[ValidateSet('SingleTransaction', 'TransactionPerScript', 'NoTransaction', 'AlwaysRollback')]
153154
[string]$DeploymentMethod = 'NoTransaction',
154155
[int]$ConnectionTimeout,
155156
[int]$ExecutionTimeout,

functions/Install-DBOScript.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- SingleTransaction: wrap all the deployment scripts into a single transaction and rollback whole deployment on error
2828
- TransactionPerScript: wrap each script into a separate transaction; rollback single script deployment in case of error
2929
- NoTransaction: deploy as is
30+
- AlwaysRollback: deploy as a single transaction, but rollback changes after the deployment is done
3031
3132
Default: NoTransaction
3233
@@ -158,7 +159,7 @@
158159
[string]$SqlInstance,
159160
[Parameter(Position = 3)]
160161
[string]$Database,
161-
[ValidateSet('SingleTransaction', 'TransactionPerScript', 'NoTransaction')]
162+
[ValidateSet('SingleTransaction', 'TransactionPerScript', 'NoTransaction', 'AlwaysRollback')]
162163
[string]$DeploymentMethod = 'NoTransaction',
163164
[int]$ConnectionTimeout,
164165
[int]$ExecutionTimeout,

internal/functions/Get-DbUpBuilder.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ function Get-DbUpBuilder {
6262
elseif ($Config.DeploymentMethod -eq 'TransactionPerScript') {
6363
$dbUp = [StandardExtensions]::WithTransactionPerScript($dbUp)
6464
}
65+
elseif ($Config.DeploymentMethod -eq 'AlwaysRollback') {
66+
$dbUp = [StandardExtensions]::WithTransactionAlwaysRollback($dbUp)
67+
}
6568
# Adding execution timeout - defaults to unlimited execution
6669
$dbUp = [StandardExtensions]::WithExecutionTimeout($dbUp, [timespan]::FromSeconds($config.ExecutionTimeout))
6770
return $dbUp

tests/functional/Install-DBOPackage.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ Describe "<type> Install-DBOPackage functional tests" -Tag FunctionalTests -ForE
7777
'd' | Should -Not -BeIn $after.(Get-ColumnName name)
7878
}
7979
}
80+
Context "testing rollback deployment" {
81+
BeforeAll {
82+
if ($Type -in 'MySQL', 'Oracle') {
83+
Set-ItResult -Skipped -Because "CREATE TABLE cannot be rolled back in $Type"
84+
}
85+
$p1 = New-DBOPackage -ScriptPath (Get-PackageScript -Version 1) -Name "$workFolder\pv1" -Build 1.0 -Force
86+
Reset-TestDatabase
87+
}
88+
It "should rollback version 1.0" {
89+
$testResults = Install-DBOPackage $p1 -Build '1.0' @dbConnectionParams -SchemaVersionTable $logTable -DeploymentMethod AlwaysRollback
90+
$testResults | Test-DeploymentOutput -Version 1 -HasJournal
91+
$testResults.SourcePath | Should -Be (Join-PSFPath -Normalize "$workFolder\pv1.zip")
92+
93+
"Beginning transaction" | Should -BeIn $testResults.DeploymentLog
94+
"Success! No errors have occured when executing scripts, transaction will be rolled back" | Should -BeIn $testResults.DeploymentLog
95+
Test-DeploymentState -Version 0
96+
}
97+
}
8098
Context "testing regular deployment" {
8199
BeforeAll {
82100
$p1 = New-DBOPackage -ScriptPath (Get-PackageScript -Version 1) -Name "$workFolder\pv1" -Build 1.0 -Force

tests/functional/Install-DBOScript.Tests.ps1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Describe "<type> Install-DBOScript functional tests" -Tag FunctionalTests -ForEa
3737
if ($Type -in 'MySQL', 'Oracle') {
3838
Set-ItResult -Skipped -Because "CREATE TABLE cannot be rolled back in $Type"
3939
}
40-
#Running package
4140
{
4241
$null = Install-DBOScript -Path $tranFailScripts @dbConnectionParams -SchemaVersionTable $logTable -DeploymentMethod SingleTransaction
4342
} | Should -Throw (Get-TableExistsMessage "a")
@@ -49,7 +48,6 @@ Describe "<type> Install-DBOScript functional tests" -Tag FunctionalTests -ForEa
4948
$null = Invoke-DBOQuery @dbConnectionParams -InputFile $cleanupScript
5049
}
5150
It "should throw an error and create one object" {
52-
#Running package
5351
try {
5452
$null = Install-DBOScript -Path $tranFailScripts @dbConnectionParams -SchemaVersionTable $logTable -DeploymentMethod NoTransaction
5553
}
@@ -89,6 +87,23 @@ Describe "<type> Install-DBOScript functional tests" -Tag FunctionalTests -ForEa
8987
Test-DeploymentState -Script -Version 2 -HasJournal
9088
}
9189
}
90+
Context "testing rollback deployment" {
91+
BeforeAll {
92+
if ($Type -in 'MySQL', 'Oracle') {
93+
Set-ItResult -Skipped -Because "CREATE TABLE cannot be rolled back in $Type"
94+
}
95+
Reset-TestDatabase
96+
}
97+
It "should rollback version 1.0" {
98+
$testResults = Install-DBOScript -ScriptPath (Get-PackageScript -Version 1) @dbConnectionParams -SchemaVersionTable $logTable -DeploymentMethod AlwaysRollback
99+
$testResults | Test-DeploymentOutput -Version 1 -HasJournal -Script
100+
$testResults.SourcePath | Should -Be (Get-PackageScript -Version 1)
101+
102+
"Beginning transaction" | Should -BeIn $testResults.DeploymentLog
103+
"Success! No errors have occured when executing scripts, transaction will be rolled back" | Should -BeIn $testResults.DeploymentLog
104+
Test-DeploymentState -Script -Version 0
105+
}
106+
}
92107
Context "testing deployment order" {
93108
BeforeAll {
94109
Reset-TestDatabase

0 commit comments

Comments
 (0)