-- final tweaks for ION_Data upgrade for ION E 7.0

--Dropping [dbo].[spCompareWaveformLogSamplesToIMAGE]'
if exists (select * from sysobjects where name='spCompareWaveformLogSamplesToIMAGE' and  xtype = 'P')
DROP PROCEDURE [dbo].[spCompareWaveformLogSamplesToIMAGE]
GO

-- Dropping [dbo].[spCompareWaveformLogSamples]'

if exists (select * from sysobjects where name='spCompareWaveformLogSamples' and  xtype = 'P')
DROP PROCEDURE [dbo].[spCompareWaveformLogSamples]
GO

-- Refreshing [dbo].[vWFLogChannelValue]'

EXEC sp_refreshview N'[dbo].[vWFLogChannelValue]'
GO

GRANT EXECUTE ON  [dbo].[DateAsUnix] TO [ION_Reader]
GO
--Altering permissions on [dbo].[UnixAsDate]'

GRANT EXECUTE ON  [dbo].[UnixAsDate] TO [ION_Reader]
GO

--Altering permissions on [dbo].[EventLog2]'

GRANT SELECT ON  [dbo].[EventLog2] TO [mma_Archiver]
GO

--Altering permissions on [dbo].[WaveformLog2]'

GRANT SELECT ON  [dbo].[WaveformLog2] TO [mma_Archiver]
GO

--Altering permissions on [dbo].[vWFLogChannelValue]'

GRANT SELECT ON  [dbo].[vWFLogChannelValue] TO [ION_Reader]
GRANT SELECT ON  [dbo].[vWFLogChannelValue] TO [mma_Archiver]
GO

--Altering permissions on [dbo].[InsertChannel]'

GRANT EXECUTE ON  [dbo].[InsertChannel] TO [mma_Archiver]
GO

-- Altering permissions on [dbo].[vEventLog]
GRANT SELECT ON  [dbo].[vEventLog] TO [ION_Reader]
GRANT SELECT ON  [dbo].[vEventLog] TO [mma_Archiver]
GO

-- Create new role for IONE DSD to access store procs, views and/or tables it needs access to in ION_Data
IF not exists (SELECT * FROM sys.database_principals WHERE name = N'ION_DSD_Reader' AND TYPE='R')
BEGIN
	CREATE ROLE [ION_DSD_Reader]	
	GRANT SELECT ON [dbo].[vQuantity] TO [ION_DSD_Reader]
	GRANT SELECT ON [dbo].[vSource] TO [ION_DSD_Reader]
	GRANT SELECT ON [dbo].[vSourceQuantityName] TO [ION_DSD_Reader]
	GRANT EXECUTE ON [dbo].[GetDatalogDataByName] TO [ION_DSD_Reader]
END

GO

