USE [DM_DB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

declare @retention datetime2
declare @i int
declare @count int
declare @BEFOREDATE varchar(10)
declare @TS varchar(22)
declare @ROWS int

set @BEFOREDATE = '2021-09-01`';

set @retention = CAST(@BEFOREDATE as datetime2)

DECLARE @TEMP_DM_ENROLMENT_OP_MAPPING TABLE (
    ENROLMENT_ID INTEGER NOT NULL,
    OPERATION_ID INTEGER NOT NULL,
    [STATUS] VARCHAR(50) NULL,
    CREATED_TIMESTAMP BIGINT NOT NULL,
    UPDATED_TIMESTAMP BIGINT NOT NULL,
    PUSH_NOTIFICATION_STATUS VARCHAR(50) NULL,
    OPERATION_CODE VARCHAR(50) NOT NULL,
	INITIATED_BY VARCHAR(100) NULL,
	[TYPE] VARCHAR(20) NOT NULL,
	DEVICE_ID INTEGER NULL,
	DEVICE_TYPE VARCHAR(300) NOT NULL,
	DEVICE_IDENTIFICATION VARCHAR(300) NULL,
	TENANT_ID INTEGER NULL
);

SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s Moving retainable operation mappings and operation mapping records created after %s', 10, 1, @TS, @BEFOREDATE) WITH NOWAIT;
INSERT INTO @TEMP_DM_ENROLMENT_OP_MAPPING
           ([ENROLMENT_ID],
            [OPERATION_ID],
            [STATUS],
            [CREATED_TIMESTAMP],
            [UPDATED_TIMESTAMP],
            [PUSH_NOTIFICATION_STATUS],
            [OPERATION_CODE],
            [INITIATED_BY],
            [TYPE],
            [DEVICE_ID],
            [DEVICE_TYPE],
            [DEVICE_IDENTIFICATION],
            [TENANT_ID])
SELECT [ENROLMENT_ID], [OPERATION_ID], [STATUS], [CREATED_TIMESTAMP], [UPDATED_TIMESTAMP], [PUSH_NOTIFICATION_STATUS], 
        [OPERATION_CODE], [INITIATED_BY], [TYPE], [DEVICE_ID], [DEVICE_TYPE], [DEVICE_IDENTIFICATION], [TENANT_ID] FROM [dbo].[DM_ENROLMENT_OP_MAPPING] 
WHERE [STATUS] = 'PENDING' OR [STATUS] = 'IN_PROGRESS' OR [UPDATED_TIMESTAMP] >= (SELECT DATEDIFF_BIG(millisecond, '1970-01-01 00:00:00', @retention));

SET @ROWS = @@ROWCOUNT
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s Added %d records to variable table @TEMP_DM_ENROLMENT_OP_MAPPING', 10, 1, @TS, @ROWS) WITH NOWAIT;

RAISERROR (N'Removing FK constraints...', 10, 1) WITH NOWAIT;
ALTER TABLE [dbo].[DM_NOTIFICATION] DROP CONSTRAINT [FL_DM_NOTIFICATION];
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE_LARGE] DROP CONSTRAINT [FK_DM_DEVICE_OPERATION_RESP_LARGE_OPERATION];
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE_LARGE] DROP CONSTRAINT [FK_DM_EN_OP_MAP_RESPONSE_LARGE];
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE] DROP CONSTRAINT [FK_DM_DEVICE_OPERATION_RESP_ENROLMENT];
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE] DROP CONSTRAINT [FK_DM_DEVICE_OPERATION_RESP_OPERATION];
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE] DROP CONSTRAINT [FK_DM_EN_OP_MAP_RESPONSE];
ALTER TABLE [dbo].[DM_ENROLMENT_OP_MAPPING] DROP CONSTRAINT [FK_DM_DEVICE_OPERATION_MAPPING_DEVICE];
ALTER TABLE [dbo].[DM_ENROLMENT_OP_MAPPING] DROP CONSTRAINT [FK_DM_DEVICE_OPERATION_MAPPING_OPERATION];
RAISERROR (N'Removing FK constraints - COMPLETED', 10, 1) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;

TRUNCATE TABLE [dbo].[DM_NOTIFICATION];
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s [DM_NOTIFICATION] table truncated', 10, 1, @TS) WITH NOWAIT;

TRUNCATE TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE_LARGE];
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s [DM_DEVICE_OPERATION_RESPONSE_LARGE] table truncated', 10, 1, @TS) WITH NOWAIT;

TRUNCATE TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE];
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s [DM_DEVICE_OPERATION_RESPONSE] table truncated', 10, 1, @TS) WITH NOWAIT;

TRUNCATE TABLE [dbo].[DM_ENROLMENT_OP_MAPPING];
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s [DM_ENROLMENT_OP_MAPPING] table truncated', 10, 1, @TS) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;

