From 86f48765e2b7a965d6040e306d235fddcaeafe32 Mon Sep 17 00:00:00 2001 From: Thierry Schork Date: Tue, 15 Apr 2025 10:26:03 +0200 Subject: [PATCH] added procs used in the dba database of HCI cloud instance --- .../drop_db_product_superset.sql | 47 +++++++ .../dba db on cloud/set_superset_source.sql | 71 ++++++++++ .../dba db on cloud/start_snapshot.sql | 124 ++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 HCI - BAG symmetricDS/dba db on cloud/drop_db_product_superset.sql create mode 100644 HCI - BAG symmetricDS/dba db on cloud/set_superset_source.sql create mode 100644 HCI - BAG symmetricDS/dba db on cloud/start_snapshot.sql diff --git a/HCI - BAG symmetricDS/dba db on cloud/drop_db_product_superset.sql b/HCI - BAG symmetricDS/dba db on cloud/drop_db_product_superset.sql new file mode 100644 index 0000000..c360f79 --- /dev/null +++ b/HCI - BAG symmetricDS/dba db on cloud/drop_db_product_superset.sql @@ -0,0 +1,47 @@ +USE [dba] +GO + +/****** Object: StoredProcedure [dbo].[drop_db_product_superset] Script Date: 15.04.2025 10:21:10 ******/ +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + +/* +drop_db_product_superset + +This procedure is called after the call to sl2007.dbo.usp_Daily_Batch_Update +It will drop the local database "product_superset", which is only needed for the update of sl2007. + +--Changelog (dd.mm.yyyy) +12.02.2025 TSC Creation +*/ +CREATE OR ALTER PROCEDURE [dbo].[drop_db_product_superset] +AS +BEGIN + DECLARE @q NVARCHAR(MAX)=''; + DECLARE @tplDrop NVARCHAR(MAX)=' +IF EXISTS(SELECT 1 FROM sys.databases WHERE name=''Product_Superset'') + DROP DATABASE [Product_Superset]; +'; + --terminate connections to product_superset + SELECT @q = @q + 'KILL '+CONVERT(varchar(5), c.session_id) + ';' + FROM sys.dm_exec_connections AS c + JOIN sys.dm_exec_sessions AS s + ON c.session_id = s.session_id + WHERE DB_NAME([s].[database_id]) = 'product_superset' + AND c.session_id <> @@SPID + ORDER BY c.connect_time ASC; + + EXEC sp_executesql @q,N''; + PRINT 'connections to product_superset killed'; + + --drop product_superset database + SELECT @q = @tplDrop; + EXEC sp_executesql @q,N''; + PRINT 'product_superset dropped'; +END +GO + + diff --git a/HCI - BAG symmetricDS/dba db on cloud/set_superset_source.sql b/HCI - BAG symmetricDS/dba db on cloud/set_superset_source.sql new file mode 100644 index 0000000..433233d --- /dev/null +++ b/HCI - BAG symmetricDS/dba db on cloud/set_superset_source.sql @@ -0,0 +1,71 @@ +USE [dba] +GO + +/****** Object: StoredProcedure [dbo].[set_superset_source] Script Date: 15.04.2025 10:22:33 ******/ +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + +/* +set_superset_source + +This procedure is set to be called from the on-prem HCI database server. +We need to sync product_superset from on-prem to cloud, but db snapshots cannot be replicated to cloud. +To get around this, a snapshot replication has been setup for both product_superset_00 and product_superset_01 and a call to this stored procedure will +create a backup of the superset that is in use on the on-prem database and restore it as [product_superset]. + +It triples the amount of data, but ensure that the sl2007 db uses the correct data. + +--Changelog (dd.mm.yyyy) +12.02.2025 TSC Creation +*/ +CREATE OR ALTER procedure [dbo].[set_superset_source] + @src_db_name varchar(100) +as +begin + set nocount on + set xact_abort on + + declare @q nvarchar(max)=''; + declare @url varchar(max) = 'https://stbagspezlisteprdsql.blob.core.windows.net/sqlbakfiles/superset.bak'; + DECLARE @tplBkp nvarchar(max) = ' +BACKUP DATABASE [@src_db_name@] +TO URL = ''@url@'' +WITH COPY_ONLY, COMPRESSION, FORMAT; +'; + + DECLARE @tplRest nvarchar(max)=' +RESTORE DATABASE [Product_Superset] +FROM URL = ''@url@''; +'; + --check that the given source db exists + IF NOT EXISTS( + SELECT 1 + FROM sys.databases d + WHERE d.name = @src_db_name + ) + begin + RAISERROR('The database specified as a source for the superset is not found on this server: %s', 16, 10, @src_db_name) + return + end + else + begin + + exec [dbo].[drop_db_product_superset] + + --backup the correct superset + set @q = REPLACE(REPLACE(@tplBkp,'@src_db_name@',@src_db_name),'@url@',@url); + exec sys.sp_executesql @q, N''; + print 'Backup of source '+@src_db_name+' done'; + + --restore to product_superset + set @q = REPLACE(@tplRest,'@url@',@url); + exec sys.sp_executesql @q, N''; + print 'restore of product_superset done'; + end + +end +GO + diff --git a/HCI - BAG symmetricDS/dba db on cloud/start_snapshot.sql b/HCI - BAG symmetricDS/dba db on cloud/start_snapshot.sql new file mode 100644 index 0000000..3563416 --- /dev/null +++ b/HCI - BAG symmetricDS/dba db on cloud/start_snapshot.sql @@ -0,0 +1,124 @@ +USE [dba] +GO + +/****** Object: StoredProcedure [dbo].[start_snapshot] Script Date: 15.04.2025 10:24:03 ******/ +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + + +/* +start_snapshot + +SP used to start the associated "repl-snapshot" job from a published database. + +Variable @published_db + If null, starts the snapshot job of all published dbs + If not null, must contain the name of a db that is published and will start the snapshot job of that db only + +15.04.2025 TSC Adapted to avoid trying to start several time the same job +*/ +CREATE OR ALTER PROCEDURE [dbo].[start_snapshot] + @published_db VARCHAR(500), + @debug BIT = 0 +AS +BEGIN + SET NOCOUNT ON; + SET XACT_ABORT ON; + + DECLARE @jobs TABLE (publisher_db sysname NOT NULL, + agent_name sysname NOT NULL, + job_name sysname NOT NULL, + start_execution_date DATETIME NULL, + stop_execution_date DATETIME NULL, + next_scheduled_run_date DATETIME NULL, + current_job_status VARCHAR(50) NOT NULL, + start_job_cmd AS 'exec msdb.dbo.sp_start_job @job_name=''' + job_name + ''';'); + DECLARE @cmd NVARCHAR(MAX) = N''; + + INSERT INTO @jobs ([publisher_db], + [agent_name], + [job_name], + [start_execution_date], + [stop_execution_date], + [next_scheduled_run_date], + [current_job_status]) + SELECT distinct + [a].[publisher_db], + [a].[name] AS [agent_name], + [s].[name] AS [job_name], + [ac].[start_execution_date], + [ac].[stop_execution_date], + [ac].[next_scheduled_run_date], + CASE + WHEN [ac].[start_execution_date] IS NOT NULL + AND [ac].[stop_execution_date] IS NULL THEN 'running' + WHEN [ac].[start_execution_date] IS NOT NULL + AND [ac].[stop_execution_date] IS NOT NULL THEN 'finished' + WHEN [ac].[start_execution_date] IS NULL + AND [ac].[stop_execution_date] IS NULL THEN 'never started' + ELSE 'unknown' END AS current_job_status + FROM [distribution].dbo.[MSsnapshot_agents] a + JOIN [msdb].[dbo].[sysjobs] s + ON [s].[job_id] = [a].[job_id] + LEFT JOIN ( SELECT TOP 1 --last execution, if ever there was one + [ja].[job_id], + [ja].[start_execution_date], + [ja].[stop_execution_date], + [ja].[next_scheduled_run_date] + FROM [msdb].[dbo].[sysjobactivity] ja + ORDER BY [start_execution_date] DESC + ) ac + ON [ac].[job_id] = [s].[job_id] + WHERE [a].[publisher_db] = COALESCE(@published_db, [a].[publisher_db]) + ORDER BY [a].[publisher_db]; + + IF NOT EXISTS (SELECT 1 FROM @jobs) + BEGIN + RAISERROR('Database %s is not existing or not published in a replication', 16, 5, @published_db); + RETURN; + END; + + if @debug=1 + begin + select * + from @jobs + end + + SELECT @cmd = @cmd + j.start_job_cmd + FROM @jobs j + WHERE [j].[current_job_status] <> 'running'; + + IF @debug = 1 + BEGIN + PRINT @cmd; + END; + ELSE + BEGIN + EXEC (@cmd); + + --give a couple seconds for the agent(s) to start + WAITFOR DELAY '00:00:15'; + + --do not exit this proc until the last snapshot job started has finished + WHILE EXISTS( + --return something while a snapshot job is running + SELECT 1 + FROM msdb.dbo.[sysjobs] j + JOIN msdb.dbo.[sysjobactivity] ja ON [j].[job_id] = [ja].[job_id] + JOIN [distribution].dbo.[MSsnapshot_agents] a ON a.[job_id] = j.[job_id] + WHERE a.[publisher_db] = COALESCE(@published_db, a.[publisher_db]) + AND [ja].[start_execution_date] IS NOT NULL + AND [ja].[stop_execution_date] IS NULL + ) + BEGIN + WAITFOR DELAY '00:00:05'; + RAISERROR('waiting for snapshot job(s) to finish',0,0) WITH NOWAIT; + END + END; +END; + +GO +