USE [DD_IIM]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID(N'_meta.TBDD_CFG_FUNCTION_MODULE', N'U') IS NULL
BEGIN
	CREATE TABLE [_meta].[TBDD_CFG_FUNCTION_MODULE]
	(
		[PK_CFG_FUNCTION_MODULE_ID] [bigint] IDENTITY(1,1) NOT NULL,
		[ACTIVE] [bit] NOT NULL,
		[INTERNAL] [bit] NOT NULL,
		[FUNCTION_NAME] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
		[FUNCTION_TYPE] [nvarchar](10) COLLATE Latin1_General_CI_AS NOT NULL,
		[FUNCTION_DEFINITION] [nvarchar](max) COLLATE Latin1_General_CI_AS NOT NULL,
		[COMMENT] [nvarchar](500) COLLATE Latin1_General_CI_AS NULL,
		[VERSION] [smallint] NOT NULL,
		[CREATED_WHO] [bigint] NOT NULL,
		[CREATED_WHEN] [datetime2](0) NOT NULL,
		[CHANGED_WHO] [bigint] NULL,
		[CHANGED_WHEN] [datetime2](0) NULL,

	 CONSTRAINT [TBDD_CFG_FUNCTION_MODULE_primaryKey]  PRIMARY KEY NONCLUSTERED HASH 
	(
		[PK_CFG_FUNCTION_MODULE_ID]
	)WITH ( BUCKET_COUNT = 65536)
	)WITH ( MEMORY_OPTIMIZED = ON , DURABILITY = SCHEMA_AND_DATA );
END;
GO

IF NOT EXISTS
(
	SELECT 1
	FROM sys.default_constraints dc
	JOIN sys.columns c
	  ON c.object_id = dc.parent_object_id
	 AND c.column_id = dc.parent_column_id
	WHERE dc.parent_object_id = OBJECT_ID(N'_meta.TBDD_CFG_FUNCTION_MODULE', N'U')
	  AND c.name = N'ACTIVE'
)
BEGIN
	ALTER TABLE [_meta].[TBDD_CFG_FUNCTION_MODULE]
	ADD CONSTRAINT [DF_TBDD_CFG_FUNCTION_MODULE_ACTIVE] DEFAULT ((1)) FOR [ACTIVE];
END;
GO

IF NOT EXISTS
(
	SELECT 1
	FROM sys.default_constraints dc
	JOIN sys.columns c
	  ON c.object_id = dc.parent_object_id
	 AND c.column_id = dc.parent_column_id
	WHERE dc.parent_object_id = OBJECT_ID(N'_meta.TBDD_CFG_FUNCTION_MODULE', N'U')
	  AND c.name = N'INTERNAL'
)
BEGIN
	ALTER TABLE [_meta].[TBDD_CFG_FUNCTION_MODULE]
	ADD CONSTRAINT [DF_TBDD_CFG_FUNCTION_MODULE_INTERNAL] DEFAULT ((0)) FOR [INTERNAL];
END;
GO

IF NOT EXISTS
(
	SELECT 1
	FROM sys.default_constraints dc
	JOIN sys.columns c
	  ON c.object_id = dc.parent_object_id
	 AND c.column_id = dc.parent_column_id
	WHERE dc.parent_object_id = OBJECT_ID(N'_meta.TBDD_CFG_FUNCTION_MODULE', N'U')
	  AND c.name = N'VERSION'
)
BEGIN
	ALTER TABLE [_meta].[TBDD_CFG_FUNCTION_MODULE]
	ADD CONSTRAINT [DF_TBDD_CFG_FUNCTION_MODULE_VERSION] DEFAULT ((1)) FOR [VERSION];
END;
GO


