-- ------------------------------------------------------------------------------------
-- ION Data v5.0 Database Upgrade: Step 3
--
-- Combines the DataLog and DataLogStamp tables into the DataLog2 table.
-- ------------------------------------------------------------------------------------
	SELECT dbo.FormatStatusMessage('Step 3: Started.')
GO
	IF(dbo.TableExists('DataLog') = 1)
	BEGIN 
		IF(dbo.TableExists('DataLog2') = 1)
		BEGIN
		-- If the DataLog and DataLog2 tables exist then a previous database upgrade attempt
		-- may have failed. Drop the DataLog2 table, it will be re-created and data migrated.
		SELECT dbo.FormatStatusMessage('Step 3: DataLog and DataLog2 tables already existed. A previous upgrade attempt may have failed. Dropping DataLog2 table.')
		DROP TABLE [dbo].[DataLog2]
		END
	END
	ELSE
	BEGIN
		SELECT dbo.FormatStatusMessage('Step 3: DataLog table did not exist, DataLog migration is not required.')
	END
GO
	IF(dbo.TableExists('DataLog2') = 0)
	BEGIN
		SELECT dbo.FormatStatusMessage('Step 3: Creating DataLog2 table.')
		CREATE TABLE [dbo].[DataLog2] (		
			[ID] BIGINT NOT NULL IDENTITY(1,1)
			, [Value] FLOAT NULL
			, [SourceID] INT NOT NULL
			, [QuantityID] SMALLINT NOT NULL
			, [TimestampUTC] DATETIME2 NOT NULL
		) ON [PRIMARY]
	END
GO
PRINT 'Step 3: Completed.'