IF OBJECT_ID('dbo.PendingDevice', 'U') IS NOT NULL
  DROP TABLE [dbo].[PendingDevice]
GO
CREATE TABLE PendingDevice (
	DeviceID          VARCHAR(80) NOT NULL PRIMARY KEY,
	NumPushRetries    TINYINT NOT NULL DEFAULT 0,
	Topic             VARCHAR(255) NOT NULL DEFAULT '',
	PubKeyData        VARCHAR(8000) NOT NULL DEFAULT '',
	PushToken         VARCHAR(255) NOT NULL DEFAULT '',
	PushMagic         VARCHAR(255) NOT NULL DEFAULT '',
	UnlockToken       VARCHAR(8000) NOT NULL DEFAULT '',
	DateCreated       BIGINT NOT NULL,
	DateLastConnected BIGINT NOT NULL
)
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'NumPushRetries' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD NumPushRetries TINYINT NOT NULL DEFAULT 0
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'LicenseSeatId' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD LicenseSeatId VARCHAR(80) NOT NULL DEFAULT ''
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'LicenseSequenceNumber' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD LicenseSequenceNumber BIGINT NOT NULL DEFAULT 0
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'LicenseFileId' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD LicenseFileId VARCHAR(80) NOT NULL DEFAULT ''
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'LicenseFile' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD LicenseFile VARBINARY(MAX)
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'LicenseExpirationTime' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD LicenseExpirationTime BIGINT DEFAULT 0
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'LicenseUpdateTime' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD LicenseUpdateTime BIGINT DEFAULT 0
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'DeviceName' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD DeviceName VARCHAR(512) NOT NULL DEFAULT ''
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'AppVersion' AND Object_ID = Object_ID(N'Device'))
BEGIN
	ALTER TABLE Device ADD AppVersion VARCHAR(80) NOT NULL DEFAULT ''
END
GO

IF NOT EXISTS(SELECT * FROM sys.indexes 
			WHERE name='Device_LicenseUpdateTime' AND object_id = OBJECT_ID('Device'))
BEGIN
	CREATE INDEX Device_LicenseUpdateTime ON Device (LicenseUpdateTime);
END
GO

IF NOT EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'RequestType' AND Object_ID = Object_ID(N'Command'))
BEGIN
	ALTER TABLE Command ADD RequestType VARCHAR(80) DEFAULT NULL
END
GO

IF NOT EXISTS(SELECT * FROM sys.indexes 
			WHERE name='Command_RequestType' AND object_id = OBJECT_ID('Command'))
BEGIN
	CREATE INDEX Command_RequestType ON Command (RequestType);
END
GO