Skip to content

Commit c61464a

Browse files
authored
Merge pull request #51 from sqlcollaborative/Invoke-DBOQuery
New command: Invoke-DBOQuery
2 parents 8426325 + 3295ee2 commit c61464a

30 files changed

+856
-875
lines changed

dbops.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
'New-DBOConfig',
8989
'Export-DBOConfig',
9090
'ConvertTo-EncryptedString',
91-
'ConvertFrom-EncryptedString'
91+
'ConvertFrom-EncryptedString',
92+
'Invoke-DBOQuery'
9293
)
9394

9495
# Cmdlets to export from this module

functions/Install-DBOPackage.ps1

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,85 @@
22
<#
33
.SYNOPSIS
44
Deploys an existing DBOps package
5-
5+
66
.DESCRIPTION
77
Deploys an existing DBOps package with optional parameters.
88
Uses a table specified in SchemaVersionTable parameter to determine scripts to run.
99
Will deploy all the builds from the package that previously have not been deployed.
10-
10+
1111
.PARAMETER Path
1212
Path to the existing DBOpsPackage.
1313
Aliases: Name, FileName, Package
1414
1515
.PARAMETER InputObject
1616
Pipeline implementation of Path. Can also contain a DBOpsPackage object.
17-
17+
1818
.PARAMETER SqlInstance
1919
Database server to connect to. SQL Server only for now.
2020
Aliases: Server, SQLServer, DBServer, Instance
21-
21+
2222
.PARAMETER Database
2323
Name of the database to execute the scripts in. Optional - will use default database if not specified.
24-
24+
2525
.PARAMETER DeploymentMethod
2626
Choose one of the following deployment methods:
2727
- SingleTransaction: wrap all the deployment scripts into a single transaction and rollback whole deployment on error
2828
- TransactionPerScript: wrap each script into a separate transaction; rollback single script deployment in case of error
2929
- NoTransaction: deploy as is
30-
30+
3131
Default: NoTransaction
32-
32+
3333
.PARAMETER ConnectionTimeout
3434
Database server connection timeout in seconds. Only affects connection attempts. Does not affect execution timeout.
3535
If 0, will wait for connection until the end of times.
36-
36+
3737
Default: 30
38-
38+
3939
.PARAMETER ExecutionTimeout
4040
Script execution timeout. The script will be aborted if the execution takes more than specified number of seconds.
4141
If 0, the script is allowed to run until the end of times.
4242
4343
Default: 0
44-
44+
4545
.PARAMETER Encrypt
4646
Enables connection encryption.
47-
47+
4848
.PARAMETER Credential
4949
PSCredential object with username and password to login to the database server.
50-
50+
5151
.PARAMETER UserName
5252
An alternative to -Credential - specify username explicitly
53-
53+
5454
.PARAMETER Password
5555
An alternative to -Credential - specify password explicitly
56-
56+
5757
.PARAMETER SchemaVersionTable
5858
A table that will hold the history of script execution. This table is used to choose what scripts are going to be
5959
run during the deployment, preventing the scripts from being execured twice.
6060
If set to $null, the deployment will not be tracked in the database. That will also mean that all the scripts
6161
and all the builds from the package are going to be deployed regardless of any previous deployment history.
6262
6363
Default: SchemaVersions
64-
64+
6565
.PARAMETER Silent
6666
Will supress all output from the command.
67-
67+
6868
.PARAMETER Variables
6969
Hashtable with variables that can be used inside the scripts and deployment parameters.
7070
Proper format of the variable tokens is #{MyVariableName}
7171
Can also be provided as a part of Configuration hashtable: -Configuration @{ Variables = @{ Var1 = ...; Var2 = ...}}
7272
Will augment and/or overwrite Variables defined inside the package.
73-
73+
7474
.PARAMETER OutputFile
7575
Log output into specified file.
76-
76+
7777
.PARAMETER Append
7878
Append output to the -OutputFile instead of overwriting it.
7979
8080
.PARAMETER ConnectionString
8181
Custom connection string that will override other connection parameters.
8282
IMPORTANT: Will also ignore user/password/credential parameters, so make sure to include proper authentication credentials into the string.
83-
83+
8484
.PARAMETER Configuration
8585
A custom configuration that will be used during a deployment, overriding existing parameters inside the package.
8686
Can be a Hashtable, a DBOpsConfig object, or a path to a json file.
@@ -90,14 +90,14 @@
9090
9191
.PARAMETER Build
9292
Only deploy certain builds from the package.
93-
93+
9494
.PARAMETER CreateDatabase
9595
Will create an empty database if missing on supported RDMBS
9696
9797
.PARAMETER ConnectionType
9898
Defines the driver to use when connecting to the database server.
9999
Available options: SqlServer (default), Oracle
100-
100+
101101
.PARAMETER Confirm
102102
Prompts to confirm certain actions
103103
@@ -107,11 +107,11 @@
107107
.EXAMPLE
108108
# Installs package with predefined configuration inside the package
109109
Install-DBOPackage .\MyPackage.zip
110-
110+
111111
.EXAMPLE
112112
# Installs package using specific connection parameters
113113
.\MyPackage.zip | Install-DBOPackage -SqlInstance 'myserver\instance1' -Database 'MyDb' -ExecutionTimeout 3600
114-
114+
115115
.EXAMPLE
116116
# Installs package using custom logging parameters and schema tracking table
117117
.\MyPackage.zip | Install-DBOPackage -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
@@ -170,7 +170,7 @@
170170
[Alias('Type', 'ServerType')]
171171
[string]$ConnectionType = 'SQLServer'
172172
)
173-
173+
174174
begin {
175175
}
176176
process {
@@ -187,29 +187,31 @@
187187
#Merging the custom configuration provided
188188
$config = $config | Get-DBOConfig -Configuration $Configuration
189189

190-
#Merge custom parameters into the configuration, excluding variables
190+
#Merge custom parameters into a configuration
191+
$newConfig = @{}
191192
foreach ($key in ($PSBoundParameters.Keys)) {
192-
if ($key -in [DBOpsConfig]::EnumProperties() -and $key -ne 'Variables') {
193+
if ($key -in [DBOpsConfig]::EnumProperties()) {
193194
Write-PSFMessage -Level Debug -Message "Overriding parameter $key with $($PSBoundParameters[$key])"
194-
$config.SetValue($key, $PSBoundParameters[$key])
195+
$newConfig.$key = $PSBoundParameters[$key]
195196
}
196197
}
197-
198+
$config.Merge($newConfig)
199+
198200
#Prepare deployment function call parameters
199201
$params = @{
200-
InputObject = $package
202+
InputObject = $package
201203
Configuration = $config
202204
}
203205
foreach ($key in ($PSBoundParameters.Keys)) {
204206
#If any custom properties were specified
205-
if ($key -in @('OutputFile', 'Append', 'Variables', 'ConnectionType', 'Build')) {
207+
if ($key -in @('OutputFile', 'Append', 'ConnectionType', 'Build')) {
206208
$params += @{ $key = $PSBoundParameters[$key] }
207209
}
208210
}
209211
Write-PSFMessage -Level Verbose -Message "Preparing to start the deployment of package $($package.FileName)"
210212
Invoke-DBODeployment @params
211213
}
212214
end {
213-
215+
214216
}
215217
}

functions/Install-DBOSqlScript.ps1

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,99 +2,99 @@
22
<#
33
.SYNOPSIS
44
Deploys genering SQL scripts to a target database
5-
5+
66
.DESCRIPTION
77
Deploys genering SQL scripts with optional parameters.
88
Uses a table specified in SchemaVersionTable parameter to determine scripts to run.
99
Will deploy all the builds from the package that previously have not been deployed.
10-
10+
1111
.PARAMETER Path
1212
Path to one or more SQL sript files or folders, containing script files.
1313
Aliases: Name, FileName, ScriptPath
1414
1515
.PARAMETER InputObject
1616
Pipeline implementation of Path. Accepts output from Get-Item and Get-ChildItem, as well as simple strings and arrays.
17-
17+
1818
.PARAMETER SqlInstance
1919
Database server to connect to. SQL Server only for now.
2020
Aliases: Server, SQLServer, DBServer, Instance
21-
21+
2222
.PARAMETER Database
2323
Name of the database to execute the scripts in. Optional - will use default database if not specified.
24-
24+
2525
.PARAMETER DeploymentMethod
2626
Choose one of the following deployment methods:
2727
- SingleTransaction: wrap all the deployment scripts into a single transaction and rollback whole deployment on error
2828
- TransactionPerScript: wrap each script into a separate transaction; rollback single script deployment in case of error
2929
- NoTransaction: deploy as is
30-
30+
3131
Default: NoTransaction
32-
32+
3333
.PARAMETER ConnectionTimeout
3434
Database server connection timeout in seconds. Only affects connection attempts. Does not affect execution timeout.
3535
If 0, will wait for connection until the end of times.
36-
36+
3737
Default: 30
38-
38+
3939
.PARAMETER ExecutionTimeout
4040
Script execution timeout. The script will be aborted if the execution takes more than specified number of seconds.
4141
If 0, the script is allowed to run until the end of times.
4242
4343
Default: 0
44-
44+
4545
.PARAMETER Encrypt
4646
Enables connection encryption.
47-
47+
4848
.PARAMETER Credential
4949
PSCredential object with username and password to login to the database server.
50-
50+
5151
.PARAMETER UserName
5252
An alternative to -Credential - specify username explicitly
53-
53+
5454
.PARAMETER Password
5555
An alternative to -Credential - specify password explicitly
56-
56+
5757
.PARAMETER SchemaVersionTable
5858
A table that will hold the history of script execution. This table is used to choose what scripts are going to be
5959
run during the deployment, preventing the scripts from being execured twice.
6060
If set to $null, the deployment will not be tracked in the database. That will also mean that all the scripts
6161
and all the builds from the package are going to be deployed regardless of any previous deployment history.
6262
6363
Default: SchemaVersions
64-
64+
6565
.PARAMETER Silent
6666
Will supress all output from the command.
67-
67+
6868
.PARAMETER Variables
6969
Hashtable with variables that can be used inside the scripts and deployment parameters.
7070
Proper format of the variable tokens is #{MyVariableName}
7171
Can also be provided as a part of Configuration hashtable: -Configuration @{ Variables = @{ Var1 = ...; Var2 = ...}}
7272
Will augment and/or overwrite Variables defined inside the package.
73-
73+
7474
.PARAMETER OutputFile
7575
Log output into specified file.
76-
76+
7777
.PARAMETER Append
7878
Append output to the -OutputFile instead of overwriting it.
7979
8080
.PARAMETER ConnectionString
8181
Custom connection string that will override other connection parameters.
8282
IMPORTANT: Will also ignore user/password/credential parameters, so make sure to include proper authentication credentials into the string.
83-
83+
8484
.PARAMETER Configuration
8585
A custom configuration that will be used during a deployment, overriding existing parameters inside the package.
8686
Can be a Hashtable, a DBOpsConfig object, or a path to a json file.
8787
8888
.PARAMETER Schema
8989
Deploy into a specific schema (if supported by RDBMS)
90-
90+
9191
.PARAMETER CreateDatabase
9292
Will create an empty database if missing on supported RDMBS
93-
93+
9494
.PARAMETER ConnectionType
9595
Defines the driver to use when connecting to the database server.
9696
Available options: SqlServer (default), Oracle
97-
97+
9898
.PARAMETER Confirm
9999
Prompts to confirm certain actions
100100
@@ -104,11 +104,11 @@
104104
.EXAMPLE
105105
# Deploys all SQL scripts from the folder .\SqlCode to the target database
106106
Install-DBOSqlScript .\SqlCode\*.sql -SqlInstance 'myserver\instance1' -Database 'MyDb'
107-
107+
108108
.EXAMPLE
109109
# Deploys script file using specific connection parameters
110110
Get-Item .\SqlCode\Script1.sql | Install-DBOSqlScript -SqlInstance 'Srv1' -Database 'MyDb' -ExecutionTimeout 3600
111-
111+
112112
.EXAMPLE
113113
# Deploys all the scripts from the .\SqlCode folder using custom logging parameters and schema tracking table
114114
Get-ChildItem .\SqlCode | Install-DBOSqlScript -SqlInstance 'Srv1' -Database 'MyDb' -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
@@ -166,7 +166,7 @@
166166
[Alias('Type', 'ServerType')]
167167
[string]$ConnectionType = 'SQLServer'
168168
)
169-
169+
170170
begin {
171171
$scripts = @()
172172
}
@@ -188,22 +188,24 @@
188188
#Getting new config with provided defaults
189189
$config = New-DBOConfig -Configuration $Configuration
190190

