Skip to content

Commit b304fa5

Browse files
authored
Merge pull request #71 from sqlcollaborative/postgresVars
0.5.4
2 parents 86bf147 + 258ab64 commit b304fa5

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

functions/Invoke-DBODeployment.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@
145145
Stop-PSFFunction -Message "Expected DBOpsFile object, got [$($scriptItem.GetType().FullName)]." -EnableException $true
146146
return
147147
}
148-
Write-PSFMessage -Level Debug -Message "Adding deployment script $($scriptItem.FullName)"
148+
Write-PSFMessage -Level Debug -Message "Adding deployment script $($scriptItem.FullName) as $($scriptItem.PackagePath)"
149149
if (!$RegisterOnly) {
150150
# Replace tokens in the scripts
151151
$scriptContent = Resolve-VariableToken $scriptItem.GetContent() $runtimeVariables
152152
}
153153
else {
154154
$scriptContent = ""
155155
}
156-
$scriptCollection += [DbUp.Engine.SqlScript]::new($scriptItem.FullName, $scriptContent)
156+
$scriptCollection += [DbUp.Engine.SqlScript]::new($scriptItem.PackagePath, $scriptContent)
157157
}
158158
}
159159

@@ -173,6 +173,9 @@
173173
$comparer = [DBOpsScriptComparer]::new($scriptCollection.Name)
174174
$dbUp = [StandardExtensions]::WithScriptNameComparer($dbUp, $comparer)
175175

176+
# Disable variable replacement
177+
$dbUp = [StandardExtensions]::WithVariablesDisabled($dbUp)
178+
176179
if ($config.DeploymentMethod -eq 'SingleTransaction') {
177180
$dbUp = [StandardExtensions]::WithTransaction($dbUp)
178181
}

