Skip to content

Commit d1500ca

Browse files
committed
Register-DBOPackage for mysql
1 parent e841f0d commit d1500ca

File tree

3 files changed

+185
-6
lines changed

3 files changed

+185
-6
lines changed

functions/Register-DBOPackage.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Register-DBOPackage {
8181
.PARAMETER CreateDatabase
8282
Will create an empty database if missing on supported RDMBS
8383
84-
.PARAMETER ConnectionType
84+
.PARAMETER Type
8585
Defines the driver to use when connecting to the database server.
8686
Available options: SqlServer (default), Oracle
8787
@@ -153,9 +153,8 @@ function Register-DBOPackage {
153153
[switch]$CreateDatabase,
154154
[AllowNull()]
155155
[string]$ConnectionString,
156-
[ValidateSet('SQLServer', 'Oracle')]
157-
[Alias('Type', 'ServerType')]
158-
[string]$ConnectionType = 'SQLServer'
156+
[Alias('ConnectionType', 'ServerType')]
157+
[DBOps.ConnectionType]$Type = (Get-DBODefaultSetting -Name rdbms.type -Value)
159158
)
160159

161160
begin {
@@ -191,7 +190,7 @@ function Register-DBOPackage {
191190
}
192191
foreach ($key in ($PSBoundParameters.Keys)) {
193192
#If any custom properties were specified
194-
if ($key -in @('OutputFile', 'Append', 'ConnectionType', 'Build')) {
193+
if ($key -in @('OutputFile', 'Append', 'Type', 'Build')) {
195194
$params += @{ $key = $PSBoundParameters[$key] }
196195
}
197196
}

tests/mysql/Install-DBOSqlScript.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $newDbName = "test_dbops_InstallDBOSqlScript"
3333
$dropDatabaseScript = 'DROP DATABASE IF EXISTS `{0}`' -f $newDbName
3434
$createDatabaseScript = 'CREATE DATABASE IF NOT EXISTS `{0}`' -f $newDbName
3535

36-
Describe "Install-DBOSqlScript -Type MySQL integration tests" -Tag $commandName, IntegrationTests {
36+
Describe "Install-DBOSqlScript MySQL integration tests" -Tag $commandName, IntegrationTests {
3737
BeforeAll {
3838
if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse }
3939
$null = New-Item $workFolder -ItemType Directory -Force
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
Param (
2+
[switch]$Batch
3+
)
4+
5+
if ($PSScriptRoot) { $commandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", ""); $here = $PSScriptRoot }
6+
else { $commandName = "_ManualExecution"; $here = (Get-Item . ).FullName }
7+
$testRoot = (Get-Item $here\.. ).FullName
8+
9+
if (!$Batch) {
10+
# Is not a part of the global batch => import module
11+
#Explicitly import the module for testing
12+
Import-Module "$testRoot\..\dbops.psd1" -Force; Get-DBOModuleFileList -Type internal | ForEach-Object { . $_.FullName }
13+
}
14+
else {
15+
# Is a part of a batch, output some eye-catching happiness
16+
Write-Host "Running MySQL $commandName tests" -ForegroundColor Cyan
17+
}
18+
19+
. "$testRoot\constants.ps1"
20+
21+
$workFolder = Join-PSFPath -Normalize "$testRoot\etc" "$commandName.Tests.dbops"
22+
$logTable = "testdeploymenthistory"
23+
$cleanupScript = Join-PSFPath -Normalize "$testRoot\etc\mysql-tests\Cleanup.sql"
24+
$v1scripts = Join-PSFPath -Normalize "$testRoot\etc\mysql-tests\success\1.sql"
25+
$v1Journal = Get-Item $v1scripts | ForEach-Object { '1.0\' + $_.Name }
26+
$v2scripts = Join-PSFPath -Normalize "$testRoot\etc\mysql-tests\success\2.sql"
27+
$v2Journal = Get-Item $v2scripts | ForEach-Object { '2.0\' + $_.Name }
28+
$verificationScript = Join-PSFPath -Normalize "$testRoot\etc\mysql-tests\verification\select.sql"
29+
30+
$newDbName = "test_dbops_RegisterDBOPackage"
31+
$dropDatabaseScript = 'DROP DATABASE IF EXISTS `{0}`' -f $newDbName
32+
$createDatabaseScript = 'CREATE DATABASE IF NOT EXISTS `{0}`' -f $newDbName
33+
34+
Describe "Register-DBOPackage MySQL integration tests" -Tag $commandName, IntegrationTests {
35+
BeforeAll {
36+
if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse }
37+
$null = New-Item $workFolder -ItemType Directory -Force
38+
$null = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database mysql -Query $dropDatabaseScript, $createDatabaseScript
39+
}
40+
AfterAll {
41+
if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse }
42+
$null = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database mysql -Query $dropDatabaseScript
43+
}
44+
Context "testing registration with CreateDatabase specified" {
45+
BeforeAll {
46+
$p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force
47+
$p1 = Add-DBOBuild -ScriptPath $v2scripts -Package $p1 -Build 2.0
48+
$null = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database mysql -Query $dropDatabaseScript
49+
}
50+
It "should register version 1.0 in a new database using -CreateDatabase switch" {
51+
$testResults = Register-DBOPackage -Type MySQL $p1 -CreateDatabase -SqlInstance $script:mysqlInstance -Credential $script:mysqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
52+
$testResults.Successful | Should Be $true
53+
$testResults.Scripts.Name | Should Be (@($v1Journal) + @($v2Journal))
54+
$testResults.SqlInstance | Should Be $script:mysqlInstance
55+
$testResults.Database | Should Be $newDbName
56+
$testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip")
57+
$testResults.ConnectionType | Should Be 'MySQL'
58+
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
59+
$testResults.Configuration.CreateDatabase | Should Be $true
60+
$testResults.Error | Should BeNullOrEmpty
61+
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
62+
$testResults.StartTime | Should Not BeNullOrEmpty
63+
$testResults.EndTime | Should Not BeNullOrEmpty
64+
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
65+
$v1Journal | ForEach-Object { "$_ was registered in table $logtable" } | Should BeIn $testResults.DeploymentLog
66+
$v2Journal | ForEach-Object { "$_ was registered in table $logtable" } | Should BeIn $testResults.DeploymentLog
67+
"Created database $newDbName" | Should BeIn $testResults.DeploymentLog
68+
69+
#Verifying objects
70+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -InputFile $verificationScript
71+
$logTable | Should BeIn $testResults.name
72+
'a' | Should Not BeIn $testResults.name
73+
'b' | Should Not BeIn $testResults.name
74+
'c' | Should Not BeIn $testResults.name
75+
'd' | Should Not BeIn $testResults.name
76+
77+
#Verifying SchemaVersions table
78+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -Query "SELECT * FROM $logTable"
79+
$testResults.ScriptName | Should Be (@($v1Journal) + @($v2Journal))
80+
}
81+
}
82+
Context "testing registration of scripts" {
83+
BeforeAll {
84+
$p2 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv2" -Build 1.0 -Force
85+
$p2 = Add-DBOBuild -ScriptPath $v2scripts -Package $p2 -Build 2.0
86+
$outputFile = "$workFolder\log.txt"
87+
$null = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database mysql -Query $dropDatabaseScript, $createDatabaseScript
88+
}
89+
It "should register version 1.0 without creating any objects" {
90+
$before = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -InputFile $verificationScript
91+
$rowsBefore = ($before | Measure-Object).Count
92+
$testResults = Register-DBOPackage -Type MySQL -Package $p2 -Build 1.0 -SqlInstance $script:mysqlInstance -Credential $script:mysqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
93+
$testResults.Successful | Should Be $true
94+
$testResults.Scripts.Name | Should Be $v1Journal
95+
$testResults.SqlInstance | Should Be $script:mysqlInstance
96+
$testResults.Database | Should Be $newDbName
97+
$testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv2.zip")
98+
$testResults.ConnectionType | Should Be 'MySQL'
99+
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
100+
$testResults.Error | Should BeNullOrEmpty
101+
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
102+
$testResults.StartTime | Should Not BeNullOrEmpty
103+
$testResults.EndTime | Should Not BeNullOrEmpty
104+
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
105+
$v1Journal | ForEach-Object { "$_ was registered in table $logtable" } | Should BeIn $testResults.DeploymentLog
106+
107+
#Verifying objects
108+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -InputFile $verificationScript
109+
$logTable | Should BeIn $testResults.name
110+
'a' | Should Not BeIn $testResults.name
111+
'b' | Should Not BeIn $testResults.name
112+
'c' | Should Not BeIn $testResults.name
113+
'd' | Should Not BeIn $testResults.name
114+
($testResults | Measure-Object).Count | Should Be ($rowsBefore + 1)
115+
116+
#Verifying SchemaVersions table
117+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -Query "SELECT * FROM $logTable"
118+
$testResults.ScriptName | Should Be $v1Journal
119+
}
120+
It "should register version 1.0 + 2.0 without creating any objects" {
121+
$before = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -InputFile $verificationScript
122+
$rowsBefore = ($before | Measure-Object).Count
123+
$testResults = Register-DBOPackage -Type MySQL -Package $p2 -SqlInstance $script:mysqlInstance -Credential $script:mysqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent
124+
$testResults.Successful | Should Be $true
125+
$testResults.Scripts.Name | Should Be $v2Journal
126+
$testResults.SqlInstance | Should Be $script:mysqlInstance
127+
$testResults.Database | Should Be $newDbName
128+
$testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv2.zip")
129+
$testResults.ConnectionType | Should Be 'MySQL'
130+
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
131+
$testResults.Error | Should BeNullOrEmpty
132+
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
133+
$testResults.StartTime | Should Not BeNullOrEmpty
134+
$testResults.EndTime | Should Not BeNullOrEmpty
135+
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
136+
$v2Journal | ForEach-Object { "$_ was registered in table $logtable" } | Should BeIn $testResults.DeploymentLog
137+
138+
#Verifying objects
139+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -InputFile $verificationScript
140+
$logTable | Should BeIn $testResults.name
141+
'a' | Should Not BeIn $testResults.name
142+
'b' | Should Not BeIn $testResults.name
143+
'c' | Should Not BeIn $testResults.name
144+
'd' | Should Not BeIn $testResults.name
145+
($testResults | Measure-Object).Count | Should Be $rowsBefore
146+
147+
#Verifying SchemaVersions table
148+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -Query "SELECT * FROM $logTable"
149+
$testResults.ScriptName | Should Be (@($v1Journal) + @($v2Journal))
150+
}
151+
}
152+
Context "$commandName whatif tests" {
153+
BeforeAll {
154+
$p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force
155+
$null = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database mysql -Query $dropDatabaseScript, $createDatabaseScript
156+
}
157+
It "should deploy nothing" {
158+
$testResults = Register-DBOPackage -Type MySQL $p1 -SqlInstance $script:mysqlInstance -Credential $script:mysqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent -WhatIf
159+
$testResults.SqlInstance | Should Be $script:mysqlInstance
160+
$testResults.Database | Should Be $newDbName
161+
$testResults.SourcePath | Should Be $p1.FullName
162+
$testResults.ConnectionType | Should Be 'MySQL'
163+
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
164+
$testResults.Error | Should BeNullOrEmpty
165+
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
166+
$testResults.StartTime | Should Not BeNullOrEmpty
167+
$testResults.EndTime | Should Not BeNullOrEmpty
168+
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
169+
"Running in WhatIf mode - no registration performed." | Should BeIn $testResults.DeploymentLog
170+
171+
#Verifying objects
172+
$testResults = Invoke-DBOQuery -Type MySQL -SqlInstance $script:mysqlInstance -Silent -Credential $script:mysqlCredential -Database $newDbName -InputFile $verificationScript
173+
$logTable | Should Not BeIn $testResults.name
174+
'a' | Should Not BeIn $testResults.name
175+
'b' | Should Not BeIn $testResults.name
176+
'c' | Should Not BeIn $testResults.name
177+
'd' | Should Not BeIn $testResults.name
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)