
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
CREATE TABLE [dbo].[bl_Application] (
		[ApplicationID]     int IDENTITY (1,1) NOT NULL,
		[Code]              varchar(100) NOT NULL,
		[Name]              varchar(250) NOT NULL
)
ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Application]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'PK_bl_Application' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Application]')))
ALTER TABLE [dbo].[bl_Application]
	ADD
	CONSTRAINT [PK_bl_Application]
	PRIMARY KEY
	([ApplicationID])
	ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Application]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'IX_bl_Application' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Application]')))
ALTER TABLE [dbo].[bl_Application]
	ADD
	CONSTRAINT [IX_bl_Application]
	UNIQUE
	([Code])
	ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
CREATE TABLE [dbo].[bl_Entry] (
		[EntryID]              int IDENTITY (1,1) NOT NULL,
		[myDateTime]           datetime NOT NULL,
		[Message]              varchar(500) NOT NULL,
		[ApplicationID]        int NOT NULL,
		[SourceID]             int NOT NULL,
		[SeverityID]           int NOT NULL,
		[HostID]               int NOT NULL,
		[ExceptionMessage]     varchar(500) NULL,
		[ExceptionDetails]     varchar(5000) NULL,
		[CFID]                 varchar(255) NULL,
		[CFTOKEN]              varchar(255) NULL,
		[UserAgent]            varchar(500) NULL,
		[TemplatePath]         varchar(500) NULL,
		[HTMLReport]           text NULL,
		[createdOn]            datetime NOT NULL
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Entry]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'PK_bl_Entry' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Entry]')))
ALTER TABLE [dbo].[bl_Entry]
	ADD
	CONSTRAINT [PK_bl_Entry]
	PRIMARY KEY
	([EntryID])
	ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Entry]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'IX_bl_Entry' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Entry]')))
ALTER TABLE [dbo].[bl_Entry]
	ADD
	CONSTRAINT [IX_bl_Entry]
	UNIQUE
	([EntryID])
	ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Entry]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.objects WHERE [object_id]=OBJECT_ID(N'[dbo].[DF_bl_Entry_createdOn]') AND [parent_object_id]=OBJECT_ID(N'[dbo].[bl_Entry]')))
ALTER TABLE [dbo].[bl_Entry]
	ADD
	CONSTRAINT [DF_bl_Entry_createdOn]
	DEFAULT (getdate()) FOR [createdOn]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[bl_Entry]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sysobjects WHERE id=OBJECT_ID(N'[dbo].[DF_bl_Entry_createdOn]') AND parent_obj=OBJECT_ID(N'[dbo].[bl_Entry]')))
ALTER TABLE [dbo].[bl_Entry]
	ADD
	CONSTRAINT [DF_bl_Entry_myDateTime]
	DEFAULT (getdate()) FOR [myDateTime]
GO


IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
CREATE TABLE [dbo].[bl_Host] (
		[HostID]       int IDENTITY (1,1) NOT NULL,
		[HostName]     varchar(250) NOT NULL
)
ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Host]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'PK_bl_Host' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Host]')))
ALTER TABLE [dbo].[bl_Host]
	ADD
	CONSTRAINT [PK_bl_Host]
	PRIMARY KEY
	([HostID])
	ON [PRIMARY]
GO


IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
CREATE TABLE [dbo].[bl_Severity] (
		[SeverityID]     int IDENTITY (1,1) NOT NULL,
		[Name]           varchar(250) NOT NULL,
		[Code]           varchar(50) NOT NULL
)
ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Severity]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'PK_bl_Severity' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Severity]')))
ALTER TABLE [dbo].[bl_Severity]
	ADD
	CONSTRAINT [PK_bl_Severity]
	PRIMARY KEY
	([SeverityID])
	ON [PRIMARY]
GO



IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
CREATE TABLE [dbo].[bl_Source] (
		[SourceID]     int IDENTITY (1,1) NOT NULL,
		[Name]         varchar(250) NOT NULL
)
ON [PRIMARY]
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF (EXISTS(SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[bl_Source]') AND [type]='U')) AND NOT (EXISTS (SELECT * FROM sys.indexes WHERE [name]=N'PK_bl_Source' AND [object_id]=OBJECT_ID(N'[dbo].[bl_Source]')))
ALTER TABLE [dbo].[bl_Source]
	ADD
	CONSTRAINT [PK_bl_Source]
	PRIMARY KEY
	([SourceID])
	ON [PRIMARY]
GO


IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO


IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF OBJECT_ID(N'[dbo].[bl_Entry]') IS NOT NULL
	AND OBJECT_ID(N'[dbo].[bl_Application]') IS NOT NULL
	AND NOT EXISTS (SELECT * FROM sys.objects WHERE [object_id]=OBJECT_ID(N'[dbo].[FK_bl_Entry_bl_Application]') AND [parent_object_id]=OBJECT_ID(N'[dbo].[bl_Entry]'))
BEGIN
		ALTER TABLE [dbo].[bl_Entry]
			WITH NOCHECK
			ADD CONSTRAINT [FK_bl_Entry_bl_Application]
			FOREIGN KEY ([ApplicationID]) REFERENCES [dbo].[bl_Application] ([ApplicationID])
		ALTER TABLE [dbo].[bl_Entry]
			CHECK CONSTRAINT [FK_bl_Entry_bl_Application]

END
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF OBJECT_ID(N'[dbo].[bl_Entry]') IS NOT NULL
	AND OBJECT_ID(N'[dbo].[bl_Host]') IS NOT NULL
	AND NOT EXISTS (SELECT * FROM sys.objects WHERE [object_id]=OBJECT_ID(N'[dbo].[FK_bl_Entry_bl_Host]') AND [parent_object_id]=OBJECT_ID(N'[dbo].[bl_Entry]'))
BEGIN
		ALTER TABLE [dbo].[bl_Entry]
			ADD CONSTRAINT [FK_bl_Entry_bl_Host]
			FOREIGN KEY ([HostID]) REFERENCES [dbo].[bl_Host] ([HostID])
END
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF OBJECT_ID(N'[dbo].[bl_Entry]') IS NOT NULL
	AND OBJECT_ID(N'[dbo].[bl_Severity]') IS NOT NULL
	AND NOT EXISTS (SELECT * FROM sys.objects WHERE [object_id]=OBJECT_ID(N'[dbo].[FK_bl_Entry_bl_Severity]') AND [parent_object_id]=OBJECT_ID(N'[dbo].[bl_Entry]'))
BEGIN
		ALTER TABLE [dbo].[bl_Entry]
			ADD CONSTRAINT [FK_bl_Entry_bl_Severity]
			FOREIGN KEY ([SeverityID]) REFERENCES [dbo].[bl_Severity] ([SeverityID])
END
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO
IF OBJECT_ID(N'[dbo].[bl_Entry]') IS NOT NULL
	AND OBJECT_ID(N'[dbo].[bl_Source]') IS NOT NULL
	AND NOT EXISTS (SELECT * FROM sys.objects WHERE [object_id]=OBJECT_ID(N'[dbo].[FK_bl_Entry_bl_Source]') AND [parent_object_id]=OBJECT_ID(N'[dbo].[bl_Entry]'))
BEGIN
		ALTER TABLE [dbo].[bl_Entry]
			ADD CONSTRAINT [FK_bl_Entry_bl_Source]
			FOREIGN KEY ([SourceID]) REFERENCES [dbo].[bl_Source] ([SourceID])
END
GO

IF @@ERROR<>0 OR @@TRANCOUNT=0 BEGIN IF @@TRANCOUNT>0 ROLLBACK SET NOEXEC ON END
GO

SET NOEXEC OFF
GO

SET NOCOUNT ON
GO

SET NOEXEC OFF
GO

CREATE TABLE [dbo].[bl_Extension] (
		[ExtensionID]     int IDENTITY (1,1) NOT NULL,
		[name]            varchar(255) NOT NULL,
		[type]            varchar(255) NOT NULL,
		[enabled]         bit NOT NULL,
		[description]     varchar(500) NULL,
		[properties]      text NULL,
		[createdBy]       int NULL,
		[createdOn]       datetime NOT NULL
)
ON [PRIMARY]
GO

ALTER TABLE [dbo].[bl_Extension]
	ADD
	CONSTRAINT [DF_bl_Extension_enabled]
	DEFAULT 0 FOR [enabled]
GO

ALTER TABLE [dbo].[bl_Extension]
	ADD
	CONSTRAINT [DF_bl_Extension_createdOn]
	DEFAULT (getdate()) FOR [createdOn]
GO

CREATE TABLE [dbo].[bl_ExtensionLog] (
		[ExtensionLogID]     int IDENTITY (1,1) NOT NULL,
		[extensionID]       int NULL,
		[createdOn]       datetime NOT NULL,
		[entryID]       int NULL
)
ON [PRIMARY]
GO

ALTER TABLE [dbo].[bl_ExtensionLog]
	ADD
	CONSTRAINT [DF_bl_ExtensionLog_createdOn]
	DEFAULT (getdate()) FOR [createdOn]
GO


CREATE TABLE [dbo].[bl_userApplication] (
		[userApplicationID]     int IDENTITY (1,1) NOT NULL,
		[userID]       int NULL,
		[applicationID]       int NULL
)
ON [PRIMARY]
GO

ALTER TABLE [dbo].[bl_userApplication]
	ADD
	CONSTRAINT [FK_bl_userApplication_bl_user]
	FOREIGN KEY ([userID]) REFERENCES [dbo].[bl_User] ([userID])
GO

ALTER TABLE [dbo].[bl_userApplication]
	ADD
	CONSTRAINT [FK_bl_userApplication_bl_application]
	FOREIGN KEY ([applicationID]) REFERENCES [dbo].[bl_application] ([applicationID])
GO