RAISERROR (N'Adding back the FK constraints', 10, 1) WITH NOWAIT;
ALTER TABLE [dbo].[DM_NOTIFICATION] WITH CHECK ADD CONSTRAINT [FL_DM_NOTIFICATION] FOREIGN KEY([DEVICE_ID]) REFERENCES [dbo].[DM_DEVICE] ([ID]);
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE_LARGE] WITH CHECK ADD CONSTRAINT [FK_DM_EN_OP_MAP_RESPONSE_LARGE] FOREIGN KEY([EN_OP_MAP_ID]) REFERENCES [dbo].[DM_ENROLMENT_OP_MAPPING] ([ID]);
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE] WITH CHECK ADD CONSTRAINT [FK_DM_DEVICE_OPERATION_RESP_ENROLMENT] FOREIGN KEY([ENROLMENT_ID]) REFERENCES [dbo].[DM_ENROLMENT] ([ID]);
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE] WITH CHECK ADD CONSTRAINT [FK_DM_EN_OP_MAP_RESPONSE] FOREIGN KEY([EN_OP_MAP_ID]) REFERENCES [dbo].[DM_ENROLMENT_OP_MAPPING] ([ID]);
ALTER TABLE [dbo].[DM_ENROLMENT_OP_MAPPING] WITH CHECK ADD CONSTRAINT [FK_DM_DEVICE_OPERATION_MAPPING_DEVICE] FOREIGN KEY([ENROLMENT_ID]) REFERENCES [dbo].[DM_ENROLMENT] ([ID]);
RAISERROR (N'Adding FK constraints - COMPLETED', 10, 1) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;

INSERT INTO [dbo].[DM_ENROLMENT_OP_MAPPING]
           ([ENROLMENT_ID],
            [OPERATION_ID],
            [STATUS],
            [PUSH_NOTIFICATION_STATUS],
            [CREATED_TIMESTAMP],
            [UPDATED_TIMESTAMP],
            [OPERATION_CODE],
            [INITIATED_BY],
            [TYPE],
            [DEVICE_ID],
            [DEVICE_TYPE],
            [DEVICE_IDENTIFICATION],
            [TENANT_ID])
SELECT [ENROLMENT_ID], [OPERATION_ID], [STATUS], [PUSH_NOTIFICATION_STATUS], [CREATED_TIMESTAMP], [UPDATED_TIMESTAMP], 
        [OPERATION_CODE], [INITIATED_BY], [TYPE], [DEVICE_ID], [DEVICE_TYPE], [DEVICE_IDENTIFICATION], [TENANT_ID] FROM @TEMP_DM_ENROLMENT_OP_MAPPING;

SET @ROWS = @@ROWCOUNT
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s Added %d records to [DM_ENROLMENT_OP_MAPPING] back from variable table @TEMP_DM_ENROLMENT_OP_MAPPING', 10, 1, @TS, @ROWS) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;


SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s Adding operation ids which needs to preserve in to [TEMP_DATA]', 10, 1, @TS) WITH NOWAIT;
DECLARE @TEMP_DATA TABLE ( 
   [OPERATION_ID] [int] NULL 
);

INSERT INTO @TEMP_DATA
           ([OPERATION_ID])
    SELECT DISTINCT OPERATION_ID FROM @TEMP_DM_ENROLMENT_OP_MAPPING;
	 
SET @ROWS = @@ROWCOUNT
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s Added %d records of Operations which needs to preserve for [DM_ENROLMENT_OP_MAPPING] in to [TEMP_DATA]', 10, 1, @TS, @ROWS) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;


DELETE dop FROM [dbo].[DM_OPERATION] AS dop
LEFT JOIN @TEMP_DATA AS td 
ON dop.ID = td.OPERATION_ID
WHERE td.OPERATION_ID IS NULL;

SET @ROWS = @@ROWCOUNT
SET @TS = CONVERT(varchar, SYSDATETIME(), 121);
RAISERROR (N'%s Removed %d of records from [DM_OPERATION]', 10, 1, @TS, @ROWS) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;
GO

RAISERROR (N'Adding back the FK constraints for DM_OPERATION', 10, 1) WITH NOWAIT;
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE_LARGE] WITH CHECK ADD CONSTRAINT [FK_DM_DEVICE_OPERATION_RESP_LARGE_OPERATION] FOREIGN KEY([OPERATION_ID]) REFERENCES [dbo].[DM_OPERATION] ([ID]);
ALTER TABLE [dbo].[DM_DEVICE_OPERATION_RESPONSE] WITH CHECK ADD CONSTRAINT [FK_DM_DEVICE_OPERATION_RESP_OPERATION] FOREIGN KEY([OPERATION_ID]) REFERENCES [dbo].[DM_OPERATION] ([ID]);
ALTER TABLE [dbo].[DM_ENROLMENT_OP_MAPPING] WITH CHECK ADD CONSTRAINT [FK_DM_DEVICE_OPERATION_MAPPING_OPERATION] FOREIGN KEY([OPERATION_ID]) REFERENCES [dbo].[DM_OPERATION] ([ID]);
RAISERROR (N'Adding FK constraints for DM_OPERATION - COMPLETED', 10, 1) WITH NOWAIT;
RAISERROR (N'-----------------------------', 10, 1) WITH NOWAIT;
GO

RAISERROR (N'Start reindexing', 10, 1) WITH NOWAIT;
ALTER INDEX ALL ON [dbo].[DM_OPERATION]  
REORGANIZE;
PRINT 'Re-indexed [DM_OPERATION]';   
GO