internal/classes/DBOps.class.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,8 @@ class DBOpsFile : DBOps {
969969
}
970970
[string] GetPackagePath() {
971971
$pPath = $this.PackagePath
972+
# removing odd symbols
973+
$pPath = $pPath -replace ':',''
972974
if ($this.Parent) {
973975
if ($parentPath = $this.Parent.GetPackagePath()) {
974976
$pPath = Join-Path $this.Parent.GetPackagePath() $pPath
@@ -978,6 +980,8 @@ class DBOpsFile : DBOps {
978980
}
979981
[string] GetDeploymentPath () {
980982
$dPath = $this.PackagePath
983+
# removing odd symbols
984+
$dPath = $dPath -replace ':',''
981985
if ($this.Parent) {
982986
if ($parentPath = $this.Parent.GetDeploymentPath()) {
983987
$dPath = Join-Path $this.Parent.GetDeploymentPath() $dPath

internal/functions/Get-DbopsFile.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# replace ^.\ ^./ ^\\ and :
3636
$slash = [IO.Path]::DirectorySeparatorChar
3737
$slashRegex = [Regex]::Escape(".$slash")
38-
$pkgPath = $pkgPath -replace "^$slashRegex|^\\\\|:", ''
38+
$pkgPath = $pkgPath -replace "^$slashRegex|^\\\\", ''
3939
[DBOpsFile]::new($childItem, $pkgPath, $true)
4040
}
4141
}

tests/postgresql/Install-DBOSqlScript.Tests.ps1

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ Describe "Install-DBOSqlScript PostgreSQL integration tests" -Tag $commandName,
177177
'd' | Should BeIn $testResults.name
178178
}
179179
}
180+
Context "testing relative paths" {
181+
BeforeAll {
182+
$null = Invoke-DBOQuery @connParams -Database postgres -Query $dropDatabaseScript
183+
[Npgsql.NpgsqlConnection]::ClearAllPools()
184+
$null = Invoke-DBOQuery @connParams -Database postgres -Query $createDatabaseScript
185+
}
186+
It "should deploy version 1.0" {
187+
$testResults = Install-DBOSqlScript -Relative -Type PostgreSQL -ScriptPath $v1scripts -SqlInstance $script:postgresqlInstance -Credential $script:postgresqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
188+
$testResults.Successful | Should Be $true
189+
$testResults.Scripts.Name | Should Be ((Resolve-Path $v1scripts -Relative) -replace '^.[\\|\/]', '')
190+
$testResults.SqlInstance | Should Be $script:postgresqlInstance
191+
$testResults.Database | Should Be $newDbName
192+
$testResults.SourcePath | Should Be $v1scripts
193+
$testResults.ConnectionType | Should Be 'PostgreSQL'
194+
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
195+
$testResults.Error | Should BeNullOrEmpty
196+
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
197+
$testResults.StartTime | Should Not BeNullOrEmpty
198+
$testResults.EndTime | Should Not BeNullOrEmpty
199+
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
200+
'Upgrade successful' | Should BeIn $testResults.DeploymentLog
201+
202+
#Verifying objects
203+
$testResults = Invoke-DBOQuery -Type PostgreSQL -SqlInstance $script:postgresqlInstance -Silent -Credential $script:postgresqlCredential -Database $newDbName -InputFile $verificationScript
204+
$logTable | Should BeIn $testResults.name
205+
'a' | Should BeIn $testResults.name
206+
'b' | Should BeIn $testResults.name
207+
}
208+
}
180209
Context "testing deployment order" {
181210
BeforeAll {
182211
$null = Invoke-DBOQuery @connParams -Database postgres -Query $dropDatabaseScript
@@ -267,6 +296,22 @@ Describe "Install-DBOSqlScript PostgreSQL integration tests" -Tag $commandName,
267296
$output | Should BeLike '*Successful!*'
268297
}
269298
}
299+
Context "testing var replacement" {
300+
BeforeAll {
301+
$file = "$workFolder\vars.sql"
302+
'SELECT ''$a$'';' | Set-Content $file
303+
}
304+
BeforeEach {
305+
$null = Invoke-DBOQuery @connParams -Database postgres -Query $dropDatabaseScript
306+
[Npgsql.NpgsqlConnection]::ClearAllPools()
307+
$null = Invoke-DBOQuery @connParams -Database postgres -Query $createDatabaseScript
308+
}
309+
It "should not fail to read the script" {
310+
$null = Install-DBOSqlScript -Absolute -Type PostgreSQL -ScriptPath "$workFolder\vars.sql" -SqlInstance $script:postgresqlInstance -Credential $script:postgresqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent
311+
$output = Get-Content "$workFolder\log.txt" -Raw
312+
$output | Should BeLike '*$a$*'
313+
}
314+
}
270315
Context "$commandName whatif tests" {
271316
BeforeAll {
272317
$null = Invoke-DBOQuery @connParams -Database postgres -Query $dropDatabaseScript
@@ -313,9 +358,9 @@ Describe "Install-DBOSqlScript PostgreSQL integration tests" -Tag $commandName,
313358
It "should deploy version 1.0" {
314359
$before = Invoke-DBOQuery -Type PostgreSQL -SqlInstance $script:postgresqlInstance -Silent -Credential $script:postgresqlCredential -Database $newDbName -InputFile $verificationScript
315360
$rowsBefore = ($before | Measure-Object).Count
316-
$testResults = Install-DBOSqlScript -Absolute -Type PostgreSQL -ScriptPath $v1scripts -SqlInstance $script:postgresqlInstance -Credential $script:postgresqlCredential -Database $newDbName -Silent
361+
$testResults = Install-DBOSqlScript -Type PostgreSQL -ScriptPath $v1scripts -SqlInstance $script:postgresqlInstance -Credential $script:postgresqlCredential -Database $newDbName -Silent
317362
$testResults.Successful | Should Be $true
318-
$testResults.Scripts.Name | Should Be (Resolve-Path $v1scripts).Path
363+
$testResults.Scripts.Name | Should Be (Get-Item $v1scripts).Name
319364
$testResults.SqlInstance | Should Be $script:postgresqlInstance
320365
$testResults.Database | Should Be $newDbName
321366
$testResults.SourcePath | Should Be $v1scripts
@@ -340,9 +385,9 @@ Describe "Install-DBOSqlScript PostgreSQL integration tests" -Tag $commandName,
340385
It "should deploy version 2.0" {
341386
$before = Invoke-DBOQuery -Type PostgreSQL -SqlInstance $script:postgresqlInstance -Silent -Credential $script:postgresqlCredential -Database $newDbName -InputFile $verificationScript
342387
$rowsBefore = ($before | Measure-Object).Count
343-
$testResults = Install-DBOSqlScript -Absolute -Type PostgreSQL -ScriptPath $v2scripts -SqlInstance $script:postgresqlInstance -Credential $script:postgresqlCredential -Database $newDbName -Silent
388+
$testResults = Install-DBOSqlScript -Type PostgreSQL -ScriptPath $v2scripts -SqlInstance $script:postgresqlInstance -Credential $script:postgresqlCredential -Database $newDbName -Silent
344389
$testResults.Successful | Should Be $true
345-
$testResults.Scripts.Name | Should Be (Resolve-Path $v2scripts).Path
390+
$testResults.Scripts.Name | Should Be (Get-Item $v2scripts).Name
346391
$testResults.SqlInstance | Should Be $script:postgresqlInstance
347392
$testResults.Database | Should Be $newDbName
348393
$testResults.SourcePath | Should Be $v2scripts

0 commit comments

Comments
 (0)