drop sequence TURBINE_PERMISSION_SEQ;
drop table TURBINE_PERMISSION;

CREATE TABLE TURBINE_PERMISSION
(
    PERMISSIONID serial,
    PERMISSION varchar (99) NOT NULL,
    OBJECTDATA oid,
    PRIMARY KEY(PERMISSIONID),
    UNIQUE (PERMISSION)
);

drop sequence TURBINE_ROLE_SEQ;
drop table TURBINE_ROLE;

CREATE TABLE TURBINE_ROLE
(
    ROLEID serial,
    ROLENAME varchar (99) NOT NULL,
    OBJECTDATA oid,
    PRIMARY KEY(ROLEID),
    UNIQUE (ROLENAME)
);

drop sequence TURBINE_GROUP_SEQ;
drop table TURBINE_GROUP;

CREATE TABLE TURBINE_GROUP
(
    GROUPID serial,
    GROUPNAME varchar (99) NOT NULL,
    OBJECTDATA oid,
    PRIMARY KEY(GROUPID),
    UNIQUE (GROUPNAME)
);

drop table TURBINE_ROLE_PERMISSION;

CREATE TABLE TURBINE_ROLE_PERMISSION
(
    ROLEID integer NOT NULL,
    PERMISSIONID integer NOT NULL,
    PRIMARY KEY(ROLEID,PERMISSIONID),
    FOREIGN KEY (ROLEID) REFERENCES TURBINE_ROLE (ROLEID),
    FOREIGN KEY (PERMISSIONID) REFERENCES TURBINE_PERMISSION (PERMISSIONID)
);
create  INDEX ROLE_PERMISSION_INDEX on TURBINE_ROLE_PERMISSION(ROLEID, PERMISSIONID);

drop sequence TURBINE_USER_SEQ;
drop table TURBINE_USER;

CREATE TABLE TURBINE_USER
(
    USERID serial,
    LOGINID varchar (32) NOT NULL,
    PASSWORD_VALUE varchar (32) NOT NULL,
    FIRST_NAME varchar (99) NOT NULL,
    LAST_NAME varchar (99) NOT NULL,
    ADDRESS1 varchar (255),
    ADDRESS2 varchar (255),
    CITY varchar (255),
    STATE varchar (32),
    POSTALCODE varchar (32),
    COUNTRY varchar (99),
    CITIZENSHIP varchar (32),
    PHONE varchar (32),
    ALTPHONE varchar (32),
    FAX varchar (32),
    CELL varchar (32),
    PAGER varchar (32),
    EMAIL varchar (99),
    MODIFIED timestamp,
    CREATED timestamp,
    LASTLOGIN timestamp,
    PREFIX_NAME varchar (16),
    MIDDLE_NAME varchar (99),
    SUFFIX_NAME varchar (16),
    COMPANY varchar (255),
    CONFIRM_VALUE varchar (99),
    OBJECTDATA oid,
    PRIMARY KEY(USERID),
    FOREIGN KEY (USERID) REFERENCES TURBINE_USER (USERID),
    UNIQUE (LOGINID)
);

drop table TURBINE_USER_GROUP_ROLE;

CREATE TABLE TURBINE_USER_GROUP_ROLE
(
    USERID integer NOT NULL,
    GROUPID integer NOT NULL,
    ROLEID integer NOT NULL,
    PRIMARY KEY(USERID,GROUPID,ROLEID),
    FOREIGN KEY (USERID) REFERENCES TURBINE_USER (USERID),
    FOREIGN KEY (GROUPID) REFERENCES TURBINE_GROUP (GROUPID),
    FOREIGN KEY (ROLEID) REFERENCES TURBINE_ROLE (ROLEID)
);
create  INDEX USER_GROUP_ROLE_INDEX on TURBINE_USER_GROUP_ROLE(USERID, GROUPID, ROLEID);

drop table TURBINE_JOBENTRY;

CREATE TABLE TURBINE_JOBENTRY
(
    JOBID integer NOT NULL,
    SECOND integer default -1 NOT NULL,
    MINUTE integer default -1 NOT NULL,
    HOUR integer default -1 NOT NULL,
    WEEKDAY integer default -1 NOT NULL,
    DAY_OF_MONTH integer default -1 NOT NULL,
    TASK varchar (99) NOT NULL,
    EMAIL varchar (99),
    PRIMARY KEY(JOBID)
);


