Skip to content

Commit 4be52de

Browse files
committed
Improving support for multiple connection types
1 parent f449a2d commit 4be52de

File tree

10 files changed

+162
-71
lines changed

10 files changed

+162
-71
lines changed

functions/Invoke-DBODeployment.ps1

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@
114114
$config.SetValue($property, (Resolve-VariableToken $config.$property $config.Variables))
115115
}
116116

117-
# Build connection string
118-
$connString = Get-ConnectionString -Configuration $config -Type $Type
119-
120117
$scriptCollection = @()
121118
if ($PsCmdlet.ParameterSetName -ne 'Script') {
122119
# Get contents of the script files
@@ -151,25 +148,12 @@
151148
}
152149
}
153150

154-
# Build dbUp object
155-
$dbUp = [DbUp.DeployChanges]::To
156-
$dbUpConnection = Get-ConnectionManager -ConnectionString $connString -Type $Type
157-
if ($Type -eq 'SqlServer') {
158-
if ($config.Schema) {
159-
$dbUp = [SqlServerExtensions]::SqlDatabase($dbUp, $dbUpConnection, $config.Schema)
160-
}
161-
else {
162-
$dbUp = [SqlServerExtensions]::SqlDatabase($dbUp, $dbUpConnection)
163-
}
164-
}
165-
elseif ($Type -eq 'Oracle') {
166-
if ($config.Schema) {
167-
$dbUp = [DbUp.Oracle.OracleExtensions]::OracleDatabase($dbUpConnection, $config.Schema)
168-
}
169-
else {
170-
$dbUp = [DbUp.Oracle.OracleExtensions]::OracleDatabase($dbUpConnection)
171-
}
172-
}
151+
# Get DbUp connection object
152+
$dbUpConnection = Get-ConnectionManager -Configuration $config -Type $Type
153+
154+
# Get DbUpBuilder based on the connection
155+
$dbUp = Get-DbUpBuilder -Connection $dbUpConnection -Type $Type
156+
173157
# Add deployment scripts to the object
174158
$dbUp = [StandardExtensions]::WithScripts($dbUp, $scriptCollection)
175159

@@ -208,43 +192,8 @@
208192
$dbUp = [StandardExtensions]::LogTo($dbUp, $dbUpLog)
209193
$dbUp = [StandardExtensions]::LogScriptOutput($dbUp)
210194

211-
# Configure schema versioning
212-
if (!$config.SchemaVersionTable) {
213-
$dbUpTableJournal = [DbUp.Helpers.NullJournal]::new()
214-
}
215-
elseif ($config.SchemaVersionTable) {
216-
$table = $config.SchemaVersionTable.Split('.')
217-
if (($table | Measure-Object).Count -gt 2) {
218-
Stop-PSFFunction -EnableException $true -Message 'Incorrect table name - use the following syntax: schema.table'
219-
return
220-
}
221-
elseif (($table | Measure-Object).Count -eq 2) {
222-
$tableName = $table[1]
223-
$schemaName = $table[0]
224-
}
225-
elseif (($table | Measure-Object).Count -eq 1) {
226-
$tableName = $table[0]
227-
if ($config.Schema) {
228-
$schemaName = $config.Schema
229-
}
230-
else {}
231-
}
232-
else {
233-
Stop-PSFFunction -EnableException $true -Message 'No table name specified'
234-
return
235-
}
236-
# Set default schema for known DB Types
237-
if (!$schemaName) {
238-
if ($Type -eq 'SqlServer') { $schemaName = 'dbo' }
239-
}
240-
#Enable schema versioning
241-
if ($Type -eq 'SqlServer') { $dbUpJournalType = [DbUp.SqlServer.SqlTableJournal] }
242-
elseif ($Type -eq 'Oracle') { $dbUpJournalType = [DbUp.Oracle.OracleTableJournal] }
243-
244-
$dbUpTableJournal = $dbUpJournalType::new( { $dbUpConnection }, { $dbUpLog }, $schemaName, $tableName)
245-
246-
#$dbUp = [SqlServerExtensions]::JournalToSqlTable($dbUp, $schemaName, $tableName)
247-
}
195+
# Define schema versioning (journalling)
196+
$dbUpTableJournal = Get-DbUpJournal -Connection { $dbUpConnection } -Log { $dbUpLog } -Schema $config.Schema -SchemaVersionTable $config.SchemaVersionTable -Type $Type
248197
$dbUp = [StandardExtensions]::JournalTo($dbUp, $dbUpTableJournal)
249198

