Skip to content

Commit cb601dd

Browse files
authored
Merge pull request #56 from sqlcollaborative/Xplat-tests
Full MySQL coverage: tests and lots of fixes
2 parents 37fc810 + 1ab4c3c commit cb601dd

File tree

62 files changed

+1947
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1947
-232
lines changed

appveyor.yml

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
build: false
44
version: 0.1.{build}
55

6-
#images
6+
#images
77

8-
image:
8+
image:
99
- Ubuntu1804
1010
- Visual Studio 2017
1111

@@ -15,38 +15,31 @@ environment:
1515
version: 0.1.$(appveyor_build_number)
1616

1717
matrix:
18-
- scenario: ALL
18+
- scenario: all
19+
# - scenario: mysql
1920

2021

2122
for:
22-
-
23-
matrix:
24-
only:
25-
- image: Visual Studio 2017
26-
environment:
27-
mssql_instance: localhost\SQL2017
28-
services:
29-
- mssql2017
23+
- matrix:
24+
only:
25+
- image: Visual Studio 2017
26+
#scenario: all
27+
environment:
28+
mssql_instance: localhost\SQL2017
29+
services:
30+
- mssql2017
31+
- mysql
32+
33+
- matrix:
34+
only:
35+
- image: Ubuntu1804
36+
#scenario: default
37+
environment:
38+
mssql_instance: localhost
39+
services:
40+
- mssql
41+
- mysql
3042

31-
- matrix:
32-
only:
33-
- image: Ubuntu1804
34-
environment:
35-
mssql_instance: localhost
36-
mssql_login: sa
37-
mssql_password: Password12!
38-
services:
39-
- mssql
40-
# before_test:
41-
# # run preparation scripts
42-
# - ps: ./tests/appveyor.prep.ps1
43-
# test_script:
44-
# # Test with native PS version
45-
# - ps: ./tests/appveyor.pester.ps1
46-
# # Collecting results
47-
# - ps: ./tests/appveyor.pester.ps1 -Finalize
48-
# after_test:
49-
# - ps: ./tests/appveyor.post.ps1
5043

5144
before_test:
5245
# run preparation scripts
@@ -57,12 +50,9 @@ test_script:
5750
# Collecting results
5851
- ps: .\tests\appveyor.pester.ps1 -Finalize
5952
after_test:
60-
- ps: .\tests\appveyor.post.ps1
53+
- ps: .\tests\appveyor.post.ps1
54+
6155

62-
# #Configure services
63-
# services:
64-
# - mssql2017
65-
6656
# Set alternative clone folder
6757
#clone_folder: c:\github\dbops
6858

bin/deploy.ps1

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Param (
2626
[switch]$CreateDatabase,
2727
[AllowNull()]
2828
[string]$ConnectionString,
29-
[ValidateSet('SQLServer', 'Oracle')]
30-
[Alias('Type', 'ServerType')]
31-
[string]$ConnectionType = 'SQLServer'
29+
[ValidateSet('SqlServer', 'Oracle', 'MySQL', 'PostgreSQL')]
30+
[Alias('ConnectionType', 'ServerType')]
31+
[string]$Type = 'SQLServer'
3232
)
3333

