if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_USER_CONTEXTS' and TABLE_SCHEMA = 'VCLib'
)
begin
	DROP VIEW [VCLib].[T_USER_CONTEXTS]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_USER_CONTEXTS' and TABLE_SCHEMA = 'VCLib'
)
begin
	/***** Object:  Table [VCLib].[T_USER_CONTEXTS] *****/
	CREATE TABLE [VCLib].[T_USER_CONTEXTS]
	(
		[UserContextID] uniqueidentifier NOT NULL,
		[Context] varchar(255) NULL,
		[ShowPreview] bit NULL,
		[PreviewRatio] decimal(31, 10) NULL,
		[ShowGrid] bit NOT NULL,
		[ScalePercent] int NOT NULL,
		[EntityID] int NOT NULL,
		[UserID] uniqueidentifier NULL,
		[ViewName] varchar(255) NOT NULL,
		[ActiveContext] bit NOT NULL,
		[ReadOnly] bit NOT NULL,
		[SortingLexem] varchar(max) NULL,
		[CustomSortingEnabled] bit NOT NULL,
		[UserContextType] int NOT NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [VCLib].[T_USER_CONTEXTS] ADD CONSTRAINT [PK_T_USER_CONTEXTS]
	PRIMARY KEY CLUSTERED ([UserContextID]) ON [PRIMARY]
end

if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_USER_COLUMNS' and TABLE_SCHEMA = 'VCLib'
)
begin
	DROP VIEW [VCLib].[T_USER_COLUMNS]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_USER_COLUMNS' and TABLE_SCHEMA = 'VCLib'
)
begin
	/***** Object:  Table [VCLib].[T_USER_COLUMNS] *****/
	CREATE TABLE [VCLib].[T_USER_COLUMNS]
	(
		[UserColsID] uniqueidentifier NOT NULL,
		[AttributePath] varchar(255) NULL,
		[DisplayName] varchar(255) NULL,
		[ColumnIndex] int NOT NULL,
		[ColumnWidth] int NULL,
		[Visible] bit NULL,
		[SortingIndex] int NULL,
		[UserContextID] uniqueidentifier NOT NULL,
		[Sorting] int NOT NULL,
		[AggregationLexem] varchar(max) NULL,
		[AggregationFormat] varchar(255) NULL,
		[AggregationGrouping] varchar(max) NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [VCLib].[T_USER_COLUMNS] ADD CONSTRAINT [PK_T_USER_COLUMNS]
	PRIMARY KEY CLUSTERED ([UserColsID]) ON [PRIMARY]

	ALTER TABLE [VCLib].[T_USER_COLUMNS] ADD CONSTRAINT [FK_T_USER_CONTEXTS_T_USER_COLUMNS_UserContextID]
	FOREIGN KEY([UserContextID]) REFERENCES [VCLib].[T_USER_CONTEXTS] ([UserContextID])
end

if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_USER_CONFIG' and TABLE_SCHEMA = 'VCLib'
)
begin
	DROP VIEW [VCLib].[T_USER_CONFIG]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_USER_CONFIG' and TABLE_SCHEMA = 'VCLib'
)
begin
	/***** Object:  Table [VCLib].[T_USER_CONFIG] *****/
	CREATE TABLE [VCLib].[T_USER_CONFIG]
	(
		[ID] uniqueidentifier NOT NULL,
		[ConfigValue] varchar(max) NULL,
		[UserID] uniqueidentifier NULL,
		[UserConfigType] int NOT NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [VCLib].[T_USER_CONFIG] ADD CONSTRAINT [PK_T_USER_CONFIG]
	PRIMARY KEY CLUSTERED ([ID]) ON [PRIMARY]
end


if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_MACHINE_CONFIG' and TABLE_SCHEMA = 'VCLib'
)
begin
	DROP VIEW [VCLib].[T_MACHINE_CONFIG]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_MACHINE_CONFIG' and TABLE_SCHEMA = 'VCLib'
)
begin
	/***** Object:  Table [VCLib].[T_MACHINE_CONFIG] *****/
	CREATE TABLE [VCLib].[T_MACHINE_CONFIG]
	(
		[MachineConfigID] uniqueidentifier NOT NULL,
		[MachineName] varchar(256) NULL,
		[ConfigValue] varchar(max) NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [VCLib].[T_MACHINE_CONFIG] ADD CONSTRAINT [PK_T_MACHINE_CONFIG]
	PRIMARY KEY CLUSTERED ([MachineConfigID]) ON [PRIMARY]
end

if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_LOG_ROW' and TABLE_SCHEMA = 'IES'
)
begin
	DROP VIEW [IES].[T_LOG_ROW]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_LOG_ROW' and TABLE_SCHEMA = 'IES'
)
begin
	/***** Object:  Table [IES].[T_LOG_ROW] *****/
	CREATE TABLE [IES].[T_LOG_ROW]
	(
		[LogRowID] uniqueidentifier NOT NULL,
		[LogDate] datetime NOT NULL,
		[LogTime] datetime NOT NULL,
		[ErrorText] varchar(max) NULL,
		[MessageContent] varchar(max) NULL,
		[Users] uniqueidentifier NULL,
		[LogDateEnd] datetime NULL,
		[type_] int NOT NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [IES].[T_LOG_ROW] ADD CONSTRAINT [PK_T_LOG_ROW]
	PRIMARY KEY CLUSTERED ([LogRowID]) ON [PRIMARY]
end

if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_LOG_ROW_WEB_SERVICE' and TABLE_SCHEMA = 'IES'
)
begin
	DROP VIEW [IES].[T_LOG_ROW_WEB_SERVICE]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_LOG_ROW_WEB_SERVICE' and TABLE_SCHEMA = 'IES'
)
begin
	/***** Object:  Table [IES].[T_LOG_ROW_WEB_SERVICE] *****/
	CREATE TABLE [IES].[T_LOG_ROW_WEB_SERVICE]
	(
		[LogRowID] uniqueidentifier NOT NULL,
		[MethodName] varchar(50) NOT NULL,
		[IPAddres] varchar(16) NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [IES].[T_LOG_ROW_WEB_SERVICE] ADD CONSTRAINT [PK_T_LOG_ROW_WEB_SERVICE]
	PRIMARY KEY CLUSTERED ([LogRowID]) ON [PRIMARY]

	ALTER TABLE [IES].[T_LOG_ROW_WEB_SERVICE] ADD CONSTRAINT [FK_T_LOG_ROW_T_LOG_ROW_WEB_SERVICE_LogRowID]
	FOREIGN KEY([LogRowID]) REFERENCES [IES].[T_LOG_ROW] ([LogRowID])
end

if exists (
	select 1 from INFORMATION_SCHEMA.VIEWS
	where TABLE_NAME = 'T_LOG_ROW_DATA_BUS' and TABLE_SCHEMA = 'IES'
)
begin
	DROP VIEW [IES].[T_LOG_ROW_DATA_BUS]
end

if not exists (
	select 1 from INFORMATION_SCHEMA.TABLES
	where TABLE_NAME = 'T_LOG_ROW_DATA_BUS' and TABLE_SCHEMA = 'IES'
)
begin
	/***** Object:  Table [IES].[T_LOG_ROW_DATA_BUS] *****/
	CREATE TABLE [IES].[T_LOG_ROW_DATA_BUS]
	(
		[LogRowID] uniqueidentifier NOT NULL,
		[RoutingKey] varchar(20) NULL,
		[PacketObjectType] varchar(50) NULL,
		[ProcessName] varchar(30) NULL,
		[QueueName] varchar(100) NULL,
		[IsRPC] bit NOT NULL,
		[Parameter] varchar(100) NULL,
		[PacketType] int NOT NULL
	)
	ON [PRIMARY]

	-- Alters

	ALTER TABLE [IES].[T_LOG_ROW_DATA_BUS] ADD CONSTRAINT [PK_T_LOG_ROW_DATA_BUS]
	PRIMARY KEY CLUSTERED ([LogRowID]) ON [PRIMARY]

	ALTER TABLE [IES].[T_LOG_ROW_DATA_BUS] ADD CONSTRAINT [FK_T_LOG_ROW_T_LOG_ROW_DATA_BUS_LogRowID]
	FOREIGN KEY([LogRowID]) REFERENCES [IES].[T_LOG_ROW] ([LogRowID])
end