-- Migration from 112 to 113

CREATE DOMAIN DATA_SET_KIND AS VARCHAR(40) CHECK (VALUE IN ('EXTERNAL', 'LINK', 'CONTAINER'));

ALTER TABLE DATA_SET_TYPES ADD COLUMN data_set_kind DATA_SET_KIND DEFAULT 'EXTERNAL' NOT NULL;

UPDATE DATA_SET_TYPES SET data_set_kind = 'CONTAINER' WHERE is_container = True;

ALTER TABLE DATA_SET_TYPES DROP COLUMN is_container;

CREATE TABLE LINK_DATA (DATA_ID TECH_ID NOT NULL, EDMS_ID TECH_ID NOT NULL, EXTERNAL_CODE TEXT_VALUE NOT NULL);

ALTER TABLE LINK_DATA ADD CONSTRAINT LNDA_PK PRIMARY KEY(DATA_ID);

ALTER TABLE LINK_DATA ADD CONSTRAINT LNDA_DATA_FK FOREIGN KEY (DATA_ID) REFERENCES DATA_ALL(ID);
ALTER TABLE LINK_DATA ADD CONSTRAINT LNDA_EDMS_FK FOREIGN KEY (EDMS_ID) REFERENCES EXTERNAL_DATA_MANAGEMENT_SYSTEMS(ID);

GRANT SELECT ON TABLE LINK_DATA TO GROUP OPENBIS_READONLY;

ALTER TABLE SAMPLE_TYPES ADD COLUMN INHERIT_PROPERTIES BOOLEAN_CHAR NOT NULL DEFAULT 'F';
