-- Migration: 002_CreateDataUpdateTable
-- Source: OLD/Database/Tables/DataUpdate.sql
-- Changes: DATETIME -> DATETIME2(7)

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'DataUpdate' AND schema_id = SCHEMA_ID('dbo'))
BEGIN
    CREATE TABLE [dbo].[DataUpdate]
    (
        [ID] INT IDENTITY(1,1) NOT NULL,
        [SourceSystem] VARCHAR(50) NOT NULL,
        [SourceData] VARCHAR(50) NOT NULL,
        [TableName] VARCHAR(50) NOT NULL,
        [StartDT] DATETIME2(7) NOT NULL,
        [EndDT] DATETIME2(7) NULL,
        [UpdateType] SMALLINT NOT NULL,
        [WasSuccessful] BIT NOT NULL,
        [NumberRecords] BIGINT NOT NULL,
        [Parameters] NVARCHAR(MAX) NULL,
        CONSTRAINT [PK_DataUpdate] PRIMARY KEY CLUSTERED([ID])
    );
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_DataUpdate_LastUpdate' AND object_id = OBJECT_ID('dbo.DataUpdate'))
BEGIN
    CREATE INDEX [IX_DataUpdate_LastUpdate] ON [dbo].[DataUpdate] ([TableName], [WasSuccessful], [StartDT] DESC);
END
GO
