<commit_before>﻿
-----------------------------------------------------------------------------------------------
-------------------   Add identity column DataChangeEvent.ReceivedOrder   ---------------------
-----------------------------------------------------------------------------------------------

-- Create temp table if not already there
IF NOT EXISTS (SELECT * FROM sys.tables where name='DataChangeEvent_TMP')
	CREATE TABLE DataChangeEvent_TMP (DataChangeEventId uniqueidentifier, PersonUuid uniqueidentifier, PersonRegistrationId uniqueidentifier, DueDate datetime, ReceivedDate DateTime)
GO

-- Copy existing data changes to tampe table
INSERT INTO DataChangeEvent_TMP (DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate) 
SELECT DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate 
FROM DataChangeEvent

GO

-- Delete from original table
DELETE DataChangeEvent
GO

-- Add identity column ReceivedOrder if not already there
IF NOT EXISTS (SELECT * FROM sys.columns c INNER JOIN sys.tables t ON c.object_id = t.object_id where t.name='DataChangeEvent' AND c.name = 'ReceivedOrder')
	ALTER TABLE dbo.DataChangeEvent 
	ADD	ReceivedOrder int NOT NULL IDENTITY (1, 1)
GO

-- Copy back into original table
INSERT INTO DataChangeEvent (DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate) 
SELECT DISTINCT DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate 
FROM DataChangeEvent_TMP
ORDER BY ReceivedDate

GO

-- Now drop temp table
IF EXISTS (SELECT * FROM sys.tables where name='DataChangeEvent_TMP')
	DROP TABLE DataChangeEvent_TMP
GO


-----------------------------------------------------------------------------------------------
-------------------------------   Drop legacy stored procedure  -------------------------------
-----------------------------------------------------------------------------------------------

IF EXISTS (SELECT * FROM sys.procedures WHERE name='InsertChangeNotificationData')
	DROP PROCEDURE InsertChangeNotificationData
GO

-----------------------------------------------------------------------------------------------
------------------------------   New column Subscription.Ready  -------------------------------
-----------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'Ready' AND object_id = OBJECT_ID('Subscription'))
	ALTER TABLE Subscription ADD Ready BIT NOT NULL DEFAULT 0
GO

UPDATE Subscription SET Ready = 1 WHERE Ready IS NULL AND LastCheckedUUID IS NULL
GO

-----------------------------------------------------------------------------------------------
------------------------------   New column EventNotification.AttemptCount  -------------------------------
-----------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'AttemptCount' AND object_id = OBJECT_ID('EventNotification'))
	ALTER TABLE EventNotification ADD AttemptCount INT NOT NULL DEFAULT 0
GO

-----------------------------------------------------------------------------------------------
--------------------   Drop default for  BirthdateSubscription.SubscriptionId -----------------
-----------------------------------------------------------------------------------------------

IF EXISTS (SELECT * FROM sys.default_constraints WHERE object_id = OBJECT_ID(N'[dbo].[DF_BirthdateSubscription_SubscriptionId]') AND parent_object_id = OBJECT_ID(N'[dbo].[BirthdateSubscription]'))
    ALTER TABLE [dbo].[BirthdateSubscription] DROP  CONSTRAINT [DF_BirthdateSubscription_SubscriptionId]  <commit_msg>Installer Bugfix: Ready = 1 for all LastCheckedUUID = null (no condition on Ready = null)<commit_after>﻿
-----------------------------------------------------------------------------------------------
-------------------   Add identity column DataChangeEvent.ReceivedOrder   ---------------------
-----------------------------------------------------------------------------------------------

-- Create temp table if not already there
IF NOT EXISTS (SELECT * FROM sys.tables where name='DataChangeEvent_TMP')
	CREATE TABLE DataChangeEvent_TMP (DataChangeEventId uniqueidentifier, PersonUuid uniqueidentifier, PersonRegistrationId uniqueidentifier, DueDate datetime, ReceivedDate DateTime)
GO

-- Copy existing data changes to tampe table
INSERT INTO DataChangeEvent_TMP (DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate) 
SELECT DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate 
FROM DataChangeEvent

GO

-- Delete from original table
DELETE DataChangeEvent
GO

-- Add identity column ReceivedOrder if not already there
IF NOT EXISTS (SELECT * FROM sys.columns c INNER JOIN sys.tables t ON c.object_id = t.object_id where t.name='DataChangeEvent' AND c.name = 'ReceivedOrder')
	ALTER TABLE dbo.DataChangeEvent 
	ADD	ReceivedOrder int NOT NULL IDENTITY (1, 1)
GO

-- Copy back into original table
INSERT INTO DataChangeEvent (DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate) 
SELECT DISTINCT DataChangeEventId , PersonUuid , PersonRegistrationId , DueDate , ReceivedDate 
FROM DataChangeEvent_TMP
ORDER BY ReceivedDate

GO

-- Now drop temp table
IF EXISTS (SELECT * FROM sys.tables where name='DataChangeEvent_TMP')
	DROP TABLE DataChangeEvent_TMP
GO


-----------------------------------------------------------------------------------------------
-------------------------------   Drop legacy stored procedure  -------------------------------
-----------------------------------------------------------------------------------------------

IF EXISTS (SELECT * FROM sys.procedures WHERE name='InsertChangeNotificationData')
	DROP PROCEDURE InsertChangeNotificationData
GO

-----------------------------------------------------------------------------------------------
------------------------------   New column Subscription.Ready  -------------------------------
-----------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'Ready' AND object_id = OBJECT_ID('Subscription'))
	ALTER TABLE Subscription ADD Ready BIT NOT NULL DEFAULT 0
GO

UPDATE Subscription SET Ready = 1 WHERE LastCheckedUUID IS NULL
GO

-----------------------------------------------------------------------------------------------
------------------------------   New column EventNotification.AttemptCount  -------------------------------
-----------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'AttemptCount' AND object_id = OBJECT_ID('EventNotification'))
	ALTER TABLE EventNotification ADD AttemptCount INT NOT NULL DEFAULT 0
GO

-----------------------------------------------------------------------------------------------
--------------------   Drop default for  BirthdateSubscription.SubscriptionId -----------------
-----------------------------------------------------------------------------------------------

IF EXISTS (SELECT * FROM sys.default_constraints WHERE object_id = OBJECT_ID(N'[dbo].[DF_BirthdateSubscription_SubscriptionId]') AND parent_object_id = OBJECT_ID(N'[dbo].[BirthdateSubscription]'))
    ALTER TABLE [dbo].[BirthdateSubscription] DROP  CONSTRAINT [DF_BirthdateSubscription_SubscriptionId]  
{'fraction_non_alphanumeric': 0.36215639178009074, 'fraction_numerical': 0.0014678409394182012, 'mean_word_length': 1.333437110834371, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '379016', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1470, 'n_words': 502}