Skip to content

Commit

Permalink
Install-DbaMaintenanceSolution - don't drop tables if not readding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpomfret authored Jan 8, 2025
1 parent 2fa45cd commit f5e587f
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions public/Install-DbaMaintenanceSolution.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function Install-DbaMaintenanceSolution {
.PARAMETER ReplaceExisting
If this switch is enabled, objects already present in the target database will be dropped and recreated.
Note - The tables for `LogToTable` and `InstallParallel` will only be dropped if those options are also specified.
.PARAMETER LogToTable
If this switch is enabled, the Maintenance Solution will be configured to log commands to a table.
Expand Down Expand Up @@ -90,7 +92,7 @@ function Install-DbaMaintenanceSolution {
https://ola.hallengren.com
.LINK
https://dbatools.io/Install-DbaMaintenanceSolution
https://dbatools.io/Install-DbaMaintenanceSolution
.EXAMPLE
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -CleanupTime 72
Expand Down Expand Up @@ -119,17 +121,33 @@ function Install-DbaMaintenanceSolution {
Installs Maintenance Solution to myserver in database. Adds Agent Jobs, and if any currently exist, they'll be replaced.
Since the `LogToTable` switch is enabled, the CommandLog table will be dropped and recreated also.
If the tables relating to `InstallParallel` are present, they will not be dropped.
.EXAMPLE
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -BackupLocation "Z:\SQLBackup" -CleanupTime 72 -ReplaceExisting
PS C:\> $params = @{
>> SqlInstance = 'RES14224'
>> Database = 'DBA'
>> InstallJobs = $true
>> BackupLocation = 'Z:\SQLBackup'
>> CleanupTime = 72
>> ReplaceExisting = $true
>> }
PS C:\> Install-DbaMaintenanceSolution @params
This will drop and then recreate the Ola Hallengren's Solution objects
The cleanup script will drop and recreate:
- TABLE [dbo].[CommandLog]
- STORED PROCEDURE [dbo].[CommandExecute]
- STORED PROCEDURE [dbo].[DatabaseBackup]
- STORED PROCEDURE [dbo].[DatabaseIntegrityCheck]
- STORED PROCEDURE [dbo].[IndexOptimize]
The tables will not be dropped as the `LogToTable` and `InstallParallel` switches are not enabled.
- [dbo].[CommandLog]
- [dbo].[Queue]
- [dbo].[QueueDatabase]
The following SQL Agent jobs will be deleted:
- 'Output File Cleanup'
- 'IndexOptimize - USER_DATABASES'
Expand Down Expand Up @@ -402,12 +420,6 @@ function Install-DbaMaintenanceSolution {
$cleanupQuery = $null
if ($ReplaceExisting) {
[string]$cleanupQuery = $("
IF OBJECT_ID('[dbo].[CommandLog]', 'U') IS NOT NULL
DROP TABLE [dbo].[CommandLog];
IF OBJECT_ID('[dbo].[QueueDatabase]', 'U') IS NOT NULL
DROP TABLE [dbo].[QueueDatabase];
IF OBJECT_ID('[dbo].[Queue]', 'U') IS NOT NULL
DROP TABLE [dbo].[Queue];
IF OBJECT_ID('[dbo].[CommandExecute]', 'P') IS NOT NULL
DROP PROCEDURE [dbo].[CommandExecute];
IF OBJECT_ID('[dbo].[DatabaseBackup]', 'P') IS NOT NULL
Expand All @@ -418,6 +430,22 @@ function Install-DbaMaintenanceSolution {
DROP PROCEDURE [dbo].[IndexOptimize];
")

if ($LogToTable) {
$cleanupQuery += $("
IF OBJECT_ID('[dbo].[CommandLog]', 'U') IS NOT NULL
DROP TABLE [dbo].[CommandLog];
")
}

if ($InstallParallel) {
$cleanupQuery += $("
IF OBJECT_ID('[dbo].[QueueDatabase]', 'U') IS NOT NULL
DROP TABLE [dbo].[QueueDatabase];
IF OBJECT_ID('[dbo].[Queue]', 'U') IS NOT NULL
DROP TABLE [dbo].[Queue];
")
}

if ($Pscmdlet.ShouldProcess($instance, "Dropping all objects created by Ola's Maintenance Solution")) {
Write-ProgressHelper -ExcludePercent -Message "Dropping objects created by Ola's Maintenance Solution"
$null = $db.Invoke($cleanupQuery)
Expand Down

0 comments on commit f5e587f

Please sign in to comment.