Skip to content

Commit 9eee49a

Browse files
authored
Merge pull request #87 from sqlcollaborative/feature/fix-absolute
Fixing absolute path in package deployments
2 parents ab60f82 + c14647a commit 9eee49a

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

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
}

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/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
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

tests/New-DBOPackage.Tests.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Describe "New-DBOPackage tests" -Tag $commandName, UnitTests {
8181
$testResults.ModuleVersion | Should Be (Get-Module dbops).Version
8282
Test-Path $packageName | Should Be $true
8383
}
84+
It "should create proper package with abolute path" {
85+
$p = New-DBOPackage -Path $packageName -ScriptPath "$here\etc\query1.sql" -Force -Absolute -Build 1;
86+
$testResults = Get-ArchiveItem $p
87+
$path = $here.Replace(':', '') -replace '^/', ''
88+
Join-PSFPath content\1 $path etc\query1.sql -Normalize | Should -BeIn $testResults.Path
89+
}
8490
}
8591
Context "current folder tests" {
8692
BeforeAll {
@@ -190,7 +196,7 @@ Describe "New-DBOPackage tests" -Tag $commandName, UnitTests {
190196
$config.Variables | Should Be $null
191197
}
192198
It "should be able to store variables" {
193-
$null = New-DBOPackage -ScriptPath "$here\etc\query1.sql" -Name $packageName -Configuration @{ ApplicationName = 'FooBar' } -Variables @{ MyVar = 'foo'; MyBar = 1; MyNull = $null} -Force
199+
$null = New-DBOPackage -ScriptPath "$here\etc\query1.sql" -Name $packageName -Configuration @{ ApplicationName = 'FooBar' } -Variables @{ MyVar = 'foo'; MyBar = 1; MyNull = $null } -Force
194200
$null = Expand-ArchiveItem -Path $packageName -DestinationPath $workFolder -Item 'dbops.config.json'
195201
$config = Get-Content "$workFolder\dbops.config.json" | ConvertFrom-Json
196202
$config.ApplicationName | Should Be 'FooBar'

0 commit comments

Comments
 (0)