USE ApplicationModules
GO

-- Deleting Real Time Tables, Real Time Trends and Active Alarm Views 
-- It is safe to delete configuration items greater then 1000. All factory configuration item IDs are less then 1000 for 8.0. 
-- Since this script is intended for 6.0 to 8.0, so it is fine for this upgrade path
-- Need to review when support for 6.0 to 9.0 is opened up.
DELETE FROM Configuration.ConfigurationValue  WHERE ConfigurationKeyId  > 1000
DELETE FROM Configuration.ConfigurationKey WHERE ID > 1000

--Delete the dashboards
-- Before deleting dashboard remove parent child relationship
--to avoid same table reference error
UPDATE Dashboard.Dashboard SET ParentId=NULL

DECLARE @ID [uniqueidentifier]

DECLARE dashboard_cursor CURSOR FOR  
SELECT DashboardId FROM Dashboard.Dashboard

OPEN dashboard_cursor   
FETCH NEXT FROM dashboard_cursor INTO @ID   

WHILE @@FETCH_STATUS = 0   
BEGIN         
	  -- The stored procedure will take care of cascade deletion
	  EXEC Dashboard.DeleteDashboard @dashboardId = @ID; 
	  FETCH NEXT FROM dashboard_cursor INTO @ID
END   

CLOSE dashboard_cursor   
DEALLOCATE dashboard_cursor

--Delete the Slideshows
DECLARE slideshow_cursor CURSOR FOR  
SELECT Id FROM Dashboard.Slideshow

OPEN slideshow_cursor   
FETCH NEXT FROM slideshow_cursor INTO @ID   

WHILE @@FETCH_STATUS = 0   
BEGIN 
	-- The stored procedure will take care of cascade deletion        
	  EXEC Dashboard.DeleteSlideshow @slideshowId = @ID;	  
	  FETCH NEXT FROM slideshow_cursor INTO @ID
END   

CLOSE slideshow_cursor   
DEALLOCATE slideshow_cursor

GO

--Clean up Hierarchies
--Cleanup Triple store tables. No factory information is saved in these Tables. Hence safe to clean this up.
DELETE FROM TripleStore.Triple
DELETE FROM TripleStore.Model
DELETE FROM TripleStore.Resource
DELETE FROM TripleStore.Namespace
GO