USE [msdb]
GO
IF EXISTS(
         SELECT 2
         FROM dbo.[sysjobs] j
         WHERE j.[name] = N'Logs cleanup'
         )
        BEGIN
            exec dbo.sp_delete_job @job_name=N'Logs cleanup', @delete_unused_schedule=1;
        END
GO

/****** Object:  Job [Logs cleanup]    Script Date: 03.09.2025 11:22:46 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [Database Maintenance]    Script Date: 03.09.2025 11:22:46 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
        BEGIN
            EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
            IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

        END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Logs cleanup',
                    @enabled=1,
                    @notify_level_eventlog=0,
                    @notify_level_email=0,
                    @notify_level_netsend=0,
                    @notify_level_page=0,
                    @delete_level=0,
                    @description=N'https://galenica.atlassian.net/browse/MDDOC-878

Implement calls to the cleanup procedures in documedisLogs',
                    @category_name=N'Database Maintenance',
                    @owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [empty step]    Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'empty step',
                   @step_id=1,
                   @cmdexec_success_code=0,
                   @on_success_action=3,
                   @on_success_step_id=0,
                   @on_fail_action=2,
                   @on_fail_step_id=0,
                   @retry_attempts=0,
                   @retry_interval=0,
                   @os_run_priority=0, @subsystem=N'TSQL',
                   @command=N'--nothing',
                   @database_name=N'master',
                   @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [call purge_documedisSecurityLogs]    Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'call DocumedisLogs.purge_documedisSecurityLogs',
                   @step_id=2,
                   @cmdexec_success_code=0,
                   @on_success_action=3,
                   @on_success_step_id=0,
                   @on_fail_action=2,
                   @on_fail_step_id=0,
                   @retry_attempts=0,
                   @retry_interval=0,
                   @os_run_priority=0, @subsystem=N'TSQL',
                   @command=N'exec dbo.purge_documedisSecurityLogs',
                   @database_name=N'DocumedisLogs',
                   @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [call purge_Documedis_VaccinationCheck_LogData]    Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'call DocumedisLogs.purge_Documedis_VaccinationCheck_LogData',
                   @step_id=3,
                   @cmdexec_success_code=0,
                   @on_success_action=3,
                   @on_success_step_id=0,
                   @on_fail_action=2,
                   @on_fail_step_id=0,
                   @retry_attempts=0,
                   @retry_interval=0,
                   @os_run_priority=0, @subsystem=N'TSQL',
                   @command=N'exec dbo.purge_Documedis_VaccinationCheck_LogData',
                   @database_name=N'DocumedisLogs',
                   @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [call purge_Documedis_ClinicalDecisionSupport_LogData]    Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'call DocumedisLogs.purge_Documedis_ClinicalDecisionSupport_LogData',
                   @step_id=4,
                   @cmdexec_success_code=0,
                   @on_success_action=3,
                   @on_success_step_id=0,
                   @on_fail_action=2,
                   @on_fail_step_id=0,
                   @retry_attempts=0,
                   @retry_interval=0,
                   @os_run_priority=0, @subsystem=N'TSQL',
                   @command=N'exec dbo.purge_Documedis_ClinicalDecisionSupport_LogData',
                   @database_name=N'DocumedisLogs',
                   @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [empty closing step]    Script Date: 03.09.2025 11:22:46 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'empty closing step',
                   @step_id=5,
                   @cmdexec_success_code=0,
                   @on_success_action=1,
                   @on_success_step_id=0,
                   @on_fail_action=2,
                   @on_fail_step_id=0,
                   @retry_attempts=0,
                   @retry_interval=0,
                   @os_run_priority=0, @subsystem=N'TSQL',
                   @command=N'--nothing',
                   @database_name=N'master',
                   @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

