From f5e587f2f9909e6717e79367d46f3db01dbbc148 Mon Sep 17 00:00:00 2001 From: Jess Pomfret Date: Wed, 8 Jan 2025 12:59:05 +0100 Subject: [PATCH] Install-DbaMaintenanceSolution - don't drop tables if not readding (#9570) --- public/Install-DbaMaintenanceSolution.ps1 | 46 ++++++++++++++++++----- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/public/Install-DbaMaintenanceSolution.ps1 b/public/Install-DbaMaintenanceSolution.ps1 index 0a2c1b1acc..b77d290a1d 100644 --- a/public/Install-DbaMaintenanceSolution.ps1 +++ b/public/Install-DbaMaintenanceSolution.ps1 @@ -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. @@ -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 @@ -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' @@ -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 @@ -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)