-----------------------------------------------------------------------------
-- Please note: early versions of DB2 don't support auto-increment fields
-- (i.e. "GENERATED ALWAYS AS IDENTITY"). With those older versions, an
-- additional ID table has to be used.
-----------------------------------------------------------------------------



-----------------------------------------------------------------------------
-- TURBINE_PERMISSION
-----------------------------------------------------------------------------
drop table TURBINE_PERMISSION;


CREATE TABLE TURBINE_PERMISSION
(
    PERMISSION_ID INT  GENERATED ALWAYS AS IDENTITY,
    PERMISSION_NAME VARCHAR (99) NOT NULL,
    OBJECTDATA BLOB (16777215),
    UNIQUE (PERMISSION_NAME)
);

ALTER TABLE TURBINE_PERMISSION
    ADD CONSTRAINT TRB_PERM_PK 
PRIMARY KEY (PERMISSION_ID);




    
-----------------------------------------------------------------------------
-- TURBINE_ROLE
-----------------------------------------------------------------------------
drop table TURBINE_ROLE;


CREATE TABLE TURBINE_ROLE
(
    ROLE_ID INT  GENERATED ALWAYS AS IDENTITY,
    ROLE_NAME VARCHAR (99) NOT NULL,
    OBJECTDATA BLOB (16777215),
    UNIQUE (ROLE_NAME)
);

ALTER TABLE TURBINE_ROLE
    ADD CONSTRAINT TRB_RL_PK 
PRIMARY KEY (ROLE_ID);




    
-----------------------------------------------------------------------------
-- TURBINE_GROUP
-----------------------------------------------------------------------------
drop table TURBINE_GROUP;


CREATE TABLE TURBINE_GROUP
(
    GROUP_ID INT  GENERATED ALWAYS AS IDENTITY,
    GROUP_NAME VARCHAR (99) NOT NULL,
    OBJECTDATA BLOB (16777215),
    UNIQUE (GROUP_NAME)
);

ALTER TABLE TURBINE_GROUP
    ADD CONSTRAINT TRB_GRP_PK 
PRIMARY KEY (GROUP_ID);




    
-----------------------------------------------------------------------------
-- TURBINE_ROLE_PERMISSION
-----------------------------------------------------------------------------
drop table TURBINE_ROLE_PERMISSION;


CREATE TABLE TURBINE_ROLE_PERMISSION
(
    ROLE_ID INT NOT NULL,
    PERMISSION_ID INT NOT NULL
);

ALTER TABLE TURBINE_ROLE_PERMISSION
    ADD CONSTRAINT TRB_RL_PERM_PK
PRIMARY KEY (ROLE_ID,PERMISSION_ID);

ALTER TABLE TURBINE_ROLE_PERMISSION
    ADD CONSTRAINT TRB_RL_PERM_FK1 FOREIGN KEY (ROLE_ID)
    REFERENCES TURBINE_ROLE (ROLE_ID);

ALTER TABLE TURBINE_ROLE_PERMISSION
    ADD CONSTRAINT TRB_RL_PERM_FK2 FOREIGN KEY (PERMISSION_ID)
    REFERENCES TURBINE_PERMISSION (PERMISSION_ID);



    
-----------------------------------------------------------------------------
-- TURBINE_USER
-----------------------------------------------------------------------------
drop table TURBINE_USER;


CREATE TABLE TURBINE_USER
(
    USER_ID INT  GENERATED ALWAYS AS IDENTITY,
    LOGIN_NAME VARCHAR (32) NOT NULL,
    PASSWORD_VALUE VARCHAR (32) NOT NULL,
    FIRST_NAME VARCHAR (99) NOT NULL,
    LAST_NAME VARCHAR (99) NOT NULL,
    EMAIL VARCHAR (99),
    CONFIRM_VALUE VARCHAR (99),
    MODIFIED DATE,
    CREATED DATE,
    LASTLOGIN DATE,
    OBJECTDATA BLOB (16777215),
    UNIQUE (LOGIN_NAME)
);

ALTER TABLE TURBINE_USER
    ADD CONSTRAINT TRB_USR_PK 
PRIMARY KEY (USER_ID);





    
-----------------------------------------------------------------------------
-- TURBINE_USER_GROUP_ROLE
-----------------------------------------------------------------------------
drop table TURBINE_USER_GROUP_ROLE;


CREATE TABLE TURBINE_USER_GROUP_ROLE
(
    USER_ID INT NOT NULL,
    GROUP_ID INT NOT NULL,
    ROLE_ID INT NOT NULL
);

ALTER TABLE TURBINE_USER_GROUP_ROLE
    ADD CONSTRAINT TRB_USR_GRP_RL_PK 
PRIMARY KEY (USER_ID,GROUP_ID,ROLE_ID);

ALTER TABLE TURBINE_USER_GROUP_ROLE
    ADD CONSTRAINT TRB_USR_GRP_RL_FK1 FOREIGN KEY (USER_ID)
    REFERENCES TURBINE_USER (USER_ID);

ALTER TABLE TURBINE_USER_GROUP_ROLE
    ADD CONSTRAINT TRB_USR_GRP_RL_FK2 FOREIGN KEY (GROUP_ID)
    REFERENCES TURBINE_GROUP (GROUP_ID);

ALTER TABLE TURBINE_USER_GROUP_ROLE
    ADD CONSTRAINT TRB_USR_GRP_RL_FK3 FOREIGN KEY (ROLE_ID)
    REFERENCES TURBINE_ROLE (ROLE_ID);




    
-----------------------------------------------------------------------------
-- TURBINE_JOBENTRY
-----------------------------------------------------------------------------
drop table TURBINE_JOBENTRY;


CREATE TABLE TURBINE_JOBENTRY
(
    JOB_ID INT NOT NULL,
    SECOND INT default -1 NOT NULL,
    MINUTE INT default -1 NOT NULL,
    HOUR INT default -1 NOT NULL,
    WEEKDAY INT default -1 NOT NULL,
    DAY_OF_MONTH INT default -1 NOT NULL,
    TASK VARCHAR (99) NOT NULL,
    EMAIL VARCHAR (99)
);

ALTER TABLE TURBINE_JOBENTRY
    ADD CONSTRAINT TRB_JOBENTRY_PK 
PRIMARY KEY (JOB_ID);




