-- ------------------------------------------------------------------------------------
-- ION Data v5.0 Database Upgrade: Step 6
--
-- Convert the RecorderState TimestampUTC/FractionOfASecond into DATETIME2.
--
-- Note: although script does not use a minimal logging approach, typically
-- RecorderState contents is rather small. Implementation is acceptable.
-- ------------------------------------------------------------------------------------
	SELECT dbo.FormatStatusMessage('Step 6: Started.')
GO
	EXEC sp_RENAME 'dbo.RecorderState.LastTimestampUTC' , 'LastTimestampUtcOld', 'COLUMN'
	EXEC sp_RENAME 'dbo.RecorderState.LogRegister' , 'LogRegisterOld', 'COLUMN'
GO
	ALTER TABLE 
		dbo.RecorderState 
	ADD
		LastTimestampUTC DATETIME2(7) NULL
		, LogRegister INT NULL
GO
	SELECT dbo.FormatStatusMessage('Step 6: Converting TimestampUTC and FoS values to DATETIME2.')
	UPDATE
		dbo.RecorderState
	SET
		LastTimestampUTC =
		(
			SELECT 
				[TimestampUTC] 
			FROM 
				dbo.CombineTimestampAndFractionOfASecondAsTable(LastTimestampUtcOld, LastFracOfASec)
		)
		, LogRegister = LogRegisterOld
GO	
	ALTER TABLE dbo.RecorderState DROP COLUMN LastTimestampUtcOld
	ALTER TABLE dbo.RecorderState DROP COLUMN LastFracOfASec
	ALTER TABLE dbo.RecorderState DROP COLUMN LogRegisterOld
GO
	SELECT dbo.FormatStatusMessage('Step 6: Completed.')