191-
#Convert custom parameters into a package configuration, excluding variables
191+
#Merge custom parameters into a configuration
192+
$newConfig = @{}
192193
foreach ($key in ($PSBoundParameters.Keys)) {
193-
if ($key -in [DBOpsConfig]::EnumProperties() -and $key -ne 'Variables') {
194+
if ($key -in [DBOpsConfig]::EnumProperties()) {
194195
Write-PSFMessage -Level Debug -Message "Overriding parameter $key with $($PSBoundParameters[$key])"
195-
$config.SetValue($key, $PSBoundParameters[$key])
196+
$newConfig.$key = $PSBoundParameters[$key]
196197
}
197198
}
198-
199+
$config.Merge($newConfig)
200+
199201
#Prepare deployment function call parameters
200202
$params = @{
201203
ScriptPath = $scripts
202204
Configuration = $config
203205
}
204206
foreach ($key in ($PSBoundParameters.Keys)) {
205207
#If any custom properties were specified
206-
if ($key -in @('OutputFile', 'Append', 'Variables', 'ConnectionType')) {
208+
if ($key -in @('OutputFile', 'Append', 'ConnectionType')) {
207209
$params += @{ $key = $PSBoundParameters[$key] }
208210
}
209211
}

0 commit comments

Comments
 (0)