-- Replace @WINDREAM_DB in code with name of WM-Database 
IF not EXISTS(SELECT GUID FROM TBDD_MODULES WHERE NAME = 'ToolCollection' )
	INSERT INTO TBDD_MODULES (NAME, SHORT_NAME, LICENSE,ACTIVE) VALUES ('ToolCollection','TC','kc6j9HSv/UJVAzFjMZv2A3wHZFy+2nNLKP9Qs8g9EVY=',1)
UPDATE TBDD_MODULES SET DB_VERSION = '1.1.0.0' where UPPER(NAME) = UPPER('ToolCollection')
GO
CREATE TABLE TBTC_MOVE_RENAME
(
	GUID				INT				IDENTITY(1,1),              		    
	PROFILE_NAME		VARCHAR(100) 						NOT NULL,
	FOLDER_FOR_INDEX	VARCHAR(500)	DEFAULT ''			NOT NULL,
	NAMECONVENTION		VARCHAR(500)	NOT NULL DEFAULT '',
	VERSION_OFF			BIT				DEFAULT 0			NOT NULL,
	ACTIVE   			BIT				DEFAULT 1 		  	NOT NULL,
	CREATED_WHO			VARCHAR(50)		DEFAULT 'DEFAULT'	NOT NULL,
	CREATED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT		PK_TBTC_MOVE_RENAME	PRIMARY KEY(GUID),
	CONSTRAINT 		UNIQUE_TBTC_MOVE_RENAME	UNIQUE(PROFILE_NAME)
)
GO
------------------------------------------------------------------------------

CREATE TRIGGER TBTC_MOVE_RENAME_AFT_UPD ON TBTC_MOVE_RENAME
FOR UPDATE
AS
BEGIN TRY
	 UPDATE TBTC_MOVE_RENAME SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	 WHERE TBTC_MOVE_RENAME.GUID = INSERTED.GUID
	
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) 
		+ ' - ERROR-MESSAGE: ' 
		+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

CREATE TABLE TBTC_DI_REGEX_MATCH
(
	GUID				INT				IDENTITY(1,1),              		    
	PROFIL_NAME		VARCHAR(250) 						NOT NULL,
	REGEX				VARCHAR(1000)						NOT NULL,
	CREATED_WHO			VARCHAR(50)		DEFAULT 'DEFAULT'	NOT NULL,
	CREATED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT		PK_TBTC_DI_REGEX_MATCH	PRIMARY KEY(GUID),
	CONSTRAINT 		UNIQUE_TBTC_DI_REGEX_MATCH	UNIQUE(PROFIL_NAME)
)
GO
------------------------------------------------------------------------------

CREATE TRIGGER TBTC_DI_REGEX_MATCH_AFT_UPD ON TBTC_DI_REGEX_MATCH
FOR UPDATE
AS
BEGIN TRY
	 UPDATE TBTC_DI_REGEX_MATCH SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	 WHERE TBTC_DI_REGEX_MATCH.GUID = INSERTED.GUID
	
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) 
		+ ' - ERROR-MESSAGE: ' 
		+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO