if not exists(select * from MwusRevision where UpdateType = 4)
begin
	update 
		MwusRevision
	set 
		UpdateType = 4
	where 
		UpdateType = 3
end
go

update 
	MwusRevision
set 
	UpdateType = 3
where 
	UpdateGuid in
(
'68C5B0A3-D1A6-4553-AE49-01D3A7827828',--	Service Packs
'CD5FFD1E-E932-4E3A-BF74-18BF0B1BBD83',--	Updates
'E0789628-CE08-4437-BE74-2495B842F43B',--	Definition Updates
'E6CF1350-C01B-414D-A61F-263D14D133B4',--	Critical Updates
'B4832BD8-E735-4761-8DAF-37F882276DAB',--	Tools
'EBFC1FC5-71A4-4F7B-9ACA-3B9A503104A0',--	Drivers
'B54E7D24-7ADD-428F-8B75-90A396FA584F',--	Feature Packs
'3689BDC8-B205-4AF4-8D4A-A63924C5E9D5',--	Upgrades
'0FA1201D-4330-4FA8-8AE9-B877473B6441',--	Security Updates
'28BC880E-0592-4CBF-8F95-C79B17911D5F'--	Update Rollups
)
and 
	UpdateType = 2
go

if (1 = dbo.udf_Conv_DoesColumnExist('MwusFile','IsSecure')) 
 BEGIN
	 if exists( select o.name from syscolumns c inner join sysobjects o on c.cdefault = o.id
												inner join sysobjects t on c.id = t.id
												where c.cdefault <> 0 and  t.name = 'MwusFile' and c.name = 'IsSecure' and o.name <> 'DF_' + t.name + '_' + c.name)
		begin
		 DECLARE @renameConstraintName  varchar(1000)
		 select @renameConstraintName =  'sp_rename ''' + o.name + ''', ''DF_' + t.name + '_' + c.name + ''', ''OBJECT'''
			from syscolumns c inner join sysobjects o	on c.cdefault = o.id
			inner join sysobjects t	on c.id = t.id	where c.cdefault <> 0
														and  t.name = 'MwusFile' and c.name = 'IsSecure'
														and o.name <> 'DF_' + t.name + '_' + c.name
			 exec (@renameConstraintName)   
		end
 END
 GO

INSERT INTO [MwusConfiguration]([Key],[Value])
select 'InitialMetadataHttpServer', 'http://download.mw-rmm.barracudamsp.com'
where not exists
(
	select * from MwusConfiguration
	 where [Key] = 'InitialMetadataHttpServer'
)
GO

if type_id('dbo.EncryptedUpdates_DTT') is null
   CREATE TYPE [dbo].[EncryptedUpdates_DTT] AS TABLE (   
       [UpdateGuid]     UNIQUEIDENTIFIER NOT NULL,   
       PRIMARY KEY CLUSTERED ([UpdateGuid] ASC) WITH (IGNORE_DUP_KEY = ON)
	);
GO

 if (1 = dbo.udf_Conv_DoesColumnExist('MwusFile','IsEncrypted')) 
 BEGIN
	 if exists( select o.name from syscolumns c inner join sysobjects o on c.cdefault = o.id
												inner join sysobjects t on c.id = t.id
												where c.cdefault <> 0 and  t.name = 'MwusFile' and c.name = 'IsEncrypted' and o.name <> 'DF_' + t.name + '_' + c.name)
		begin
		 DECLARE @renameConstraintName  varchar(1000)
		 select @renameConstraintName =  'sp_rename ''' + o.name + ''', ''DF_' + t.name + '_' + c.name + ''', ''OBJECT'''
			from syscolumns c inner join sysobjects o	on c.cdefault = o.id
			inner join sysobjects t	on c.id = t.id	where c.cdefault <> 0
														and  t.name = 'MwusFile' and c.name = 'IsEncrypted'
														and o.name <> 'DF_' + t.name + '_' + c.name
			 exec (@renameConstraintName)   
		end
END
GO