250199
# Adding execution timeout - defaults to unlimited execution

functions/Invoke-DBOQuery.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function Invoke-DBOQuery {
180180
$status = [DBOpsDeploymentStatus]::new()
181181
$dbUpLog = [DBOpsLog]::new($config.Silent, $OutputFile, $Append, $status)
182182
$dbUpLog.CallStack = (Get-PSCallStack)[0]
183-
if (-Not $config.Silent) {
183+
if ($null -ne $dbUpConnection.IsScriptOutputLogged -and -Not $config.Silent) {
184184
$dbUpConnection.IsScriptOutputLogged = $true
185185
}
186186
try {

internal/classes/DBOps.enums.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ $enums = @'
22
namespace DBOps {
33
public enum ConnectionType {
44
SQLServer,
5-
Oracle
5+
Oracle,
6+
MySql
67
}
78
public enum ConfigProperty {
89
ApplicationName,

internal/functions/Get-ConnectionManager.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ function Get-ConnectionManager {
1616
elseif ($Type -eq 'Oracle') {
1717
return [DbUp.Oracle.OracleConnectionManager]::new($ConnectionString)
1818
}
19+
elseif ($Type -eq 'MySql') {
20+
return [DbUp.MySql.MySqlConnectionManager]::new($ConnectionString)
21+
}
22+
else {
23+
Stop-PSFFunction -Message "Unknown type $Type" -EnableException $true
24+
return
25+
}
1926
}

internal/functions/Get-ConnectionString.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function Get-ConnectionString {
2929
else {
3030
$CSBuilder["Integrated Security"] = $true
3131
}
32-
if ($ConnectionType -eq 'SQLServer') {
32+
if ($Type -eq 'SQLServer') {
3333
$CSBuilder["Application Name"] = $Configuration.ApplicationName
3434
}
3535
return $CSBuilder.ToString()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function Get-DbUpBuilder {
2+
# Returns a DbUp builder with a proper connection object
3+
Param (
4+
[Parameter(Mandatory)]
5+
[object]$Connection,
6+
[string]$Schema,
7+
[DBOps.ConnectionType]$Type
8+
)
9+
$dbUp = [DbUp.DeployChanges]::To
10+
if ($Type -eq [DBOps.ConnectionType]::SqlServer) {
11+
if ($Schema) {
12+
$dbUp = [SqlServerExtensions]::SqlDatabase($dbUp, $dbUpConnection, $Schema)
13+
}
14+
else {
15+
$dbUp = [SqlServerExtensions]::SqlDatabase($dbUp, $dbUpConnection)
16+
}
17+
}
18+
elseif ($Type -eq [DBOps.ConnectionType]::Oracle) {
19+
if ($Schema) {
20+
$dbUp = [DbUp.Oracle.OracleExtensions]::OracleDatabase($dbUp, $dbUpConnection, $Schema)
21+
}
22+
else {
23+
$dbUp = [DbUp.Oracle.OracleExtensions]::OracleDatabase($dbUp, $dbUpConnection)
24+
}
25+
}
26+
elseif ($Type -eq [DBOps.ConnectionType]::MySql) {
27+
if ($Schema) {
28+
$dbUp = [MySqlExtensions]::MySqlDatabase($dbUp, $dbUpConnection, $Schema)
29+
}
30+
else {
31+
$dbUp = [MySqlExtensions]::MySqlDatabase($dbUp, $dbUpConnection)
32+
}
33+
}
34+
else {
35+
Stop-PSFFunction -Message "Unknown type $Type" -EnableException $true
36+
return
37+
}
38+
return $dbUp
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function Get-DbUpJournal {
2+
# Returns a DbUp builder with a proper connection object
3+
Param (
4+
[Parameter(Mandatory)]
5+
[scriptblock]$Connection,
6+
[scriptblock]$Log,
7+
[string]$Schema,
8+
[string]$SchemaVersionTable,
9+
[Parameter(Mandatory)]
10+
[DBOps.ConnectionType]$Type
11+
)
12+
if ($SchemaVersionTable) {
13+
# retrieve schema and table names
14+
$table = $SchemaVersionTable.Split('.')
15+
if ($table.Count -gt 2) {
16+
Stop-PSFFunction -EnableException $true -Message 'Incorrect table name - use the following syntax: schema.table'
17+
return
18+
}
19+
elseif ($table.Count -eq 2) {
20+
$tableName = $table[1]
21+
$schemaName = $table[0]
22+
}
23+
else {
24+
$tableName = $table[0]
25+
if ($Schema) {
26+
$schemaName = $Schema
27+
}
28+
}
29+
# define journal type based on target connection type
30+
if ($Type -eq [DBOps.ConnectionType]::SQLServer) {
31+
if (!$schemaName) {
32+
$schemaName = 'dbo'
33+
}
34+
$dbUpJournalType = [DbUp.SqlServer.SqlTableJournal]
35+
}
36+
elseif ($Type -eq [DBOps.ConnectionType]::Oracle) {
37+
$dbUpJournalType = [DbUp.Oracle.OracleTableJournal]
38+
}
39+
elseif ($Type -eq [DBOps.ConnectionType]::MySql) {
40+
$dbUpJournalType = [DbUp.MySql.MySqlTableJournal]
41+
}
42+
else {
43+
Stop-PSFFunction -Message "Unknown type $Type" -EnableException $true
44+
return
45+
}
46+
# return a journal object
47+
Write-PSFMessage -Level Verbose -Message "Creating journal object for $Type in $schemaName.$tableName"
48+
return $dbUpJournalType::new( $Connection, $Log, $schemaName, $tableName)
49+
}
50+
else {
51+
# return a null journal to disable journalling
52+
return [DbUp.Helpers.NullJournal]::new()
53+
}
54+
}

internal/functions/Get-SqlParser.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ function Get-SqlParser {
1010
elseif ($Type -eq 'Oracle') {
1111
return [DbUp.Oracle.OracleObjectParser]::new()
1212
}
13+
elseif ($Type -eq 'MySql') {
14+
return [DbUp.MySql.MySqlObjectParser]::new()
15+
}
1316
}

internal/json/dbops.dependencies.json

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
{
22
"Oracle": [
3-
{
4-
"Name": "Oracle.ManagedDataAccess",
5-
"Version": "12.2.1100",
6-
"Path": [
7-
"lib\\net40\\Oracle.ManagedDataAccess.dll"
8-
]
9-
}
3+
{
4+
"Name": "Oracle.ManagedDataAccess",
5+
"Version": "12.2.1100",
6+
"Path": [
7+
"lib\\net40\\Oracle.ManagedDataAccess.dll"
8+
],
9+
"PSEdition": [
10+
"Desktop"
11+
]
12+
},
13+
{
14+
"Name": "Oracle.ManagedDataAccess.Core",
15+
"Version": "2.12.0-beta2",
16+
"Path": [
17+
"lib\\netstandard2.0\\Oracle.ManagedDataAccess.Core.dll"
18+
],
19+
"PSEdition": [
20+
"Core"
21+
]
22+
}
1023
],
11-
"SQLServer": []
24+
"SQLServer": [],
25+
"MySQL": [
26+
{
27+
"Name": "MySql.Data",
28+
"Version": "6.9.5",
29+
"Path": [
30+
"lib\\net452\\MySql.Data.dll"
31+
],
32+
"PSEdition": [
33+
"Desktop"
34+
]
35+
},
36+
{
37+
"Name": "MySql.Data",
38+
"Version": "6.10.6",
39+
"Path": [
40+
"lib\\netstandard2.0\\MySql.Data.dll"
41+
],
42+
"PSEdition": [
43+
"Core"
44+
]
45+
}
46+
]
1247
}

internal/json/dbops.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Libraries": [
33
"bin\\dbup-core.dll",
44
"bin\\dbup-sqlserver.dll",
5-
"bin\\dbup-oracle.dll"
5+
"bin\\dbup-oracle.dll",
6+
"bin\\dbup-mysql.dll"
67
],
78
"Misc": [
89
"bin\\deploy.ps1",
@@ -50,6 +51,8 @@
5051
"internal\\functions\\Get-ConnectionString.ps1",
5152
"internal\\functions\\Get-SqlParser.ps1",
5253
"internal\\functions\\Initialize-ExternalLibrary.ps1",
54+
"internal\\functions\\Get-DbUpBuilder.ps1",
55+
"internal\\functions\\Get-DbUpJournal.ps1",
5356
"internal\\classes\\DBOpsHelper.class.ps1",
5457
"internal\\classes\\DBOps.class.ps1",
5558
"internal\\classes\\DBOpsDeploymentStatus.class.ps1",

0 commit comments

Comments
 (0)