if not exists (select * FROM sys.syscolumns where name = 'DataSourceID' AND id = object_id(N'dbo.Connections')) BEGIN
  alter table dbo.Connections ADD DataSourceID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'DataSourceID' AND id = object_id(N'dbo.ConnectionLog')) BEGIN
  alter table dbo.ConnectionLog ADD DataSourceID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'DataSourceID' AND id = object_id(N'dbo.GroupInfo')) BEGIN
  alter table dbo.GroupInfo ADD DataSourceID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'DataSourceID' AND id = object_id(N'dbo.UserGroupInfo')) BEGIN
  alter table dbo.UserGroupInfo ADD DataSourceID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'DataSourceID' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD DataSourceID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'DataSourceID' AND id = object_id(N'dbo.Attachment')) BEGIN
  alter table dbo.Attachment ADD DataSourceID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'CanAddCredentials' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD CanAddCredentials bit NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'CanAddDataEntry' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD CanAddDataEntry bit NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'CanEditCredentials' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD CanEditCredentials bit NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'CanEditDataEntry' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD CanEditDataEntry bit NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'CanDeleteCredentials' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD CanDeleteCredentials bit NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'CanDeleteDataEntry' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD CanDeleteDataEntry bit NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'FileSize' AND id = object_id(N'dbo.Attachment')) BEGIN
   alter table dbo.Attachment ADD FileSize Int NOT NULL Default 0
END;
if not exists (select * FROM sys.syscolumns where name = 'IsEnabled' AND id = object_id(N'dbo.UserInfo')) BEGIN
   alter table dbo.UserInfo ADD IsEnabled int NOT NULL DEFAULT 1
END;
if not exists (select * FROM sys.syscolumns where name = 'MetaData' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD MetaData nvarchar(max) NULL;
END;
if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Attachment')) BEGIN
   alter table dbo.Attachment ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;
   
if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.ConnectionLog')) BEGIN
   alter table dbo.ConnectionLog ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END; 

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Credential')) BEGIN
   alter table dbo.Credential ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Customer')) BEGIN
   alter table dbo.Customer ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.GroupInfo')) BEGIN
   alter table dbo.GroupInfo ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END; 

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Inventory')) BEGIN
   alter table dbo.Inventory ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Monitoring')) BEGIN
   alter table dbo.Monitoring ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.UserInfo')) BEGIN
   alter table dbo.UserInfo ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END; 

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.UserGroupInfo')) BEGIN
   alter table dbo.UserGroupInfo ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END; 

if not exists (select * FROM sys.syscolumns where name = 'CreationDate' AND id = object_id(N'dbo.Todo')) BEGIN
   alter table dbo.Todo ADD CreationDate Datetime NOT NULL DEFAULT GetDate()
END;

if not exists (select * FROM sys.sysobjects where id = object_id(N'dbo.UserProfile')) BEGIN
  create table dbo.UserProfile (
    ID uniqueidentifier DEFAULT newid() NOT NULL,
    Name VARCHAR(100) NOT NULL,
    Email VARCHAR(100) NOT NULL,
    FirstName VARCHAR(100) NULL,
    LastName VARCHAR(100) NULL,
    CompanyName VARCHAR(100) NULL,
    Phone VARCHAR(50) NULL,
    Fax VARCHAR(50) NULL,
    [State] VARCHAR(50) NULL,
    [Address] VARCHAR(100) NULL,
    CountryCode VARCHAR(5) NULL,
    CountryName VARCHAR(100) NULL,
    GravatarEmail VARCHAR(100) NULL,
    CreationDate Datetime NOT NULL DEFAULT GetDate(),
    SubscribeToNewsletter bit NOT NULL Default 0,
    constraint PK_UserProfile primary key clustered (ID)
  );
END;

if not exists (select * FROM sys.syscolumns where name = 'ConnectionType' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD ConnectionType int NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'ConnectionSubType' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD ConnectionSubType VARCHAR(100) NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'GroupName' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD GroupName VARCHAR(400) NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'Description' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD Description nvarchar(max) NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'Name' AND id = object_id(N'dbo.Connections')) BEGIN
   alter table dbo.Connections ADD Name VARCHAR(200) NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'Flags' AND id = object_id(N'dbo.Attachment')) BEGIN
   alter table dbo.Attachment ADD Flags int NOT NULL DEFAULT (0)
END;

---
if not exists (select * FROM sys.syscolumns where name = 'Email' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD Email VARCHAR(200) NULL
END;

if not exists (select * FROM sys.syscolumns where name = 'ID' AND id = object_id(N'dbo.DatabaseInfo')) BEGIN
  alter table dbo.DatabaseInfo ADD ID uniqueidentifier NOT NULL Default('{00000000-0000-0000-0000-000000000000}')
END;

if not exists (select * FROM sys.syscolumns where name = 'IsOwner' AND id = object_id(N'dbo.UserInfo')) BEGIN
  alter table dbo.UserInfo ADD IsOwner bit NOT NULL Default 0
END;

if not exists (select * FROM sys.sysobjects where id = object_id(N'dbo.LogMessage')) BEGIN
  create table dbo.LogMessage (
    ID uniqueidentifier DEFAULT newid() NOT NULL,
    Message nvarchar(max) NULL,
    UserName VARCHAR(100) NULL,
    Title VARCHAR(100) NULL,
    Category int NOT NULL,
    MessageType int NOT NULL,
    ServerName VARCHAR(100) NULL,
    ContextID uniqueidentifier NULL,
    CreationDate Datetime NOT NULL DEFAULT GetDate(),
    UserID uniqueidentifier NULL,
    constraint PK_LogMessage primary key clustered (ID)
  );
END;