Skip to content

Commit f5e587f

Browse files
authored
Install-DbaMaintenanceSolution - don't drop tables if not readding (#9570)
1 parent 2fa45cd commit f5e587f

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

public/Install-DbaMaintenanceSolution.ps1

+37-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function Install-DbaMaintenanceSolution {
3131
.PARAMETER ReplaceExisting
3232
If this switch is enabled, objects already present in the target database will be dropped and recreated.
3333
34+
Note - The tables for `LogToTable` and `InstallParallel` will only be dropped if those options are also specified.
35+
3436
.PARAMETER LogToTable
3537
If this switch is enabled, the Maintenance Solution will be configured to log commands to a table.
3638
@@ -90,7 +92,7 @@ function Install-DbaMaintenanceSolution {
9092
https://ola.hallengren.com
9193
9294
.LINK
93-
https://dbatools.io/Install-DbaMaintenanceSolution
95+
https://dbatools.io/Install-DbaMaintenanceSolution
9496
9597
.EXAMPLE
9698
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -CleanupTime 72
@@ -119,17 +121,33 @@ function Install-DbaMaintenanceSolution {
119121
120122
Installs Maintenance Solution to myserver in database. Adds Agent Jobs, and if any currently exist, they'll be replaced.
121123
124+
Since the `LogToTable` switch is enabled, the CommandLog table will be dropped and recreated also.
125+
126+
If the tables relating to `InstallParallel` are present, they will not be dropped.
127+
122128
.EXAMPLE
123-
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -BackupLocation "Z:\SQLBackup" -CleanupTime 72 -ReplaceExisting
129+
PS C:\> $params = @{
130+
>> SqlInstance = 'RES14224'
131+
>> Database = 'DBA'
132+
>> InstallJobs = $true
133+
>> BackupLocation = 'Z:\SQLBackup'
134+
>> CleanupTime = 72
135+
>> ReplaceExisting = $true
136+
>> }
137+
PS C:\> Install-DbaMaintenanceSolution @params
124138
125139
This will drop and then recreate the Ola Hallengren's Solution objects
126140
The cleanup script will drop and recreate:
127-
- TABLE [dbo].[CommandLog]
128141
- STORED PROCEDURE [dbo].[CommandExecute]
129142
- STORED PROCEDURE [dbo].[DatabaseBackup]
130143
- STORED PROCEDURE [dbo].[DatabaseIntegrityCheck]
131144
- STORED PROCEDURE [dbo].[IndexOptimize]
132145
146+
The tables will not be dropped as the `LogToTable` and `InstallParallel` switches are not enabled.
147+
- [dbo].[CommandLog]
148+
- [dbo].[Queue]
149+
- [dbo].[QueueDatabase]
150+
133151
The following SQL Agent jobs will be deleted:
134152
- 'Output File Cleanup'
135153
- 'IndexOptimize - USER_DATABASES'
@@ -402,12 +420,6 @@ function Install-DbaMaintenanceSolution {
402420
$cleanupQuery = $null
403421
if ($ReplaceExisting) {
404422
[string]$cleanupQuery = $("
405-
IF OBJECT_ID('[dbo].[CommandLog]', 'U') IS NOT NULL
406-
DROP TABLE [dbo].[CommandLog];
407-
IF OBJECT_ID('[dbo].[QueueDatabase]', 'U') IS NOT NULL
408-
DROP TABLE [dbo].[QueueDatabase];
409-
IF OBJECT_ID('[dbo].[Queue]', 'U') IS NOT NULL
410-
DROP TABLE [dbo].[Queue];
411423
IF OBJECT_ID('[dbo].[CommandExecute]', 'P') IS NOT NULL
412424
DROP PROCEDURE [dbo].[CommandExecute];
413425
IF OBJECT_ID('[dbo].[DatabaseBackup]', 'P') IS NOT NULL
@@ -418,6 +430,22 @@ function Install-DbaMaintenanceSolution {
418430
DROP PROCEDURE [dbo].[IndexOptimize];
419431
")
420432

433+
if ($LogToTable) {
434+
$cleanupQuery += $("
435+
IF OBJECT_ID('[dbo].[CommandLog]', 'U') IS NOT NULL
436+
DROP TABLE [dbo].[CommandLog];
437+
")
438+
}
439+
440+
if ($InstallParallel) {
441+
$cleanupQuery += $("
442+
IF OBJECT_ID('[dbo].[QueueDatabase]', 'U') IS NOT NULL
443+
DROP TABLE [dbo].[QueueDatabase];
444+
IF OBJECT_ID('[dbo].[Queue]', 'U') IS NOT NULL
445+
DROP TABLE [dbo].[Queue];
446+
")
447+
}
448+
421449
if ($Pscmdlet.ShouldProcess($instance, "Dropping all objects created by Ola's Maintenance Solution")) {
422450
Write-ProgressHelper -ExcludePercent -Message "Dropping objects created by Ola's Maintenance Solution"
423451
$null = $db.Invoke($cleanupQuery)

0 commit comments

Comments
 (0)