IF NOT EXISTS  (SELECT *
    FROM sys.columns 
    WHERE Name = N'ADId')
	BEGIN
		ALTER TABLE dbo.Principal
		ADD ADId uniqueidentifier NULL
	END

GO

IF NOT EXISTS ( SELECT * FROM sysindexes ix WHERE  ix.name = 'WIZ_IX_Principal_ADId')
BEGIN
	CREATE INDEX [WIZ_IX_Principal_ADId] ON [dbo].[Principal] ([ADId])
END

GO

-- Ensure all ad groups have filled out their ADId
UPDATE Principal
SET ADId = GroupId
WHERE Type = 2 AND ADId IS NULL