/* Add new columns for T_CommentLike */
ALTER TABLE T_CommentLike ADD CreateDate DATETIME NULL;
ALTER TABLE T_CommentLike ADD UpdateDate DATETIME NULL;
GO

UPDATE T_CommentLike SET CreateDate = GETDATE();
GO

UPDATE T_CommentLike SET UpdateDate = GETDATE();
GO

ALTER TABLE T_CommentLike ALTER COLUMN CreateDate DATETIME NOT NULL;
ALTER TABLE T_CommentLike ALTER COLUMN UpdateDate DATETIME NOT NULL;
GO

/* Add new columns for npzr */
ALTER TABLE T_Edit ADD DefaultRoleForZone NVARCHAR(128) NULL;
GO

/* Change ReviewZoneRole to Viewer for all participants except Owner when ReviewZone is ReadOnly */
UPDATE T_EditRole SET T_EditRole.ReviewZoneRole = 'Viewer' 
WHERE T_EditRole.EditId IN (SELECT E.objectId FROM T_Edit E WHERE E.ZoneType = 'R' AND E.ReadOnly = 1 AND T_EditRole.SubjectId != E.SubjectId);
GO

UPDATE T_Edit SET DefaultRoleForZone = 'Viewer' WHERE ZoneType = 'R' AND ReadOnly = 1;
GO

UPDATE T_Edit SET DefaultRoleForZone = 'Inherit' WHERE ZoneType = 'R' AND ReadOnly = 0;
GO

ALTER TABLE T_Edit DROP COLUMN ReadOnly;
GO

/* Change ReviewZoneReadOnly settings to ReviewZoneDefaultRole with default Viewer ReviewZone role as value */
UPDATE T_Settings SET EntryName = 'ReviewZoneDefaultRole', StringParam1 = 'Viewer', IntParam1 = 0
WHERE EntryName = 'ReviewZoneReadOnly' AND IntParam1 = 1;
GO

DELETE FROM T_Settings WHERE EntryName = 'ReviewZoneReadOnly';
GO

/* Schema Versioning */
UPDATE T_SchemaInfo SET SchemaVersion = '6.3.0';
GO

/* PR-4893 - new index for performance */
CREATE UNIQUE INDEX IX_Participant3 ON T_Participant (SubjectId, ReviewId);
GO