3434
#Import module
@@ -39,22 +39,24 @@ If (-not (Get-Module dbops)) {
3939

4040
$config = Get-DBOConfig -Path "$PSScriptRoot\dbops.config.json" -Configuration $Configuration
4141

42-
#Convert custom parameters into a package configuration, excluding variables
42+
#Merge custom parameters into a configuration
43+
$newConfig = @{}
4344
foreach ($key in ($PSBoundParameters.Keys)) {
44-
if ($key -in [DBOps.ConfigProperty].GetEnumNames() -and $key -ne 'Variables') {
45+
if ($key -in [DBOpsConfig]::EnumProperties()) {
4546
Write-PSFMessage -Level Debug -Message "Overriding parameter $key with $($PSBoundParameters[$key])"
46-
$config.SetValue($key, $PSBoundParameters[$key])
47+
$newConfig.$key = $PSBoundParameters[$key]
4748
}
4849
}
50+
$config.Merge($newConfig)
4951

5052
#Prepare deployment function call parameters
5153
$params = @{
52-
PackageFile = "$PSScriptRoot\dbops.package.json"
54+
PackageFile = "$PSScriptRoot\dbops.package.json"
5355
Configuration = $config
5456
}
5557
foreach ($key in ($PSBoundParameters.Keys)) {
5658
#If any custom properties were specified
57-
if ($key -in @('OutputFile', 'Append', 'Variables', 'ConnectionType', 'Build')) {
59+
if ($key -in @('OutputFile', 'Append', 'Type', 'Build')) {
5860
$params += @{ $key = $PSBoundParameters[$key] }
5961
}
6062
}

functions/Invoke-DBODeployment.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@
155155

156156
Write-PSFMessage -Level Debug -Message "Creating DbUp objects"
157157
# Create DbUp connection object
158-
$connString = Get-ConnectionString -Configuration $config -Type $Type
158+
$csBuilder = Get-ConnectionString -Configuration $config -Type $Type -Raw
159+
$connString = $csBuilder.ToString()
159160
$dbUpConnection = Get-ConnectionManager -ConnectionString $connString -Type $Type
160161

161162
# Create DbUpBuilder based on the connection

functions/Invoke-DBOQuery.ps1

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,12 @@ function Invoke-DBOQuery {
173173
}
174174

175175
#Build connection string
176-
#$connString = Get-ConnectionString -Configuration $config -Type $Type
177-
#$dbUpConnection = Get-ConnectionManager -ConnectionString $connString -Type $Type
178176
Write-PSFMessage -Level Debug -Message "Getting the connection object"
179-
$dbUpConnection = Get-ConnectionManager -Configuration $config -Type $Type
180-
$dbUpSqlParser = Get-SqlParser -Type $Type
181-
$status = [DBOpsDeploymentStatus]::new()
182-
$dbUpLog = [DBOpsLog]::new($config.Silent, $OutputFile, $Append, $status)
183-
$dbUpLog.CallStack = (Get-PSCallStack)[0]
184-
if ($null -ne $dbUpConnection.IsScriptOutputLogged -and -Not $config.Silent) {
185-
$dbUpConnection.IsScriptOutputLogged = $true
186-
}
177+
$dbUpConnection = Get-ConnectionManager -ConnectionString $null -Type $Type # only needed for query parsing for now, connection string is pointless
178+
$dataConnection = Get-DatabaseConnection -Configuration $config -Type $Type # the 'real' connection
187179
Write-PSFMessage -Level Verbose -Message "Establishing connection with $Type $($config.SqlInstance)"
188180
try {
189-
$managedConnection = $dbUpConnection.OperationStarting($dbUpLog, $null)
181+
$dataConnection.Open()
190182
}
191183
catch {
192184
Stop-PSFFunction -EnableException $true -Message "Failed to connect to the server" -ErrorRecord $_
@@ -216,51 +208,49 @@ function Invoke-DBOQuery {
216208
foreach ($qText in $queryText) {
217209
$queryList += Resolve-VariableToken $qText $config.Variables
218210
}
211+
219212
Write-PSFMessage -Level Debug -Message "Preparing to run $($queryList.Count) queries"
213+
$ds = [System.Data.DataSet]::new()
220214
try {
221-
$ds = [System.Data.DataSet]::new()
222215
$qCount = 0
223216
foreach ($queryItem in $queryList) {
224217
$qCount++
225218
if ($PSCmdlet.ShouldProcess("Executing query $qCount", $config.SqlInstance)) {
219+
# split commands using DbUp parser
226220
foreach ($splitQuery in $dbUpConnection.SplitScriptIntoCommands($queryItem)) {
227-
$dt = [System.Data.DataTable]::new()
228-
$rows = $dbUpConnection.ExecuteCommandsWithManagedConnection( [Func[Func[Data.IDbCommand], pscustomobject]] {
229-
Param (
230-
$dbCommandFactory
231-
)
232-
# Compile the parameters as Expression functions
233-
$exp = [System.Linq.Expressions.Expression]
234-
$parameterList = @()
235-
foreach ($key in $Parameter.Keys) {
236-
$param = $exp::Parameter([string], $key)
237-
$value = $exp::Constant($Parameter.$key)
238-
$body = $exp::Convert($value, [System.Object])
239-
$lambda = $exp::Lambda([Func[string, object]], $body, $param)
240-
$parameterList += $lambda
241-
}
242-
# Initiating the AdHocSqlRunner
243-
$sqlRunner = [DbUp.Helpers.AdHocSqlRunner]::new($dbCommandFactory, $dbUpSqlParser, $config.Schema)
244-
return $sqlRunner.ExecuteReader($splitQuery, $parameterList)
245-
})
246-
$rowCount = ($rows | Measure-Object).Count
247-
if ($rowCount -gt 0) {
248-
$keys = switch ($rowCount) {
249-
1 { $rows.Keys }
250-
default { $rows[0].Keys }
221+
# create a new command object and define text/parameters
222+
$command = $dataConnection.CreateCommand()
223+
$command.CommandText = $splitQuery
224+
foreach ($key in $Parameter.Keys) {
225+
$null = switch ($Type) {
226+
Oracle { $command.Parameters.Add($key, $Parameter[$key]) }
227+
default { $command.Parameters.AddWithValue($key, $Parameter[$key]) }
251228
}
252-
foreach ($column in $keys) {
253-
$null = $dt.Columns.Add($column)
229+
}
230+
# create a reader and define output table columns
231+
$reader = $command.ExecuteReader()
232+
$table = [System.Data.DataTable]::new()
233+
$definition = $reader.GetSchemaTable()
234+
foreach ($column in $definition) {
235+
$name = $column.ColumnName
236+
$datatype = $column.DataType
237+
for ($j = 1; -not $name; $j++) {
238+
if ($table.Columns.ColumnName -notcontains "Column$j") { $name = "Column$j" }
254239
}
255-
foreach ($row in $rows) {
256-
$dr = $dt.NewRow()
257-
foreach ($col in $row.Keys) {
258-
$dr[$col] = $row[$col]
259-
}
260-
$null = $dt.Rows.Add($dr);
240+
$null = $table.Columns.Add($name, $datatype)
241+
}
242+
# read rows and assign values
243+
while ($reader.Read()) {
244+
$row = $table.NewRow()
245+
for ($i = 0; $i -lt $reader.FieldCount; $i++) {
246+
$row[$table.Columns[$i].ColumnName] = $reader.GetValue($i);
261247
}
248+
$table.Rows.Add($row)
262249
}
263-
$null = $ds.Tables.Add($dt);
250+
$ds.Tables.Add($table)
251+
$reader.Close()
252+
$reader.Dispose()
253+
$command.Dispose()
264254
}
265255
}
266256
}
@@ -269,7 +259,7 @@ function Invoke-DBOQuery {
269259
Stop-PSFFunction -EnableException $true -Message "Failed to run the query" -ErrorRecord $_
270260
}
271261
finally {
272-
$managedConnection.Dispose()
262+
$dataConnection.Dispose()
273263
}
274264
switch ($As) {
275265
'DataSet' {

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
}

functions/Test-DBOSupportedSystem.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ Function Test-DBOSupportedSystem {
2424
)
2525
begin {}
2626
process {
27+
# try looking up already loaded assemblies
28+
$lookupClass = switch ($Type) {
29+
SqlServer { 'System.Data.SqlClient.SqlConnection' }
30+
Oracle { 'Oracle.DataAccess.Client.OracleConnection' }
31+
MySQL { 'MySql.Data.MySqlClient.MySqlConnection' }
32+
PostgreSQL { 'Npgsql.NpgsqlConnection' }
33+
default { Stop-PSFFunction -Message "Unknown type $Type" -EnableException $true }
34+
}
35+
if ([System.AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $_.GetType($lookupClass, 0) }) {
36+
return $true
37+
}
38+
# otherwise get package from the local system
2739
$dependencies = Get-ExternalLibrary -Type $Type
2840
foreach ($package in $dependencies) {
2941
$packageEntry = Get-Package $package.Name -RequiredVersion $package.Version -ProviderName nuget -ErrorAction SilentlyContinue
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class DBOpsMySqlTableJournal : DbUp.MySql.MySqlTableJournal {
2+
# Constructors
3+
DBOpsMySqlTableJournal([Func[DbUp.Engine.Transactions.IConnectionManager]] $connectionManager, [Func[DbUp.Engine.Output.IUpgradeLog]] $logger, [string] $schema, [string] $table) :base($connectionManager, $logger, $schema, $table) {}
4+
# Overriding DoesTalbeExist method to get the proper schema table when schemaversiontable name is not specified
5+
[string] DoesTableExistSql() {
6+
if ($this.SchemaTableSchema) {
7+
return "select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = '{0}' and TABLE_SCHEMA = '{1}'" -f $this.UnquotedSchemaTableName, $this.SchemaTableSchema
8+
}
9+
else {
10+
return "select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = '{0}' and TABLE_SCHEMA = DATABASE()" -f $this.UnquotedSchemaTableName
11+
}
12+
}
13+
}

internal/functions/Get-ConnectionManager.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function Get-ConnectionManager {
22
# Returns a connection manager object
33
Param (
4-
[Parameter(ParameterSetName = 'ConnString', Mandatory)]
4+
[Parameter(ParameterSetName = 'ConnString')]
55
[string]$ConnectionString,
66
[Parameter(ParameterSetName = 'Config', Mandatory)]
77
[DBOpsConfig]$Configuration,

0 commit comments

Comments
 (0)