CREATE TABLE USER_TO_DEPARTMENT (
  DEPARTMENT_ID INTEGER NOT NULL,
  USER_ID       INTEGER NOT NULL,
  PRIMARY KEY (DEPARTMENT_ID, USER_ID),
  CONSTRAINT FK_USER_TO_USER FOREIGN KEY (USER_ID) REFERENCES USERS (USER_ID),
  CONSTRAINT FK_USER_TO_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES USER_DEPARTMENT (DEPARTMENT_ID)
);


ALTER TABLE USER_DEPARTMENT ADD COLUMN MANAGER_USER_ID INTEGER DEFAULT NULL;
ALTER TABLE USER_DEPARTMENT ADD COLUMN TIMEZONE VARCHAR(128) DEFAULT NULL;
ALTER TABLE USER_DEPARTMENT ADD COLUMN PARENT_DEPARTMENT_ID INTEGER DEFAULT NULL;

ALTER TABLE USERS ALTER COLUMN DEPARTMENT_ID DROP NOT NULL;

UPDATE CONFIGURATION SET CONFIG_VALUE = '1.4.2' WHERE CONFIG_KEY = 'version';

